eab6b13bf59913b8f550f1ced2bc1a309aaf40e5
[moodle.git] / lib / tests / navigationlib_test.php
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/>.
17 /**
18  * Unit tests for lib/navigationlib.php
19  *
20  * @package   core
21  * @category  phpunit
22  * @copyright 2009 Sam Hemelryk
23  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later (5)
24  */
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->libdir . '/navigationlib.php');
32 class core_navigationlib_testcase extends advanced_testcase {
33     /**
34      * @var navigation_node
35      */
36     public $node;
38     protected function setup_node() {
39         global $PAGE, $SITE;
41         $PAGE->set_url('/');
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;
63     }
65     public function test_node__construct() {
66         $this->setup_node();
68         $fakeproperties = array(
69             'text' => 'text',
70             'shorttext' => 'A very silly extra long short text string, more than 25 characters',
71             'key' => 'key',
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);
81     }
83     public function test_node_add() {
84         $this->setup_node();
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);
107     }
109     public function test_node_add_before() {
110         $this->setup_node();
112         // Create 3 nodes.
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;
126         }
127         $this->assertSame('testadd1', $keys[count($keys)-3]);
128         $this->assertSame('testadd2', $keys[count($keys)-2]);
129         $this->assertSame('testadd3', $keys[count($keys)-1]);
130     }
132     public function test_node_add_class() {
133         $this->setup_node();
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);
141         }
142     }
144     public function test_node_check_if_active() {
145         $this->setup_node();
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());
152         }
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());
158         }
159     }
161     public function test_node_contains_active_node() {
162         $this->setup_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());
169         // Obviously duff.
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());
175         // Obviously duff.
176         $this->assertFalse($this->node->get('demo3')->get('demo4')->contains_active_node());
177     }
179     public function test_node_find_active_node() {
180         $this->setup_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);
188         }
190         $this->assertNotInstanceOf('navigation_node', $activenode2);
191     }
193     public function test_node_find() {
194         $this->setup_node();
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);
204     }
206     public function test_node_find_expandable() {
207         $this->setup_node();
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);
219         }
220     }
222     public function test_node_get() {
223         $this->setup_node();
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);
233     }
235     public function test_node_get_css_type() {
236         $this->setup_node();
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);
245     }
247     public function test_node_make_active() {
248         global $CFG;
249         $this->setup_node();
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);
257     }
259     public function test_node_remove() {
260         $this->setup_node();
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'));
296     }
298     public function test_node_remove_class() {
299         $this->setup_node();
301         $this->node->add_class('testclass');
302         $this->assertTrue($this->node->remove_class('testclass'));
303         $this->assertNotContains('testclass', $this->node->classes);
304     }
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'));
330     }
332     public function test_navbar_prepend_and_add() {
333         global $PAGE;
334         // Unfortunate hack needed because people use global $PAGE around the place.
335         $PAGE->set_url('/');
337         // We need to reset after this test because we using the generator.
338         $this->resetAfterTest();
340         $generator = self::getDataGenerator();
341         $cat1 = $generator->create_category();
342         $cat2 = $generator->create_category(array('parent' => $cat1->id));
343         $course = $generator->create_course(array('category' => $cat2->id));
345         $page = new moodle_page();
346         $page->set_course($course);
347         $page->set_url(new moodle_url('/course/view.php', array('id' => $course->id)));
348         $page->navbar->prepend('test 1');
349         $page->navbar->prepend('test 2');
350         $page->navbar->add('test 3');
351         $page->navbar->add('test 4');
353         $items = $page->navbar->get_items();
354         foreach ($items as $item) {
355             $this->assertInstanceOf('navigation_node', $item);
356         }
358         $i = 0;
359         $this->assertSame('test 1', $items[$i++]->text);
360         $this->assertSame('test 2', $items[$i++]->text);
361         $this->assertSame('home', $items[$i++]->key);
362         $this->assertSame('courses', $items[$i++]->key);
363         $this->assertSame($cat1->id, $items[$i++]->key);
364         $this->assertSame($cat2->id, $items[$i++]->key);
365         $this->assertSame($course->id, $items[$i++]->key);
366         $this->assertSame('test 3', $items[$i++]->text);
367         $this->assertSame('test 4', $items[$i++]->text);
369         return $page;
370     }
372     /**
373      * @depends test_navbar_prepend_and_add
374      * @param $node
375      */
376     public function test_navbar_has_items(moodle_page $page) {
377         $this->resetAfterTest();
379         $this->assertTrue($page->navbar->has_items());
380     }
382     public function test_cache__get() {
383         $cache = new navigation_cache('unittest_nav');
384         $cache->anysetvariable = true;
386         $this->assertTrue($cache->anysetvariable);
387         $this->assertEquals($cache->notasetvariable, null);
388     }
390     public function test_cache__set() {
391         $cache = new navigation_cache('unittest_nav');
392         $cache->anysetvariable = true;
394         $cache->myname = 'Sam Hemelryk';
395         $this->assertTrue($cache->cached('myname'));
396         $this->assertSame('Sam Hemelryk', $cache->myname);
397     }
399     public function test_cache_cached() {
400         $cache = new navigation_cache('unittest_nav');
401         $cache->anysetvariable = true;
403         $this->assertTrue($cache->cached('anysetvariable'));
404         $this->assertFalse($cache->cached('notasetvariable'));
405     }
407     public function test_cache_clear() {
408         $cache = new navigation_cache('unittest_nav');
409         $cache->anysetvariable = true;
411         $cache = clone($cache);
412         $this->assertTrue($cache->cached('anysetvariable'));
413         $cache->clear();
414         $this->assertFalse($cache->cached('anysetvariable'));
415     }
417     public function test_cache_set() {
418         $cache = new navigation_cache('unittest_nav');
419         $cache->anysetvariable = true;
421         $cache->set('software', 'Moodle');
422         $this->assertTrue($cache->cached('software'));
423         $this->assertEquals($cache->software, 'Moodle');
424     }
426     public function test_setting___construct() {
427         global $PAGE, $SITE;
429         $this->resetAfterTest(false);
431         $PAGE->set_url('/');
432         $PAGE->set_course($SITE);
434         $node = new exposed_settings_navigation();
436         return $node;
437     }
439     /**
440      * @depends test_setting___construct
441      * @param mixed $node
442      * @return mixed
443      */
444     public function test_setting__initialise($node) {
445         $this->resetAfterTest(false);
447         $node->initialise();
448         $this->assertEquals($node->id, 'settingsnav');
450         return $node;
451     }
453     /**
454      * @depends test_setting__initialise
455      * @param mixed $node
456      * @return mixed
457      */
458     public function test_setting_in_alternative_role($node) {
459         $this->resetAfterTest();
461         $this->assertFalse($node->exposed_in_alternative_role());
462     }
466 /**
467  * This is a dummy object that allows us to call protected methods within the
468  * global navigation class by prefixing the methods with `exposed_`
469  */
470 class exposed_global_navigation extends global_navigation {
471     protected $exposedkey = 'exposed_';
472     public function __construct(moodle_page $page=null) {
473         global $PAGE;
474         if ($page === null) {
475             $page = $PAGE;
476         }
477         parent::__construct($page);
478         $this->cache = new navigation_cache('unittest_nav');
479     }
480     public function __call($method, $arguments) {
481         if (strpos($method, $this->exposedkey) !== false) {
482             $method = substr($method, strlen($this->exposedkey));
483         }
484         if (method_exists($this, $method)) {
485             return call_user_func_array(array($this, $method), $arguments);
486         }
487         throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
488     }
489     public function set_initialised() {
490         $this->initialised = true;
491     }
495 class mock_initialise_global_navigation extends global_navigation {
497     protected static $count = 1;
499     public function load_for_category() {
500         $this->add('load_for_category', null, null, null, 'initcall'.self::$count);
501         self::$count++;
502         return 0;
503     }
505     public function load_for_course() {
506         $this->add('load_for_course', null, null, null, 'initcall'.self::$count);
507         self::$count++;
508         return 0;
509     }
511     public function load_for_activity() {
512         $this->add('load_for_activity', null, null, null, 'initcall'.self::$count);
513         self::$count++;
514         return 0;
515     }
517     public function load_for_user($user=null, $forceforcontext=false) {
518         $this->add('load_for_user', null, null, null, 'initcall'.self::$count);
519         self::$count++;
520         return 0;
521     }
524 /**
525  * This is a dummy object that allows us to call protected methods within the
526  * global navigation class by prefixing the methods with `exposed_`.
527  */
528 class exposed_navbar extends navbar {
529     protected $exposedkey = 'exposed_';
530     public function __construct(moodle_page $page) {
531         parent::__construct($page);
532         $this->cache = new navigation_cache('unittest_nav');
533     }
534     public function __call($method, $arguments) {
535         if (strpos($method, $this->exposedkey) !== false) {
536             $method = substr($method, strlen($this->exposedkey));
537         }
538         if (method_exists($this, $method)) {
539             return call_user_func_array(array($this, $method), $arguments);
540         }
541         throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
542     }
545 class navigation_exposed_moodle_page extends moodle_page {
546     public function set_navigation(navigation_node $node) {
547         $this->_navigation = $node;
548     }
551 /**
552  * This is a dummy object that allows us to call protected methods within the
553  * global navigation class by prefixing the methods with `exposed_`.
554  */
555 class exposed_settings_navigation extends settings_navigation {
556     protected $exposedkey = 'exposed_';
557     public function __construct() {
558         global $PAGE;
559         parent::__construct($PAGE);
560         $this->cache = new navigation_cache('unittest_nav');
561     }
562     public function __call($method, $arguments) {
563         if (strpos($method, $this->exposedkey) !== false) {
564             $method = substr($method, strlen($this->exposedkey));
565         }
566         if (method_exists($this, $method)) {
567             return call_user_func_array(array($this, $method), $arguments);
568         }
569         throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
570     }