Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]
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 /** 18 * Manual enrolment tests. 19 * 20 * @package enrol_manual 21 * @category phpunit 22 * @copyright 2012 Petr Skoda {@link http://skodak.org} 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 29 /** 30 * Manual enrolment tests. 31 * 32 * @package enrol_manual 33 * @category phpunit 34 * @copyright 2012 Petr Skoda {@link http://skodak.org} 35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 36 */ 37 class enrol_manual_lib_testcase extends advanced_testcase { 38 /** 39 * Test enrol migration function used when uninstalling enrol plugins. 40 */ 41 public function test_migrate_plugin_enrolments() { 42 global $DB, $CFG; 43 require_once($CFG->dirroot.'/enrol/manual/locallib.php'); 44 45 $this->resetAfterTest(); 46 47 /** @var $manplugin enrol_manual_plugin */ 48 $manplugin = enrol_get_plugin('manual'); 49 50 // Setup a few courses and users. 51 52 $studentrole = $DB->get_record('role', array('shortname'=>'student')); 53 $this->assertNotEmpty($studentrole); 54 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher')); 55 $this->assertNotEmpty($teacherrole); 56 57 $course1 = $this->getDataGenerator()->create_course(); 58 $course2 = $this->getDataGenerator()->create_course(); 59 $course3 = $this->getDataGenerator()->create_course(); 60 $course4 = $this->getDataGenerator()->create_course(); 61 $course5 = $this->getDataGenerator()->create_course(); 62 63 $context1 = context_course::instance($course1->id); 64 $context2 = context_course::instance($course2->id); 65 $context3 = context_course::instance($course3->id); 66 $context4 = context_course::instance($course4->id); 67 68 $user1 = $this->getDataGenerator()->create_user(); 69 $user2 = $this->getDataGenerator()->create_user(); 70 $user3 = $this->getDataGenerator()->create_user(); 71 $user4 = $this->getDataGenerator()->create_user(); 72 73 // We expect manual, self and guest instances to be created by default. 74 75 $this->assertEquals(5, $DB->count_records('enrol', array('enrol'=>'manual'))); 76 $this->assertEquals(5, $DB->count_records('enrol', array('enrol'=>'self'))); 77 $this->assertEquals(5, $DB->count_records('enrol', array('enrol'=>'guest'))); 78 $this->assertEquals(15, $DB->count_records('enrol', array())); 79 80 $this->assertEquals(0, $DB->count_records('user_enrolments', array())); 81 82 // Enrol some users to manual instances. 83 84 $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST); 85 $DB->set_field('enrol', 'status', ENROL_INSTANCE_DISABLED, array('id'=>$maninstance1->id)); 86 $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST); 87 $maninstance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST); 88 $DB->delete_records('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual')); 89 $DB->delete_records('enrol', array('courseid'=>$course4->id, 'enrol'=>'manual')); 90 $DB->delete_records('enrol', array('courseid'=>$course5->id, 'enrol'=>'manual')); 91 92 $manplugin->enrol_user($maninstance1, $user1->id, $studentrole->id); 93 $manplugin->enrol_user($maninstance1, $user2->id, $studentrole->id); 94 $manplugin->enrol_user($maninstance1, $user3->id, $teacherrole->id); 95 $manplugin->enrol_user($maninstance2, $user3->id, $teacherrole->id); 96 97 $this->assertEquals(4, $DB->count_records('user_enrolments', array())); 98 99 // Set up some bogus enrol plugin instances and enrolments. 100 101 $xxxinstance1 = $DB->insert_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'xxx', 'status'=>ENROL_INSTANCE_ENABLED)); 102 $xxxinstance1 = $DB->get_record('enrol', array('id'=>$xxxinstance1)); 103 $xxxinstance3 = $DB->insert_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'xxx', 'status'=>ENROL_INSTANCE_DISABLED)); 104 $xxxinstance3 = $DB->get_record('enrol', array('id'=>$xxxinstance3)); 105 $xxxinstance4 = $DB->insert_record('enrol', array('courseid'=>$course4->id, 'enrol'=>'xxx', 'status'=>ENROL_INSTANCE_ENABLED)); 106 $xxxinstance4 = $DB->get_record('enrol', array('id'=>$xxxinstance4)); 107 $xxxinstance4b = $DB->insert_record('enrol', array('courseid'=>$course4->id, 'enrol'=>'xxx', 'status'=>ENROL_INSTANCE_DISABLED)); 108 $xxxinstance4b = $DB->get_record('enrol', array('id'=>$xxxinstance4b)); 109 110 111 $DB->insert_record('user_enrolments', array('enrolid'=>$xxxinstance1->id, 'userid'=>$user1->id, 'status'=>ENROL_USER_SUSPENDED)); 112 role_assign($studentrole->id, $user1->id, $context1->id, 'enrol_xxx', $xxxinstance1->id); 113 role_assign($teacherrole->id, $user1->id, $context1->id, 'enrol_xxx', $xxxinstance1->id); 114 $DB->insert_record('user_enrolments', array('enrolid'=>$xxxinstance1->id, 'userid'=>$user4->id, 'status'=>ENROL_USER_ACTIVE)); 115 role_assign($studentrole->id, $user4->id, $context1->id, 'enrol_xxx', $xxxinstance1->id); 116 $this->assertEquals(2, $DB->count_records('user_enrolments', array('enrolid'=>$xxxinstance1->id))); 117 $this->assertEquals(6, $DB->count_records('role_assignments', array('contextid'=>$context1->id))); 118 119 120 $DB->insert_record('user_enrolments', array('enrolid'=>$xxxinstance3->id, 'userid'=>$user1->id, 'status'=>ENROL_USER_ACTIVE)); 121 role_assign($studentrole->id, $user1->id, $context3->id, 'enrol_xxx', $xxxinstance3->id); 122 $DB->insert_record('user_enrolments', array('enrolid'=>$xxxinstance3->id, 'userid'=>$user2->id, 'status'=>ENROL_USER_SUSPENDED)); 123 $this->assertEquals(2, $DB->count_records('user_enrolments', array('enrolid'=>$xxxinstance3->id))); 124 $this->assertEquals(1, $DB->count_records('role_assignments', array('contextid'=>$context3->id))); 125 126 $DB->insert_record('user_enrolments', array('enrolid'=>$xxxinstance4->id, 'userid'=>$user1->id, 'status'=>ENROL_USER_ACTIVE)); 127 role_assign($studentrole->id, $user1->id, $context4->id, 'enrol_xxx', $xxxinstance4->id); 128 $DB->insert_record('user_enrolments', array('enrolid'=>$xxxinstance4->id, 'userid'=>$user2->id, 'status'=>ENROL_USER_ACTIVE)); 129 role_assign($studentrole->id, $user2->id, $context4->id, 'enrol_xxx', $xxxinstance4->id); 130 $DB->insert_record('user_enrolments', array('enrolid'=>$xxxinstance4b->id, 'userid'=>$user1->id, 'status'=>ENROL_USER_SUSPENDED)); 131 role_assign($teacherrole->id, $user1->id, $context4->id, 'enrol_xxx', $xxxinstance4b->id); 132 $DB->insert_record('user_enrolments', array('enrolid'=>$xxxinstance4b->id, 'userid'=>$user4->id, 'status'=>ENROL_USER_ACTIVE)); 133 role_assign($teacherrole->id, $user4->id, $context4->id, 'enrol_xxx', $xxxinstance4b->id); 134 $this->assertEquals(2, $DB->count_records('user_enrolments', array('enrolid'=>$xxxinstance4->id))); 135 $this->assertEquals(2, $DB->count_records('user_enrolments', array('enrolid'=>$xxxinstance4b->id))); 136 $this->assertEquals(4, $DB->count_records('role_assignments', array('contextid'=>$context4->id))); 137 138 // Finally do the migration. 139 140 enrol_manual_migrate_plugin_enrolments('xxx'); 141 142 // Verify results. 143 144 $this->assertEquals(1, $DB->count_records('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'))); 145 $this->assertEquals(1, $DB->count_records('enrol', array('courseid'=>$course1->id, 'enrol'=>'xxx'))); 146 $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST); 147 $this->assertEquals(ENROL_INSTANCE_DISABLED, $maninstance1->status); 148 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance1->id, 'userid'=>$user1->id, 'status'=>ENROL_USER_ACTIVE))); 149 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance1->id, 'userid'=>$user2->id, 'status'=>ENROL_USER_ACTIVE))); 150 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance1->id, 'userid'=>$user3->id, 'status'=>ENROL_USER_ACTIVE))); 151 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance1->id, 'userid'=>$user4->id, 'status'=>ENROL_USER_ACTIVE))); 152 $this->assertEquals(4, $DB->count_records('user_enrolments', array('enrolid'=>$maninstance1->id))); 153 $this->assertEquals(0, $DB->count_records('user_enrolments', array('enrolid'=>$xxxinstance1->id))); 154 $this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>'', 'userid'=>$user1->id, 'roleid'=>$studentrole->id, 'contextid'=>$context1->id))); 155 $this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>'', 'userid'=>$user1->id, 'roleid'=>$teacherrole->id, 'contextid'=>$context1->id))); 156 $this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>'', 'userid'=>$user2->id, 'roleid'=>$studentrole->id, 'contextid'=>$context1->id))); 157 $this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>'', 'userid'=>$user3->id, 'roleid'=>$teacherrole->id, 'contextid'=>$context1->id))); 158 $this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>'', 'userid'=>$user4->id, 'roleid'=>$studentrole->id, 'contextid'=>$context1->id))); 159 $this->assertEquals(5, $DB->count_records('role_assignments', array('contextid'=>$context1->id))); 160 161 162 $this->assertEquals(1, $DB->count_records('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'))); 163 $this->assertEquals(0, $DB->count_records('enrol', array('courseid'=>$course2->id, 'enrol'=>'xxx'))); 164 $maninstance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST); 165 $this->assertEquals(ENROL_INSTANCE_ENABLED, $maninstance2->status); 166 167 168 $this->assertEquals(1, $DB->count_records('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'))); 169 $this->assertEquals(1, $DB->count_records('enrol', array('courseid'=>$course3->id, 'enrol'=>'xxx'))); 170 $maninstance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST); 171 $this->assertEquals(ENROL_INSTANCE_DISABLED, $maninstance3->status); 172 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance3->id, 'userid'=>$user1->id, 'status'=>ENROL_USER_ACTIVE))); 173 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance3->id, 'userid'=>$user2->id, 'status'=>ENROL_USER_SUSPENDED))); 174 $this->assertEquals(2, $DB->count_records('user_enrolments', array('enrolid'=>$maninstance3->id))); 175 $this->assertEquals(0, $DB->count_records('user_enrolments', array('enrolid'=>$xxxinstance3->id))); 176 $this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>'', 'userid'=>$user1->id, 'roleid'=>$studentrole->id, 'contextid'=>$context3->id))); 177 $this->assertEquals(1, $DB->count_records('role_assignments', array('contextid'=>$context3->id))); 178 179 180 $this->assertEquals(1, $DB->count_records('enrol', array('courseid'=>$course4->id, 'enrol'=>'manual'))); 181 $this->assertEquals(2, $DB->count_records('enrol', array('courseid'=>$course4->id, 'enrol'=>'xxx'))); 182 $maninstance4 = $DB->get_record('enrol', array('courseid'=>$course4->id, 'enrol'=>'manual'), '*', MUST_EXIST); 183 $this->assertEquals(ENROL_INSTANCE_ENABLED, $maninstance4->status); 184 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance4->id, 'userid'=>$user1->id, 'status'=>ENROL_USER_ACTIVE))); 185 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance4->id, 'userid'=>$user2->id, 'status'=>ENROL_USER_ACTIVE))); 186 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance4->id, 'userid'=>$user4->id, 'status'=>ENROL_USER_SUSPENDED))); 187 $this->assertEquals(3, $DB->count_records('user_enrolments', array('enrolid'=>$maninstance4->id))); 188 $this->assertEquals(0, $DB->count_records('user_enrolments', array('enrolid'=>$xxxinstance4->id))); 189 $this->assertEquals(0, $DB->count_records('user_enrolments', array('enrolid'=>$xxxinstance4b->id))); 190 $this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>'', 'userid'=>$user1->id, 'roleid'=>$studentrole->id, 'contextid'=>$context4->id))); 191 $this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>'', 'userid'=>$user1->id, 'roleid'=>$teacherrole->id, 'contextid'=>$context4->id))); 192 $this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>'', 'userid'=>$user2->id, 'roleid'=>$studentrole->id, 'contextid'=>$context4->id))); 193 $this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>'', 'userid'=>$user4->id, 'roleid'=>$teacherrole->id, 'contextid'=>$context4->id))); 194 $this->assertEquals(4, $DB->count_records('role_assignments', array('contextid'=>$context4->id))); 195 196 197 $this->assertEquals(0, $DB->count_records('enrol', array('courseid'=>$course5->id, 'enrol'=>'manual'))); 198 $this->assertEquals(0, $DB->count_records('enrol', array('courseid'=>$course5->id, 'enrol'=>'xxx'))); 199 200 // Make sure wrong params do not produce errors or notices. 201 202 enrol_manual_migrate_plugin_enrolments('manual'); 203 enrol_manual_migrate_plugin_enrolments('yyyy'); 204 } 205 206 public function test_expired() { 207 global $DB; 208 $this->resetAfterTest(); 209 210 /** @var $manualplugin enrol_manual_plugin */ 211 $manualplugin = enrol_get_plugin('manual'); 212 213 $trace = new null_progress_trace(); 214 215 $now = time(); 216 217 // Prepare some data. 218 219 $studentrole = $DB->get_record('role', array('shortname'=>'student')); 220 $this->assertNotEmpty($studentrole); 221 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher')); 222 $this->assertNotEmpty($teacherrole); 223 $managerrole = $DB->get_record('role', array('shortname'=>'manager')); 224 $this->assertNotEmpty($managerrole); 225 226 $user1 = $this->getDataGenerator()->create_user(); 227 $user2 = $this->getDataGenerator()->create_user(); 228 $user3 = $this->getDataGenerator()->create_user(); 229 $user4 = $this->getDataGenerator()->create_user(); 230 231 $course1 = $this->getDataGenerator()->create_course(); 232 $course2 = $this->getDataGenerator()->create_course(); 233 $course3 = $this->getDataGenerator()->create_course(); 234 $context1 = context_course::instance($course1->id); 235 $context2 = context_course::instance($course2->id); 236 $context3 = context_course::instance($course3->id); 237 238 $this->assertEquals(3, $DB->count_records('enrol', array('enrol'=>'manual'))); 239 $instance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST); 240 $this->assertEquals($studentrole->id, $instance1->roleid); 241 $instance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST); 242 $this->assertEquals($studentrole->id, $instance2->roleid); 243 $instance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST); 244 $this->assertEquals($studentrole->id, $instance3->roleid); 245 246 $this->assertEquals(0, $DB->count_records('user_enrolments')); 247 $this->assertEquals(0, $DB->count_records('role_assignments')); 248 249 $manualplugin->enrol_user($instance1, $user1->id, $studentrole->id); 250 $manualplugin->enrol_user($instance1, $user2->id, $studentrole->id); 251 $manualplugin->enrol_user($instance1, $user3->id, $studentrole->id, 0, $now-60); 252 253 $manualplugin->enrol_user($instance3, $user1->id, $studentrole->id, 0, 0); 254 $manualplugin->enrol_user($instance3, $user2->id, $studentrole->id, 0, $now+60*60); 255 $manualplugin->enrol_user($instance3, $user3->id, $teacherrole->id, 0, $now-60*60); 256 257 role_assign($managerrole->id, $user4->id, $context1->id); 258 259 $this->assertEquals(6, $DB->count_records('user_enrolments')); 260 $this->assertEquals(7, $DB->count_records('role_assignments')); 261 $this->assertEquals(5, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id))); 262 $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id))); 263 $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$managerrole->id))); 264 265 // Execute tests. 266 267 $this->assertEquals(ENROL_EXT_REMOVED_KEEP, $manualplugin->get_config('expiredaction')); 268 $manualplugin->sync($trace, null); 269 $this->assertEquals(6, $DB->count_records('user_enrolments')); 270 $this->assertEquals(7, $DB->count_records('role_assignments')); 271 272 273 $manualplugin->set_config('expiredaction', ENROL_EXT_REMOVED_SUSPENDNOROLES); 274 $manualplugin->sync($trace, $course2->id); 275 $this->assertEquals(6, $DB->count_records('user_enrolments')); 276 $this->assertEquals(7, $DB->count_records('role_assignments')); 277 278 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user3->id, 'roleid'=>$studentrole->id))); 279 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context3->id, 'userid'=>$user3->id, 'roleid'=>$teacherrole->id))); 280 $manualplugin->sync($trace, null); 281 $this->assertEquals(6, $DB->count_records('user_enrolments')); 282 $this->assertEquals(5, $DB->count_records('role_assignments')); 283 $this->assertEquals(4, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id))); 284 $this->assertEquals(0, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id))); 285 $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user3->id, 'roleid'=>$studentrole->id))); 286 $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context3->id, 'userid'=>$user3->id, 'roleid'=>$teacherrole->id))); 287 288 289 $manualplugin->set_config('expiredaction', ENROL_EXT_REMOVED_UNENROL); 290 291 role_assign($studentrole->id, $user3->id, $context1->id); 292 role_assign($teacherrole->id, $user3->id, $context3->id); 293 $this->assertEquals(6, $DB->count_records('user_enrolments')); 294 $this->assertEquals(7, $DB->count_records('role_assignments')); 295 $this->assertEquals(5, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id))); 296 $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id))); 297 $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$managerrole->id))); 298 299 $manualplugin->sync($trace, null); 300 $this->assertEquals(4, $DB->count_records('user_enrolments')); 301 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id, 'userid'=>$user3->id))); 302 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance3->id, 'userid'=>$user3->id))); 303 $this->assertEquals(5, $DB->count_records('role_assignments')); 304 $this->assertEquals(4, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id))); 305 $this->assertEquals(0, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id))); 306 $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$managerrole->id))); 307 308 309 $manualplugin->set_config('expiredaction', ENROL_EXT_REMOVED_SUSPEND); 310 $manualplugin->enrol_user($instance1, $user3->id, $studentrole->id, 0, $now-60); 311 $manualplugin->enrol_user($instance3, $user3->id, $teacherrole->id, 0, $now-60*60); 312 $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST); 313 $maninstance2 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST); 314 315 $this->assertEquals(6, $DB->count_records('user_enrolments')); 316 $this->assertEquals(7, $DB->count_records('role_assignments')); 317 $this->assertEquals(5, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id))); 318 $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id))); 319 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance1->id, 'userid'=>$user3->id, 'status'=>ENROL_USER_ACTIVE))); 320 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance2->id, 'userid'=>$user3->id, 'status'=>ENROL_USER_ACTIVE))); 321 322 $manualplugin->sync($trace, null); 323 $this->assertEquals(6, $DB->count_records('user_enrolments')); 324 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id, 'userid'=>$user3->id))); 325 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$instance3->id, 'userid'=>$user3->id))); 326 $this->assertEquals(7, $DB->count_records('role_assignments')); 327 $this->assertEquals(5, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id))); 328 $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id))); 329 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance1->id, 'userid'=>$user3->id, 'status'=>ENROL_USER_SUSPENDED))); 330 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance2->id, 'userid'=>$user3->id, 'status'=>ENROL_USER_SUSPENDED))); 331 } 332 333 public function test_send_expiry_notifications() { 334 global $DB, $CFG; 335 $this->resetAfterTest(); 336 $this->preventResetByRollback(); // Messaging does not like transactions... 337 338 $trace = new null_progress_trace(); 339 340 /** @var $manualplugin enrol_manual_plugin */ 341 $manualplugin = enrol_get_plugin('manual'); 342 $now = time(); 343 $admin = get_admin(); 344 345 // Note: hopefully nobody executes the unit tests the last second before midnight... 346 347 $manualplugin->set_config('expirynotifylast', $now - 60*60*24); 348 $manualplugin->set_config('expirynotifyhour', 0); 349 350 $studentrole = $DB->get_record('role', array('shortname'=>'student')); 351 $this->assertNotEmpty($studentrole); 352 $editingteacherrole = $DB->get_record('role', array('shortname'=>'editingteacher')); 353 $this->assertNotEmpty($editingteacherrole); 354 $managerrole = $DB->get_record('role', array('shortname'=>'manager')); 355 $this->assertNotEmpty($managerrole); 356 357 $user1 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser1')); 358 $user2 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser2')); 359 $user3 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser3')); 360 $user4 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser4')); 361 $user5 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser5')); 362 $user6 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser6')); 363 $user7 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser6')); 364 $user8 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser6')); 365 366 $course1 = $this->getDataGenerator()->create_course(array('fullname'=>'xcourse1')); 367 $course2 = $this->getDataGenerator()->create_course(array('fullname'=>'xcourse2')); 368 $course3 = $this->getDataGenerator()->create_course(array('fullname'=>'xcourse3')); 369 $course4 = $this->getDataGenerator()->create_course(array('fullname'=>'xcourse4')); 370 371 $this->assertEquals(4, $DB->count_records('enrol', array('enrol'=>'manual'))); 372 373 $instance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST); 374 $instance1->expirythreshold = 60*60*24*4; 375 $instance1->expirynotify = 1; 376 $instance1->notifyall = 1; 377 $DB->update_record('enrol', $instance1); 378 379 $instance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST); 380 $instance2->expirythreshold = 60*60*24*1; 381 $instance2->expirynotify = 1; 382 $instance2->notifyall = 1; 383 $DB->update_record('enrol', $instance2); 384 385 $instance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST); 386 $instance3->expirythreshold = 60*60*24*1; 387 $instance3->expirynotify = 1; 388 $instance3->notifyall = 0; 389 $DB->update_record('enrol', $instance3); 390 391 $instance4 = $DB->get_record('enrol', array('courseid'=>$course4->id, 'enrol'=>'manual'), '*', MUST_EXIST); 392 $instance4->expirythreshold = 60*60*24*1; 393 $instance4->expirynotify = 0; 394 $instance4->notifyall = 0; 395 $DB->update_record('enrol', $instance4); 396 397 $manualplugin->enrol_user($instance1, $user1->id, $editingteacherrole->id, 0, $now + 60*60*24*1, ENROL_USER_SUSPENDED); // Suspended users are not notified. 398 $manualplugin->enrol_user($instance1, $user2->id, $studentrole->id, 0, $now + 60*60*24*5); // Above threshold are not notified. 399 $manualplugin->enrol_user($instance1, $user3->id, $studentrole->id, 0, $now + 60*60*24*3 + 60*60); // Less than one day after threshold - should be notified. 400 $manualplugin->enrol_user($instance1, $user4->id, $studentrole->id, 0, $now + 60*60*24*4 - 60*3); // Less than one day after threshold - should be notified. 401 $manualplugin->enrol_user($instance1, $user5->id, $studentrole->id, 0, $now + 60*60); // Should have been already notified. 402 $manualplugin->enrol_user($instance1, $user6->id, $studentrole->id, 0, $now - 60); // Already expired. 403 $manualplugin->enrol_user($instance1, $user7->id, $editingteacherrole->id); 404 $manualplugin->enrol_user($instance1, $user8->id, $managerrole->id); // Highest role --> enroller. 405 406 $manualplugin->enrol_user($instance2, $user1->id, $studentrole->id); 407 $manualplugin->enrol_user($instance2, $user2->id, $studentrole->id, 0, $now + 60*60*24*1 + 60*3); // Above threshold are not notified. 408 $manualplugin->enrol_user($instance2, $user3->id, $studentrole->id, 0, $now + 60*60*24*1 - 60*60); // Less than one day after threshold - should be notified. 409 410 $manualplugin->enrol_user($instance3, $user1->id, $editingteacherrole->id); 411 $manualplugin->enrol_user($instance3, $user2->id, $studentrole->id, 0, $now + 60*60*24*1 + 60); // Above threshold are not notified. 412 $manualplugin->enrol_user($instance3, $user3->id, $studentrole->id, 0, $now + 60*60*24*1 - 60*60); // Less than one day after threshold - should be notified. 413 414 $manualplugin->enrol_user($instance4, $user4->id, $editingteacherrole->id); 415 $manualplugin->enrol_user($instance4, $user5->id, $studentrole->id, 0, $now + 60*60*24*1 + 60); 416 $manualplugin->enrol_user($instance4, $user6->id, $studentrole->id, 0, $now + 60*60*24*1 - 60*60); 417 418 // The notification is sent out in fixed order first individual users, 419 // then summary per course by enrolid, user lastname, etc. 420 $this->assertGreaterThan($instance1->id, $instance2->id); 421 $this->assertGreaterThan($instance2->id, $instance3->id); 422 423 $sink = $this->redirectMessages(); 424 425 $manualplugin->send_expiry_notifications($trace); 426 427 $messages = $sink->get_messages(); 428 429 $this->assertEquals(2+1 + 1+1 + 1 + 0, count($messages)); 430 431 // First individual notifications from course1. 432 $this->assertEquals($user3->id, $messages[0]->useridto); 433 $this->assertEquals($user8->id, $messages[0]->useridfrom); 434 $this->assertStringContainsString('xcourse1', $messages[0]->fullmessagehtml); 435 436 $this->assertEquals($user4->id, $messages[1]->useridto); 437 $this->assertEquals($user8->id, $messages[1]->useridfrom); 438 $this->assertStringContainsString('xcourse1', $messages[1]->fullmessagehtml); 439 440 // Then summary for course1. 441 $this->assertEquals($user8->id, $messages[2]->useridto); 442 $this->assertEquals($admin->id, $messages[2]->useridfrom); 443 $this->assertStringContainsString('xcourse1', $messages[2]->fullmessagehtml); 444 $this->assertStringNotContainsString('xuser1', $messages[2]->fullmessagehtml); 445 $this->assertStringNotContainsString('xuser2', $messages[2]->fullmessagehtml); 446 $this->assertStringContainsString('xuser3', $messages[2]->fullmessagehtml); 447 $this->assertStringContainsString('xuser4', $messages[2]->fullmessagehtml); 448 $this->assertStringContainsString('xuser5', $messages[2]->fullmessagehtml); 449 $this->assertStringNotContainsString('xuser6', $messages[2]->fullmessagehtml); 450 451 // First individual notifications from course2. 452 $this->assertEquals($user3->id, $messages[3]->useridto); 453 $this->assertEquals($admin->id, $messages[3]->useridfrom); 454 $this->assertStringContainsString('xcourse2', $messages[3]->fullmessagehtml); 455 456 // Then summary for course2. 457 $this->assertEquals($admin->id, $messages[4]->useridto); 458 $this->assertEquals($admin->id, $messages[4]->useridfrom); 459 $this->assertStringContainsString('xcourse2', $messages[4]->fullmessagehtml); 460 $this->assertStringNotContainsString('xuser1', $messages[4]->fullmessagehtml); 461 $this->assertStringNotContainsString('xuser2', $messages[4]->fullmessagehtml); 462 $this->assertStringContainsString('xuser3', $messages[4]->fullmessagehtml); 463 $this->assertStringNotContainsString('xuser4', $messages[4]->fullmessagehtml); 464 $this->assertStringNotContainsString('xuser5', $messages[4]->fullmessagehtml); 465 $this->assertStringNotContainsString('xuser6', $messages[4]->fullmessagehtml); 466 467 // Only summary in course3. 468 $this->assertEquals($user1->id, $messages[5]->useridto); 469 $this->assertEquals($admin->id, $messages[5]->useridfrom); 470 $this->assertStringContainsString('xcourse3', $messages[5]->fullmessagehtml); 471 $this->assertStringNotContainsString('xuser1', $messages[5]->fullmessagehtml); 472 $this->assertStringNotContainsString('xuser2', $messages[5]->fullmessagehtml); 473 $this->assertStringContainsString('xuser3', $messages[5]->fullmessagehtml); 474 $this->assertStringNotContainsString('xuser4', $messages[5]->fullmessagehtml); 475 $this->assertStringNotContainsString('xuser5', $messages[5]->fullmessagehtml); 476 $this->assertStringNotContainsString('xuser6', $messages[5]->fullmessagehtml); 477 478 479 // Make sure that notifications are not repeated. 480 $sink->clear(); 481 482 $manualplugin->send_expiry_notifications($trace); 483 $this->assertEquals(0, $sink->count()); 484 485 // use invalid notification hour to verify that before the hour the notifications are not sent. 486 $manualplugin->set_config('expirynotifylast', time() - 60*60*24); 487 $manualplugin->set_config('expirynotifyhour', '24'); 488 489 $manualplugin->send_expiry_notifications($trace); 490 $this->assertEquals(0, $sink->count()); 491 492 $manualplugin->set_config('expirynotifyhour', '0'); 493 $manualplugin->send_expiry_notifications($trace); 494 $this->assertEquals(6, $sink->count()); 495 } 496 497 /** 498 * Test for getting user enrolment actions. 499 */ 500 public function test_get_user_enrolment_actions() { 501 global $CFG, $PAGE; 502 $this->resetAfterTest(); 503 504 // Set page URL to prevent debugging messages. 505 $PAGE->set_url('/enrol/editinstance.php'); 506 507 $pluginname = 'manual'; 508 509 // Only enable the manual enrol plugin. 510 $CFG->enrol_plugins_enabled = $pluginname; 511 512 $generator = $this->getDataGenerator(); 513 514 // Get the enrol plugin. 515 $plugin = enrol_get_plugin($pluginname); 516 517 // Create a course. 518 $course = $generator->create_course(); 519 // Enable this enrol plugin for the course. 520 $plugin->add_instance($course); 521 522 // Create a teacher. 523 $teacher = $generator->create_user(); 524 // Enrol the teacher to the course. 525 $generator->enrol_user($teacher->id, $course->id, 'editingteacher', $pluginname); 526 // Create a student. 527 $student = $generator->create_user(); 528 // Enrol the student to the course. 529 $generator->enrol_user($student->id, $course->id, 'student', $pluginname); 530 531 // Login as the teacher. 532 $this->setUser($teacher); 533 require_once($CFG->dirroot . '/enrol/locallib.php'); 534 $manager = new course_enrolment_manager($PAGE, $course); 535 $userenrolments = $manager->get_user_enrolments($student->id); 536 $this->assertCount(1, $userenrolments); 537 538 $ue = reset($userenrolments); 539 $actions = $plugin->get_user_enrolment_actions($manager, $ue); 540 // Manual enrol has 2 enrol actions -- edit and unenrol. 541 $this->assertCount(2, $actions); 542 } 543 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body