@@ -3904,4 +3904,174 @@ public function test_course_module_bulk_update_calendar_events() {
3904
3904
// Update the assign instances for this course.
3905
3905
$ this ->assertTrue (course_module_bulk_update_calendar_events ('assign ' , $ course ->id ));
3906
3906
}
3907
+
3908
+ /**
3909
+ * Test that a student can view participants in a course they are enrolled in.
3910
+ */
3911
+ public function test_course_can_view_participants_as_student () {
3912
+ $ this ->resetAfterTest ();
3913
+
3914
+ $ course = $ this ->getDataGenerator ()->create_course ();
3915
+ $ coursecontext = context_course::instance ($ course ->id );
3916
+
3917
+ $ user = $ this ->getDataGenerator ()->create_user ();
3918
+ $ this ->getDataGenerator ()->enrol_user ($ user ->id , $ course ->id );
3919
+
3920
+ $ this ->setUser ($ user );
3921
+
3922
+ $ this ->assertTrue (course_can_view_participants ($ coursecontext ));
3923
+ }
3924
+
3925
+ /**
3926
+ * Test that a student in a course can not view participants on the site.
3927
+ */
3928
+ public function test_course_can_view_participants_as_student_on_site () {
3929
+ $ this ->resetAfterTest ();
3930
+
3931
+ $ course = $ this ->getDataGenerator ()->create_course ();
3932
+
3933
+ $ user = $ this ->getDataGenerator ()->create_user ();
3934
+ $ this ->getDataGenerator ()->enrol_user ($ user ->id , $ course ->id );
3935
+
3936
+ $ this ->setUser ($ user );
3937
+
3938
+ $ this ->assertFalse (course_can_view_participants (context_system::instance ()));
3939
+ }
3940
+
3941
+ /**
3942
+ * Test that an admin can view participants on the site.
3943
+ */
3944
+ public function test_course_can_view_participants_as_admin_on_site () {
3945
+ $ this ->resetAfterTest ();
3946
+
3947
+ $ this ->setAdminUser ();
3948
+
3949
+ $ this ->assertTrue (course_can_view_participants (context_system::instance ()));
3950
+ }
3951
+
3952
+ /**
3953
+ * Test teachers can view participants in a course they are enrolled in.
3954
+ */
3955
+ public function test_course_can_view_participants_as_teacher () {
3956
+ global $ DB ;
3957
+
3958
+ $ this ->resetAfterTest ();
3959
+
3960
+ $ course = $ this ->getDataGenerator ()->create_course ();
3961
+ $ coursecontext = context_course::instance ($ course ->id );
3962
+
3963
+ $ user = $ this ->getDataGenerator ()->create_user ();
3964
+ $ roleid = $ DB ->get_field ('role ' , 'id ' , array ('shortname ' => 'editingteacher ' ));
3965
+ $ this ->getDataGenerator ()->enrol_user ($ user ->id , $ course ->id , $ roleid );
3966
+
3967
+ $ this ->setUser ($ user );
3968
+
3969
+ $ this ->assertTrue (course_can_view_participants ($ coursecontext ));
3970
+ }
3971
+
3972
+ /**
3973
+ * Check the teacher can still view the participants page without the 'viewparticipants' cap.
3974
+ */
3975
+ public function test_course_can_view_participants_as_teacher_without_view_participants_cap () {
3976
+ global $ DB ;
3977
+
3978
+ $ this ->resetAfterTest ();
3979
+
3980
+ $ course = $ this ->getDataGenerator ()->create_course ();
3981
+ $ coursecontext = context_course::instance ($ course ->id );
3982
+
3983
+ $ user = $ this ->getDataGenerator ()->create_user ();
3984
+ $ roleid = $ DB ->get_field ('role ' , 'id ' , array ('shortname ' => 'editingteacher ' ));
3985
+ $ this ->getDataGenerator ()->enrol_user ($ user ->id , $ course ->id , $ roleid );
3986
+
3987
+ $ this ->setUser ($ user );
3988
+
3989
+ // Disable one of the capabilties.
3990
+ assign_capability ('moodle/course:viewparticipants ' , CAP_PROHIBIT , $ roleid , $ coursecontext );
3991
+
3992
+ // Should still be able to view the page as they have the 'moodle/course:enrolreview' cap.
3993
+ $ this ->assertTrue (course_can_view_participants ($ coursecontext ));
3994
+ }
3995
+
3996
+ /**
3997
+ * Check the teacher can still view the participants page without the 'moodle/course:enrolreview' cap.
3998
+ */
3999
+ public function test_course_can_view_participants_as_teacher_without_enrol_review_cap () {
4000
+ global $ DB ;
4001
+
4002
+ $ this ->resetAfterTest ();
4003
+
4004
+ $ course = $ this ->getDataGenerator ()->create_course ();
4005
+ $ coursecontext = context_course::instance ($ course ->id );
4006
+
4007
+ $ user = $ this ->getDataGenerator ()->create_user ();
4008
+ $ roleid = $ DB ->get_field ('role ' , 'id ' , array ('shortname ' => 'editingteacher ' ));
4009
+ $ this ->getDataGenerator ()->enrol_user ($ user ->id , $ course ->id , $ roleid );
4010
+
4011
+ $ this ->setUser ($ user );
4012
+
4013
+ // Disable one of the capabilties.
4014
+ assign_capability ('moodle/course:enrolreview ' , CAP_PROHIBIT , $ roleid , $ coursecontext );
4015
+
4016
+ // Should still be able to view the page as they have the 'moodle/course:viewparticipants' cap.
4017
+ $ this ->assertTrue (course_can_view_participants ($ coursecontext ));
4018
+ }
4019
+
4020
+ /**
4021
+ * Check the teacher can not view the participants page without the required caps.
4022
+ */
4023
+ public function test_course_can_view_participants_as_teacher_without_required_caps () {
4024
+ global $ DB ;
4025
+
4026
+ $ this ->resetAfterTest ();
4027
+
4028
+ $ course = $ this ->getDataGenerator ()->create_course ();
4029
+ $ coursecontext = context_course::instance ($ course ->id );
4030
+
4031
+ $ user = $ this ->getDataGenerator ()->create_user ();
4032
+ $ roleid = $ DB ->get_field ('role ' , 'id ' , array ('shortname ' => 'editingteacher ' ));
4033
+ $ this ->getDataGenerator ()->enrol_user ($ user ->id , $ course ->id , $ roleid );
4034
+
4035
+ $ this ->setUser ($ user );
4036
+
4037
+ // Disable the capabilities.
4038
+ assign_capability ('moodle/course:viewparticipants ' , CAP_PROHIBIT , $ roleid , $ coursecontext );
4039
+ assign_capability ('moodle/course:enrolreview ' , CAP_PROHIBIT , $ roleid , $ coursecontext );
4040
+
4041
+ $ this ->assertFalse (course_can_view_participants ($ coursecontext ));
4042
+ }
4043
+
4044
+ /**
4045
+ * Check that an exception is not thrown if we can view the participants page.
4046
+ */
4047
+ public function test_course_require_view_participants () {
4048
+ $ this ->resetAfterTest ();
4049
+
4050
+ $ course = $ this ->getDataGenerator ()->create_course ();
4051
+ $ coursecontext = context_course::instance ($ course ->id );
4052
+
4053
+ $ user = $ this ->getDataGenerator ()->create_user ();
4054
+ $ this ->getDataGenerator ()->enrol_user ($ user ->id , $ course ->id );
4055
+
4056
+ $ this ->setUser ($ user );
4057
+
4058
+ course_require_view_participants ($ coursecontext );
4059
+ }
4060
+
4061
+ /**
4062
+ * Check that an exception is thrown if we can't view the participants page.
4063
+ */
4064
+ public function test_course_require_view_participants_as_student_on_site () {
4065
+ $ this ->resetAfterTest ();
4066
+
4067
+ $ course = $ this ->getDataGenerator ()->create_course ();
4068
+
4069
+ $ user = $ this ->getDataGenerator ()->create_user ();
4070
+ $ this ->getDataGenerator ()->enrol_user ($ user ->id , $ course ->id );
4071
+
4072
+ $ this ->setUser ($ user );
4073
+
4074
+ $ this ->expectException ('required_capability_exception ' );
4075
+ course_require_view_participants (context_system::instance ());
4076
+ }
3907
4077
}
0 commit comments