[ 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 * Test for various bits of datalib.php. 19 * 20 * @package core 21 * @category phpunit 22 * @copyright 2012 The Open University 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 * Test for various bits of datalib.php. 31 * 32 * @package core 33 * @category phpunit 34 * @copyright 2012 The Open University 35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 36 */ 37 class core_datalib_testcase extends advanced_testcase { 38 protected function normalise_sql($sort) { 39 return preg_replace('~\s+~', ' ', $sort); 40 } 41 42 protected function assert_same_sql($expected, $actual) { 43 $this->assertSame($this->normalise_sql($expected), $this->normalise_sql($actual)); 44 } 45 46 /** 47 * Do a test of the user search SQL with database users. 48 */ 49 public function test_users_search_sql() { 50 global $DB; 51 $this->resetAfterTest(); 52 53 // Set up test users. 54 $user1 = array( 55 'username' => 'usernametest1', 56 'idnumber' => 'idnumbertest1', 57 'firstname' => 'First Name User Test 1', 58 'lastname' => 'Last Name User Test 1', 59 'email' => 'usertest1@example.com', 60 'address' => '2 Test Street Perth 6000 WA', 61 'phone1' => '01010101010', 62 'phone2' => '02020203', 63 'icq' => 'testuser1', 64 'skype' => 'testuser1', 65 'yahoo' => 'testuser1', 66 'aim' => 'testuser1', 67 'msn' => 'testuser1', 68 'department' => 'Department of user 1', 69 'institution' => 'Institution of user 1', 70 'description' => 'This is a description for user 1', 71 'descriptionformat' => FORMAT_MOODLE, 72 'city' => 'Perth', 73 'url' => 'http://moodle.org', 74 'country' => 'AU' 75 ); 76 $user1 = self::getDataGenerator()->create_user($user1); 77 $user2 = array( 78 'username' => 'usernametest2', 79 'idnumber' => 'idnumbertest2', 80 'firstname' => 'First Name User Test 2', 81 'lastname' => 'Last Name User Test 2', 82 'email' => 'usertest2@example.com', 83 'address' => '222 Test Street Perth 6000 WA', 84 'phone1' => '01010101010', 85 'phone2' => '02020203', 86 'icq' => 'testuser1', 87 'skype' => 'testuser1', 88 'yahoo' => 'testuser1', 89 'aim' => 'testuser1', 90 'msn' => 'testuser1', 91 'department' => 'Department of user 2', 92 'institution' => 'Institution of user 2', 93 'description' => 'This is a description for user 2', 94 'descriptionformat' => FORMAT_MOODLE, 95 'city' => 'Perth', 96 'url' => 'http://moodle.org', 97 'country' => 'AU' 98 ); 99 $user2 = self::getDataGenerator()->create_user($user2); 100 101 // Search by name (anywhere in text). 102 list($sql, $params) = users_search_sql('User Test 2', ''); 103 $results = $DB->get_records_sql("SELECT id FROM {user} WHERE $sql ORDER BY username", $params); 104 $this->assertFalse(array_key_exists($user1->id, $results)); 105 $this->assertTrue(array_key_exists($user2->id, $results)); 106 107 // Search by (most of) full name. 108 list($sql, $params) = users_search_sql('First Name User Test 2 Last Name User', ''); 109 $results = $DB->get_records_sql("SELECT id FROM {user} WHERE $sql ORDER BY username", $params); 110 $this->assertFalse(array_key_exists($user1->id, $results)); 111 $this->assertTrue(array_key_exists($user2->id, $results)); 112 113 // Search by name (start of text) valid or not. 114 list($sql, $params) = users_search_sql('User Test 2', '', false); 115 $results = $DB->get_records_sql("SELECT id FROM {user} WHERE $sql ORDER BY username", $params); 116 $this->assertEquals(0, count($results)); 117 list($sql, $params) = users_search_sql('First Name User Test 2', '', false); 118 $results = $DB->get_records_sql("SELECT id FROM {user} WHERE $sql ORDER BY username", $params); 119 $this->assertFalse(array_key_exists($user1->id, $results)); 120 $this->assertTrue(array_key_exists($user2->id, $results)); 121 122 // Search by extra fields included or not (address). 123 list($sql, $params) = users_search_sql('Test Street', '', true); 124 $results = $DB->get_records_sql("SELECT id FROM {user} WHERE $sql ORDER BY username", $params); 125 $this->assertCount(0, $results); 126 list($sql, $params) = users_search_sql('Test Street', '', true, array('address')); 127 $results = $DB->get_records_sql("SELECT id FROM {user} WHERE $sql ORDER BY username", $params); 128 $this->assertCount(2, $results); 129 130 // Exclude user. 131 list($sql, $params) = users_search_sql('User Test', '', true, array(), array($user1->id)); 132 $results = $DB->get_records_sql("SELECT id FROM {user} WHERE $sql ORDER BY username", $params); 133 $this->assertFalse(array_key_exists($user1->id, $results)); 134 $this->assertTrue(array_key_exists($user2->id, $results)); 135 136 // Include only user. 137 list($sql, $params) = users_search_sql('User Test', '', true, array(), array(), array($user1->id)); 138 $results = $DB->get_records_sql("SELECT id FROM {user} WHERE $sql ORDER BY username", $params); 139 $this->assertTrue(array_key_exists($user1->id, $results)); 140 $this->assertFalse(array_key_exists($user2->id, $results)); 141 142 // Join with another table and use different prefix. 143 set_user_preference('amphibian', 'frog', $user1); 144 set_user_preference('amphibian', 'salamander', $user2); 145 list($sql, $params) = users_search_sql('User Test 1', 'qq'); 146 $results = $DB->get_records_sql(" 147 SELECT up.id, up.value 148 FROM {user} qq 149 JOIN {user_preferences} up ON up.userid = qq.id 150 WHERE up.name = :prefname 151 AND $sql", array_merge(array('prefname' => 'amphibian'), $params)); 152 $this->assertEquals(1, count($results)); 153 foreach ($results as $record) { 154 $this->assertSame('frog', $record->value); 155 } 156 } 157 158 public function test_users_order_by_sql_simple() { 159 list($sort, $params) = users_order_by_sql(); 160 $this->assert_same_sql('lastname, firstname, id', $sort); 161 $this->assertEquals(array(), $params); 162 } 163 164 public function test_users_order_by_sql_table_prefix() { 165 list($sort, $params) = users_order_by_sql('u'); 166 $this->assert_same_sql('u.lastname, u.firstname, u.id', $sort); 167 $this->assertEquals(array(), $params); 168 } 169 170 public function test_users_order_by_sql_search_no_extra_fields() { 171 global $CFG, $DB; 172 $this->resetAfterTest(true); 173 174 $CFG->showuseridentity = ''; 175 176 list($sort, $params) = users_order_by_sql('', 'search', context_system::instance()); 177 $this->assert_same_sql('CASE WHEN 178 ' . $DB->sql_fullname() . ' = :usersortexact1 OR 179 LOWER(firstname) = LOWER(:usersortexact2) OR 180 LOWER(lastname) = LOWER(:usersortexact3) 181 THEN 0 ELSE 1 END, lastname, firstname, id', $sort); 182 $this->assertEquals(array('usersortexact1' => 'search', 'usersortexact2' => 'search', 183 'usersortexact3' => 'search'), $params); 184 } 185 186 public function test_users_order_by_sql_search_with_extra_fields_and_prefix() { 187 global $CFG, $DB; 188 $this->resetAfterTest(); 189 190 $CFG->showuseridentity = 'email,idnumber'; 191 $this->setAdminUser(); 192 193 list($sort, $params) = users_order_by_sql('u', 'search', context_system::instance()); 194 $this->assert_same_sql('CASE WHEN 195 ' . $DB->sql_fullname('u.firstname', 'u.lastname') . ' = :usersortexact1 OR 196 LOWER(u.firstname) = LOWER(:usersortexact2) OR 197 LOWER(u.lastname) = LOWER(:usersortexact3) OR 198 LOWER(u.email) = LOWER(:usersortexact4) OR 199 LOWER(u.idnumber) = LOWER(:usersortexact5) 200 THEN 0 ELSE 1 END, u.lastname, u.firstname, u.id', $sort); 201 $this->assertEquals(array('usersortexact1' => 'search', 'usersortexact2' => 'search', 202 'usersortexact3' => 'search', 'usersortexact4' => 'search', 'usersortexact5' => 'search'), $params); 203 } 204 205 public function test_get_admin() { 206 global $CFG, $DB; 207 $this->resetAfterTest(); 208 209 $this->assertSame('2', $CFG->siteadmins); // Admin always has id 2 in new installs. 210 $defaultadmin = get_admin(); 211 $this->assertEquals($defaultadmin->id, 2); 212 213 unset_config('siteadmins'); 214 $this->assertFalse(get_admin()); 215 216 set_config('siteadmins', -1); 217 $this->assertFalse(get_admin()); 218 219 $user1 = $this->getDataGenerator()->create_user(); 220 $user2 = $this->getDataGenerator()->create_user(); 221 222 set_config('siteadmins', $user1->id.','.$user2->id); 223 $admin = get_admin(); 224 $this->assertEquals($user1->id, $admin->id); 225 226 set_config('siteadmins', '-1,'.$user2->id.','.$user1->id); 227 $admin = get_admin(); 228 $this->assertEquals($user2->id, $admin->id); 229 230 $odlread = $DB->perf_get_reads(); 231 get_admin(); // No DB queries on repeated call expected. 232 get_admin(); 233 get_admin(); 234 $this->assertEquals($odlread, $DB->perf_get_reads()); 235 } 236 237 public function test_get_admins() { 238 global $CFG, $DB; 239 $this->resetAfterTest(); 240 241 $this->assertSame('2', $CFG->siteadmins); // Admin always has id 2 in new installs. 242 243 $user1 = $this->getDataGenerator()->create_user(); 244 $user2 = $this->getDataGenerator()->create_user(); 245 $user3 = $this->getDataGenerator()->create_user(); 246 $user4 = $this->getDataGenerator()->create_user(); 247 248 $admins = get_admins(); 249 $this->assertCount(1, $admins); 250 $admin = reset($admins); 251 $this->assertTrue(isset($admins[$admin->id])); 252 $this->assertEquals(2, $admin->id); 253 254 unset_config('siteadmins'); 255 $this->assertSame(array(), get_admins()); 256 257 set_config('siteadmins', -1); 258 $this->assertSame(array(), get_admins()); 259 260 set_config('siteadmins', '-1,'.$user2->id.','.$user1->id.','.$user3->id); 261 $this->assertEquals(array($user2->id=>$user2, $user1->id=>$user1, $user3->id=>$user3), get_admins()); 262 263 $odlread = $DB->perf_get_reads(); 264 get_admins(); // This should make just one query. 265 $this->assertEquals($odlread+1, $DB->perf_get_reads()); 266 } 267 268 public function test_get_course() { 269 global $DB, $PAGE, $SITE; 270 $this->resetAfterTest(); 271 272 // First test course will be current course ($COURSE). 273 $course1obj = $this->getDataGenerator()->create_course(array('shortname' => 'FROGS')); 274 $PAGE->set_course($course1obj); 275 276 // Second test course is not current course. 277 $course2obj = $this->getDataGenerator()->create_course(array('shortname' => 'ZOMBIES')); 278 279 // Check it does not make any queries when requesting the $COURSE/$SITE. 280 $before = $DB->perf_get_queries(); 281 $result = get_course($course1obj->id); 282 $this->assertEquals($before, $DB->perf_get_queries()); 283 $this->assertSame('FROGS', $result->shortname); 284 $result = get_course($SITE->id); 285 $this->assertEquals($before, $DB->perf_get_queries()); 286 287 // Check it makes 1 query to request other courses. 288 $result = get_course($course2obj->id); 289 $this->assertSame('ZOMBIES', $result->shortname); 290 $this->assertEquals($before + 1, $DB->perf_get_queries()); 291 } 292 293 public function test_increment_revision_number() { 294 global $DB; 295 $this->resetAfterTest(); 296 297 // Use one of the fields that are used with increment_revision_number(). 298 $course1 = $this->getDataGenerator()->create_course(); 299 $course2 = $this->getDataGenerator()->create_course(); 300 $DB->set_field('course', 'cacherev', 1, array()); 301 302 $record1 = $DB->get_record('course', array('id'=>$course1->id)); 303 $record2 = $DB->get_record('course', array('id'=>$course2->id)); 304 $this->assertEquals(1, $record1->cacherev); 305 $this->assertEquals(1, $record2->cacherev); 306 307 // Incrementing some lower value. 308 $this->setCurrentTimeStart(); 309 increment_revision_number('course', 'cacherev', 'id = :id', array('id'=>$course1->id)); 310 $record1 = $DB->get_record('course', array('id'=>$course1->id)); 311 $record2 = $DB->get_record('course', array('id'=>$course2->id)); 312 $this->assertTimeCurrent($record1->cacherev); 313 $this->assertEquals(1, $record2->cacherev); 314 315 // Incrementing in the same second. 316 $rev1 = $DB->get_field('course', 'cacherev', array('id'=>$course1->id)); 317 $now = time(); 318 $DB->set_field('course', 'cacherev', $now, array('id'=>$course1->id)); 319 increment_revision_number('course', 'cacherev', 'id = :id', array('id'=>$course1->id)); 320 $rev2 = $DB->get_field('course', 'cacherev', array('id'=>$course1->id)); 321 $this->assertGreaterThan($rev1, $rev2); 322 increment_revision_number('course', 'cacherev', 'id = :id', array('id'=>$course1->id)); 323 $rev3 = $DB->get_field('course', 'cacherev', array('id'=>$course1->id)); 324 $this->assertGreaterThan($rev2, $rev3); 325 $this->assertGreaterThan($now+1, $rev3); 326 increment_revision_number('course', 'cacherev', 'id = :id', array('id'=>$course1->id)); 327 $rev4 = $DB->get_field('course', 'cacherev', array('id'=>$course1->id)); 328 $this->assertGreaterThan($rev3, $rev4); 329 $this->assertGreaterThan($now+2, $rev4); 330 331 // Recovering from runaway revision. 332 $DB->set_field('course', 'cacherev', time()+60*60*60, array('id'=>$course2->id)); 333 $record2 = $DB->get_record('course', array('id'=>$course2->id)); 334 $this->assertGreaterThan(time(), $record2->cacherev); 335 $this->setCurrentTimeStart(); 336 increment_revision_number('course', 'cacherev', 'id = :id', array('id'=>$course2->id)); 337 $record2b = $DB->get_record('course', array('id'=>$course2->id)); 338 $this->assertTimeCurrent($record2b->cacherev); 339 340 // Update all revisions. 341 $DB->set_field('course', 'cacherev', 1, array()); 342 $this->setCurrentTimeStart(); 343 increment_revision_number('course', 'cacherev', ''); 344 $record1 = $DB->get_record('course', array('id'=>$course1->id)); 345 $record2 = $DB->get_record('course', array('id'=>$course2->id)); 346 $this->assertTimeCurrent($record1->cacherev); 347 $this->assertEquals($record1->cacherev, $record2->cacherev); 348 } 349 350 public function test_get_coursemodule_from_id() { 351 global $CFG; 352 353 $this->resetAfterTest(); 354 $this->setAdminUser(); // Some generators have bogus access control. 355 356 $this->assertFileExists("$CFG->dirroot/mod/folder/lib.php"); 357 $this->assertFileExists("$CFG->dirroot/mod/glossary/lib.php"); 358 359 $course1 = $this->getDataGenerator()->create_course(); 360 $course2 = $this->getDataGenerator()->create_course(); 361 362 $folder1a = $this->getDataGenerator()->create_module('folder', array('course' => $course1, 'section' => 3)); 363 $folder1b = $this->getDataGenerator()->create_module('folder', array('course' => $course1)); 364 $glossary1 = $this->getDataGenerator()->create_module('glossary', array('course' => $course1)); 365 366 $folder2 = $this->getDataGenerator()->create_module('folder', array('course' => $course2)); 367 368 $cm = get_coursemodule_from_id('folder', $folder1a->cmid); 369 $this->assertInstanceOf('stdClass', $cm); 370 $this->assertSame('folder', $cm->modname); 371 $this->assertSame($folder1a->id, $cm->instance); 372 $this->assertSame($folder1a->course, $cm->course); 373 $this->assertObjectNotHasAttribute('sectionnum', $cm); 374 375 $this->assertEquals($cm, get_coursemodule_from_id('', $folder1a->cmid)); 376 $this->assertEquals($cm, get_coursemodule_from_id('folder', $folder1a->cmid, $course1->id)); 377 $this->assertEquals($cm, get_coursemodule_from_id('folder', $folder1a->cmid, 0)); 378 $this->assertFalse(get_coursemodule_from_id('folder', $folder1a->cmid, -10)); 379 380 $cm2 = get_coursemodule_from_id('folder', $folder1a->cmid, 0, true); 381 $this->assertEquals(3, $cm2->sectionnum); 382 unset($cm2->sectionnum); 383 $this->assertEquals($cm, $cm2); 384 385 $this->assertFalse(get_coursemodule_from_id('folder', -11)); 386 387 try { 388 get_coursemodule_from_id('folder', -11, 0, false, MUST_EXIST); 389 $this->fail('dml_missing_record_exception expected'); 390 } catch (moodle_exception $e) { 391 $this->assertInstanceOf('dml_missing_record_exception', $e); 392 } 393 394 try { 395 get_coursemodule_from_id('', -11, 0, false, MUST_EXIST); 396 $this->fail('dml_missing_record_exception expected'); 397 } catch (moodle_exception $e) { 398 $this->assertInstanceOf('dml_missing_record_exception', $e); 399 } 400 401 try { 402 get_coursemodule_from_id('a b', $folder1a->cmid, 0, false, MUST_EXIST); 403 $this->fail('coding_exception expected'); 404 } catch (moodle_exception $e) { 405 $this->assertInstanceOf('coding_exception', $e); 406 } 407 408 try { 409 get_coursemodule_from_id('abc', $folder1a->cmid, 0, false, MUST_EXIST); 410 $this->fail('dml_read_exception expected'); 411 } catch (moodle_exception $e) { 412 $this->assertInstanceOf('dml_read_exception', $e); 413 } 414 } 415 416 public function test_get_coursemodule_from_instance() { 417 global $CFG; 418 419 $this->resetAfterTest(); 420 $this->setAdminUser(); // Some generators have bogus access control. 421 422 $this->assertFileExists("$CFG->dirroot/mod/folder/lib.php"); 423 $this->assertFileExists("$CFG->dirroot/mod/glossary/lib.php"); 424 425 $course1 = $this->getDataGenerator()->create_course(); 426 $course2 = $this->getDataGenerator()->create_course(); 427 428 $folder1a = $this->getDataGenerator()->create_module('folder', array('course' => $course1, 'section' => 3)); 429 $folder1b = $this->getDataGenerator()->create_module('folder', array('course' => $course1)); 430 431 $folder2 = $this->getDataGenerator()->create_module('folder', array('course' => $course2)); 432 433 $cm = get_coursemodule_from_instance('folder', $folder1a->id); 434 $this->assertInstanceOf('stdClass', $cm); 435 $this->assertSame('folder', $cm->modname); 436 $this->assertSame($folder1a->id, $cm->instance); 437 $this->assertSame($folder1a->course, $cm->course); 438 $this->assertObjectNotHasAttribute('sectionnum', $cm); 439 440 $this->assertEquals($cm, get_coursemodule_from_instance('folder', $folder1a->id, $course1->id)); 441 $this->assertEquals($cm, get_coursemodule_from_instance('folder', $folder1a->id, 0)); 442 $this->assertFalse(get_coursemodule_from_instance('folder', $folder1a->id, -10)); 443 444 $cm2 = get_coursemodule_from_instance('folder', $folder1a->id, 0, true); 445 $this->assertEquals(3, $cm2->sectionnum); 446 unset($cm2->sectionnum); 447 $this->assertEquals($cm, $cm2); 448 449 $this->assertFalse(get_coursemodule_from_instance('folder', -11)); 450 451 try { 452 get_coursemodule_from_instance('folder', -11, 0, false, MUST_EXIST); 453 $this->fail('dml_missing_record_exception expected'); 454 } catch (moodle_exception $e) { 455 $this->assertInstanceOf('dml_missing_record_exception', $e); 456 } 457 458 try { 459 get_coursemodule_from_instance('a b', $folder1a->cmid, 0, false, MUST_EXIST); 460 $this->fail('coding_exception expected'); 461 } catch (moodle_exception $e) { 462 $this->assertInstanceOf('coding_exception', $e); 463 } 464 465 try { 466 get_coursemodule_from_instance('', $folder1a->cmid, 0, false, MUST_EXIST); 467 $this->fail('coding_exception expected'); 468 } catch (moodle_exception $e) { 469 $this->assertInstanceOf('coding_exception', $e); 470 } 471 472 try { 473 get_coursemodule_from_instance('abc', $folder1a->cmid, 0, false, MUST_EXIST); 474 $this->fail('dml_read_exception expected'); 475 } catch (moodle_exception $e) { 476 $this->assertInstanceOf('dml_read_exception', $e); 477 } 478 } 479 480 public function test_get_coursemodules_in_course() { 481 global $CFG; 482 483 $this->resetAfterTest(); 484 $this->setAdminUser(); // Some generators have bogus access control. 485 486 $this->assertFileExists("$CFG->dirroot/mod/folder/lib.php"); 487 $this->assertFileExists("$CFG->dirroot/mod/glossary/lib.php"); 488 $this->assertFileExists("$CFG->dirroot/mod/label/lib.php"); 489 490 $course1 = $this->getDataGenerator()->create_course(); 491 $course2 = $this->getDataGenerator()->create_course(); 492 493 $folder1a = $this->getDataGenerator()->create_module('folder', array('course' => $course1, 'section' => 3)); 494 $folder1b = $this->getDataGenerator()->create_module('folder', array('course' => $course1)); 495 $glossary1 = $this->getDataGenerator()->create_module('glossary', array('course' => $course1)); 496 497 $folder2 = $this->getDataGenerator()->create_module('folder', array('course' => $course2)); 498 $glossary2a = $this->getDataGenerator()->create_module('glossary', array('course' => $course2)); 499 $glossary2b = $this->getDataGenerator()->create_module('glossary', array('course' => $course2)); 500 501 $modules = get_coursemodules_in_course('folder', $course1->id); 502 $this->assertCount(2, $modules); 503 504 $cm = $modules[$folder1a->cmid]; 505 $this->assertSame('folder', $cm->modname); 506 $this->assertSame($folder1a->id, $cm->instance); 507 $this->assertSame($folder1a->course, $cm->course); 508 $this->assertObjectNotHasAttribute('sectionnum', $cm); 509 $this->assertObjectNotHasAttribute('revision', $cm); 510 $this->assertObjectNotHasAttribute('display', $cm); 511 512 $cm = $modules[$folder1b->cmid]; 513 $this->assertSame('folder', $cm->modname); 514 $this->assertSame($folder1b->id, $cm->instance); 515 $this->assertSame($folder1b->course, $cm->course); 516 $this->assertObjectNotHasAttribute('sectionnum', $cm); 517 $this->assertObjectNotHasAttribute('revision', $cm); 518 $this->assertObjectNotHasAttribute('display', $cm); 519 520 $modules = get_coursemodules_in_course('folder', $course1->id, 'revision, display'); 521 $this->assertCount(2, $modules); 522 523 $cm = $modules[$folder1a->cmid]; 524 $this->assertSame('folder', $cm->modname); 525 $this->assertSame($folder1a->id, $cm->instance); 526 $this->assertSame($folder1a->course, $cm->course); 527 $this->assertObjectNotHasAttribute('sectionnum', $cm); 528 $this->assertObjectHasAttribute('revision', $cm); 529 $this->assertObjectHasAttribute('display', $cm); 530 531 $modules = get_coursemodules_in_course('label', $course1->id); 532 $this->assertCount(0, $modules); 533 534 try { 535 get_coursemodules_in_course('a b', $course1->id); 536 $this->fail('coding_exception expected'); 537 } catch (moodle_exception $e) { 538 $this->assertInstanceOf('coding_exception', $e); 539 } 540 541 try { 542 get_coursemodules_in_course('abc', $course1->id); 543 $this->fail('dml_read_exception expected'); 544 } catch (moodle_exception $e) { 545 $this->assertInstanceOf('dml_read_exception', $e); 546 } 547 } 548 549 public function test_get_all_instances_in_courses() { 550 global $CFG; 551 552 $this->resetAfterTest(); 553 $this->setAdminUser(); // Some generators have bogus access control. 554 555 $this->assertFileExists("$CFG->dirroot/mod/folder/lib.php"); 556 $this->assertFileExists("$CFG->dirroot/mod/glossary/lib.php"); 557 558 $course1 = $this->getDataGenerator()->create_course(); 559 $course2 = $this->getDataGenerator()->create_course(); 560 $course3 = $this->getDataGenerator()->create_course(); 561 562 $folder1a = $this->getDataGenerator()->create_module('folder', array('course' => $course1, 'section' => 3)); 563 $folder1b = $this->getDataGenerator()->create_module('folder', array('course' => $course1)); 564 $glossary1 = $this->getDataGenerator()->create_module('glossary', array('course' => $course1)); 565 566 $folder2 = $this->getDataGenerator()->create_module('folder', array('course' => $course2)); 567 $glossary2a = $this->getDataGenerator()->create_module('glossary', array('course' => $course2)); 568 $glossary2b = $this->getDataGenerator()->create_module('glossary', array('course' => $course2)); 569 570 $folder3 = $this->getDataGenerator()->create_module('folder', array('course' => $course3)); 571 572 $modules = get_all_instances_in_courses('folder', array($course1->id => $course1, $course2->id => $course2)); 573 $this->assertCount(3, $modules); 574 575 foreach ($modules as $cm) { 576 if ($folder1a->cmid == $cm->coursemodule) { 577 $folder = $folder1a; 578 } else if ($folder1b->cmid == $cm->coursemodule) { 579 $folder = $folder1b; 580 } else if ($folder2->cmid == $cm->coursemodule) { 581 $folder = $folder2; 582 } else { 583 $this->fail('Unexpected cm'. $cm->coursemodule); 584 } 585 $this->assertSame($folder->name, $cm->name); 586 $this->assertSame($folder->course, $cm->course); 587 } 588 589 try { 590 get_all_instances_in_courses('a b', array($course1->id => $course1, $course2->id => $course2)); 591 $this->fail('coding_exception expected'); 592 } catch (moodle_exception $e) { 593 $this->assertInstanceOf('coding_exception', $e); 594 } 595 596 try { 597 get_all_instances_in_courses('', array($course1->id => $course1, $course2->id => $course2)); 598 $this->fail('coding_exception expected'); 599 } catch (moodle_exception $e) { 600 $this->assertInstanceOf('coding_exception', $e); 601 } 602 } 603 604 public function test_get_all_instances_in_course() { 605 global $CFG; 606 607 $this->resetAfterTest(); 608 $this->setAdminUser(); // Some generators have bogus access control. 609 610 $this->assertFileExists("$CFG->dirroot/mod/folder/lib.php"); 611 $this->assertFileExists("$CFG->dirroot/mod/glossary/lib.php"); 612 613 $course1 = $this->getDataGenerator()->create_course(); 614 $course2 = $this->getDataGenerator()->create_course(); 615 $course3 = $this->getDataGenerator()->create_course(); 616 617 $folder1a = $this->getDataGenerator()->create_module('folder', array('course' => $course1, 'section' => 3)); 618 $folder1b = $this->getDataGenerator()->create_module('folder', array('course' => $course1)); 619 $glossary1 = $this->getDataGenerator()->create_module('glossary', array('course' => $course1)); 620 621 $folder2 = $this->getDataGenerator()->create_module('folder', array('course' => $course2)); 622 $glossary2a = $this->getDataGenerator()->create_module('glossary', array('course' => $course2)); 623 $glossary2b = $this->getDataGenerator()->create_module('glossary', array('course' => $course2)); 624 625 $folder3 = $this->getDataGenerator()->create_module('folder', array('course' => $course3)); 626 627 $modules = get_all_instances_in_course('folder', $course1); 628 $this->assertCount(2, $modules); 629 630 foreach ($modules as $cm) { 631 if ($folder1a->cmid == $cm->coursemodule) { 632 $folder = $folder1a; 633 } else if ($folder1b->cmid == $cm->coursemodule) { 634 $folder = $folder1b; 635 } else { 636 $this->fail('Unexpected cm'. $cm->coursemodule); 637 } 638 $this->assertSame($folder->name, $cm->name); 639 $this->assertSame($folder->course, $cm->course); 640 } 641 642 try { 643 get_all_instances_in_course('a b', $course1); 644 $this->fail('coding_exception expected'); 645 } catch (moodle_exception $e) { 646 $this->assertInstanceOf('coding_exception', $e); 647 } 648 649 try { 650 get_all_instances_in_course('', $course1); 651 $this->fail('coding_exception expected'); 652 } catch (moodle_exception $e) { 653 $this->assertInstanceOf('coding_exception', $e); 654 } 655 } 656 }
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 |