2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Unit tests for lib/navigationlib.php
22 * @copyright 2009 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later (5)
26 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->libdir . '/navigationlib.php');
32 class core_navigationlib_testcase extends advanced_testcase {
34 * @var navigation_node
38 protected function setup_node() {
42 $PAGE->set_course($SITE);
44 $activeurl = $PAGE->url;
45 $inactiveurl = new moodle_url('http://www.moodle.com/');
47 navigation_node::override_active_url($PAGE->url);
49 $this->node = new navigation_node('Test Node');
50 $this->node->type = navigation_node::TYPE_SYSTEM;
51 // We add the first child without key. This way we make sure all keys search by comparison is performed using ===.
52 $this->node->add('first child without key', null, navigation_node::TYPE_CUSTOM);
53 $demo1 = $this->node->add('demo1', $inactiveurl, navigation_node::TYPE_COURSE, null, 'demo1', new pix_icon('i/course', ''));
54 $demo2 = $this->node->add('demo2', $inactiveurl, navigation_node::TYPE_COURSE, null, 'demo2', new pix_icon('i/course', ''));
55 $demo3 = $this->node->add('demo3', $inactiveurl, navigation_node::TYPE_CATEGORY, null, 'demo3', new pix_icon('i/course', ''));
56 $demo4 = $demo3->add('demo4', $inactiveurl, navigation_node::TYPE_COURSE, null, 'demo4', new pix_icon('i/course', ''));
57 $demo5 = $demo3->add('demo5', $activeurl, navigation_node::TYPE_COURSE, null, 'demo5', new pix_icon('i/course', ''));
58 $demo5->add('activity1', null, navigation_node::TYPE_ACTIVITY, null, 'activity1')->make_active();
59 $hiddendemo1 = $this->node->add('hiddendemo1', $inactiveurl, navigation_node::TYPE_CATEGORY, null, 'hiddendemo1', new pix_icon('i/course', ''));
60 $hiddendemo1->hidden = true;
61 $hiddendemo1->add('hiddendemo2', $inactiveurl, navigation_node::TYPE_COURSE, null, 'hiddendemo2', new pix_icon('i/course', ''))->helpbutton = 'Here is a help button';
62 $hiddendemo1->add('hiddendemo3', $inactiveurl, navigation_node::TYPE_COURSE, null, 'hiddendemo3', new pix_icon('i/course', ''))->display = false;
65 public function test_node__construct() {
68 $fakeproperties = array(
70 'shorttext' => 'A very silly extra long short text string, more than 25 characters',
72 'type' => 'navigation_node::TYPE_COURSE',
73 'action' => new moodle_url('http://www.moodle.org/'));
75 $node = new navigation_node($fakeproperties);
76 $this->assertSame($fakeproperties['text'], $node->text);
77 $this->assertTrue(strpos($fakeproperties['shorttext'], substr($node->shorttext, 0, -3)) === 0);
78 $this->assertSame($fakeproperties['key'], $node->key);
79 $this->assertSame($fakeproperties['type'], $node->type);
80 $this->assertSame($fakeproperties['action'], $node->action);
83 public function test_node_add() {
86 // Add a node with all args set.
87 $node1 = $this->node->add('test_add_1', 'http://www.moodle.org/', navigation_node::TYPE_COURSE, 'testadd1', 'key', new pix_icon('i/course', ''));
88 // Add a node with the minimum args required.
89 $node2 = $this->node->add('test_add_2', null, navigation_node::TYPE_CUSTOM, 'testadd2');
90 $node3 = $this->node->add(str_repeat('moodle ', 15), str_repeat('moodle', 15));
92 $this->assertInstanceOf('navigation_node', $node1);
93 $this->assertInstanceOf('navigation_node', $node2);
94 $this->assertInstanceOf('navigation_node', $node3);
96 $ref = $this->node->get('key');
97 $this->assertSame($node1, $ref);
99 $ref = $this->node->get($node2->key);
100 $this->assertSame($node2, $ref);
102 $ref = $this->node->get($node2->key, $node2->type);
103 $this->assertSame($node2, $ref);
105 $ref = $this->node->get($node3->key, $node3->type);
106 $this->assertSame($node3, $ref);
109 public function test_node_add_before() {
113 $node1 = navigation_node::create('test_add_1', null, navigation_node::TYPE_CUSTOM,
114 'test 1', 'testadd1');
115 $node2 = navigation_node::create('test_add_2', null, navigation_node::TYPE_CUSTOM,
116 'test 2', 'testadd2');
117 $node3 = navigation_node::create('test_add_3', null, navigation_node::TYPE_CUSTOM,
118 'test 3', 'testadd3');
119 // Add node 2, then node 1 before 2, then node 3 at end.
120 $this->node->add_node($node2);
121 $this->node->add_node($node1, 'testadd2');
122 $this->node->add_node($node3);
123 // Check the last 3 nodes are in 1, 2, 3 order and have those indexes.
124 foreach ($this->node->children as $child) {
125 $keys[] = $child->key;
127 $this->assertSame('testadd1', $keys[count($keys)-3]);
128 $this->assertSame('testadd2', $keys[count($keys)-2]);
129 $this->assertSame('testadd3', $keys[count($keys)-1]);
132 public function test_node_add_class() {
135 $node = $this->node->get('demo1');
136 $this->assertInstanceOf('navigation_node', $node);
137 if ($node !== false) {
138 $node->add_class('myclass');
139 $classes = $node->classes;
140 $this->assertContains('myclass', $classes);
144 public function test_node_check_if_active() {
147 // First test the string urls
148 // Demo1 -> action is http://www.moodle.org/, thus should be true.
149 $demo5 = $this->node->find('demo5', navigation_node::TYPE_COURSE);
150 if ($this->assertInstanceOf('navigation_node', $demo5)) {
151 $this->assertTrue($demo5->check_if_active());
154 // Demo2 -> action is http://www.moodle.com/, thus should be false.
155 $demo2 = $this->node->get('demo2');
156 if ($this->assertInstanceOf('navigation_node', $demo2)) {
157 $this->assertFalse($demo2->check_if_active());
161 public function test_node_contains_active_node() {
164 // Demo5, and activity1 were set to active during setup.
165 // Should be true as it contains all nodes.
166 $this->assertTrue($this->node->contains_active_node());
167 // Should be true as demo5 is a child of demo3.
168 $this->assertTrue($this->node->get('demo3')->contains_active_node());
170 $this->assertFalse($this->node->get('demo1')->contains_active_node());
171 // Should be true as demo5 contains activity1.
172 $this->assertTrue($this->node->get('demo3')->get('demo5')->contains_active_node());
173 // Should be true activity1 is the active node.
174 $this->assertTrue($this->node->get('demo3')->get('demo5')->get('activity1')->contains_active_node());
176 $this->assertFalse($this->node->get('demo3')->get('demo4')->contains_active_node());
179 public function test_node_find_active_node() {
182 $activenode1 = $this->node->find_active_node();
183 $activenode2 = $this->node->get('demo1')->find_active_node();
185 if ($this->assertInstanceOf('navigation_node', $activenode1)) {
186 $ref = $this->node->get('demo3')->get('demo5')->get('activity1');
187 $this->assertSame($activenode1, $ref);
190 $this->assertNotInstanceOf('navigation_node', $activenode2);
193 public function test_node_find() {
196 $node1 = $this->node->find('demo1', navigation_node::TYPE_COURSE);
197 $node2 = $this->node->find('demo5', navigation_node::TYPE_COURSE);
198 $node3 = $this->node->find('demo5', navigation_node::TYPE_CATEGORY);
199 $node4 = $this->node->find('demo0', navigation_node::TYPE_COURSE);
200 $this->assertInstanceOf('navigation_node', $node1);
201 $this->assertInstanceOf('navigation_node', $node2);
202 $this->assertNotInstanceOf('navigation_node', $node3);
203 $this->assertNotInstanceOf('navigation_node', $node4);
206 public function test_node_find_expandable() {
209 $expandable = array();
210 $this->node->find_expandable($expandable);
212 $this->assertCount(0, $expandable);
213 if (count($expandable) === 4) {
214 $name = $expandable[0]['key'];
215 $name .= $expandable[1]['key'];
216 $name .= $expandable[2]['key'];
217 $name .= $expandable[3]['key'];
218 $this->assertSame('demo1demo2demo4hiddendemo2', $name);
222 public function test_node_get() {
225 $node1 = $this->node->get('demo1'); // Exists.
226 $node2 = $this->node->get('demo4'); // Doesn't exist for this node.
227 $node3 = $this->node->get('demo0'); // Doesn't exist at all.
228 $node4 = $this->node->get(false); // Sometimes occurs in nature code.
229 $this->assertInstanceOf('navigation_node', $node1);
230 $this->assertFalse($node2);
231 $this->assertFalse($node3);
232 $this->assertFalse($node4);
235 public function test_node_get_css_type() {
238 $csstype1 = $this->node->get('demo3')->get_css_type();
239 $csstype2 = $this->node->get('demo3')->get('demo5')->get_css_type();
240 $this->node->get('demo3')->get('demo5')->type = 1000;
241 $csstype3 = $this->node->get('demo3')->get('demo5')->get_css_type();
242 $this->assertSame('type_category', $csstype1);
243 $this->assertSame('type_course', $csstype2);
244 $this->assertSame('type_unknown', $csstype3);
247 public function test_node_make_active() {
251 $node1 = $this->node->add('active node 1', null, navigation_node::TYPE_CUSTOM, null, 'anode1');
252 $node2 = $this->node->add('active node 2', new moodle_url($CFG->wwwroot), navigation_node::TYPE_COURSE, null, 'anode2');
253 $node1->make_active();
254 $this->node->get('anode2')->make_active();
255 $this->assertTrue($node1->isactive);
256 $this->assertTrue($this->node->get('anode2')->isactive);
259 public function test_node_remove() {
262 $remove1 = $this->node->add('child to remove 1', null, navigation_node::TYPE_CUSTOM, null, 'remove1');
263 $remove2 = $this->node->add('child to remove 2', null, navigation_node::TYPE_CUSTOM, null, 'remove2');
264 $remove3 = $remove2->add('child to remove 3', null, navigation_node::TYPE_CUSTOM, null, 'remove3');
266 $this->assertInstanceOf('navigation_node', $remove1);
267 $this->assertInstanceOf('navigation_node', $remove2);
268 $this->assertInstanceOf('navigation_node', $remove3);
270 $this->assertInstanceOf('navigation_node', $this->node->get('remove1'));
271 $this->assertInstanceOf('navigation_node', $this->node->get('remove2'));
272 $this->assertInstanceOf('navigation_node', $remove2->get('remove3'));
274 // Remove element and make sure this is no longer a child.
275 $this->assertTrue($remove1->remove());
276 $this->assertFalse($this->node->get('remove1'));
277 $this->assertFalse(in_array('remove1', $this->node->get_children_key_list(), true));
279 // Make sure that we can insert element after removal.
280 $insertelement = navigation_node::create('extra element 4', null, navigation_node::TYPE_CUSTOM, null, 'element4');
281 $this->node->add_node($insertelement, 'remove2');
282 $this->assertNotEmpty($this->node->get('element4'));
284 // Remove more elements.
285 $this->assertTrue($this->node->get('remove2')->remove());
286 $this->assertFalse($this->node->get('remove2'));
288 // Make sure that we can add element after removal.
289 $this->node->add('extra element 5', null, navigation_node::TYPE_CUSTOM, null, 'element5');
290 $this->assertNotEmpty($this->node->get('element5'));
292 $this->assertTrue($remove2->get('remove3')->remove());
294 $this->assertFalse($this->node->get('remove1'));
295 $this->assertFalse($this->node->get('remove2'));
298 public function test_node_remove_class() {
301 $this->node->add_class('testclass');
302 $this->assertTrue($this->node->remove_class('testclass'));
303 $this->assertNotContains('testclass', $this->node->classes);
306 public function test_module_extends_navigation() {
307 $node = new exposed_global_navigation();
308 // Create an initial tree structure to work with.
309 $cat1 = $node->add('category 1', null, navigation_node::TYPE_CATEGORY, null, 'cat1');
310 $cat2 = $node->add('category 2', null, navigation_node::TYPE_CATEGORY, null, 'cat2');
311 $cat3 = $node->add('category 3', null, navigation_node::TYPE_CATEGORY, null, 'cat3');
312 $sub1 = $cat2->add('sub category 1', null, navigation_node::TYPE_CATEGORY, null, 'sub1');
313 $sub2 = $cat2->add('sub category 2', null, navigation_node::TYPE_CATEGORY, null, 'sub2');
314 $sub3 = $cat2->add('sub category 3', null, navigation_node::TYPE_CATEGORY, null, 'sub3');
315 $course1 = $sub2->add('course 1', null, navigation_node::TYPE_COURSE, null, 'course1');
316 $course2 = $sub2->add('course 2', null, navigation_node::TYPE_COURSE, null, 'course2');
317 $course3 = $sub2->add('course 3', null, navigation_node::TYPE_COURSE, null, 'course3');
318 $section1 = $course2->add('section 1', null, navigation_node::TYPE_SECTION, null, 'sec1');
319 $section2 = $course2->add('section 2', null, navigation_node::TYPE_SECTION, null, 'sec2');
320 $section3 = $course2->add('section 3', null, navigation_node::TYPE_SECTION, null, 'sec3');
321 $act1 = $section2->add('activity 1', null, navigation_node::TYPE_ACTIVITY, null, 'act1');
322 $act2 = $section2->add('activity 2', null, navigation_node::TYPE_ACTIVITY, null, 'act2');
323 $act3 = $section2->add('activity 3', null, navigation_node::TYPE_ACTIVITY, null, 'act3');
324 $res1 = $section2->add('resource 1', null, navigation_node::TYPE_RESOURCE, null, 'res1');
325 $res2 = $section2->add('resource 2', null, navigation_node::TYPE_RESOURCE, null, 'res2');
326 $res3 = $section2->add('resource 3', null, navigation_node::TYPE_RESOURCE, null, 'res3');
328 $this->assertTrue($node->exposed_module_extends_navigation('data'));
329 $this->assertFalse($node->exposed_module_extends_navigation('test1'));
332 public function test_navbar_add() {
334 $this->resetAfterTest(false);
337 $PAGE->set_course($SITE);
339 $tempnode = new exposed_global_navigation();
340 // Create an initial tree structure to work with.
341 $cat1 = $tempnode->add('category 1', null, navigation_node::TYPE_CATEGORY, null, 'cat1');
342 $cat2 = $tempnode->add('category 2', null, navigation_node::TYPE_CATEGORY, null, 'cat2');
343 $cat3 = $tempnode->add('category 3', null, navigation_node::TYPE_CATEGORY, null, 'cat3');
344 $sub1 = $cat2->add('sub category 1', null, navigation_node::TYPE_CATEGORY, null, 'sub1');
345 $sub2 = $cat2->add('sub category 2', null, navigation_node::TYPE_CATEGORY, null, 'sub2');
346 $sub3 = $cat2->add('sub category 3', null, navigation_node::TYPE_CATEGORY, null, 'sub3');
347 $course1 = $sub2->add('course 1', null, navigation_node::TYPE_COURSE, null, 'course1');
348 $course2 = $sub2->add('course 2', null, navigation_node::TYPE_COURSE, null, 'course2');
349 $course3 = $sub2->add('course 3', null, navigation_node::TYPE_COURSE, null, 'course3');
350 $section1 = $course2->add('section 1', null, navigation_node::TYPE_SECTION, null, 'sec1');
351 $section2 = $course2->add('section 2', null, navigation_node::TYPE_SECTION, null, 'sec2');
352 $section3 = $course2->add('section 3', null, navigation_node::TYPE_SECTION, null, 'sec3');
353 $act1 = $section2->add('activity 1', null, navigation_node::TYPE_ACTIVITY, null, 'act1');
354 $act2 = $section2->add('activity 2', null, navigation_node::TYPE_ACTIVITY, null, 'act2');
355 $act3 = $section2->add('activity 3', null, navigation_node::TYPE_ACTIVITY, null, 'act3');
356 $res1 = $section2->add('resource 1', null, navigation_node::TYPE_RESOURCE, null, 'res1');
357 $res2 = $section2->add('resource 2', null, navigation_node::TYPE_RESOURCE, null, 'res2');
358 $res3 = $section2->add('resource 3', null, navigation_node::TYPE_RESOURCE, null, 'res3');
359 $tempnode->find('course2', navigation_node::TYPE_COURSE)->make_active();
361 $page = new navigation_exposed_moodle_page();
362 $page->set_url($PAGE->url);
363 $page->set_context($PAGE->context);
365 $navigation = new exposed_global_navigation($page);
366 $navigation->children = $tempnode->children;
367 $navigation->set_initialised();
368 $page->set_navigation($navigation);
370 $cache = new navigation_cache('unittest_nav');
371 $node = new exposed_navbar($page);
373 // Add a node with all args set.
374 $node->add('test_add_1', 'http://www.moodle.org/', navigation_node::TYPE_COURSE, 'testadd1', 'testadd1', new pix_icon('i/course', ''));
375 // Add a node with the minimum args required.
376 $node->add('test_add_2', 'http://www.moodle.org/', navigation_node::TYPE_COURSE, 'testadd2', 'testadd2', new pix_icon('i/course', ''));
377 $this->assertInstanceOf('navigation_node', $node->get('testadd1'));
378 $this->assertInstanceOf('navigation_node', $node->get('testadd2'));
384 * @depends test_navbar_add
387 public function test_navbar_has_items($node) {
388 $this->resetAfterTest();
390 $this->assertTrue($node->has_items());
393 public function test_cache__get() {
394 $cache = new navigation_cache('unittest_nav');
395 $cache->anysetvariable = true;
397 $this->assertTrue($cache->anysetvariable);
398 $this->assertEquals($cache->notasetvariable, null);
401 public function test_cache__set() {
402 $cache = new navigation_cache('unittest_nav');
403 $cache->anysetvariable = true;
405 $cache->myname = 'Sam Hemelryk';
406 $this->assertTrue($cache->cached('myname'));
407 $this->assertSame('Sam Hemelryk', $cache->myname);
410 public function test_cache_cached() {
411 $cache = new navigation_cache('unittest_nav');
412 $cache->anysetvariable = true;
414 $this->assertTrue($cache->cached('anysetvariable'));
415 $this->assertFalse($cache->cached('notasetvariable'));
418 public function test_cache_clear() {
419 $cache = new navigation_cache('unittest_nav');
420 $cache->anysetvariable = true;
422 $cache = clone($cache);
423 $this->assertTrue($cache->cached('anysetvariable'));
425 $this->assertFalse($cache->cached('anysetvariable'));
428 public function test_cache_set() {
429 $cache = new navigation_cache('unittest_nav');
430 $cache->anysetvariable = true;
432 $cache->set('software', 'Moodle');
433 $this->assertTrue($cache->cached('software'));
434 $this->assertEquals($cache->software, 'Moodle');
437 public function test_setting___construct() {
440 $this->resetAfterTest(false);
443 $PAGE->set_course($SITE);
445 $node = new exposed_settings_navigation();
451 * @depends test_setting___construct
455 public function test_setting__initialise($node) {
456 $this->resetAfterTest(false);
459 $this->assertEquals($node->id, 'settingsnav');
465 * @depends test_setting__initialise
469 public function test_setting_in_alternative_role($node) {
470 $this->resetAfterTest();
472 $this->assertFalse($node->exposed_in_alternative_role());
478 * This is a dummy object that allows us to call protected methods within the
479 * global navigation class by prefixing the methods with `exposed_`
481 class exposed_global_navigation extends global_navigation {
482 protected $exposedkey = 'exposed_';
483 public function __construct(moodle_page $page=null) {
485 if ($page === null) {
488 parent::__construct($page);
489 $this->cache = new navigation_cache('unittest_nav');
491 public function __call($method, $arguments) {
492 if (strpos($method, $this->exposedkey) !== false) {
493 $method = substr($method, strlen($this->exposedkey));
495 if (method_exists($this, $method)) {
496 return call_user_func_array(array($this, $method), $arguments);
498 throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
500 public function set_initialised() {
501 $this->initialised = true;
506 class mock_initialise_global_navigation extends global_navigation {
508 protected static $count = 1;
510 public function load_for_category() {
511 $this->add('load_for_category', null, null, null, 'initcall'.self::$count);
516 public function load_for_course() {
517 $this->add('load_for_course', null, null, null, 'initcall'.self::$count);
522 public function load_for_activity() {
523 $this->add('load_for_activity', null, null, null, 'initcall'.self::$count);
528 public function load_for_user($user=null, $forceforcontext=false) {
529 $this->add('load_for_user', null, null, null, 'initcall'.self::$count);
536 * This is a dummy object that allows us to call protected methods within the
537 * global navigation class by prefixing the methods with `exposed_`.
539 class exposed_navbar extends navbar {
540 protected $exposedkey = 'exposed_';
541 public function __construct(moodle_page $page) {
542 parent::__construct($page);
543 $this->cache = new navigation_cache('unittest_nav');
545 public function __call($method, $arguments) {
546 if (strpos($method, $this->exposedkey) !== false) {
547 $method = substr($method, strlen($this->exposedkey));
549 if (method_exists($this, $method)) {
550 return call_user_func_array(array($this, $method), $arguments);
552 throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
556 class navigation_exposed_moodle_page extends moodle_page {
557 public function set_navigation(navigation_node $node) {
558 $this->_navigation = $node;
563 * This is a dummy object that allows us to call protected methods within the
564 * global navigation class by prefixing the methods with `exposed_`.
566 class exposed_settings_navigation extends settings_navigation {
567 protected $exposedkey = 'exposed_';
568 public function __construct() {
570 parent::__construct($PAGE);
571 $this->cache = new navigation_cache('unittest_nav');
573 public function __call($method, $arguments) {
574 if (strpos($method, $this->exposedkey) !== false) {
575 $method = substr($method, strlen($this->exposedkey));
577 if (method_exists($this, $method)) {
578 return call_user_func_array(array($this, $method), $arguments);
580 throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);