Merge branch 'MDL-55609-master' of git://github.com/andrewnicols/moodle
[moodle.git] / lib / tests / datalib_test.php
blobb334a3f61c7ffe543e5a80e7f9879e7787aea7d8
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.
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/>.
17 /**
18 * Test for various bits of datalib.php.
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
26 defined('MOODLE_INTERNAL') || die();
29 /**
30 * Test for various bits of datalib.php.
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
37 class core_datalib_testcase extends advanced_testcase {
38 protected function normalise_sql($sort) {
39 return preg_replace('~\s+~', ' ', $sort);
42 protected function assert_same_sql($expected, $actual) {
43 $this->assertSame($this->normalise_sql($expected), $this->normalise_sql($actual));
46 /**
47 * Do a test of the user search SQL with database users.
49 public function test_users_search_sql() {
50 global $DB;
51 $this->resetAfterTest();
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'
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'
99 $user2 = self::getDataGenerator()->create_user($user2);
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));
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));
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));
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);
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));
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));
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);
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);
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);
170 public function test_users_order_by_sql_search_no_extra_fields() {
171 global $CFG, $DB;
172 $this->resetAfterTest(true);
174 $CFG->showuseridentity = '';
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);
186 public function test_users_order_by_sql_search_with_extra_fields_and_prefix() {
187 global $CFG, $DB;
188 $this->resetAfterTest();
190 $CFG->showuseridentity = 'email,idnumber';
191 $this->setAdminUser();
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);
205 public function test_get_admin() {
206 global $CFG, $DB;
207 $this->resetAfterTest();
209 $this->assertSame('2', $CFG->siteadmins); // Admin always has id 2 in new installs.
210 $defaultadmin = get_admin();
211 $this->assertEquals($defaultadmin->id, 2);
213 unset_config('siteadmins');
214 $this->assertFalse(get_admin());
216 set_config('siteadmins', -1);
217 $this->assertFalse(get_admin());
219 $user1 = $this->getDataGenerator()->create_user();
220 $user2 = $this->getDataGenerator()->create_user();
222 set_config('siteadmins', $user1->id.','.$user2->id);
223 $admin = get_admin();
224 $this->assertEquals($user1->id, $admin->id);
226 set_config('siteadmins', '-1,'.$user2->id.','.$user1->id);
227 $admin = get_admin();
228 $this->assertEquals($user2->id, $admin->id);
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());
237 public function test_get_admins() {
238 global $CFG, $DB;
239 $this->resetAfterTest();
241 $this->assertSame('2', $CFG->siteadmins); // Admin always has id 2 in new installs.
243 $user1 = $this->getDataGenerator()->create_user();
244 $user2 = $this->getDataGenerator()->create_user();
245 $user3 = $this->getDataGenerator()->create_user();
246 $user4 = $this->getDataGenerator()->create_user();
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);
254 unset_config('siteadmins');
255 $this->assertSame(array(), get_admins());
257 set_config('siteadmins', -1);
258 $this->assertSame(array(), get_admins());
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());
263 $odlread = $DB->perf_get_reads();
264 get_admins(); // This should make just one query.
265 $this->assertEquals($odlread+1, $DB->perf_get_reads());
268 public function test_get_course() {
269 global $DB, $PAGE, $SITE;
270 $this->resetAfterTest();
272 // First test course will be current course ($COURSE).
273 $course1obj = $this->getDataGenerator()->create_course(array('shortname' => 'FROGS'));
274 $PAGE->set_course($course1obj);
276 // Second test course is not current course.
277 $course2obj = $this->getDataGenerator()->create_course(array('shortname' => 'ZOMBIES'));
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());
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());
293 public function test_increment_revision_number() {
294 global $DB;
295 $this->resetAfterTest();
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());
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);
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);
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);
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);
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);
350 public function test_get_coursemodule_from_id() {
351 global $CFG;
353 $this->resetAfterTest();
354 $this->setAdminUser(); // Some generators have bogus access control.
356 $this->assertFileExists("$CFG->dirroot/mod/folder/lib.php");
357 $this->assertFileExists("$CFG->dirroot/mod/glossary/lib.php");
359 $course1 = $this->getDataGenerator()->create_course();
360 $course2 = $this->getDataGenerator()->create_course();
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));
366 $folder2 = $this->getDataGenerator()->create_module('folder', array('course' => $course2));
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);
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));
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);
385 $this->assertFalse(get_coursemodule_from_id('folder', -11));
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);
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);
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);
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);
416 public function test_get_coursemodule_from_instance() {
417 global $CFG;
419 $this->resetAfterTest();
420 $this->setAdminUser(); // Some generators have bogus access control.
422 $this->assertFileExists("$CFG->dirroot/mod/folder/lib.php");
423 $this->assertFileExists("$CFG->dirroot/mod/glossary/lib.php");
425 $course1 = $this->getDataGenerator()->create_course();
426 $course2 = $this->getDataGenerator()->create_course();
428 $folder1a = $this->getDataGenerator()->create_module('folder', array('course' => $course1, 'section' => 3));
429 $folder1b = $this->getDataGenerator()->create_module('folder', array('course' => $course1));
431 $folder2 = $this->getDataGenerator()->create_module('folder', array('course' => $course2));
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);
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));
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);
449 $this->assertFalse(get_coursemodule_from_instance('folder', -11));
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);
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);
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);
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);
480 public function test_get_coursemodules_in_course() {
481 global $CFG;
483 $this->resetAfterTest();
484 $this->setAdminUser(); // Some generators have bogus access control.
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");
490 $course1 = $this->getDataGenerator()->create_course();
491 $course2 = $this->getDataGenerator()->create_course();
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));
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));
501 $modules = get_coursemodules_in_course('folder', $course1->id);
502 $this->assertCount(2, $modules);
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);
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);
520 $modules = get_coursemodules_in_course('folder', $course1->id, 'revision, display');
521 $this->assertCount(2, $modules);
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);
531 $modules = get_coursemodules_in_course('label', $course1->id);
532 $this->assertCount(0, $modules);
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);
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);
549 public function test_get_all_instances_in_courses() {
550 global $CFG;
552 $this->resetAfterTest();
553 $this->setAdminUser(); // Some generators have bogus access control.
555 $this->assertFileExists("$CFG->dirroot/mod/folder/lib.php");
556 $this->assertFileExists("$CFG->dirroot/mod/glossary/lib.php");
558 $course1 = $this->getDataGenerator()->create_course();
559 $course2 = $this->getDataGenerator()->create_course();
560 $course3 = $this->getDataGenerator()->create_course();
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));
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));
570 $folder3 = $this->getDataGenerator()->create_module('folder', array('course' => $course3));
572 $modules = get_all_instances_in_courses('folder', array($course1->id => $course1, $course2->id => $course2));
573 $this->assertCount(3, $modules);
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);
585 $this->assertSame($folder->name, $cm->name);
586 $this->assertSame($folder->course, $cm->course);
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);
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);
604 public function test_get_all_instances_in_course() {
605 global $CFG;
607 $this->resetAfterTest();
608 $this->setAdminUser(); // Some generators have bogus access control.
610 $this->assertFileExists("$CFG->dirroot/mod/folder/lib.php");
611 $this->assertFileExists("$CFG->dirroot/mod/glossary/lib.php");
613 $course1 = $this->getDataGenerator()->create_course();
614 $course2 = $this->getDataGenerator()->create_course();
615 $course3 = $this->getDataGenerator()->create_course();
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));
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));
625 $folder3 = $this->getDataGenerator()->create_module('folder', array('course' => $course3));
627 $modules = get_all_instances_in_course('folder', $course1);
628 $this->assertCount(2, $modules);
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);
638 $this->assertSame($folder->name, $cm->name);
639 $this->assertSame($folder->course, $cm->course);
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);
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);