MDL-34967 PHPunit test: enrol/externallib.php and enrol/manual/externallib.php
[moodle.git] / enrol / tests / externallib_test.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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.
13 //
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/>.
17 /**
18  * Enrol/Role external PHPunit tests
19  *
20  * @package    core_enrol
21  * @category   external
22  * @copyright  2012 Jerome Mouneyrac
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  * @since Moodle 2.4
25  */
27 defined('MOODLE_INTERNAL') || die();
29 global $CFG;
31 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
32 require_once($CFG->dirroot . '/enrol/externallib.php');
34 /**
35  * Enrol external PHPunit tests
36  *
37  * @package    core_enrol
38  * @category   external
39  * @copyright  2012 Jerome Mouneyrac
40  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41  * @since Moodle 2.4
42  */
43 class core_enrol_external_testcase extends externallib_advanced_testcase {
45     /**
46      * Test get_enrolled_users
47      */
48     public function test_get_enrolled_users() {
49         global $USER;
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;
69                 break;
70             }
71         }
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);
86     }
88     /**
89      * Test get_users_courses
90      */
91     public function test_get_users_courses() {
92         global $USER;
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');
103         $roleid = null;
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;
113                     break;
114                 }
115             }
116             $enrol->enrol_user($instance, $USER->id, $roleid);
117         }
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));
124     }
127 /**
128  * Role external PHPunit tests
129  *
130  * @package    core_enrol
131  * @category   external
132  * @copyright  2012 Jerome Mouneyrac
133  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
134  * @since Moodle 2.4
135  */
136 class core_role_external_testcase extends externallib_advanced_testcase {
138     /**
139      * Tests set up
140      */
141     protected function setUp() {
142         global $CFG;
143         require_once($CFG->dirroot . '/enrol/externallib.php');
144     }
146     /**
147      * Test assign_roles
148      */
149     public function test_assign_roles() {
150         global $USER;
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));
182     }
184     /**
185      * Test unassign_roles
186      */
187     public function test_unassign_roles() {
188         global $USER;
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));
223     }