2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Enrol/Role external PHPunit tests
22 * @copyright 2012 Jerome Mouneyrac
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
31 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
32 require_once($CFG->dirroot . '/enrol/externallib.php');
35 * Enrol external PHPunit tests
39 * @copyright 2012 Jerome Mouneyrac
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 class core_enrol_external_testcase extends externallib_advanced_testcase {
46 * Test get_enrolled_users
48 public function test_get_enrolled_users() {
51 $this->resetAfterTest(true);
53 $course = self::getDataGenerator()->create_course();
54 $user1 = self::getDataGenerator()->create_user();
55 $user2 = self::getDataGenerator()->create_user();
57 // Set the required capabilities by the external function.
58 $context = context_course::instance($course->id);
59 $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $context->id);
60 $this->assignUserCapability('moodle/user:viewdetails', $context->id, $roleid);
62 // Enrol the users in the course.
63 // We use the manual plugin.
64 $enrol = enrol_get_plugin('manual');
65 $enrolinstances = enrol_get_instances($course->id, true);
66 foreach ($enrolinstances as $courseenrolinstance) {
67 if ($courseenrolinstance->enrol == "manual") {
68 $instance = $courseenrolinstance;
72 $enrol->enrol_user($instance, $user1->id, $roleid);
73 $enrol->enrol_user($instance, $user2->id, $roleid);
74 $enrol->enrol_user($instance, $USER->id, $roleid);
76 // Call the external function.
77 $enrolledusers = core_enrol_external::get_enrolled_users($course->id);
79 // Check we retrieve the good total number of enrolled users.
80 $this->assertEquals(3, count($enrolledusers));
82 // Call without required capability.
83 $this->unassignUserCapability('moodle/course:viewparticipants', $context->id, $roleid);
84 $this->setExpectedException('moodle_exception');
85 $categories = core_enrol_external::get_enrolled_users($course->id);
89 * Test get_users_courses
91 public function test_get_users_courses() {
94 $this->resetAfterTest(true);
96 $course1 = self::getDataGenerator()->create_course();
97 $course2 = self::getDataGenerator()->create_course();
98 $courses = array($course1, $course2);
100 // Enrol $USER in the courses.
101 // We use the manual plugin.
102 $enrol = enrol_get_plugin('manual');
104 foreach ($courses as $course) {
105 $context = context_course::instance($course->id);
106 $roleid = $this->assignUserCapability('moodle/course:viewparticipants',
107 $context->id, $roleid);
109 $enrolinstances = enrol_get_instances($course->id, true);
110 foreach ($enrolinstances as $courseenrolinstance) {
111 if ($courseenrolinstance->enrol == "manual") {
112 $instance = $courseenrolinstance;
116 $enrol->enrol_user($instance, $USER->id, $roleid);
119 // Call the external function.
120 $enrolledincourses = core_enrol_external::get_users_courses($USER->id);
122 // Check we retrieve the good total number of enrolled users.
123 $this->assertEquals(2, count($enrolledincourses));
128 * Role external PHPunit tests
130 * @package core_enrol
132 * @copyright 2012 Jerome Mouneyrac
133 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
136 class core_role_external_testcase extends externallib_advanced_testcase {
141 protected function setUp() {
143 require_once($CFG->dirroot . '/enrol/externallib.php');
149 public function test_assign_roles() {
152 $this->resetAfterTest(true);
154 $course = self::getDataGenerator()->create_course();
156 // Set the required capabilities by the external function.
157 $context = context_course::instance($course->id);
158 $roleid = $this->assignUserCapability('moodle/role:assign', $context->id);
159 $this->assignUserCapability('moodle/course:view', $context->id, $roleid);
161 // Add manager role to $USER.
162 // So $USER is allowed to assign 'manager', 'editingteacher', 'teacher' and 'student'.
163 role_assign(1, $USER->id, context_system::instance()->id);
165 // Check the teacher role has not been assigned to $USER.
166 $users = get_role_users(3, $context);
167 $this->assertEquals(count($users), 0);
169 // Call the external function. Assign teacher role to $USER.
170 core_role_external::assign_roles(array(
171 array('roleid' => 3, 'userid' => $USER->id, 'contextid' => $context->id)));
173 // Check the role has been assigned.
174 $users = get_role_users(3, $context);
175 $this->assertEquals(count($users), 1);
177 // Call without required capability.
178 $this->unassignUserCapability('moodle/role:assign', $context->id, $roleid);
179 $this->setExpectedException('moodle_exception');
180 $categories = core_role_external::assign_roles(
181 array('roleid' => 3, 'userid' => $USER->id, 'contextid' => $context->id));
185 * Test unassign_roles
187 public function test_unassign_roles() {
190 $this->resetAfterTest(true);
192 $course = self::getDataGenerator()->create_course();
194 // Set the required capabilities by the external function.
195 $context = context_course::instance($course->id);
196 $roleid = $this->assignUserCapability('moodle/role:assign', $context->id);
197 $this->assignUserCapability('moodle/course:view', $context->id, $roleid);
199 // Add manager role to $USER.
200 // So $USER is allowed to assign 'manager', 'editingteacher', 'teacher' and 'student'.
201 role_assign(1, $USER->id, context_system::instance()->id);
203 // Add teacher role to $USER on course context.
204 role_assign(3, $USER->id, $context->id);
206 // Check the teacher role has been assigned to $USER on course context.
207 $users = get_role_users(3, $context);
208 $this->assertEquals(count($users), 1);
210 // Call the external function. Assign teacher role to $USER.
211 core_role_external::unassign_roles(array(
212 array('roleid' => 3, 'userid' => $USER->id, 'contextid' => $context->id)));
214 // Check the role has been unassigned on course context.
215 $users = get_role_users(3, $context);
216 $this->assertEquals(count($users), 0);
218 // Call without required capability.
219 $this->unassignUserCapability('moodle/role:assign', $context->id, $roleid);
220 $this->setExpectedException('moodle_exception');
221 $categories = core_role_external::unassign_roles(
222 array('roleid' => 3, 'userid' => $USER->id, 'contextid' => $context->id));