PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/tests/datalib_test.php

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