Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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 /** 18 * External database enrolment sync tests, this also tests adodb drivers 19 * that are matching our four supported Moodle database drivers. 20 * 21 * @package enrol_database 22 * @category phpunit 23 * @copyright 2011 Petr Skoda {@link http://skodak.org} 24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 25 */ 26 27 defined('MOODLE_INTERNAL') || die(); 28 29 class enrol_database_testcase extends advanced_testcase { 30 protected static $courses = array(); 31 protected static $users = array(); 32 protected static $roles = array(); 33 34 /** @var string Original error log */ 35 protected $oldlog; 36 37 protected function init_enrol_database() { 38 global $DB, $CFG; 39 40 // Discard error logs from AdoDB. 41 $this->oldlog = ini_get('error_log'); 42 ini_set('error_log', "$CFG->dataroot/testlog.log"); 43 44 $dbman = $DB->get_manager(); 45 46 set_config('dbencoding', 'utf-8', 'enrol_database'); 47 48 set_config('dbhost', $CFG->dbhost, 'enrol_database'); 49 set_config('dbuser', $CFG->dbuser, 'enrol_database'); 50 set_config('dbpass', $CFG->dbpass, 'enrol_database'); 51 set_config('dbname', $CFG->dbname, 'enrol_database'); 52 53 if (!empty($CFG->dboptions['dbport'])) { 54 set_config('dbhost', $CFG->dbhost.':'.$CFG->dboptions['dbport'], 'enrol_database'); 55 } 56 57 switch ($DB->get_dbfamily()) { 58 59 case 'mysql': 60 set_config('dbtype', 'mysqli', 'enrol_database'); 61 set_config('dbsetupsql', "SET NAMES 'UTF-8'", 'enrol_database'); 62 set_config('dbsybasequoting', '0', 'enrol_database'); 63 if (!empty($CFG->dboptions['dbsocket'])) { 64 $dbsocket = $CFG->dboptions['dbsocket']; 65 if ((strpos($dbsocket, '/') === false and strpos($dbsocket, '\\') === false)) { 66 $dbsocket = ini_get('mysqli.default_socket'); 67 } 68 set_config('dbtype', 'mysqli://'.rawurlencode($CFG->dbuser).':'.rawurlencode($CFG->dbpass).'@'.rawurlencode($CFG->dbhost).'/'.rawurlencode($CFG->dbname).'?socket='.rawurlencode($dbsocket), 'enrol_database'); 69 } 70 break; 71 72 case 'oracle': 73 set_config('dbtype', 'oci8po', 'enrol_database'); 74 set_config('dbsybasequoting', '1', 'enrol_database'); 75 break; 76 77 case 'postgres': 78 set_config('dbtype', 'postgres7', 'enrol_database'); 79 $setupsql = "SET NAMES 'UTF-8'"; 80 if (!empty($CFG->dboptions['dbschema'])) { 81 $setupsql .= "; SET search_path = '".$CFG->dboptions['dbschema']."'"; 82 } 83 set_config('dbsetupsql', $setupsql, 'enrol_database'); 84 set_config('dbsybasequoting', '0', 'enrol_database'); 85 if (!empty($CFG->dboptions['dbsocket']) and ($CFG->dbhost === 'localhost' or $CFG->dbhost === '127.0.0.1')) { 86 if (strpos($CFG->dboptions['dbsocket'], '/') !== false) { 87 $socket = $CFG->dboptions['dbsocket']; 88 if (!empty($CFG->dboptions['dbport'])) { 89 $socket .= ':' . $CFG->dboptions['dbport']; 90 } 91 set_config('dbhost', $socket, 'enrol_database'); 92 } else { 93 set_config('dbhost', '', 'enrol_database'); 94 } 95 } 96 break; 97 98 case 'mssql': 99 set_config('dbtype', 'mssqlnative', 'enrol_database'); 100 set_config('dbsybasequoting', '1', 'enrol_database'); 101 102 // The native sqlsrv driver uses a comma as separator between host and port. 103 $dbhost = $CFG->dbhost; 104 if (!empty($dboptions['dbport'])) { 105 $dbhost .= ',' . $dboptions['dbport']; 106 } 107 set_config('dbhost', $dbhost, 'enrol_database'); 108 break; 109 110 default: 111 throw new exception('Unknown database driver '.get_class($DB)); 112 } 113 114 // NOTE: It is stongly discouraged to create new tables in advanced_testcase classes, 115 // but there is no other simple way to test ext database enrol sync, so let's 116 // disable transactions are try to cleanup after the tests. 117 118 $table = new xmldb_table('enrol_database_test_enrols'); 119 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); 120 $table->add_field('courseid', XMLDB_TYPE_CHAR, '255', null, null, null); 121 $table->add_field('userid', XMLDB_TYPE_CHAR, '255', null, null, null); 122 $table->add_field('roleid', XMLDB_TYPE_CHAR, '255', null, null, null); 123 $table->add_field('otheruser', XMLDB_TYPE_CHAR, '1', null, XMLDB_NOTNULL, null, '0'); 124 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); 125 if ($dbman->table_exists($table)) { 126 $dbman->drop_table($table); 127 } 128 $dbman->create_table($table); 129 set_config('remoteenroltable', $CFG->prefix.'enrol_database_test_enrols', 'enrol_database'); 130 set_config('remotecoursefield', 'courseid', 'enrol_database'); 131 set_config('remoteuserfield', 'userid', 'enrol_database'); 132 set_config('remoterolefield', 'roleid', 'enrol_database'); 133 set_config('remoteotheruserfield', 'otheruser', 'enrol_database'); 134 135 $table = new xmldb_table('enrol_database_test_courses'); 136 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); 137 $table->add_field('fullname', XMLDB_TYPE_CHAR, '255', null, null, null); 138 $table->add_field('shortname', XMLDB_TYPE_CHAR, '255', null, null, null); 139 $table->add_field('idnumber', XMLDB_TYPE_CHAR, '255', null, null, null); 140 $table->add_field('category', XMLDB_TYPE_CHAR, '255', null, null, null); 141 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); 142 if ($dbman->table_exists($table)) { 143 $dbman->drop_table($table); 144 } 145 $dbman->create_table($table); 146 set_config('newcoursetable', $CFG->prefix.'enrol_database_test_courses', 'enrol_database'); 147 set_config('newcoursefullname', 'fullname', 'enrol_database'); 148 set_config('newcourseshortname', 'shortname', 'enrol_database'); 149 set_config('newcourseidnumber', 'idnumber', 'enrol_database'); 150 set_config('newcoursecategory', 'category', 'enrol_database'); 151 152 // Create some test users and courses. 153 for ($i = 1; $i <= 4; $i++) { 154 self::$courses[$i] = $this->getDataGenerator()->create_course(array('fullname' => 'Test course '.$i, 'shortname' => 'tc'.$i, 'idnumber' => 'courseid'.$i)); 155 } 156 157 for ($i = 1; $i <= 10; $i++) { 158 self::$users[$i] = $this->getDataGenerator()->create_user(array('username' => 'username'.$i, 'idnumber' => 'userid'.$i, 'email' => 'user'.$i.'@example.com')); 159 } 160 161 foreach (get_all_roles() as $role) { 162 self::$roles[$role->shortname] = $role; 163 } 164 } 165 166 protected function cleanup_enrol_database() { 167 global $DB; 168 169 $dbman = $DB->get_manager(); 170 $table = new xmldb_table('enrol_database_test_enrols'); 171 $dbman->drop_table($table); 172 $table = new xmldb_table('enrol_database_test_courses'); 173 $dbman->drop_table($table); 174 175 self::$courses = null; 176 self::$users = null; 177 self::$roles = null; 178 179 ini_set('error_log', $this->oldlog); 180 } 181 182 protected function reset_enrol_database() { 183 global $DB; 184 185 $DB->delete_records('enrol_database_test_enrols', array()); 186 $DB->delete_records('enrol_database_test_courses', array()); 187 188 $plugin = enrol_get_plugin('database'); 189 $instances = $DB->get_records('enrol', array('enrol' => 'database')); 190 foreach($instances as $instance) { 191 $plugin->delete_instance($instance); 192 } 193 } 194 195 protected function assertIsEnrolled($userindex, $courseindex, $status=null, $rolename = null) { 196 global $DB; 197 $dbinstance = $DB->get_record('enrol', array('courseid' => self::$courses[$courseindex]->id, 'enrol' => 'database'), '*', MUST_EXIST); 198 199 $conditions = array('enrolid' => $dbinstance->id, 'userid' => self::$users[$userindex]->id); 200 if ($status !== null) { 201 $conditions['status'] = $status; 202 } 203 $this->assertTrue($DB->record_exists('user_enrolments', $conditions)); 204 205 $this->assertHasRoleAssignment($userindex, $courseindex, $rolename); 206 } 207 208 protected function assertHasRoleAssignment($userindex, $courseindex, $rolename = null) { 209 global $DB; 210 $dbinstance = $DB->get_record('enrol', array('courseid' => self::$courses[$courseindex]->id, 'enrol' => 'database'), '*', MUST_EXIST); 211 212 $coursecontext = context_course::instance(self::$courses[$courseindex]->id); 213 if ($rolename === false) { 214 $this->assertFalse($DB->record_exists('role_assignments', array('component' => 'enrol_database', 'itemid' => $dbinstance->id, 'userid' => self::$users[$userindex]->id, 'contextid' => $coursecontext->id))); 215 } else if ($rolename !== null) { 216 $this->assertTrue($DB->record_exists('role_assignments', array('component' => 'enrol_database', 'itemid' => $dbinstance->id, 'userid' => self::$users[$userindex]->id, 'contextid' => $coursecontext->id, 'roleid' => self::$roles[$rolename]->id))); 217 } 218 } 219 220 protected function assertIsNotEnrolled($userindex, $courseindex) { 221 global $DB; 222 if (!$dbinstance = $DB->get_record('enrol', array('courseid' => self::$courses[$courseindex]->id, 'enrol' => 'database'))) { 223 return; 224 } 225 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid' => $dbinstance->id, 'userid' => self::$users[$userindex]->id))); 226 } 227 228 public function test_sync_user_enrolments() { 229 global $DB; 230 231 $this->init_enrol_database(); 232 233 $this->resetAfterTest(false); 234 $this->preventResetByRollback(); 235 236 $plugin = enrol_get_plugin('database'); 237 238 // Test basic enrol sync for one user after login. 239 240 $this->reset_enrol_database(); 241 $plugin->set_config('localcoursefield', 'idnumber'); 242 $plugin->set_config('localuserfield', 'idnumber'); 243 $plugin->set_config('localrolefield', 'shortname'); 244 245 $plugin->set_config('defaultrole', self::$roles['student']->id); 246 247 $DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student')); 248 $DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid2', 'roleid' => 'teacher')); 249 $DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid2', 'courseid' => 'courseid1', 'roleid' => null)); 250 $DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid4', 'courseid' => 'courseid4', 'roleid' => 'editingteacher', 'otheruser' => '1')); 251 $DB->insert_record('enrol_database_test_enrols', array('userid' => 'xxxxxxx', 'courseid' => 'courseid1', 'roleid' => 'student')); // Bogus record to be ignored. 252 $DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'xxxxxxxxx', 'roleid' => 'student')); // Bogus record to be ignored. 253 254 $this->assertEquals(0, $DB->count_records('user_enrolments', array())); 255 $this->assertEquals(0, $DB->count_records('enrol', array('enrol' => 'database'))); 256 $this->assertEquals(0, $DB->count_records('role_assignments', array('component' => 'enrol_database'))); 257 258 $plugin->sync_user_enrolments(self::$users[1]); 259 $this->assertEquals(2, $DB->count_records('user_enrolments', array())); 260 $this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database'))); 261 $this->assertEquals(2, $DB->count_records('role_assignments', array('component' => 'enrol_database'))); 262 $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student'); 263 $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher'); 264 265 // Make sure there are no errors or changes on the next login. 266 267 $plugin->sync_user_enrolments(self::$users[1]); 268 $this->assertEquals(2, $DB->count_records('user_enrolments', array())); 269 $this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database'))); 270 $this->assertEquals(2, $DB->count_records('role_assignments', array('component' => 'enrol_database'))); 271 $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student'); 272 $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher'); 273 274 $plugin->sync_user_enrolments(self::$users[2]); 275 $this->assertEquals(3, $DB->count_records('user_enrolments', array())); 276 $this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database'))); 277 $this->assertEquals(3, $DB->count_records('role_assignments', array('component' => 'enrol_database'))); 278 $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student'); 279 $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher'); 280 $this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student'); 281 282 $plugin->sync_user_enrolments(self::$users[4]); 283 $this->assertEquals(3, $DB->count_records('user_enrolments', array())); 284 $this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database'))); 285 $this->assertEquals(4, $DB->count_records('role_assignments', array('component' => 'enrol_database'))); 286 $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student'); 287 $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher'); 288 $this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student'); 289 $this->assertIsNotEnrolled(4, 4); 290 $this->assertHasRoleAssignment(4, 4, 'editingteacher'); 291 292 // Enrolment removals. 293 294 $DB->delete_records('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student')); 295 $plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_KEEP); 296 $plugin->sync_user_enrolments(self::$users[1]); 297 $this->assertEquals(3, $DB->count_records('user_enrolments', array())); 298 $this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database'))); 299 $this->assertEquals(4, $DB->count_records('role_assignments', array('component' => 'enrol_database'))); 300 $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student'); 301 $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher'); 302 303 304 $plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPEND); 305 $plugin->sync_user_enrolments(self::$users[1]); 306 $this->assertEquals(3, $DB->count_records('user_enrolments', array())); 307 $this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database'))); 308 $this->assertEquals(4, $DB->count_records('role_assignments', array('component' => 'enrol_database'))); 309 $this->assertIsEnrolled(1, 1, ENROL_USER_SUSPENDED, 'student'); 310 $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher'); 311 312 $DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student')); 313 $plugin->sync_user_enrolments(self::$users[1]); 314 $this->assertEquals(3, $DB->count_records('user_enrolments', array())); 315 $this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database'))); 316 $this->assertEquals(4, $DB->count_records('role_assignments', array('component' => 'enrol_database'))); 317 $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student'); 318 $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher'); 319 320 321 $DB->delete_records('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student')); 322 $plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES); 323 $plugin->sync_user_enrolments(self::$users[1]); 324 $this->assertEquals(3, $DB->count_records('user_enrolments', array())); 325 $this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database'))); 326 $this->assertEquals(3, $DB->count_records('role_assignments', array('component' => 'enrol_database'))); 327 $this->assertIsEnrolled(1, 1, ENROL_USER_SUSPENDED, false); 328 $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher'); 329 330 $DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student')); 331 $plugin->sync_user_enrolments(self::$users[1]); 332 $this->assertEquals(3, $DB->count_records('user_enrolments', array())); 333 $this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database'))); 334 $this->assertEquals(4, $DB->count_records('role_assignments', array('component' => 'enrol_database'))); 335 $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student'); 336 $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher'); 337 338 339 $DB->delete_records('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student')); 340 $plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL); 341 $plugin->sync_user_enrolments(self::$users[1]); 342 $this->assertEquals(2, $DB->count_records('user_enrolments', array())); 343 $this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database'))); 344 $this->assertEquals(3, $DB->count_records('role_assignments', array('component' => 'enrol_database'))); 345 $this->assertIsNotEnrolled(1, 1); 346 $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher'); 347 348 $DB->delete_records('enrol_database_test_enrols', array('userid' => 'userid4', 'courseid' => 'courseid4', 'roleid' => 'editingteacher')); 349 $plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES); 350 $plugin->sync_user_enrolments(self::$users[4]); 351 $this->assertEquals(2, $DB->count_records('user_enrolments', array())); 352 $this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database'))); 353 $this->assertEquals(2, $DB->count_records('role_assignments', array('component' => 'enrol_database'))); 354 $this->assertIsNotEnrolled(4, 4); 355 $this->assertHasRoleAssignment(4, 4, false); 356 357 // Test all other mapping options. 358 359 $this->reset_enrol_database(); 360 361 $this->assertEquals(0, $DB->count_records('user_enrolments', array())); 362 $this->assertEquals(0, $DB->count_records('enrol', array('enrol' => 'database'))); 363 $this->assertEquals(0, $DB->count_records('role_assignments', array('component' => 'enrol_database'))); 364 365 $plugin->set_config('localcoursefield', 'id'); 366 $plugin->set_config('localuserfield', 'id'); 367 $plugin->set_config('localrolefield', 'id'); 368 369 $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->id, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id)); 370 $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->id, 'courseid' => self::$courses[2]->id, 'roleid' => self::$roles['teacher']->id)); 371 $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[2]->id, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id)); 372 373 $plugin->sync_user_enrolments(self::$users[1]); 374 $this->assertEquals(2, $DB->count_records('user_enrolments', array())); 375 $this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database'))); 376 $this->assertEquals(2, $DB->count_records('role_assignments', array('component' => 'enrol_database'))); 377 $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student'); 378 $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher'); 379 380 381 $this->reset_enrol_database(); 382 $plugin->set_config('localcoursefield', 'shortname'); 383 $plugin->set_config('localuserfield', 'email'); 384 $plugin->set_config('localrolefield', 'id'); 385 386 $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->email, 'courseid' => self::$courses[1]->shortname, 'roleid' => self::$roles['student']->id)); 387 $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->email, 'courseid' => self::$courses[2]->shortname, 'roleid' => self::$roles['teacher']->id)); 388 $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[2]->email, 'courseid' => self::$courses[1]->shortname, 'roleid' => self::$roles['student']->id)); 389 390 $plugin->sync_user_enrolments(self::$users[1]); 391 $this->assertEquals(2, $DB->count_records('user_enrolments', array())); 392 $this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database'))); 393 $this->assertEquals(2, $DB->count_records('role_assignments', array('component' => 'enrol_database'))); 394 $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student'); 395 $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher'); 396 397 398 $this->reset_enrol_database(); 399 $plugin->set_config('localcoursefield', 'id'); 400 $plugin->set_config('localuserfield', 'username'); 401 $plugin->set_config('localrolefield', 'id'); 402 403 $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->username, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id)); 404 $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->username, 'courseid' => self::$courses[2]->id, 'roleid' => self::$roles['teacher']->id)); 405 $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[2]->username, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id)); 406 407 $plugin->sync_user_enrolments(self::$users[1]); 408 $this->assertEquals(2, $DB->count_records('user_enrolments', array())); 409 $this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database'))); 410 $this->assertEquals(2, $DB->count_records('role_assignments', array('component' => 'enrol_database'))); 411 $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student'); 412 $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher'); 413 } 414 415 /** 416 * @depends test_sync_user_enrolments 417 */ 418 public function test_sync_users() { 419 global $DB; 420 421 $this->resetAfterTest(false); 422 $this->preventResetByRollback(); 423 $this->reset_enrol_database(); 424 425 $plugin = enrol_get_plugin('database'); 426 427 $trace = new null_progress_trace(); 428 429 // Test basic enrol sync for one user after login. 430 431 $this->reset_enrol_database(); 432 $plugin->set_config('localcoursefield', 'idnumber'); 433 $plugin->set_config('localuserfield', 'idnumber'); 434 $plugin->set_config('localrolefield', 'shortname'); 435 436 $DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student')); 437 $DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid2', 'roleid' => 'editingteacher')); 438 $DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid2', 'courseid' => 'courseid1', 'roleid' => 'student')); 439 $DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid4', 'courseid' => 'courseid4', 'roleid' => 'editingteacher', 'otheruser' => '1')); 440 $DB->insert_record('enrol_database_test_enrols', array('userid' => 'xxxxxxx', 'courseid' => 'courseid1', 'roleid' => 'student')); // Bogus record to be ignored. 441 $DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'xxxxxxxxx', 'roleid' => 'student')); // Bogus record to be ignored. 442 $this->assertEquals(0, $DB->count_records('user_enrolments', array())); 443 $this->assertEquals(0, $DB->count_records('enrol', array('enrol' => 'database'))); 444 $this->assertEquals(0, $DB->count_records('role_assignments', array('component' => 'enrol_database'))); 445 446 $plugin->sync_enrolments($trace); 447 $this->assertEquals(3, $DB->count_records('user_enrolments', array())); 448 $this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database'))); 449 $this->assertEquals(4, $DB->count_records('role_assignments', array('component' => 'enrol_database'))); 450 $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student'); 451 $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher'); 452 $this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student'); 453 $this->assertIsNotEnrolled(4, 4); 454 $this->assertHasRoleAssignment(4, 4, 'editingteacher'); 455 456 $plugin->set_config('defaultrole', self::$roles['teacher']->id); 457 $DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid3', 'courseid' => 'courseid3')); 458 $plugin->sync_enrolments($trace); 459 $this->assertEquals(4, $DB->count_records('user_enrolments', array())); 460 $this->assertEquals(4, $DB->count_records('enrol', array('enrol' => 'database'))); 461 $this->assertEquals(5, $DB->count_records('role_assignments', array('component' => 'enrol_database'))); 462 $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student'); 463 $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher'); 464 $this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student'); 465 $this->assertIsNotEnrolled(4, 4); 466 $this->assertHasRoleAssignment(4, 4, 'editingteacher'); 467 $this->assertIsEnrolled(3, 3, ENROL_USER_ACTIVE, 'teacher'); 468 469 470 // Test different unenrolment options. 471 472 $DB->delete_records('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student')); 473 $plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_KEEP); 474 $plugin->sync_enrolments($trace); 475 $this->assertEquals(4, $DB->count_records('user_enrolments', array())); 476 $this->assertEquals(4, $DB->count_records('enrol', array('enrol' => 'database'))); 477 $this->assertEquals(5, $DB->count_records('role_assignments', array('component' => 'enrol_database'))); 478 $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student'); 479 $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher'); 480 $this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student'); 481 $this->assertIsNotEnrolled(4, 4); 482 $this->assertHasRoleAssignment(4, 4, 'editingteacher'); 483 $this->assertIsEnrolled(3, 3, ENROL_USER_ACTIVE, 'teacher'); 484 485 486 $plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPEND); 487 $plugin->sync_enrolments($trace); 488 $this->assertEquals(4, $DB->count_records('user_enrolments', array())); 489 $this->assertEquals(4, $DB->count_records('enrol', array('enrol' => 'database'))); 490 $this->assertEquals(5, $DB->count_records('role_assignments', array('component' => 'enrol_database'))); 491 $this->assertIsEnrolled(1, 1, ENROL_USER_SUSPENDED, 'student'); 492 $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher'); 493 $this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student'); 494 $this->assertIsNotEnrolled(4, 4); 495 $this->assertHasRoleAssignment(4, 4, 'editingteacher'); 496 $this->assertIsEnrolled(3, 3, ENROL_USER_ACTIVE, 'teacher'); 497 498 $DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student')); 499 $plugin->sync_enrolments($trace); 500 $this->assertEquals(4, $DB->count_records('user_enrolments', array())); 501 $this->assertEquals(4, $DB->count_records('enrol', array('enrol' => 'database'))); 502 $this->assertEquals(5, $DB->count_records('role_assignments', array('component' => 'enrol_database'))); 503 $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student'); 504 $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher'); 505 $this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student'); 506 $this->assertIsNotEnrolled(4, 4); 507 $this->assertHasRoleAssignment(4, 4, 'editingteacher'); 508 $this->assertIsEnrolled(3, 3, ENROL_USER_ACTIVE, 'teacher'); 509 510 511 $DB->delete_records('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student')); 512 $plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES); 513 $plugin->sync_enrolments($trace); 514 $this->assertEquals(4, $DB->count_records('user_enrolments', array())); 515 $this->assertEquals(4, $DB->count_records('enrol', array('enrol' => 'database'))); 516 $this->assertEquals(4, $DB->count_records('role_assignments', array('component' => 'enrol_database'))); 517 $this->assertIsEnrolled(1, 1, ENROL_USER_SUSPENDED, false); 518 $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher'); 519 $this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student'); 520 $this->assertIsNotEnrolled(4, 4); 521 $this->assertHasRoleAssignment(4, 4, 'editingteacher'); 522 $this->assertIsEnrolled(3, 3, ENROL_USER_ACTIVE, 'teacher'); 523 524 $DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student')); 525 $plugin->sync_enrolments($trace); 526 $this->assertEquals(4, $DB->count_records('user_enrolments', array())); 527 $this->assertEquals(4, $DB->count_records('enrol', array('enrol' => 'database'))); 528 $this->assertEquals(5, $DB->count_records('role_assignments', array('component' => 'enrol_database'))); 529 $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student'); 530 $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher'); 531 $this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student'); 532 $this->assertIsNotEnrolled(4, 4); 533 $this->assertHasRoleAssignment(4, 4, 'editingteacher'); 534 $this->assertIsEnrolled(3, 3, ENROL_USER_ACTIVE, 'teacher'); 535 536 537 $DB->delete_records('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student')); 538 $plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL); 539 $plugin->sync_enrolments($trace); 540 $this->assertEquals(3, $DB->count_records('user_enrolments', array())); 541 $this->assertEquals(4, $DB->count_records('enrol', array('enrol' => 'database'))); 542 $this->assertEquals(4, $DB->count_records('role_assignments', array('component' => 'enrol_database'))); 543 $this->assertIsNotEnrolled(1, 1); 544 $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher'); 545 $this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student'); 546 $this->assertIsNotEnrolled(4, 4); 547 $this->assertHasRoleAssignment(4, 4, 'editingteacher'); 548 $this->assertIsEnrolled(3, 3, ENROL_USER_ACTIVE, 'teacher'); 549 550 $DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student')); 551 $DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'teacher')); 552 $plugin->sync_enrolments($trace); 553 $this->assertEquals(4, $DB->count_records('user_enrolments', array())); 554 $this->assertEquals(4, $DB->count_records('enrol', array('enrol' => 'database'))); 555 $this->assertEquals(6, $DB->count_records('role_assignments', array('component' => 'enrol_database'))); 556 $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student'); 557 $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'teacher'); 558 $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher'); 559 $this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student'); 560 $this->assertIsNotEnrolled(4, 4); 561 $this->assertHasRoleAssignment(4, 4, 'editingteacher'); 562 $this->assertIsEnrolled(3, 3, ENROL_USER_ACTIVE, 'teacher'); 563 564 $DB->delete_records('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'teacher')); 565 $plugin->sync_enrolments($trace); 566 $this->assertEquals(4, $DB->count_records('user_enrolments', array())); 567 $this->assertEquals(4, $DB->count_records('enrol', array('enrol' => 'database'))); 568 $this->assertEquals(5, $DB->count_records('role_assignments', array('component' => 'enrol_database'))); 569 $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student'); 570 $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher'); 571 $this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student'); 572 $this->assertIsNotEnrolled(4, 4); 573 $this->assertHasRoleAssignment(4, 4, 'editingteacher'); 574 $this->assertIsEnrolled(3, 3, ENROL_USER_ACTIVE, 'teacher'); 575 576 577 // Test all other mapping options. 578 579 $this->reset_enrol_database(); 580 581 $this->assertEquals(0, $DB->count_records('user_enrolments', array())); 582 $this->assertEquals(0, $DB->count_records('enrol', array('enrol' => 'database'))); 583 $this->assertEquals(0, $DB->count_records('role_assignments', array('component' => 'enrol_database'))); 584 585 $plugin->set_config('localcoursefield', 'id'); 586 $plugin->set_config('localuserfield', 'id'); 587 $plugin->set_config('localrolefield', 'id'); 588 589 $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->id, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id)); 590 $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->id, 'courseid' => self::$courses[2]->id, 'roleid' => self::$roles['teacher']->id)); 591 $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[2]->id, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id)); 592 593 $plugin->sync_enrolments($trace); 594 $this->assertEquals(3, $DB->count_records('user_enrolments', array())); 595 $this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database'))); 596 $this->assertEquals(3, $DB->count_records('role_assignments', array('component' => 'enrol_database'))); 597 $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student'); 598 $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher'); 599 $this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student'); 600 601 602 $this->reset_enrol_database(); 603 $plugin->set_config('localcoursefield', 'shortname'); 604 $plugin->set_config('localuserfield', 'email'); 605 $plugin->set_config('localrolefield', 'id'); 606 607 $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->email, 'courseid' => self::$courses[1]->shortname, 'roleid' => self::$roles['student']->id)); 608 $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->email, 'courseid' => self::$courses[2]->shortname, 'roleid' => self::$roles['teacher']->id)); 609 $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[2]->email, 'courseid' => self::$courses[1]->shortname, 'roleid' => self::$roles['student']->id)); 610 611 $plugin->sync_enrolments($trace); 612 $this->assertEquals(3, $DB->count_records('user_enrolments', array())); 613 $this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database'))); 614 $this->assertEquals(3, $DB->count_records('role_assignments', array('component' => 'enrol_database'))); 615 $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student'); 616 $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher'); 617 $this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student'); 618 619 620 $this->reset_enrol_database(); 621 $plugin->set_config('localcoursefield', 'id'); 622 $plugin->set_config('localuserfield', 'username'); 623 $plugin->set_config('localrolefield', 'id'); 624 625 $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->username, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id)); 626 $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->username, 'courseid' => self::$courses[2]->id, 'roleid' => self::$roles['teacher']->id)); 627 $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[2]->username, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id)); 628 629 $plugin->sync_enrolments($trace); 630 $this->assertEquals(3, $DB->count_records('user_enrolments', array())); 631 $this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database'))); 632 $this->assertEquals(3, $DB->count_records('role_assignments', array('component' => 'enrol_database'))); 633 $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student'); 634 $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher'); 635 $this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student'); 636 637 638 // Test sync of one course only. 639 640 $this->reset_enrol_database(); 641 642 $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->username, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id)); 643 $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->username, 'courseid' => self::$courses[2]->id, 'roleid' => self::$roles['teacher']->id)); 644 $DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[2]->username, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id)); 645 646 $this->assertEquals(0, $DB->count_records('user_enrolments', array())); 647 $this->assertEquals(0, $DB->count_records('enrol', array('enrol' => 'database'))); 648 $this->assertEquals(0, $DB->count_records('role_assignments', array('component' => 'enrol_database'))); 649 650 $plugin->sync_enrolments($trace, self::$courses[3]->id); 651 $this->assertEquals(0, $DB->count_records('user_enrolments', array())); 652 $this->assertEquals(1, $DB->count_records('enrol', array('enrol' => 'database'))); 653 $this->assertEquals(0, $DB->count_records('role_assignments', array('component' => 'enrol_database'))); 654 655 $plugin->sync_enrolments($trace, self::$courses[1]->id); 656 $this->assertEquals(2, $DB->count_records('user_enrolments', array())); 657 $this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database'))); 658 $this->assertEquals(2, $DB->count_records('role_assignments', array('component' => 'enrol_database'))); 659 $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student'); 660 $this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student'); 661 662 $plugin->sync_enrolments($trace, self::$courses[2]->id); 663 $this->assertEquals(3, $DB->count_records('user_enrolments', array())); 664 $this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database'))); 665 $this->assertEquals(3, $DB->count_records('role_assignments', array('component' => 'enrol_database'))); 666 $this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student'); 667 $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher'); 668 $this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student'); 669 670 671 $plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL); 672 673 $DB->delete_records('enrol_database_test_enrols', array()); 674 675 $plugin->sync_enrolments($trace, self::$courses[1]->id); 676 $this->assertEquals(1, $DB->count_records('user_enrolments', array())); 677 $this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database'))); 678 $this->assertEquals(1, $DB->count_records('role_assignments', array('component' => 'enrol_database'))); 679 $this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher'); 680 681 $plugin->sync_enrolments($trace, self::$courses[2]->id); 682 $this->assertEquals(0, $DB->count_records('user_enrolments', array())); 683 $this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database'))); 684 $this->assertEquals(0, $DB->count_records('role_assignments', array('component' => 'enrol_database'))); 685 } 686 687 /** 688 * @depends test_sync_users 689 */ 690 public function test_sync_courses() { 691 global $DB; 692 693 $this->resetAfterTest(true); 694 $this->preventResetByRollback(); 695 $this->reset_enrol_database(); 696 697 $plugin = enrol_get_plugin('database'); 698 699 $trace = new null_progress_trace(); 700 701 $plugin->set_config('localcategoryfield', 'id'); 702 $coursecat = $this->getDataGenerator()->create_category(array('name' => 'Test category 1', 'idnumber' => 'tcid1')); 703 $defcat = $DB->get_record('course_categories', array('id' => $plugin->get_config('defaultcategory'))); 704 705 $course1 = array('fullname' => 'New course 1', 'shortname' => 'nc1', 'idnumber' => 'ncid1', 'category' => $coursecat->id); 706 $course2 = array('fullname' => 'New course 2', 'shortname' => 'nc2', 'idnumber' => 'ncid2', 'category' => null); 707 // Duplicate records are to be ignored. 708 $course3 = array('fullname' => 'New course 3', 'shortname' => 'xx', 'idnumber' => 'yy2', 'category' => $defcat->id); 709 $course4 = array('fullname' => 'New course 4', 'shortname' => 'xx', 'idnumber' => 'yy3', 'category' => $defcat->id); 710 $course5 = array('fullname' => 'New course 5', 'shortname' => 'xx1', 'idnumber' => 'yy', 'category' => $defcat->id); 711 $course6 = array('fullname' => 'New course 6', 'shortname' => 'xx2', 'idnumber' => 'yy', 'category' => $defcat->id); 712 713 $DB->insert_record('enrol_database_test_courses', $course1); 714 $DB->insert_record('enrol_database_test_courses', $course2); 715 $DB->insert_record('enrol_database_test_courses', $course3); 716 $DB->insert_record('enrol_database_test_courses', $course4); 717 $DB->insert_record('enrol_database_test_courses', $course5); 718 $DB->insert_record('enrol_database_test_courses', $course6); 719 720 $this->assertEquals(1+count(self::$courses), $DB->count_records('course')); 721 722 $plugin->sync_courses($trace); 723 724 $this->assertEquals(4+1+count(self::$courses), $DB->count_records('course')); 725 726 $this->assertTrue($DB->record_exists('course', $course1)); 727 $course2['category'] = $defcat->id; 728 $this->assertTrue($DB->record_exists('course', $course2)); 729 730 731 // People should NOT push duplicates there because the results are UNDEFINED! But anyway skip the duplicates. 732 733 $this->assertEquals(1, $DB->count_records('course', array('idnumber' => 'yy'))); 734 $this->assertEquals(1, $DB->count_records('course', array('shortname' => 'xx'))); 735 736 // Check default number of sections matches with the created course sections. 737 738 $recordcourse1 = $DB->get_record('course', $course1); 739 $courseconfig = get_config('moodlecourse'); 740 $numsections = $DB->count_records('course_sections', array('course' => $recordcourse1->id)); 741 // To compare numsections we have to add topic 0 to default numsections. 742 $this->assertEquals(($courseconfig->numsections + 1), $numsections); 743 744 // Test category mapping via idnumber. 745 746 $plugin->set_config('localcategoryfield', 'idnumber'); 747 $course7 = array('fullname' => 'New course 7', 'shortname' => 'nc7', 'idnumber' => 'ncid7', 'category' => 'tcid1'); 748 $DB->insert_record('enrol_database_test_courses', $course7); 749 $plugin->sync_courses($trace); 750 751 $this->assertEquals(1+4+1+count(self::$courses), $DB->count_records('course')); 752 $this->assertTrue($DB->record_exists('course', $course1)); 753 $this->assertTrue($DB->record_exists('course', $course2)); 754 $course7['category'] = $coursecat->id; 755 $this->assertTrue($DB->record_exists('course', $course7)); 756 757 758 // Test course template. 759 760 $template = $this->getDataGenerator()->create_course(array('numsections' => 666, 'shortname' => 'crstempl')); 761 $plugin->set_config('templatecourse', 'crstempl'); 762 763 $course8 = array('fullname' => 'New course 8', 'shortname' => 'nc8', 'idnumber' => 'ncid8', 'category' => null); 764 $DB->insert_record('enrol_database_test_courses', $course8); 765 $plugin->sync_courses($trace); 766 767 $this->assertEquals(2+1+4+1+count(self::$courses), $DB->count_records('course')); 768 $course8['category'] = $defcat->id; 769 $record = $DB->get_record('course', $course8); 770 $this->assertFalse(empty($record)); 771 $this->assertEquals(666, course_get_format($record)->get_last_section_number()); 772 773 // Test invalid category. 774 775 $course9 = array('fullname' => 'New course 9', 'shortname' => 'nc9', 'idnumber' => 'ncid9', 'category' => 'xxxxxxx'); 776 $DB->insert_record('enrol_database_test_courses', $course9); 777 $plugin->sync_courses($trace); 778 $this->assertEquals(2+1+4+1+count(self::$courses), $DB->count_records('course')); 779 $this->assertFalse($DB->record_exists('course', array('idnumber' => 'ncid9'))); 780 781 782 // Test when categories not specified. 783 784 $plugin->set_config('newcoursecategory', ''); 785 $plugin->sync_courses($trace); 786 $this->assertEquals(1+2+1+4+1+count(self::$courses), $DB->count_records('course')); 787 $this->assertTrue($DB->record_exists('course', array('idnumber' => 'ncid9'))); 788 789 790 // Final cleanup - remove extra tables, fixtures and caches. 791 $this->cleanup_enrol_database(); 792 } 793 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body