MDL-34967 PHPunit test: enrol/externallib.php and enrol/manual/externallib.php
[moodle.git] / enrol / manual / 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 manual external PHPunit tests
19  *
20  * @package    enrol_manual
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/manual/externallib.php');
34 class enrol_manual_external_testcase extends externallib_advanced_testcase {
36     /**
37      * Test get_enrolled_users
38      */
39     public function test_enrol_users() {
40         global $USER, $CFG;
42         $this->resetAfterTest(true);
44         $course = self::getDataGenerator()->create_course();
45         $user1 = self::getDataGenerator()->create_user();
46         $user2 = self::getDataGenerator()->create_user();
48         // Set the required capabilities by the external function.
49         $context = context_course::instance($course->id);
50         $roleid = $this->assignUserCapability('enrol/manual:enrol', $context->id);
51         $this->assignUserCapability('moodle/course:view', $context->id, $roleid);
53         // Add manager role to $USER.
54         // So $USER is allowed to assign 'manager', 'editingteacher', 'teacher' and 'student'.
55         role_assign(1, $USER->id, context_system::instance()->id);
57         // Call the external function.
58         enrol_manual_external::enrol_users(array(
59             array('roleid' => 3, 'userid' => $user1->id, 'courseid' => $course->id),
60             array('roleid' => 3, 'userid' => $user2->id, 'courseid' => $course->id)
61         ));
63         // Check we retrieve the good total number of enrolled users.
64         require_once($CFG->dirroot . '/enrol/externallib.php');
65         $enrolledusers = core_enrol_external::get_enrolled_users($course->id);
66         $this->assertEquals(2, count($enrolledusers));
68         // Call without required capability.
69         $this->unassignUserCapability('enrol/manual:enrol', $context->id, $roleid);
70         $this->setExpectedException('moodle_exception');
71         $categories = enrol_manual_external::enrol_users($course->id);
72     }
73 }