[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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 * Self enrolment plugin tests. 19 * 20 * @package enrol_self 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 global $CFG; 29 require_once($CFG->dirroot.'/enrol/self/lib.php'); 30 require_once($CFG->dirroot.'/enrol/self/locallib.php'); 31 32 class enrol_self_testcase extends advanced_testcase { 33 34 public function test_basics() { 35 $this->assertTrue(enrol_is_enabled('self')); 36 $plugin = enrol_get_plugin('self'); 37 $this->assertInstanceOf('enrol_self_plugin', $plugin); 38 $this->assertEquals(1, get_config('enrol_self', 'defaultenrol')); 39 $this->assertEquals(ENROL_EXT_REMOVED_KEEP, get_config('enrol_self', 'expiredaction')); 40 } 41 42 public function test_sync_nothing() { 43 global $SITE; 44 45 $selfplugin = enrol_get_plugin('self'); 46 47 $trace = new null_progress_trace(); 48 49 // Just make sure the sync does not throw any errors when nothing to do. 50 $selfplugin->sync($trace, null); 51 $selfplugin->sync($trace, $SITE->id); 52 } 53 54 public function test_longtimnosee() { 55 global $DB; 56 $this->resetAfterTest(); 57 58 $selfplugin = enrol_get_plugin('self'); 59 $manualplugin = enrol_get_plugin('manual'); 60 $this->assertNotEmpty($manualplugin); 61 62 $now = time(); 63 64 $trace = new null_progress_trace(); 65 66 // Prepare some data. 67 68 $studentrole = $DB->get_record('role', array('shortname'=>'student')); 69 $this->assertNotEmpty($studentrole); 70 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher')); 71 $this->assertNotEmpty($teacherrole); 72 73 $record = array('firstaccess'=>$now-60*60*24*800); 74 $record['lastaccess'] = $now-60*60*24*100; 75 $user1 = $this->getDataGenerator()->create_user($record); 76 $record['lastaccess'] = $now-60*60*24*10; 77 $user2 = $this->getDataGenerator()->create_user($record); 78 $record['lastaccess'] = $now-60*60*24*1; 79 $user3 = $this->getDataGenerator()->create_user($record); 80 $record['lastaccess'] = $now-10; 81 $user4 = $this->getDataGenerator()->create_user($record); 82 83 $course1 = $this->getDataGenerator()->create_course(); 84 $course2 = $this->getDataGenerator()->create_course(); 85 $course3 = $this->getDataGenerator()->create_course(); 86 $context1 = context_course::instance($course1->id); 87 $context2 = context_course::instance($course2->id); 88 $context3 = context_course::instance($course3->id); 89 90 $this->assertEquals(3, $DB->count_records('enrol', array('enrol'=>'self'))); 91 $instance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'self'), '*', MUST_EXIST); 92 $instance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'self'), '*', MUST_EXIST); 93 $instance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'self'), '*', MUST_EXIST); 94 $id = $selfplugin->add_instance($course3, array('status'=>ENROL_INSTANCE_ENABLED, 'roleid'=>$teacherrole->id)); 95 $instance3b = $DB->get_record('enrol', array('id'=>$id), '*', MUST_EXIST); 96 unset($id); 97 98 $this->assertEquals($studentrole->id, $instance1->roleid); 99 $instance1->customint2 = 60*60*24*14; 100 $DB->update_record('enrol', $instance1); 101 $selfplugin->enrol_user($instance1, $user1->id, $studentrole->id); 102 $selfplugin->enrol_user($instance1, $user2->id, $studentrole->id); 103 $selfplugin->enrol_user($instance1, $user3->id, $studentrole->id); 104 $this->assertEquals(3, $DB->count_records('user_enrolments')); 105 $DB->insert_record('user_lastaccess', array('userid'=>$user2->id, 'courseid'=>$course1->id, 'timeaccess'=>$now-60*60*24*20)); 106 $DB->insert_record('user_lastaccess', array('userid'=>$user3->id, 'courseid'=>$course1->id, 'timeaccess'=>$now-60*60*24*2)); 107 $DB->insert_record('user_lastaccess', array('userid'=>$user4->id, 'courseid'=>$course1->id, 'timeaccess'=>$now-60)); 108 109 $this->assertEquals($studentrole->id, $instance3->roleid); 110 $instance3->customint2 = 60*60*24*50; 111 $DB->update_record('enrol', $instance3); 112 $selfplugin->enrol_user($instance3, $user1->id, $studentrole->id); 113 $selfplugin->enrol_user($instance3, $user2->id, $studentrole->id); 114 $selfplugin->enrol_user($instance3, $user3->id, $studentrole->id); 115 $selfplugin->enrol_user($instance3b, $user1->id, $teacherrole->id); 116 $selfplugin->enrol_user($instance3b, $user4->id, $teacherrole->id); 117 $this->assertEquals(8, $DB->count_records('user_enrolments')); 118 $DB->insert_record('user_lastaccess', array('userid'=>$user2->id, 'courseid'=>$course3->id, 'timeaccess'=>$now-60*60*24*11)); 119 $DB->insert_record('user_lastaccess', array('userid'=>$user3->id, 'courseid'=>$course3->id, 'timeaccess'=>$now-60*60*24*200)); 120 $DB->insert_record('user_lastaccess', array('userid'=>$user4->id, 'courseid'=>$course3->id, 'timeaccess'=>$now-60*60*24*200)); 121 122 $maninstance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST); 123 $maninstance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST); 124 125 $manualplugin->enrol_user($maninstance2, $user1->id, $studentrole->id); 126 $manualplugin->enrol_user($maninstance3, $user1->id, $teacherrole->id); 127 128 $this->assertEquals(10, $DB->count_records('user_enrolments')); 129 $this->assertEquals(9, $DB->count_records('role_assignments')); 130 $this->assertEquals(7, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id))); 131 $this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id))); 132 133 // Execute sync - this is the same thing used from cron. 134 135 $selfplugin->sync($trace, $course2->id); 136 $this->assertEquals(10, $DB->count_records('user_enrolments')); 137 138 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id, 'userid'=>$user1->id))); 139 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id, 'userid'=>$user2->id))); 140 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$instance3->id, 'userid'=>$user1->id))); 141 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$instance3->id, 'userid'=>$user3->id))); 142 $selfplugin->sync($trace, null); 143 $this->assertEquals(6, $DB->count_records('user_enrolments')); 144 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id, 'userid'=>$user1->id))); 145 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id, 'userid'=>$user2->id))); 146 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance3->id, 'userid'=>$user1->id))); 147 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance3->id, 'userid'=>$user3->id))); 148 149 $this->assertEquals(6, $DB->count_records('role_assignments')); 150 $this->assertEquals(4, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id))); 151 $this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id))); 152 } 153 154 public function test_expired() { 155 global $DB; 156 $this->resetAfterTest(); 157 158 $selfplugin = enrol_get_plugin('self'); 159 $manualplugin = enrol_get_plugin('manual'); 160 $this->assertNotEmpty($manualplugin); 161 162 $now = time(); 163 164 $trace = new null_progress_trace(); 165 166 // Prepare some data. 167 168 $studentrole = $DB->get_record('role', array('shortname'=>'student')); 169 $this->assertNotEmpty($studentrole); 170 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher')); 171 $this->assertNotEmpty($teacherrole); 172 $managerrole = $DB->get_record('role', array('shortname'=>'manager')); 173 $this->assertNotEmpty($managerrole); 174 175 $user1 = $this->getDataGenerator()->create_user(); 176 $user2 = $this->getDataGenerator()->create_user(); 177 $user3 = $this->getDataGenerator()->create_user(); 178 $user4 = $this->getDataGenerator()->create_user(); 179 180 $course1 = $this->getDataGenerator()->create_course(); 181 $course2 = $this->getDataGenerator()->create_course(); 182 $course3 = $this->getDataGenerator()->create_course(); 183 $context1 = context_course::instance($course1->id); 184 $context2 = context_course::instance($course2->id); 185 $context3 = context_course::instance($course3->id); 186 187 $this->assertEquals(3, $DB->count_records('enrol', array('enrol'=>'self'))); 188 $instance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'self'), '*', MUST_EXIST); 189 $this->assertEquals($studentrole->id, $instance1->roleid); 190 $instance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'self'), '*', MUST_EXIST); 191 $this->assertEquals($studentrole->id, $instance2->roleid); 192 $instance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'self'), '*', MUST_EXIST); 193 $this->assertEquals($studentrole->id, $instance3->roleid); 194 $id = $selfplugin->add_instance($course3, array('status'=>ENROL_INSTANCE_ENABLED, 'roleid'=>$teacherrole->id)); 195 $instance3b = $DB->get_record('enrol', array('id'=>$id), '*', MUST_EXIST); 196 $this->assertEquals($teacherrole->id, $instance3b->roleid); 197 unset($id); 198 199 $maninstance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST); 200 $maninstance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST); 201 202 $manualplugin->enrol_user($maninstance2, $user1->id, $studentrole->id); 203 $manualplugin->enrol_user($maninstance3, $user1->id, $teacherrole->id); 204 205 $this->assertEquals(2, $DB->count_records('user_enrolments')); 206 $this->assertEquals(2, $DB->count_records('role_assignments')); 207 $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id))); 208 $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id))); 209 210 $selfplugin->enrol_user($instance1, $user1->id, $studentrole->id); 211 $selfplugin->enrol_user($instance1, $user2->id, $studentrole->id); 212 $selfplugin->enrol_user($instance1, $user3->id, $studentrole->id, 0, $now-60); 213 214 $selfplugin->enrol_user($instance3, $user1->id, $studentrole->id, 0, 0); 215 $selfplugin->enrol_user($instance3, $user2->id, $studentrole->id, 0, $now-60*60); 216 $selfplugin->enrol_user($instance3, $user3->id, $studentrole->id, 0, $now+60*60); 217 $selfplugin->enrol_user($instance3b, $user1->id, $teacherrole->id, $now-60*60*24*7, $now-60); 218 $selfplugin->enrol_user($instance3b, $user4->id, $teacherrole->id); 219 220 role_assign($managerrole->id, $user3->id, $context1->id); 221 222 $this->assertEquals(10, $DB->count_records('user_enrolments')); 223 $this->assertEquals(10, $DB->count_records('role_assignments')); 224 $this->assertEquals(7, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id))); 225 $this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id))); 226 227 // Execute tests. 228 229 $this->assertEquals(ENROL_EXT_REMOVED_KEEP, $selfplugin->get_config('expiredaction')); 230 $selfplugin->sync($trace, null); 231 $this->assertEquals(10, $DB->count_records('user_enrolments')); 232 $this->assertEquals(10, $DB->count_records('role_assignments')); 233 234 235 $selfplugin->set_config('expiredaction', ENROL_EXT_REMOVED_SUSPENDNOROLES); 236 $selfplugin->sync($trace, $course2->id); 237 $this->assertEquals(10, $DB->count_records('user_enrolments')); 238 $this->assertEquals(10, $DB->count_records('role_assignments')); 239 240 $selfplugin->sync($trace, null); 241 $this->assertEquals(10, $DB->count_records('user_enrolments')); 242 $this->assertEquals(7, $DB->count_records('role_assignments')); 243 $this->assertEquals(5, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id))); 244 $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id))); 245 $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user3->id, 'roleid'=>$studentrole->id))); 246 $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context3->id, 'userid'=>$user2->id, 'roleid'=>$studentrole->id))); 247 $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context3->id, 'userid'=>$user1->id, 'roleid'=>$teacherrole->id))); 248 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context3->id, 'userid'=>$user1->id, 'roleid'=>$studentrole->id))); 249 250 251 $selfplugin->set_config('expiredaction', ENROL_EXT_REMOVED_UNENROL); 252 253 role_assign($studentrole->id, $user3->id, $context1->id); 254 role_assign($studentrole->id, $user2->id, $context3->id); 255 role_assign($teacherrole->id, $user1->id, $context3->id); 256 $this->assertEquals(10, $DB->count_records('user_enrolments')); 257 $this->assertEquals(10, $DB->count_records('role_assignments')); 258 $this->assertEquals(7, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id))); 259 $this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id))); 260 261 $selfplugin->sync($trace, null); 262 $this->assertEquals(7, $DB->count_records('user_enrolments')); 263 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id, 'userid'=>$user3->id))); 264 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance3->id, 'userid'=>$user2->id))); 265 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance3b->id, 'userid'=>$user1->id))); 266 $this->assertEquals(6, $DB->count_records('role_assignments')); 267 $this->assertEquals(5, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id))); 268 $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id))); 269 } 270 271 public function test_send_expiry_notifications() { 272 global $DB, $CFG; 273 $this->resetAfterTest(); 274 $this->preventResetByRollback(); // Messaging does not like transactions... 275 276 /** @var $selfplugin enrol_self_plugin */ 277 $selfplugin = enrol_get_plugin('self'); 278 /** @var $manualplugin enrol_manual_plugin */ 279 $manualplugin = enrol_get_plugin('manual'); 280 $now = time(); 281 $admin = get_admin(); 282 283 $trace = new null_progress_trace(); 284 285 // Note: hopefully nobody executes the unit tests the last second before midnight... 286 287 $selfplugin->set_config('expirynotifylast', $now - 60*60*24); 288 $selfplugin->set_config('expirynotifyhour', 0); 289 290 $studentrole = $DB->get_record('role', array('shortname'=>'student')); 291 $this->assertNotEmpty($studentrole); 292 $editingteacherrole = $DB->get_record('role', array('shortname'=>'editingteacher')); 293 $this->assertNotEmpty($editingteacherrole); 294 $managerrole = $DB->get_record('role', array('shortname'=>'manager')); 295 $this->assertNotEmpty($managerrole); 296 297 $user1 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser1')); 298 $user2 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser2')); 299 $user3 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser3')); 300 $user4 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser4')); 301 $user5 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser5')); 302 $user6 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser6')); 303 $user7 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser6')); 304 $user8 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser6')); 305 306 $course1 = $this->getDataGenerator()->create_course(array('fullname'=>'xcourse1')); 307 $course2 = $this->getDataGenerator()->create_course(array('fullname'=>'xcourse2')); 308 $course3 = $this->getDataGenerator()->create_course(array('fullname'=>'xcourse3')); 309 $course4 = $this->getDataGenerator()->create_course(array('fullname'=>'xcourse4')); 310 311 $this->assertEquals(4, $DB->count_records('enrol', array('enrol'=>'manual'))); 312 $this->assertEquals(4, $DB->count_records('enrol', array('enrol'=>'self'))); 313 314 $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST); 315 $instance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'self'), '*', MUST_EXIST); 316 $instance1->expirythreshold = 60*60*24*4; 317 $instance1->expirynotify = 1; 318 $instance1->notifyall = 1; 319 $instance1->status = ENROL_INSTANCE_ENABLED; 320 $DB->update_record('enrol', $instance1); 321 322 $maninstance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST); 323 $instance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'self'), '*', MUST_EXIST); 324 $instance2->expirythreshold = 60*60*24*1; 325 $instance2->expirynotify = 1; 326 $instance2->notifyall = 1; 327 $instance2->status = ENROL_INSTANCE_ENABLED; 328 $DB->update_record('enrol', $instance2); 329 330 $maninstance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST); 331 $instance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'self'), '*', MUST_EXIST); 332 $instance3->expirythreshold = 60*60*24*1; 333 $instance3->expirynotify = 1; 334 $instance3->notifyall = 0; 335 $instance3->status = ENROL_INSTANCE_ENABLED; 336 $DB->update_record('enrol', $instance3); 337 338 $maninstance4 = $DB->get_record('enrol', array('courseid'=>$course4->id, 'enrol'=>'manual'), '*', MUST_EXIST); 339 $instance4 = $DB->get_record('enrol', array('courseid'=>$course4->id, 'enrol'=>'self'), '*', MUST_EXIST); 340 $instance4->expirythreshold = 60*60*24*1; 341 $instance4->expirynotify = 0; 342 $instance4->notifyall = 0; 343 $instance4->status = ENROL_INSTANCE_ENABLED; 344 $DB->update_record('enrol', $instance4); 345 346 $selfplugin->enrol_user($instance1, $user1->id, $studentrole->id, 0, $now + 60*60*24*1, ENROL_USER_SUSPENDED); // Suspended users are not notified. 347 $selfplugin->enrol_user($instance1, $user2->id, $studentrole->id, 0, $now + 60*60*24*5); // Above threshold are not notified. 348 $selfplugin->enrol_user($instance1, $user3->id, $studentrole->id, 0, $now + 60*60*24*3 + 60*60); // Less than one day after threshold - should be notified. 349 $selfplugin->enrol_user($instance1, $user4->id, $studentrole->id, 0, $now + 60*60*24*4 - 60*3); // Less than one day after threshold - should be notified. 350 $selfplugin->enrol_user($instance1, $user5->id, $studentrole->id, 0, $now + 60*60); // Should have been already notified. 351 $selfplugin->enrol_user($instance1, $user6->id, $studentrole->id, 0, $now - 60); // Already expired. 352 $manualplugin->enrol_user($maninstance1, $user7->id, $editingteacherrole->id); 353 $manualplugin->enrol_user($maninstance1, $user8->id, $managerrole->id); // Highest role --> enroller. 354 355 $selfplugin->enrol_user($instance2, $user1->id, $studentrole->id); 356 $selfplugin->enrol_user($instance2, $user2->id, $studentrole->id, 0, $now + 60*60*24*1 + 60*3); // Above threshold are not notified. 357 $selfplugin->enrol_user($instance2, $user3->id, $studentrole->id, 0, $now + 60*60*24*1 - 60*60); // Less than one day after threshold - should be notified. 358 359 $manualplugin->enrol_user($maninstance3, $user1->id, $editingteacherrole->id); 360 $selfplugin->enrol_user($instance3, $user2->id, $studentrole->id, 0, $now + 60*60*24*1 + 60); // Above threshold are not notified. 361 $selfplugin->enrol_user($instance3, $user3->id, $studentrole->id, 0, $now + 60*60*24*1 - 60*60); // Less than one day after threshold - should be notified. 362 363 $manualplugin->enrol_user($maninstance4, $user4->id, $editingteacherrole->id); 364 $selfplugin->enrol_user($instance4, $user5->id, $studentrole->id, 0, $now + 60*60*24*1 + 60); 365 $selfplugin->enrol_user($instance4, $user6->id, $studentrole->id, 0, $now + 60*60*24*1 - 60*60); 366 367 // The notification is sent out in fixed order first individual users, 368 // then summary per course by enrolid, user lastname, etc. 369 $this->assertGreaterThan($instance1->id, $instance2->id); 370 $this->assertGreaterThan($instance2->id, $instance3->id); 371 372 $sink = $this->redirectMessages(); 373 374 $selfplugin->send_expiry_notifications($trace); 375 376 $messages = $sink->get_messages(); 377 378 $this->assertEquals(2+1 + 1+1 + 1 + 0, count($messages)); 379 380 // First individual notifications from course1. 381 $this->assertEquals($user3->id, $messages[0]->useridto); 382 $this->assertEquals($user8->id, $messages[0]->useridfrom); 383 $this->assertContains('xcourse1', $messages[0]->fullmessagehtml); 384 385 $this->assertEquals($user4->id, $messages[1]->useridto); 386 $this->assertEquals($user8->id, $messages[1]->useridfrom); 387 $this->assertContains('xcourse1', $messages[1]->fullmessagehtml); 388 389 // Then summary for course1. 390 $this->assertEquals($user8->id, $messages[2]->useridto); 391 $this->assertEquals($admin->id, $messages[2]->useridfrom); 392 $this->assertContains('xcourse1', $messages[2]->fullmessagehtml); 393 $this->assertNotContains('xuser1', $messages[2]->fullmessagehtml); 394 $this->assertNotContains('xuser2', $messages[2]->fullmessagehtml); 395 $this->assertContains('xuser3', $messages[2]->fullmessagehtml); 396 $this->assertContains('xuser4', $messages[2]->fullmessagehtml); 397 $this->assertContains('xuser5', $messages[2]->fullmessagehtml); 398 $this->assertNotContains('xuser6', $messages[2]->fullmessagehtml); 399 400 // First individual notifications from course2. 401 $this->assertEquals($user3->id, $messages[3]->useridto); 402 $this->assertEquals($admin->id, $messages[3]->useridfrom); 403 $this->assertContains('xcourse2', $messages[3]->fullmessagehtml); 404 405 // Then summary for course2. 406 $this->assertEquals($admin->id, $messages[4]->useridto); 407 $this->assertEquals($admin->id, $messages[4]->useridfrom); 408 $this->assertContains('xcourse2', $messages[4]->fullmessagehtml); 409 $this->assertNotContains('xuser1', $messages[4]->fullmessagehtml); 410 $this->assertNotContains('xuser2', $messages[4]->fullmessagehtml); 411 $this->assertContains('xuser3', $messages[4]->fullmessagehtml); 412 $this->assertNotContains('xuser4', $messages[4]->fullmessagehtml); 413 $this->assertNotContains('xuser5', $messages[4]->fullmessagehtml); 414 $this->assertNotContains('xuser6', $messages[4]->fullmessagehtml); 415 416 // Only summary in course3. 417 $this->assertEquals($user1->id, $messages[5]->useridto); 418 $this->assertEquals($admin->id, $messages[5]->useridfrom); 419 $this->assertContains('xcourse3', $messages[5]->fullmessagehtml); 420 $this->assertNotContains('xuser1', $messages[5]->fullmessagehtml); 421 $this->assertNotContains('xuser2', $messages[5]->fullmessagehtml); 422 $this->assertContains('xuser3', $messages[5]->fullmessagehtml); 423 $this->assertNotContains('xuser4', $messages[5]->fullmessagehtml); 424 $this->assertNotContains('xuser5', $messages[5]->fullmessagehtml); 425 $this->assertNotContains('xuser6', $messages[5]->fullmessagehtml); 426 427 428 // Make sure that notifications are not repeated. 429 $sink->clear(); 430 431 $selfplugin->send_expiry_notifications($trace); 432 $this->assertEquals(0, $sink->count()); 433 434 // use invalid notification hour to verify that before the hour the notifications are not sent. 435 $selfplugin->set_config('expirynotifylast', time() - 60*60*24); 436 $selfplugin->set_config('expirynotifyhour', '24'); 437 438 $selfplugin->send_expiry_notifications($trace); 439 $this->assertEquals(0, $sink->count()); 440 441 $selfplugin->set_config('expirynotifyhour', '0'); 442 $selfplugin->send_expiry_notifications($trace); 443 $this->assertEquals(6, $sink->count()); 444 } 445 446 public function test_show_enrolme_link() { 447 global $DB, $CFG; 448 $this->resetAfterTest(); 449 $this->preventResetByRollback(); // Messaging does not like transactions... 450 451 /** @var $selfplugin enrol_self_plugin */ 452 $selfplugin = enrol_get_plugin('self'); 453 454 $user1 = $this->getDataGenerator()->create_user(); 455 $user2 = $this->getDataGenerator()->create_user(); 456 457 $studentrole = $DB->get_record('role', array('shortname'=>'student')); 458 $this->assertNotEmpty($studentrole); 459 460 $course1 = $this->getDataGenerator()->create_course(); 461 $course2 = $this->getDataGenerator()->create_course(); 462 $course3 = $this->getDataGenerator()->create_course(); 463 $course4 = $this->getDataGenerator()->create_course(); 464 $course5 = $this->getDataGenerator()->create_course(); 465 $course6 = $this->getDataGenerator()->create_course(); 466 $course7 = $this->getDataGenerator()->create_course(); 467 $course8 = $this->getDataGenerator()->create_course(); 468 $course9 = $this->getDataGenerator()->create_course(); 469 $course10 = $this->getDataGenerator()->create_course(); 470 $course11 = $this->getDataGenerator()->create_course(); 471 472 $cohort1 = $this->getDataGenerator()->create_cohort(); 473 $cohort2 = $this->getDataGenerator()->create_cohort(); 474 475 // New enrolments are allowed and enrolment instance is enabled. 476 $instance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'self'), '*', MUST_EXIST); 477 $instance1->customint6 = 1; 478 $DB->update_record('enrol', $instance1); 479 $selfplugin->update_status($instance1, ENROL_INSTANCE_ENABLED); 480 481 // New enrolments are not allowed, but enrolment instance is enabled. 482 $instance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'self'), '*', MUST_EXIST); 483 $instance2->customint6 = 0; 484 $DB->update_record('enrol', $instance2); 485 $selfplugin->update_status($instance2, ENROL_INSTANCE_ENABLED); 486 487 // New enrolments are allowed , but enrolment instance is disabled. 488 $instance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'self'), '*', MUST_EXIST); 489 $instance3->customint6 = 1; 490 $DB->update_record('enrol', $instance3); 491 $selfplugin->update_status($instance3, ENROL_INSTANCE_DISABLED); 492 493 // New enrolments are not allowed and enrolment instance is disabled. 494 $instance4 = $DB->get_record('enrol', array('courseid'=>$course4->id, 'enrol'=>'self'), '*', MUST_EXIST); 495 $instance4->customint6 = 0; 496 $DB->update_record('enrol', $instance4); 497 $selfplugin->update_status($instance4, ENROL_INSTANCE_DISABLED); 498 499 // Cohort member test. 500 $instance5 = $DB->get_record('enrol', array('courseid'=>$course5->id, 'enrol'=>'self'), '*', MUST_EXIST); 501 $instance5->customint6 = 1; 502 $instance5->customint5 = $cohort1->id; 503 $DB->update_record('enrol', $instance1); 504 $selfplugin->update_status($instance5, ENROL_INSTANCE_ENABLED); 505 506 $id = $selfplugin->add_instance($course5, $selfplugin->get_instance_defaults()); 507 $instance6 = $DB->get_record('enrol', array('id'=>$id), '*', MUST_EXIST); 508 $instance6->customint6 = 1; 509 $instance6->customint5 = $cohort2->id; 510 $DB->update_record('enrol', $instance1); 511 $selfplugin->update_status($instance6, ENROL_INSTANCE_ENABLED); 512 513 // Enrol start date is in future. 514 $instance7 = $DB->get_record('enrol', array('courseid'=>$course6->id, 'enrol'=>'self'), '*', MUST_EXIST); 515 $instance7->customint6 = 1; 516 $instance7->enrolstartdate = time() + 60; 517 $DB->update_record('enrol', $instance7); 518 $selfplugin->update_status($instance7, ENROL_INSTANCE_ENABLED); 519 520 // Enrol start date is in past. 521 $instance8 = $DB->get_record('enrol', array('courseid'=>$course7->id, 'enrol'=>'self'), '*', MUST_EXIST); 522 $instance8->customint6 = 1; 523 $instance8->enrolstartdate = time() - 60; 524 $DB->update_record('enrol', $instance8); 525 $selfplugin->update_status($instance8, ENROL_INSTANCE_ENABLED); 526 527 // Enrol end date is in future. 528 $instance9 = $DB->get_record('enrol', array('courseid'=>$course8->id, 'enrol'=>'self'), '*', MUST_EXIST); 529 $instance9->customint6 = 1; 530 $instance9->enrolenddate = time() + 60; 531 $DB->update_record('enrol', $instance9); 532 $selfplugin->update_status($instance9, ENROL_INSTANCE_ENABLED); 533 534 // Enrol end date is in past. 535 $instance10 = $DB->get_record('enrol', array('courseid'=>$course9->id, 'enrol'=>'self'), '*', MUST_EXIST); 536 $instance10->customint6 = 1; 537 $instance10->enrolenddate = time() - 60; 538 $DB->update_record('enrol', $instance10); 539 $selfplugin->update_status($instance10, ENROL_INSTANCE_ENABLED); 540 541 // Maximum enrolments reached. 542 $instance11 = $DB->get_record('enrol', array('courseid'=>$course10->id, 'enrol'=>'self'), '*', MUST_EXIST); 543 $instance11->customint6 = 1; 544 $instance11->customint3 = 1; 545 $DB->update_record('enrol', $instance11); 546 $selfplugin->update_status($instance11, ENROL_INSTANCE_ENABLED); 547 $selfplugin->enrol_user($instance11, $user2->id, $studentrole->id); 548 549 // Maximum enrolments not reached. 550 $instance12 = $DB->get_record('enrol', array('courseid'=>$course11->id, 'enrol'=>'self'), '*', MUST_EXIST); 551 $instance12->customint6 = 1; 552 $instance12->customint3 = 1; 553 $DB->update_record('enrol', $instance12); 554 $selfplugin->update_status($instance12, ENROL_INSTANCE_ENABLED); 555 556 $this->setUser($user1); 557 $this->assertTrue($selfplugin->show_enrolme_link($instance1)); 558 $this->assertFalse($selfplugin->show_enrolme_link($instance2)); 559 $this->assertFalse($selfplugin->show_enrolme_link($instance3)); 560 $this->assertFalse($selfplugin->show_enrolme_link($instance4)); 561 $this->assertFalse($selfplugin->show_enrolme_link($instance7)); 562 $this->assertTrue($selfplugin->show_enrolme_link($instance8)); 563 $this->assertTrue($selfplugin->show_enrolme_link($instance9)); 564 $this->assertFalse($selfplugin->show_enrolme_link($instance10)); 565 $this->assertFalse($selfplugin->show_enrolme_link($instance11)); 566 $this->assertTrue($selfplugin->show_enrolme_link($instance12)); 567 568 require_once("$CFG->dirroot/cohort/lib.php"); 569 cohort_add_member($cohort1->id, $user1->id); 570 571 $this->assertTrue($selfplugin->show_enrolme_link($instance5)); 572 $this->assertFalse($selfplugin->show_enrolme_link($instance6)); 573 } 574 575 /** 576 * This will check user enrolment only, rest has been tested in test_show_enrolme_link. 577 */ 578 public function test_can_self_enrol() { 579 global $DB, $CFG, $OUTPUT; 580 $this->resetAfterTest(); 581 $this->preventResetByRollback(); 582 583 $selfplugin = enrol_get_plugin('self'); 584 585 $expectederrorstring = get_string('canntenrol', 'enrol_self'); 586 587 $user1 = $this->getDataGenerator()->create_user(); 588 $user2 = $this->getDataGenerator()->create_user(); 589 $guest = $DB->get_record('user', array('id' => $CFG->siteguest)); 590 591 $studentrole = $DB->get_record('role', array('shortname'=>'student')); 592 $this->assertNotEmpty($studentrole); 593 $editingteacherrole = $DB->get_record('role', array('shortname'=>'editingteacher')); 594 $this->assertNotEmpty($editingteacherrole); 595 596 $course1 = $this->getDataGenerator()->create_course(); 597 598 $instance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'self'), '*', MUST_EXIST); 599 $instance1->customint6 = 1; 600 $DB->update_record('enrol', $instance1); 601 $selfplugin->update_status($instance1, ENROL_INSTANCE_ENABLED); 602 $selfplugin->enrol_user($instance1, $user2->id, $editingteacherrole->id); 603 604 $this->setUser($guest); 605 $noaccesshtml = get_string('noguestaccess', 'enrol') . $OUTPUT->continue_button(get_login_url()); 606 $this->assertSame($noaccesshtml, $selfplugin->can_self_enrol($instance1, true)); 607 608 $this->setUser($user1); 609 $this->assertTrue($selfplugin->can_self_enrol($instance1, true)); 610 611 // Active enroled user. 612 $this->setUser($user2); 613 $selfplugin->enrol_user($instance1, $user1->id, $studentrole->id); 614 $this->setUser($user1); 615 $this->assertSame($expectederrorstring, $selfplugin->can_self_enrol($instance1, true)); 616 } 617 618 /** 619 * Test enrol_self_check_group_enrolment_key 620 */ 621 public function test_enrol_self_check_group_enrolment_key() { 622 global $DB; 623 self::resetAfterTest(true); 624 625 // Test in course with groups. 626 $course = self::getDataGenerator()->create_course(array('groupmode' => SEPARATEGROUPS, 'groupmodeforce' => 1)); 627 628 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id)); 629 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id, 'enrolmentkey' => 'thepassword')); 630 631 $result = enrol_self_check_group_enrolment_key($course->id, 'invalidpassword'); 632 $this->assertFalse($result); 633 634 $result = enrol_self_check_group_enrolment_key($course->id, 'thepassword'); 635 $this->assertTrue($result); 636 637 // Test disabling group options. 638 $course->groupmode = NOGROUPS; 639 $course->groupmodeforce = 0; 640 $DB->update_record('course', $course); 641 642 $result = enrol_self_check_group_enrolment_key($course->id, 'invalidpassword'); 643 $this->assertFalse($result); 644 645 $result = enrol_self_check_group_enrolment_key($course->id, 'thepassword'); 646 $this->assertTrue($result); 647 648 // Test without groups. 649 $othercourse = self::getDataGenerator()->create_course(); 650 $result = enrol_self_check_group_enrolment_key($othercourse->id, 'thepassword'); 651 $this->assertFalse($result); 652 653 } 654 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |