See Release Notes
Long Term Support Release
Differences Between: [Versions 310 and 401] [Versions 39 and 401]
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/>. 16 17 namespace enrol_manual; 18 19 use enrol_manual_external; 20 use externallib_advanced_testcase; 21 22 defined('MOODLE_INTERNAL') || die(); 23 24 global $CFG; 25 26 require_once($CFG->dirroot . '/webservice/tests/helpers.php'); 27 require_once($CFG->dirroot . '/enrol/manual/externallib.php'); 28 29 /** 30 * Enrol manual external PHPunit tests 31 * 32 * @package enrol_manual 33 * @category phpunit 34 * @copyright 2012 Jerome Mouneyrac 35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 36 * @since Moodle 2.4 37 */ 38 class externallib_test extends externallib_advanced_testcase { 39 40 /** 41 * Test get_enrolled_users 42 */ 43 public function test_enrol_users() { 44 global $DB; 45 46 $this->resetAfterTest(true); 47 48 $user = self::getDataGenerator()->create_user(); 49 $this->setUser($user); 50 51 $course1 = self::getDataGenerator()->create_course(); 52 $course2 = self::getDataGenerator()->create_course(); 53 $user1 = self::getDataGenerator()->create_user(); 54 $user2 = self::getDataGenerator()->create_user(); 55 56 $context1 = \context_course::instance($course1->id); 57 $context2 = \context_course::instance($course2->id); 58 $instance1 = $DB->get_record('enrol', array('courseid' => $course1->id, 'enrol' => 'manual'), '*', MUST_EXIST); 59 $instance2 = $DB->get_record('enrol', array('courseid' => $course2->id, 'enrol' => 'manual'), '*', MUST_EXIST); 60 61 // Set the required capabilities by the external function. 62 $roleid = $this->assignUserCapability('enrol/manual:enrol', $context1->id); 63 $this->assignUserCapability('moodle/course:view', $context1->id, $roleid); 64 $this->assignUserCapability('moodle/role:assign', $context1->id, $roleid); 65 $this->assignUserCapability('enrol/manual:enrol', $context2->id, $roleid); 66 $this->assignUserCapability('moodle/course:view', $context2->id, $roleid); 67 $this->assignUserCapability('moodle/role:assign', $context2->id, $roleid); 68 69 core_role_set_assign_allowed($roleid, 3); 70 71 // Call the external function. 72 enrol_manual_external::enrol_users(array( 73 array('roleid' => 3, 'userid' => $user1->id, 'courseid' => $course1->id), 74 array('roleid' => 3, 'userid' => $user2->id, 'courseid' => $course1->id), 75 )); 76 77 $this->assertEquals(2, $DB->count_records('user_enrolments', array('enrolid' => $instance1->id))); 78 $this->assertEquals(0, $DB->count_records('user_enrolments', array('enrolid' => $instance2->id))); 79 $this->assertTrue(is_enrolled($context1, $user1)); 80 $this->assertTrue(is_enrolled($context1, $user2)); 81 82 // Call without required capability. 83 $DB->delete_records('user_enrolments'); 84 $this->unassignUserCapability('enrol/manual:enrol', $context1->id, $roleid); 85 try { 86 enrol_manual_external::enrol_users(array( 87 array('roleid' => 3, 'userid' => $user1->id, 'courseid' => $course1->id), 88 )); 89 $this->fail('Exception expected if not having capability to enrol'); 90 } catch (\moodle_exception $e) { 91 $this->assertInstanceOf('required_capability_exception', $e); 92 $this->assertSame('nopermissions', $e->errorcode); 93 } 94 $this->assignUserCapability('enrol/manual:enrol', $context1->id, $roleid); 95 $this->assertEquals(0, $DB->count_records('user_enrolments')); 96 97 // Call with forbidden role. 98 try { 99 enrol_manual_external::enrol_users(array( 100 array('roleid' => 1, 'userid' => $user1->id, 'courseid' => $course1->id), 101 )); 102 $this->fail('Exception expected if not allowed to assign role.'); 103 } catch (\moodle_exception $e) { 104 $this->assertSame('wsusercannotassign', $e->errorcode); 105 } 106 $this->assertEquals(0, $DB->count_records('user_enrolments')); 107 108 // Call for course without manual instance. 109 $DB->delete_records('user_enrolments'); 110 $DB->delete_records('enrol', array('courseid' => $course2->id)); 111 try { 112 enrol_manual_external::enrol_users(array( 113 array('roleid' => 3, 'userid' => $user1->id, 'courseid' => $course1->id), 114 array('roleid' => 3, 'userid' => $user1->id, 'courseid' => $course2->id), 115 )); 116 $this->fail('Exception expected if course does not have manual instance'); 117 } catch (\moodle_exception $e) { 118 $this->assertSame('wsnoinstance', $e->errorcode); 119 } 120 } 121 122 /** 123 * Test for unerolling a single user. 124 * @throws coding_exception 125 * @throws invalid_parameter_exception 126 * @throws moodle_exception 127 */ 128 public function test_unenrol_user_single() { 129 global $CFG, $DB; 130 require_once($CFG->libdir . '/enrollib.php'); 131 $this->resetAfterTest(true); 132 // The user who perform the action. 133 $user = $this->getDataGenerator()->create_user(); 134 $this->setUser($user); // Log this user in. 135 $enrol = enrol_get_plugin('manual'); 136 // Create a course. 137 $course = self::getDataGenerator()->create_course(); 138 $coursecontext = \context_course::instance($course->id); 139 $enrolinstance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'), '*', MUST_EXIST); 140 // Set the capability for the user. 141 $roleid = $this->assignUserCapability('enrol/manual:enrol', $coursecontext); 142 $this->assignUserCapability('enrol/manual:unenrol', $coursecontext, $roleid); 143 $this->assignUserCapability('moodle/course:view', $coursecontext, $roleid); 144 $this->assignUserCapability('moodle/role:assign', $coursecontext, $roleid); 145 // Create a student and enrol them into the course. 146 $student = $this->getDataGenerator()->create_user(); 147 $enrol->enrol_user($enrolinstance, $student->id); 148 $this->assertTrue(is_enrolled($coursecontext, $student)); 149 // Call the web service to unenrol. 150 enrol_manual_external::unenrol_users(array( 151 array('userid' => $student->id, 'courseid' => $course->id), 152 )); 153 $this->assertFalse(is_enrolled($coursecontext, $student)); 154 } 155 156 /** 157 * Test for unenrolling multiple users. 158 * @throws coding_exception 159 * @throws invalid_parameter_exception 160 * @throws moodle_exception 161 */ 162 public function test_unenrol_user_multiple() { 163 global $CFG, $DB; 164 require_once($CFG->libdir . '/enrollib.php'); 165 $this->resetAfterTest(true); 166 // The user who perform the action. 167 $user = $this->getDataGenerator()->create_user(); 168 $this->setUser($user); // Log this user in. 169 // Create a course. 170 $course = self::getDataGenerator()->create_course(); 171 $coursecontext = \context_course::instance($course->id); 172 $enrolinstance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'), '*', MUST_EXIST); 173 // Set the capability for the user. 174 $roleid = $this->assignUserCapability('enrol/manual:enrol', $coursecontext); 175 $this->assignUserCapability('enrol/manual:unenrol', $coursecontext, $roleid); 176 $this->assignUserCapability('moodle/course:view', $coursecontext, $roleid); 177 $this->assignUserCapability('moodle/role:assign', $coursecontext, $roleid); 178 $enrol = enrol_get_plugin('manual'); 179 // Create a student and enrol them into the course. 180 $student1 = $this->getDataGenerator()->create_user(); 181 $enrol->enrol_user($enrolinstance, $student1->id); 182 $this->assertTrue(is_enrolled($coursecontext, $student1)); 183 $student2 = $this->getDataGenerator()->create_user(); 184 $enrol->enrol_user($enrolinstance, $student2->id); 185 $this->assertTrue(is_enrolled($coursecontext, $student2)); 186 // Call the web service to unenrol. 187 enrol_manual_external::unenrol_users(array( 188 array('userid' => $student1->id, 'courseid' => $course->id), 189 array('userid' => $student2->id, 'courseid' => $course->id), 190 )); 191 $this->assertFalse(is_enrolled($coursecontext, $student1)); 192 $this->assertFalse(is_enrolled($coursecontext, $student2)); 193 } 194 195 /** 196 * Test for unenrol capability. 197 * @throws coding_exception 198 * @throws invalid_parameter_exception 199 * @throws moodle_exception 200 */ 201 public function test_unenrol_user_error_no_capability() { 202 global $CFG, $DB; 203 require_once($CFG->libdir . '/enrollib.php'); 204 $this->resetAfterTest(true); 205 // The user who perform the action. 206 $user = $this->getDataGenerator()->create_user(); 207 $this->setUser($user); // Log this user in. 208 // Create a course. 209 $course = self::getDataGenerator()->create_course(); 210 $coursecontext = \context_course::instance($course->id); 211 $enrolinstance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'), '*', MUST_EXIST); 212 $enrol = enrol_get_plugin('manual'); 213 // Create a student and enrol them into the course. 214 $student = $this->getDataGenerator()->create_user(); 215 $enrol->enrol_user($enrolinstance, $student->id); 216 $this->assertTrue(is_enrolled($coursecontext, $student)); 217 // Call the web service to unenrol. 218 try { 219 enrol_manual_external::unenrol_users(array( 220 array('userid' => $student->id, 'courseid' => $course->id), 221 )); 222 $this->fail('Exception expected: User cannot log in to the course'); 223 } catch (\Exception $ex) { 224 $this->assertTrue($ex instanceof \require_login_exception); 225 } 226 // Set the capability for the course, then try again. 227 $roleid = $this->assignUserCapability('moodle/course:view', $coursecontext); 228 try { 229 enrol_manual_external::unenrol_users(array( 230 array('userid' => $student->id, 'courseid' => $course->id), 231 )); 232 $this->fail('Exception expected: User cannot log in to the course'); 233 } catch (\Exception $ex) { 234 $this->assertTrue($ex instanceof \required_capability_exception); 235 } 236 // Assign unenrol capability. 237 $this->assignUserCapability('enrol/manual:unenrol', $coursecontext, $roleid); 238 enrol_manual_external::unenrol_users(array( 239 array('userid' => $student->id, 'courseid' => $course->id), 240 )); 241 $this->assertFalse(is_enrolled($coursecontext, $student)); 242 } 243 244 /** 245 * Test for unenrol if user does not exist. 246 * @throws coding_exception 247 */ 248 public function test_unenrol_user_error_not_exist() { 249 global $CFG, $DB; 250 require_once($CFG->libdir . '/enrollib.php'); 251 $this->resetAfterTest(true); 252 // The user who perform the action. 253 $user = $this->getDataGenerator()->create_user(); 254 $this->setUser($user); // Log this user in. 255 $enrol = enrol_get_plugin('manual'); 256 // Create a course. 257 $course = self::getDataGenerator()->create_course(); 258 $coursecontext = \context_course::instance($course->id); 259 $enrolinstance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'), '*', MUST_EXIST); 260 // Set the capability for the user. 261 $roleid = $this->assignUserCapability('enrol/manual:enrol', $coursecontext); 262 $this->assignUserCapability('enrol/manual:unenrol', $coursecontext, $roleid); 263 $this->assignUserCapability('moodle/course:view', $coursecontext, $roleid); 264 $this->assignUserCapability('moodle/role:assign', $coursecontext, $roleid); 265 // Create a student and enrol them into the course. 266 $student = $this->getDataGenerator()->create_user(); 267 $enrol->enrol_user($enrolinstance, $student->id); 268 $this->assertTrue(is_enrolled($coursecontext, $student)); 269 try { 270 enrol_manual_external::unenrol_users(array( 271 array('userid' => $student->id + 1, 'courseid' => $course->id), 272 )); 273 $this->fail('Exception expected: invalid student id'); 274 } catch (\Exception $ex) { 275 $this->assertTrue($ex instanceof \invalid_parameter_exception); 276 } 277 $DB->delete_records('enrol', array('id' => $enrolinstance->id)); 278 try { 279 enrol_manual_external::unenrol_users(array( 280 array('userid' => $student->id + 1, 'courseid' => $course->id), 281 )); 282 $this->fail('Exception expected: invalid student id'); 283 } catch (\Exception $ex) { 284 $this->assertTrue($ex instanceof \moodle_exception); 285 } 286 } 287 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body