See Release Notes
Long Term Support Release
Differences Between: [Versions 310 and 401] [Versions 39 and 401] [Versions 401 and 402] [Versions 401 and 403]
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_self; 18 19 use enrol_self_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/self/externallib.php'); 28 29 /** 30 * Self enrol external PHPunit tests 31 * 32 * @package enrol_self 33 * @copyright 2013 Rajesh Taneja <rajesh@moodle.com> 34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 35 * @since Moodle 2.6 36 */ 37 class externallib_test extends externallib_advanced_testcase { 38 39 /** 40 * Test get_instance_info 41 */ 42 public function test_get_instance_info() { 43 global $DB; 44 45 $this->resetAfterTest(true); 46 47 // Check if self enrolment plugin is enabled. 48 $selfplugin = enrol_get_plugin('self'); 49 $this->assertNotEmpty($selfplugin); 50 51 $studentrole = $DB->get_record('role', array('shortname'=>'student')); 52 $this->assertNotEmpty($studentrole); 53 54 $coursedata = new \stdClass(); 55 $coursedata->visible = 0; 56 $course = self::getDataGenerator()->create_course($coursedata); 57 58 // Add enrolment methods for course. 59 $instanceid1 = $selfplugin->add_instance($course, array('status' => ENROL_INSTANCE_ENABLED, 60 'name' => 'Test instance 1', 61 'customint6' => 1, 62 'roleid' => $studentrole->id)); 63 $instanceid2 = $selfplugin->add_instance($course, array('status' => ENROL_INSTANCE_DISABLED, 64 'customint6' => 1, 65 'name' => 'Test instance 2', 66 'roleid' => $studentrole->id)); 67 68 $instanceid3 = $selfplugin->add_instance($course, array('status' => ENROL_INSTANCE_ENABLED, 69 'roleid' => $studentrole->id, 70 'customint6' => 1, 71 'name' => 'Test instance 3', 72 'password' => 'test')); 73 74 $enrolmentmethods = $DB->get_records('enrol', array('courseid' => $course->id, 'status' => ENROL_INSTANCE_ENABLED)); 75 $this->assertCount(3, $enrolmentmethods); 76 77 $this->setAdminUser(); 78 $instanceinfo1 = enrol_self_external::get_instance_info($instanceid1); 79 $instanceinfo1 = \external_api::clean_returnvalue(enrol_self_external::get_instance_info_returns(), $instanceinfo1); 80 81 $this->assertEquals($instanceid1, $instanceinfo1['id']); 82 $this->assertEquals($course->id, $instanceinfo1['courseid']); 83 $this->assertEquals('self', $instanceinfo1['type']); 84 $this->assertEquals('Test instance 1', $instanceinfo1['name']); 85 $this->assertTrue($instanceinfo1['status']); 86 $this->assertFalse(isset($instanceinfo1['enrolpassword'])); 87 88 $instanceinfo2 = enrol_self_external::get_instance_info($instanceid2); 89 $instanceinfo2 = \external_api::clean_returnvalue(enrol_self_external::get_instance_info_returns(), $instanceinfo2); 90 $this->assertEquals($instanceid2, $instanceinfo2['id']); 91 $this->assertEquals($course->id, $instanceinfo2['courseid']); 92 $this->assertEquals('self', $instanceinfo2['type']); 93 $this->assertEquals('Test instance 2', $instanceinfo2['name']); 94 $this->assertEquals(get_string('canntenrol', 'enrol_self'), $instanceinfo2['status']); 95 $this->assertFalse(isset($instanceinfo2['enrolpassword'])); 96 97 $instanceinfo3 = enrol_self_external::get_instance_info($instanceid3); 98 $instanceinfo3 = \external_api::clean_returnvalue(enrol_self_external::get_instance_info_returns(), $instanceinfo3); 99 $this->assertEquals($instanceid3, $instanceinfo3['id']); 100 $this->assertEquals($course->id, $instanceinfo3['courseid']); 101 $this->assertEquals('self', $instanceinfo3['type']); 102 $this->assertEquals('Test instance 3', $instanceinfo3['name']); 103 $this->assertTrue($instanceinfo3['status']); 104 $this->assertEquals(get_string('password', 'enrol_self'), $instanceinfo3['enrolpassword']); 105 106 // Try to retrieve information using a normal user for a hidden course. 107 $user = self::getDataGenerator()->create_user(); 108 $this->setUser($user); 109 try { 110 enrol_self_external::get_instance_info($instanceid3); 111 } catch (\moodle_exception $e) { 112 $this->assertEquals('coursehidden', $e->errorcode); 113 } 114 } 115 116 /** 117 * Test enrol_user 118 */ 119 public function test_enrol_user() { 120 global $DB; 121 122 self::resetAfterTest(true); 123 124 $user = self::getDataGenerator()->create_user(); 125 self::setUser($user); 126 127 $course1 = self::getDataGenerator()->create_course(); 128 $course2 = self::getDataGenerator()->create_course(array('groupmode' => SEPARATEGROUPS, 'groupmodeforce' => 1)); 129 $user1 = self::getDataGenerator()->create_user(); 130 $user2 = self::getDataGenerator()->create_user(); 131 $user3 = self::getDataGenerator()->create_user(); 132 $user4 = self::getDataGenerator()->create_user(); 133 134 $context1 = \context_course::instance($course1->id); 135 $context2 = \context_course::instance($course2->id); 136 137 $selfplugin = enrol_get_plugin('self'); 138 $studentrole = $DB->get_record('role', array('shortname' => 'student')); 139 $instance1id = $selfplugin->add_instance($course1, array('status' => ENROL_INSTANCE_ENABLED, 140 'name' => 'Test instance 1', 141 'customint6' => 1, 142 'roleid' => $studentrole->id)); 143 $instance2id = $selfplugin->add_instance($course2, array('status' => ENROL_INSTANCE_DISABLED, 144 'customint6' => 1, 145 'name' => 'Test instance 2', 146 'roleid' => $studentrole->id)); 147 $instance1 = $DB->get_record('enrol', array('id' => $instance1id), '*', MUST_EXIST); 148 $instance2 = $DB->get_record('enrol', array('id' => $instance2id), '*', MUST_EXIST); 149 150 self::setUser($user1); 151 152 // Self enrol me. 153 $result = enrol_self_external::enrol_user($course1->id); 154 $result = \external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result); 155 156 self::assertTrue($result['status']); 157 self::assertEquals(1, $DB->count_records('user_enrolments', array('enrolid' => $instance1->id))); 158 self::assertTrue(is_enrolled($context1, $user1)); 159 160 // Add password. 161 $instance2->password = 'abcdef'; 162 $DB->update_record('enrol', $instance2); 163 164 // Try instance not enabled. 165 try { 166 enrol_self_external::enrol_user($course2->id); 167 } catch (\moodle_exception $e) { 168 self::assertEquals('canntenrol', $e->errorcode); 169 } 170 171 // Enable the instance. 172 $selfplugin->update_status($instance2, ENROL_INSTANCE_ENABLED); 173 174 // Try not passing a key. 175 $result = enrol_self_external::enrol_user($course2->id); 176 $result = \external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result); 177 self::assertFalse($result['status']); 178 self::assertCount(1, $result['warnings']); 179 self::assertEquals('4', $result['warnings'][0]['warningcode']); 180 181 // Try passing an invalid key. 182 $result = enrol_self_external::enrol_user($course2->id, 'invalidkey'); 183 $result = \external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result); 184 self::assertFalse($result['status']); 185 self::assertCount(1, $result['warnings']); 186 self::assertEquals('4', $result['warnings'][0]['warningcode']); 187 188 // Try passing an invalid key with hint. 189 $selfplugin->set_config('showhint', true); 190 $result = enrol_self_external::enrol_user($course2->id, 'invalidkey'); 191 $result = \external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result); 192 self::assertFalse($result['status']); 193 self::assertCount(1, $result['warnings']); 194 self::assertEquals('3', $result['warnings'][0]['warningcode']); 195 196 // Everything correct, now. 197 $result = enrol_self_external::enrol_user($course2->id, 'abcdef'); 198 $result = \external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result); 199 200 self::assertTrue($result['status']); 201 self::assertEquals(1, $DB->count_records('user_enrolments', array('enrolid' => $instance2->id))); 202 self::assertTrue(is_enrolled($context2, $user1)); 203 204 // Try group password now, other user. 205 $instance2->customint1 = 1; 206 $instance2->password = 'zyx'; 207 $DB->update_record('enrol', $instance2); 208 209 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course2->id)); 210 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'enrolmentkey' => 'zyx')); 211 212 self::setUser($user2); 213 // Try passing and invalid key for group. 214 $result = enrol_self_external::enrol_user($course2->id, 'invalidkey'); 215 $result = \external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result); 216 self::assertFalse($result['status']); 217 self::assertCount(1, $result['warnings']); 218 self::assertEquals('2', $result['warnings'][0]['warningcode']); 219 220 // Now, everything ok. 221 $result = enrol_self_external::enrol_user($course2->id, 'zyx'); 222 $result = \external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result); 223 224 self::assertTrue($result['status']); 225 self::assertEquals(2, $DB->count_records('user_enrolments', array('enrolid' => $instance2->id))); 226 self::assertTrue(is_enrolled($context2, $user2)); 227 228 // Try multiple instances now, multiple errors. 229 $instance3id = $selfplugin->add_instance($course2, array('status' => ENROL_INSTANCE_ENABLED, 230 'customint6' => 1, 231 'name' => 'Test instance 2', 232 'roleid' => $studentrole->id)); 233 $instance3 = $DB->get_record('enrol', array('id' => $instance3id), '*', MUST_EXIST); 234 $instance3->password = 'abcdef'; 235 $DB->update_record('enrol', $instance3); 236 237 self::setUser($user3); 238 $result = enrol_self_external::enrol_user($course2->id, 'invalidkey'); 239 $result = \external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result); 240 self::assertFalse($result['status']); 241 self::assertCount(2, $result['warnings']); 242 243 // Now, everything ok. 244 $result = enrol_self_external::enrol_user($course2->id, 'zyx'); 245 $result = \external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result); 246 self::assertTrue($result['status']); 247 self::assertTrue(is_enrolled($context2, $user3)); 248 249 // Now test passing an instance id. 250 self::setUser($user4); 251 $result = enrol_self_external::enrol_user($course2->id, 'abcdef', $instance3id); 252 $result = \external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result); 253 self::assertTrue($result['status']); 254 self::assertTrue(is_enrolled($context2, $user3)); 255 self::assertCount(0, $result['warnings']); 256 self::assertEquals(1, $DB->count_records('user_enrolments', array('enrolid' => $instance3->id))); 257 } 258 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body