PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/tests/navigationlib_test.php

https://bitbucket.org/moodle/moodle
PHP | 708 lines | 480 code | 122 blank | 106 comment | 15 complexity | 677e9bce1044e9796241b24d61302aba MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  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. * Unit tests for lib/navigationlib.php
  18. *
  19. * @package core
  20. * @category phpunit
  21. * @copyright 2009 Sam Hemelryk
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later (5)
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. global $CFG;
  26. require_once($CFG->libdir . '/navigationlib.php');
  27. class core_navigationlib_testcase extends advanced_testcase {
  28. /**
  29. * @var navigation_node
  30. */
  31. public $node;
  32. protected function setup_node() {
  33. global $PAGE, $SITE;
  34. $PAGE->set_url('/');
  35. $PAGE->set_course($SITE);
  36. $activeurl = $PAGE->url;
  37. $inactiveurl = new moodle_url('http://www.moodle.com/');
  38. navigation_node::override_active_url($PAGE->url);
  39. $this->node = new navigation_node('Test Node');
  40. $this->node->type = navigation_node::TYPE_SYSTEM;
  41. // We add the first child without key. This way we make sure all keys search by comparison is performed using ===.
  42. $this->node->add('first child without key', null, navigation_node::TYPE_CUSTOM);
  43. $demo1 = $this->node->add('demo1', $inactiveurl, navigation_node::TYPE_COURSE, null, 'demo1', new pix_icon('i/course', ''));
  44. $demo2 = $this->node->add('demo2', $inactiveurl, navigation_node::TYPE_COURSE, null, 'demo2', new pix_icon('i/course', ''));
  45. $demo3 = $this->node->add('demo3', $inactiveurl, navigation_node::TYPE_CATEGORY, null, 'demo3', new pix_icon('i/course', ''));
  46. $demo4 = $demo3->add('demo4', $inactiveurl, navigation_node::TYPE_COURSE, null, 'demo4', new pix_icon('i/course', ''));
  47. $demo5 = $demo3->add('demo5', $activeurl, navigation_node::TYPE_COURSE, null, 'demo5', new pix_icon('i/course', ''));
  48. $demo5->add('activity1', null, navigation_node::TYPE_ACTIVITY, null, 'activity1')->make_active();
  49. $demo6 = $demo3->add('demo6', null, navigation_node::TYPE_CONTAINER, 'container node test', 'demo6');
  50. $hiddendemo1 = $this->node->add('hiddendemo1', $inactiveurl, navigation_node::TYPE_CATEGORY, null, 'hiddendemo1', new pix_icon('i/course', ''));
  51. $hiddendemo1->hidden = true;
  52. $hiddendemo1->add('hiddendemo2', $inactiveurl, navigation_node::TYPE_COURSE, null, 'hiddendemo2', new pix_icon('i/course', ''))->helpbutton = 'Here is a help button';
  53. $hiddendemo1->add('hiddendemo3', $inactiveurl, navigation_node::TYPE_COURSE, null, 'hiddendemo3', new pix_icon('i/course', ''))->display = false;
  54. }
  55. public function test_node__construct() {
  56. $this->setup_node();
  57. $fakeproperties = array(
  58. 'text' => 'text',
  59. 'shorttext' => 'A very silly extra long short text string, more than 25 characters',
  60. 'key' => 'key',
  61. 'type' => 'navigation_node::TYPE_COURSE',
  62. 'action' => new moodle_url('http://www.moodle.org/'));
  63. $node = new navigation_node($fakeproperties);
  64. $this->assertSame($fakeproperties['text'], $node->text);
  65. $this->assertTrue(strpos($fakeproperties['shorttext'], substr($node->shorttext, 0, -3)) === 0);
  66. $this->assertSame($fakeproperties['key'], $node->key);
  67. $this->assertSame($fakeproperties['type'], $node->type);
  68. $this->assertSame($fakeproperties['action'], $node->action);
  69. }
  70. public function test_node_add() {
  71. $this->setup_node();
  72. // Add a node with all args set.
  73. $node1 = $this->node->add('test_add_1', 'http://www.moodle.org/', navigation_node::TYPE_COURSE, 'testadd1', 'key', new pix_icon('i/course', ''));
  74. // Add a node with the minimum args required.
  75. $node2 = $this->node->add('test_add_2', null, navigation_node::TYPE_CUSTOM, 'testadd2');
  76. $node3 = $this->node->add(str_repeat('moodle ', 15), str_repeat('moodle', 15));
  77. $this->assertInstanceOf('navigation_node', $node1);
  78. $this->assertInstanceOf('navigation_node', $node2);
  79. $this->assertInstanceOf('navigation_node', $node3);
  80. $ref = $this->node->get('key');
  81. $this->assertSame($node1, $ref);
  82. $ref = $this->node->get($node2->key);
  83. $this->assertSame($node2, $ref);
  84. $ref = $this->node->get($node2->key, $node2->type);
  85. $this->assertSame($node2, $ref);
  86. $ref = $this->node->get($node3->key, $node3->type);
  87. $this->assertSame($node3, $ref);
  88. }
  89. public function test_node_add_before() {
  90. $this->setup_node();
  91. // Create 3 nodes.
  92. $node1 = navigation_node::create('test_add_1', null, navigation_node::TYPE_CUSTOM,
  93. 'test 1', 'testadd1');
  94. $node2 = navigation_node::create('test_add_2', null, navigation_node::TYPE_CUSTOM,
  95. 'test 2', 'testadd2');
  96. $node3 = navigation_node::create('test_add_3', null, navigation_node::TYPE_CUSTOM,
  97. 'test 3', 'testadd3');
  98. // Add node 2, then node 1 before 2, then node 3 at end.
  99. $this->node->add_node($node2);
  100. $this->node->add_node($node1, 'testadd2');
  101. $this->node->add_node($node3);
  102. // Check the last 3 nodes are in 1, 2, 3 order and have those indexes.
  103. foreach ($this->node->children as $child) {
  104. $keys[] = $child->key;
  105. }
  106. $this->assertSame('testadd1', $keys[count($keys)-3]);
  107. $this->assertSame('testadd2', $keys[count($keys)-2]);
  108. $this->assertSame('testadd3', $keys[count($keys)-1]);
  109. }
  110. public function test_node_add_class() {
  111. $this->setup_node();
  112. $node = $this->node->get('demo1');
  113. $this->assertInstanceOf('navigation_node', $node);
  114. if ($node !== false) {
  115. $node->add_class('myclass');
  116. $classes = $node->classes;
  117. $this->assertContains('myclass', $classes);
  118. }
  119. }
  120. public function test_node_check_if_active() {
  121. $this->setup_node();
  122. // First test the string urls
  123. // Demo1 -> action is http://www.moodle.org/, thus should be true.
  124. $demo5 = $this->node->find('demo5', navigation_node::TYPE_COURSE);
  125. if ($this->assertInstanceOf('navigation_node', $demo5)) {
  126. $this->assertTrue($demo5->check_if_active());
  127. }
  128. // Demo2 -> action is http://www.moodle.com/, thus should be false.
  129. $demo2 = $this->node->get('demo2');
  130. if ($this->assertInstanceOf('navigation_node', $demo2)) {
  131. $this->assertFalse($demo2->check_if_active());
  132. }
  133. }
  134. public function test_node_contains_active_node() {
  135. $this->setup_node();
  136. // Demo5, and activity1 were set to active during setup.
  137. // Should be true as it contains all nodes.
  138. $this->assertTrue($this->node->contains_active_node());
  139. // Should be true as demo5 is a child of demo3.
  140. $this->assertTrue($this->node->get('demo3')->contains_active_node());
  141. // Obviously duff.
  142. $this->assertFalse($this->node->get('demo1')->contains_active_node());
  143. // Should be true as demo5 contains activity1.
  144. $this->assertTrue($this->node->get('demo3')->get('demo5')->contains_active_node());
  145. // Should be true activity1 is the active node.
  146. $this->assertTrue($this->node->get('demo3')->get('demo5')->get('activity1')->contains_active_node());
  147. // Obviously duff.
  148. $this->assertFalse($this->node->get('demo3')->get('demo4')->contains_active_node());
  149. }
  150. public function test_node_find_active_node() {
  151. $this->setup_node();
  152. $activenode1 = $this->node->find_active_node();
  153. $activenode2 = $this->node->get('demo1')->find_active_node();
  154. if ($this->assertInstanceOf('navigation_node', $activenode1)) {
  155. $ref = $this->node->get('demo3')->get('demo5')->get('activity1');
  156. $this->assertSame($activenode1, $ref);
  157. }
  158. $this->assertNotInstanceOf('navigation_node', $activenode2);
  159. }
  160. public function test_node_find() {
  161. $this->setup_node();
  162. $node1 = $this->node->find('demo1', navigation_node::TYPE_COURSE);
  163. $node2 = $this->node->find('demo5', navigation_node::TYPE_COURSE);
  164. $node3 = $this->node->find('demo5', navigation_node::TYPE_CATEGORY);
  165. $node4 = $this->node->find('demo0', navigation_node::TYPE_COURSE);
  166. $this->assertInstanceOf('navigation_node', $node1);
  167. $this->assertInstanceOf('navigation_node', $node2);
  168. $this->assertNotInstanceOf('navigation_node', $node3);
  169. $this->assertNotInstanceOf('navigation_node', $node4);
  170. }
  171. public function test_node_find_expandable() {
  172. $this->setup_node();
  173. $expandable = array();
  174. $this->node->find_expandable($expandable);
  175. $this->assertCount(0, $expandable);
  176. if (count($expandable) === 4) {
  177. $name = $expandable[0]['key'];
  178. $name .= $expandable[1]['key'];
  179. $name .= $expandable[2]['key'];
  180. $name .= $expandable[3]['key'];
  181. $this->assertSame('demo1demo2demo4hiddendemo2', $name);
  182. }
  183. }
  184. public function test_node_get() {
  185. $this->setup_node();
  186. $node1 = $this->node->get('demo1'); // Exists.
  187. $node2 = $this->node->get('demo4'); // Doesn't exist for this node.
  188. $node3 = $this->node->get('demo0'); // Doesn't exist at all.
  189. $node4 = $this->node->get(false); // Sometimes occurs in nature code.
  190. $this->assertInstanceOf('navigation_node', $node1);
  191. $this->assertFalse($node2);
  192. $this->assertFalse($node3);
  193. $this->assertFalse($node4);
  194. }
  195. public function test_node_get_css_type() {
  196. $this->setup_node();
  197. $csstype1 = $this->node->get('demo3')->get_css_type();
  198. $csstype2 = $this->node->get('demo3')->get('demo5')->get_css_type();
  199. $this->node->get('demo3')->get('demo5')->type = 1000;
  200. $csstype3 = $this->node->get('demo3')->get('demo5')->get_css_type();
  201. $csstype4 = $this->node->get('demo3')->get('demo6')->get_css_type();
  202. $this->assertSame('type_category', $csstype1);
  203. $this->assertSame('type_course', $csstype2);
  204. $this->assertSame('type_unknown', $csstype3);
  205. $this->assertSame('type_container', $csstype4);
  206. }
  207. public function test_node_make_active() {
  208. global $CFG;
  209. $this->setup_node();
  210. $node1 = $this->node->add('active node 1', null, navigation_node::TYPE_CUSTOM, null, 'anode1');
  211. $node2 = $this->node->add('active node 2', new moodle_url($CFG->wwwroot), navigation_node::TYPE_COURSE, null, 'anode2');
  212. $node1->make_active();
  213. $this->node->get('anode2')->make_active();
  214. $this->assertTrue($node1->isactive);
  215. $this->assertTrue($this->node->get('anode2')->isactive);
  216. }
  217. public function test_node_remove() {
  218. $this->setup_node();
  219. $remove1 = $this->node->add('child to remove 1', null, navigation_node::TYPE_CUSTOM, null, 'remove1');
  220. $remove2 = $this->node->add('child to remove 2', null, navigation_node::TYPE_CUSTOM, null, 'remove2');
  221. $remove3 = $remove2->add('child to remove 3', null, navigation_node::TYPE_CUSTOM, null, 'remove3');
  222. $this->assertInstanceOf('navigation_node', $remove1);
  223. $this->assertInstanceOf('navigation_node', $remove2);
  224. $this->assertInstanceOf('navigation_node', $remove3);
  225. $this->assertInstanceOf('navigation_node', $this->node->get('remove1'));
  226. $this->assertInstanceOf('navigation_node', $this->node->get('remove2'));
  227. $this->assertInstanceOf('navigation_node', $remove2->get('remove3'));
  228. // Remove element and make sure this is no longer a child.
  229. $this->assertTrue($remove1->remove());
  230. $this->assertFalse($this->node->get('remove1'));
  231. $this->assertFalse(in_array('remove1', $this->node->get_children_key_list(), true));
  232. // Make sure that we can insert element after removal.
  233. $insertelement = navigation_node::create('extra element 4', null, navigation_node::TYPE_CUSTOM, null, 'element4');
  234. $this->node->add_node($insertelement, 'remove2');
  235. $this->assertNotEmpty($this->node->get('element4'));
  236. // Remove more elements.
  237. $this->assertTrue($this->node->get('remove2')->remove());
  238. $this->assertFalse($this->node->get('remove2'));
  239. // Make sure that we can add element after removal.
  240. $this->node->add('extra element 5', null, navigation_node::TYPE_CUSTOM, null, 'element5');
  241. $this->assertNotEmpty($this->node->get('element5'));
  242. $this->assertTrue($remove2->get('remove3')->remove());
  243. $this->assertFalse($this->node->get('remove1'));
  244. $this->assertFalse($this->node->get('remove2'));
  245. }
  246. public function test_node_remove_class() {
  247. $this->setup_node();
  248. $this->node->add_class('testclass');
  249. $this->assertTrue($this->node->remove_class('testclass'));
  250. $this->assertNotContains('testclass', $this->node->classes);
  251. }
  252. public function test_module_extends_navigation() {
  253. $node = new exposed_global_navigation();
  254. // Create an initial tree structure to work with.
  255. $cat1 = $node->add('category 1', null, navigation_node::TYPE_CATEGORY, null, 'cat1');
  256. $cat2 = $node->add('category 2', null, navigation_node::TYPE_CATEGORY, null, 'cat2');
  257. $cat3 = $node->add('category 3', null, navigation_node::TYPE_CATEGORY, null, 'cat3');
  258. $sub1 = $cat2->add('sub category 1', null, navigation_node::TYPE_CATEGORY, null, 'sub1');
  259. $sub2 = $cat2->add('sub category 2', null, navigation_node::TYPE_CATEGORY, null, 'sub2');
  260. $sub3 = $cat2->add('sub category 3', null, navigation_node::TYPE_CATEGORY, null, 'sub3');
  261. $course1 = $sub2->add('course 1', null, navigation_node::TYPE_COURSE, null, 'course1');
  262. $course2 = $sub2->add('course 2', null, navigation_node::TYPE_COURSE, null, 'course2');
  263. $course3 = $sub2->add('course 3', null, navigation_node::TYPE_COURSE, null, 'course3');
  264. $section1 = $course2->add('section 1', null, navigation_node::TYPE_SECTION, null, 'sec1');
  265. $section2 = $course2->add('section 2', null, navigation_node::TYPE_SECTION, null, 'sec2');
  266. $section3 = $course2->add('section 3', null, navigation_node::TYPE_SECTION, null, 'sec3');
  267. $act1 = $section2->add('activity 1', null, navigation_node::TYPE_ACTIVITY, null, 'act1');
  268. $act2 = $section2->add('activity 2', null, navigation_node::TYPE_ACTIVITY, null, 'act2');
  269. $act3 = $section2->add('activity 3', null, navigation_node::TYPE_ACTIVITY, null, 'act3');
  270. $res1 = $section2->add('resource 1', null, navigation_node::TYPE_RESOURCE, null, 'res1');
  271. $res2 = $section2->add('resource 2', null, navigation_node::TYPE_RESOURCE, null, 'res2');
  272. $res3 = $section2->add('resource 3', null, navigation_node::TYPE_RESOURCE, null, 'res3');
  273. $this->assertTrue($node->exposed_module_extends_navigation('data'));
  274. $this->assertFalse($node->exposed_module_extends_navigation('test1'));
  275. }
  276. public function test_navbar_prepend_and_add() {
  277. global $PAGE;
  278. // Unfortunate hack needed because people use global $PAGE around the place.
  279. $PAGE->set_url('/');
  280. // We need to reset after this test because we using the generator.
  281. $this->resetAfterTest();
  282. $generator = self::getDataGenerator();
  283. $cat1 = $generator->create_category();
  284. $cat2 = $generator->create_category(array('parent' => $cat1->id));
  285. $course = $generator->create_course(array('category' => $cat2->id));
  286. $page = new moodle_page();
  287. $page->set_course($course);
  288. $page->set_url(new moodle_url('/course/view.php', array('id' => $course->id)));
  289. $page->navbar->prepend('test 1');
  290. $page->navbar->prepend('test 2');
  291. $page->navbar->add('test 3');
  292. $page->navbar->add('test 4');
  293. $items = $page->navbar->get_items();
  294. foreach ($items as $item) {
  295. $this->assertInstanceOf('navigation_node', $item);
  296. }
  297. $i = 0;
  298. $this->assertSame('test 1', $items[$i++]->text);
  299. $this->assertSame('test 2', $items[$i++]->text);
  300. $this->assertSame('home', $items[$i++]->key);
  301. $this->assertSame('courses', $items[$i++]->key);
  302. $this->assertSame($cat1->id, $items[$i++]->key);
  303. $this->assertSame($cat2->id, $items[$i++]->key);
  304. $this->assertSame($course->id, $items[$i++]->key);
  305. $this->assertSame('test 3', $items[$i++]->text);
  306. $this->assertSame('test 4', $items[$i++]->text);
  307. return $page;
  308. }
  309. /**
  310. * @depends test_navbar_prepend_and_add
  311. * @param $node
  312. */
  313. public function test_navbar_has_items(moodle_page $page) {
  314. $this->resetAfterTest();
  315. $this->assertTrue($page->navbar->has_items());
  316. }
  317. public function test_cache__get() {
  318. $cache = new navigation_cache('unittest_nav');
  319. $cache->anysetvariable = true;
  320. $this->assertTrue($cache->anysetvariable);
  321. $this->assertEquals($cache->notasetvariable, null);
  322. }
  323. public function test_cache__set() {
  324. $cache = new navigation_cache('unittest_nav');
  325. $cache->anysetvariable = true;
  326. $cache->myname = 'Sam Hemelryk';
  327. $this->assertTrue($cache->cached('myname'));
  328. $this->assertSame('Sam Hemelryk', $cache->myname);
  329. }
  330. public function test_cache_cached() {
  331. $cache = new navigation_cache('unittest_nav');
  332. $cache->anysetvariable = true;
  333. $this->assertTrue($cache->cached('anysetvariable'));
  334. $this->assertFalse($cache->cached('notasetvariable'));
  335. }
  336. public function test_cache_clear() {
  337. $cache = new navigation_cache('unittest_nav');
  338. $cache->anysetvariable = true;
  339. $cache = clone($cache);
  340. $this->assertTrue($cache->cached('anysetvariable'));
  341. $cache->clear();
  342. $this->assertFalse($cache->cached('anysetvariable'));
  343. }
  344. public function test_cache_set() {
  345. $cache = new navigation_cache('unittest_nav');
  346. $cache->anysetvariable = true;
  347. $cache->set('software', 'Moodle');
  348. $this->assertTrue($cache->cached('software'));
  349. $this->assertEquals($cache->software, 'Moodle');
  350. }
  351. public function test_setting___construct() {
  352. global $PAGE, $SITE;
  353. $this->resetAfterTest(false);
  354. $PAGE->set_url('/');
  355. $PAGE->set_course($SITE);
  356. $node = new exposed_settings_navigation();
  357. return $node;
  358. }
  359. /**
  360. * @depends test_setting___construct
  361. * @param mixed $node
  362. * @return mixed
  363. */
  364. public function test_setting__initialise($node) {
  365. $this->resetAfterTest(false);
  366. $node->initialise();
  367. $this->assertEquals($node->id, 'settingsnav');
  368. return $node;
  369. }
  370. /**
  371. * Test that users with the correct permissions can view the preferences page.
  372. */
  373. public function test_can_view_user_preferences() {
  374. global $PAGE, $DB, $SITE;
  375. $this->resetAfterTest();
  376. $persontoview = $this->getDataGenerator()->create_user();
  377. $persondoingtheviewing = $this->getDataGenerator()->create_user();
  378. $PAGE->set_url('/');
  379. $PAGE->set_course($SITE);
  380. // Check that a standard user can not view the preferences page.
  381. $studentrole = $DB->get_record('role', array('shortname' => 'student'));
  382. $this->getDataGenerator()->role_assign($studentrole->id, $persondoingtheviewing->id);
  383. $this->setUser($persondoingtheviewing);
  384. $settingsnav = new exposed_settings_navigation();
  385. $settingsnav->initialise();
  386. $settingsnav->extend_for_user($persontoview->id);
  387. $this->assertFalse($settingsnav->can_view_user_preferences($persontoview->id));
  388. // Set persondoingtheviewing as a manager.
  389. $managerrole = $DB->get_record('role', array('shortname' => 'manager'));
  390. $this->getDataGenerator()->role_assign($managerrole->id, $persondoingtheviewing->id);
  391. $settingsnav = new exposed_settings_navigation();
  392. $settingsnav->initialise();
  393. $settingsnav->extend_for_user($persontoview->id);
  394. $this->assertTrue($settingsnav->can_view_user_preferences($persontoview->id));
  395. // Check that the admin can view the preferences page.
  396. $this->setAdminUser();
  397. $settingsnav = new exposed_settings_navigation();
  398. $settingsnav->initialise();
  399. $settingsnav->extend_for_user($persontoview->id);
  400. $preferencenode = $settingsnav->find('userviewingsettings' . $persontoview->id, null);
  401. $this->assertTrue($settingsnav->can_view_user_preferences($persontoview->id));
  402. }
  403. /**
  404. * @depends test_setting__initialise
  405. * @param mixed $node
  406. * @return mixed
  407. */
  408. public function test_setting_in_alternative_role($node) {
  409. $this->resetAfterTest();
  410. $this->assertFalse($node->exposed_in_alternative_role());
  411. }
  412. public function test_navigation_node_collection_remove_with_no_type() {
  413. $navigationnodecollection = new navigation_node_collection();
  414. $this->setup_node();
  415. $this->node->key = 100;
  416. // Test it's empty
  417. $this->assertEquals(0, count($navigationnodecollection->get_key_list()));
  418. // Add a node
  419. $navigationnodecollection->add($this->node);
  420. // Test it's not empty
  421. $this->assertEquals(1, count($navigationnodecollection->get_key_list()));
  422. // Remove a node - passing key only!
  423. $this->assertTrue($navigationnodecollection->remove(100));
  424. // Test it's empty again!
  425. $this->assertEquals(0, count($navigationnodecollection->get_key_list()));
  426. }
  427. public function test_navigation_node_collection_remove_with_type() {
  428. $navigationnodecollection = new navigation_node_collection();
  429. $this->setup_node();
  430. $this->node->key = 100;
  431. // Test it's empty
  432. $this->assertEquals(0, count($navigationnodecollection->get_key_list()));
  433. // Add a node
  434. $navigationnodecollection->add($this->node);
  435. // Test it's not empty
  436. $this->assertEquals(1, count($navigationnodecollection->get_key_list()));
  437. // Remove a node - passing type
  438. $this->assertTrue($navigationnodecollection->remove(100, 1));
  439. // Test it's empty again!
  440. $this->assertEquals(0, count($navigationnodecollection->get_key_list()));
  441. }
  442. /**
  443. * Test the set_force_into_more_menu method.
  444. *
  445. * @param bool $haschildren Whether the navigation node has children nodes
  446. * @param bool $forceintomoremenu Whether to force the navigation node and its children into the "more" menu
  447. * @dataProvider test_set_force_into_more_menu_provider
  448. */
  449. public function test_set_force_into_more_menu(bool $haschildren, bool $forceintomoremenu) {
  450. // Create a navigation node.
  451. $node = new navigation_node(['text' => 'Navigation node', 'key' => 'navnode']);
  452. // If required, add some children nodes to the navigation node.
  453. if ($haschildren) {
  454. for ($i = 1; $i <= 3; $i++) {
  455. $node->add("Child navigation node {$i}");
  456. }
  457. }
  458. $node->set_force_into_more_menu($forceintomoremenu);
  459. // Assert that the expected value has been assigned to the 'forceintomoremenu' property
  460. // in the navigation node and its children.
  461. $this->assertEquals($forceintomoremenu, $node->forceintomoremenu);
  462. foreach ($node->children as $child) {
  463. $this->assertEquals($forceintomoremenu, $child->forceintomoremenu);
  464. }
  465. }
  466. /**
  467. * Data provider for the test_set_force_into_more_menu function.
  468. *
  469. * @return array
  470. */
  471. public function test_set_force_into_more_menu_provider(): array {
  472. return [
  473. 'Navigation node without any children nodes; Force into "more" menu => true.' =>
  474. [
  475. false,
  476. true,
  477. ],
  478. 'Navigation node with children nodes; Force into "more" menu => true.' =>
  479. [
  480. true,
  481. true,
  482. ],
  483. 'Navigation node with children nodes; Force into "more" menu => false.' =>
  484. [
  485. true,
  486. false,
  487. ],
  488. ];
  489. }
  490. }
  491. /**
  492. * This is a dummy object that allows us to call protected methods within the
  493. * global navigation class by prefixing the methods with `exposed_`
  494. */
  495. class exposed_global_navigation extends global_navigation {
  496. protected $exposedkey = 'exposed_';
  497. public function __construct(moodle_page $page=null) {
  498. global $PAGE;
  499. if ($page === null) {
  500. $page = $PAGE;
  501. }
  502. parent::__construct($page);
  503. $this->cache = new navigation_cache('unittest_nav');
  504. }
  505. public function __call($method, $arguments) {
  506. if (strpos($method, $this->exposedkey) !== false) {
  507. $method = substr($method, strlen($this->exposedkey));
  508. }
  509. if (method_exists($this, $method)) {
  510. return call_user_func_array(array($this, $method), $arguments);
  511. }
  512. throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
  513. }
  514. public function set_initialised() {
  515. $this->initialised = true;
  516. }
  517. }
  518. class mock_initialise_global_navigation extends global_navigation {
  519. protected static $count = 1;
  520. public function load_for_category() {
  521. $this->add('load_for_category', null, null, null, 'initcall'.self::$count);
  522. self::$count++;
  523. return 0;
  524. }
  525. public function load_for_course() {
  526. $this->add('load_for_course', null, null, null, 'initcall'.self::$count);
  527. self::$count++;
  528. return 0;
  529. }
  530. public function load_for_activity() {
  531. $this->add('load_for_activity', null, null, null, 'initcall'.self::$count);
  532. self::$count++;
  533. return 0;
  534. }
  535. public function load_for_user($user=null, $forceforcontext=false) {
  536. $this->add('load_for_user', null, null, null, 'initcall'.self::$count);
  537. self::$count++;
  538. return 0;
  539. }
  540. }
  541. /**
  542. * This is a dummy object that allows us to call protected methods within the
  543. * global navigation class by prefixing the methods with `exposed_`.
  544. */
  545. class exposed_navbar extends navbar {
  546. protected $exposedkey = 'exposed_';
  547. public function __construct(moodle_page $page) {
  548. parent::__construct($page);
  549. $this->cache = new navigation_cache('unittest_nav');
  550. }
  551. public function __call($method, $arguments) {
  552. if (strpos($method, $this->exposedkey) !== false) {
  553. $method = substr($method, strlen($this->exposedkey));
  554. }
  555. if (method_exists($this, $method)) {
  556. return call_user_func_array(array($this, $method), $arguments);
  557. }
  558. throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
  559. }
  560. }
  561. class navigation_exposed_moodle_page extends moodle_page {
  562. public function set_navigation(navigation_node $node) {
  563. $this->_navigation = $node;
  564. }
  565. }
  566. /**
  567. * This is a dummy object that allows us to call protected methods within the
  568. * global navigation class by prefixing the methods with `exposed_`.
  569. */
  570. class exposed_settings_navigation extends settings_navigation {
  571. protected $exposedkey = 'exposed_';
  572. public function __construct() {
  573. global $PAGE;
  574. parent::__construct($PAGE);
  575. $this->cache = new navigation_cache('unittest_nav');
  576. }
  577. public function __call($method, $arguments) {
  578. if (strpos($method, $this->exposedkey) !== false) {
  579. $method = substr($method, strlen($this->exposedkey));
  580. }
  581. if (method_exists($this, $method)) {
  582. return call_user_func_array(array($this, $method), $arguments);
  583. }
  584. throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
  585. }
  586. }