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 navigation_node_testcase
extends basic_testcase
{
34 protected $fakeproperties = array(
36 'shorttext' => 'A very silly extra long short text string, more than 25 characters',
38 'type' => 'navigation_node::TYPE_COURSE',
39 'action' => 'http://www.moodle.org/');
40 protected $activeurl = null;
41 protected $inactivenode = null;
44 * @var navigation_node
48 protected function setUp() {
49 global $CFG, $PAGE, $SITE;
53 $PAGE->set_course($SITE);
55 $this->activeurl
= $PAGE->url
;
56 navigation_node
::override_active_url($this->activeurl
);
58 $this->inactiveurl
= new moodle_url('http://www.moodle.com/');
59 $this->fakeproperties
['action'] = $this->inactiveurl
;
61 $this->node
= new navigation_node('Test Node');
62 $this->node
->type
= navigation_node
::TYPE_SYSTEM
;
63 // We add the first child without key. This way we make sure all keys search by comparision is performed using ===
64 $this->node
->add('first child without key', null, navigation_node
::TYPE_CUSTOM
);
65 $demo1 = $this->node
->add('demo1', $this->inactiveurl
, navigation_node
::TYPE_COURSE
, null, 'demo1', new pix_icon('i/course', ''));
66 $demo2 = $this->node
->add('demo2', $this->inactiveurl
, navigation_node
::TYPE_COURSE
, null, 'demo2', new pix_icon('i/course', ''));
67 $demo3 = $this->node
->add('demo3', $this->inactiveurl
, navigation_node
::TYPE_CATEGORY
, null, 'demo3',new pix_icon('i/course', ''));
68 $demo4 = $demo3->add('demo4', $this->inactiveurl
,navigation_node
::TYPE_COURSE
, null, 'demo4', new pix_icon('i/course', ''));
69 $demo5 = $demo3->add('demo5', $this->activeurl
, navigation_node
::TYPE_COURSE
, null, 'demo5',new pix_icon('i/course', ''));
70 $demo5->add('activity1', null, navigation_node
::TYPE_ACTIVITY
, null, 'activity1')->make_active();
71 $hiddendemo1 = $this->node
->add('hiddendemo1', $this->inactiveurl
, navigation_node
::TYPE_CATEGORY
, null, 'hiddendemo1', new pix_icon('i/course', ''));
72 $hiddendemo1->hidden
= true;
73 $hiddendemo1->add('hiddendemo2', $this->inactiveurl
, navigation_node
::TYPE_COURSE
, null, 'hiddendemo2', new pix_icon('i/course', ''))->helpbutton
= 'Here is a help button';;
74 $hiddendemo1->add('hiddendemo3', $this->inactiveurl
, navigation_node
::TYPE_COURSE
,null, 'hiddendemo3', new pix_icon('i/course', ''))->display
= false;
77 public function test___construct() {
79 $node = new navigation_node($this->fakeproperties
);
80 $this->assertEquals($node->text
, $this->fakeproperties
['text']);
81 $this->assertTrue(strpos($this->fakeproperties
['shorttext'], substr($node->shorttext
,0, -3))===0);
82 $this->assertEquals($node->key
, $this->fakeproperties
['key']);
83 $this->assertEquals($node->type
, $this->fakeproperties
['type']);
84 $this->assertEquals($node->action
, $this->fakeproperties
['action']);
86 public function test_add() {
88 // Add a node with all args set
89 $node1 = $this->node
->add('test_add_1','http://www.moodle.org/',navigation_node
::TYPE_COURSE
,'testadd1','key',new pix_icon('i/course', ''));
90 // Add a node with the minimum args required
91 $node2 = $this->node
->add('test_add_2',null, navigation_node
::TYPE_CUSTOM
,'testadd2');
92 $node3 = $this->node
->add(str_repeat('moodle ', 15),str_repeat('moodle', 15));
94 $this->assertInstanceOf('navigation_node', $node1);
95 $this->assertInstanceOf('navigation_node', $node2);
96 $this->assertInstanceOf('navigation_node', $node3);
98 $ref = $this->node
->get('key');
99 $this->assertSame($node1, $ref);
101 $ref = $this->node
->get($node2->key
);
102 $this->assertSame($node2, $ref);
104 $ref = $this->node
->get($node2->key
, $node2->type
);
105 $this->assertSame($node2, $ref);
107 $ref = $this->node
->get($node3->key
, $node3->type
);
108 $this->assertSame($node3, $ref);
111 public function test_add_before() {
114 $node1 = navigation_node
::create('test_add_1', null, navigation_node
::TYPE_CUSTOM
,
115 'test 1', 'testadd1');
116 $node2 = navigation_node
::create('test_add_2', null, navigation_node
::TYPE_CUSTOM
,
117 'test 2', 'testadd2');
118 $node3 = navigation_node
::create('test_add_3', null, navigation_node
::TYPE_CUSTOM
,
119 'test 3', 'testadd3');
120 // Add node 2, then node 1 before 2, then node 3 at end
121 $this->node
->add_node($node2);
122 $this->node
->add_node($node1, 'testadd2');
123 $this->node
->add_node($node3);
124 // Check the last 3 nodes are in 1, 2, 3 order and have those indexes
125 foreach($this->node
->children
as $child) {
126 $keys[] = $child->key
;
128 $this->assertEquals('testadd1', $keys[count($keys)-3]);
129 $this->assertEquals('testadd2', $keys[count($keys)-2]);
130 $this->assertEquals('testadd3', $keys[count($keys)-1]);
133 public function test_add_class() {
134 $node = $this->node
->get('demo1');
135 $this->assertInstanceOf('navigation_node', $node);
136 if ($node !== false) {
137 $node->add_class('myclass');
138 $classes = $node->classes
;
139 $this->assertTrue(in_array('myclass', $classes));
144 public function test_check_if_active() {
145 // First test the string urls
146 // demo1 -> action is http://www.moodle.org/, thus should be true
147 $demo5 = $this->node
->find('demo5', navigation_node
::TYPE_COURSE
);
148 if ($this->assertInstanceOf('navigation_node', $demo5)) {
149 $this->assertTrue($demo5->check_if_active());
152 // demo2 -> action is http://www.moodle.com/, thus should be false
153 $demo2 = $this->node
->get('demo2');
154 if ($this->assertInstanceOf('navigation_node', $demo2)) {
155 $this->assertFalse($demo2->check_if_active());
159 public function test_contains_active_node() {
160 // demo5, and activity1 were set to active during setup
161 // Should be true as it contains all nodes
162 $this->assertTrue($this->node
->contains_active_node());
163 // Should be true as demo5 is a child of demo3
164 $this->assertTrue($this->node
->get('demo3')->contains_active_node());
166 $this->assertFalse($this->node
->get('demo1')->contains_active_node());
167 // Should be true as demo5 contains activity1
168 $this->assertTrue($this->node
->get('demo3')->get('demo5')->contains_active_node());
169 // Should be true activity1 is the active node
170 $this->assertTrue($this->node
->get('demo3')->get('demo5')->get('activity1')->contains_active_node());
172 $this->assertFalse($this->node
->get('demo3')->get('demo4')->contains_active_node());
175 public function test_find_active_node() {
176 $activenode1 = $this->node
->find_active_node();
177 $activenode2 = $this->node
->get('demo1')->find_active_node();
179 if ($this->assertInstanceOf('navigation_node', $activenode1)) {
180 $ref = $this->node
->get('demo3')->get('demo5')->get('activity1');
181 $this->assertSame($activenode1, $ref);
184 $this->assertNotInstanceOf('navigation_node', $activenode2);
187 public function test_find() {
188 $node1 = $this->node
->find('demo1', navigation_node
::TYPE_COURSE
);
189 $node2 = $this->node
->find('demo5', navigation_node
::TYPE_COURSE
);
190 $node3 = $this->node
->find('demo5', navigation_node
::TYPE_CATEGORY
);
191 $node4 = $this->node
->find('demo0', navigation_node
::TYPE_COURSE
);
192 $this->assertInstanceOf('navigation_node', $node1);
193 $this->assertInstanceOf('navigation_node', $node2);
194 $this->assertNotInstanceOf('navigation_node', $node3);
195 $this->assertNotInstanceOf('navigation_node', $node4);
198 public function test_find_expandable() {
199 $expandable = array();
200 $this->node
->find_expandable($expandable);
201 //TODO: find out what is wrong here - it was returning 4 before the conversion
202 //$this->assertEquals(count($expandable), 4);
203 $this->assertEquals(count($expandable), 0);
204 if (count($expandable) === 4) {
205 $name = $expandable[0]['key'];
206 $name .= $expandable[1]['key'];
207 $name .= $expandable[2]['key'];
208 $name .= $expandable[3]['key'];
209 $this->assertEquals($name, 'demo1demo2demo4hiddendemo2');
213 public function test_get() {
214 $node1 = $this->node
->get('demo1'); // Exists
215 $node2 = $this->node
->get('demo4'); // Doesn't exist for this node
216 $node3 = $this->node
->get('demo0'); // Doesn't exist at all
217 $node4 = $this->node
->get(false); // Sometimes occurs in nature code
218 $this->assertInstanceOf('navigation_node', $node1);
219 $this->assertFalse($node2);
220 $this->assertFalse($node3);
221 $this->assertFalse($node4);
224 public function test_get_css_type() {
225 $csstype1 = $this->node
->get('demo3')->get_css_type();
226 $csstype2 = $this->node
->get('demo3')->get('demo5')->get_css_type();
227 $this->node
->get('demo3')->get('demo5')->type
= 1000;
228 $csstype3 = $this->node
->get('demo3')->get('demo5')->get_css_type();
229 $this->assertEquals($csstype1, 'type_category');
230 $this->assertEquals($csstype2, 'type_course');
231 $this->assertEquals($csstype3, 'type_unknown');
234 public function test_make_active() {
236 $node1 = $this->node
->add('active node 1', null, navigation_node
::TYPE_CUSTOM
, null, 'anode1');
237 $node2 = $this->node
->add('active node 2', new moodle_url($CFG->wwwroot
), navigation_node
::TYPE_COURSE
, null, 'anode2');
238 $node1->make_active();
239 $this->node
->get('anode2')->make_active();
240 $this->assertTrue($node1->isactive
);
241 $this->assertTrue($this->node
->get('anode2')->isactive
);
243 public function test_remove() {
244 $remove1 = $this->node
->add('child to remove 1', null, navigation_node
::TYPE_CUSTOM
, null, 'remove1');
245 $remove2 = $this->node
->add('child to remove 2', null, navigation_node
::TYPE_CUSTOM
, null, 'remove2');
246 $remove3 = $remove2->add('child to remove 3', null, navigation_node
::TYPE_CUSTOM
, null, 'remove3');
248 $this->assertInstanceOf('navigation_node', $remove1);
249 $this->assertInstanceOf('navigation_node', $remove2);
250 $this->assertInstanceOf('navigation_node', $remove3);
252 $this->assertInstanceOf('navigation_node', $this->node
->get('remove1'));
253 $this->assertInstanceOf('navigation_node', $this->node
->get('remove2'));
254 $this->assertInstanceOf('navigation_node', $remove2->get('remove3'));
256 // Remove element and make sure this is no longer a child.
257 $this->assertTrue($remove1->remove());
258 $this->assertFalse($this->node
->get('remove1'));
259 $this->assertFalse(in_array('remove1', $this->node
->get_children_key_list(), true));
261 // Remove more elements
262 $this->assertTrue($this->node
->get('remove2')->remove());
263 $this->assertFalse($this->node
->get('remove2'));
264 $this->assertTrue($remove2->get('remove3')->remove());
266 $this->assertFalse($this->node
->get('remove1'));
267 $this->assertFalse($this->node
->get('remove2'));
269 public function test_remove_class() {
270 $this->node
->add_class('testclass');
271 $this->assertTrue($this->node
->remove_class('testclass'));
272 $this->assertFalse(in_array('testclass', $this->node
->classes
));
277 * This is a dummy object that allows us to call protected methods within the
278 * global navigation class by prefixing the methods with `exposed_`
280 class exposed_global_navigation
extends global_navigation
{
281 protected $exposedkey = 'exposed_';
282 public function __construct(moodle_page
$page=null) {
284 if ($page === null) {
287 parent
::__construct($page);
288 $this->cache
= new navigation_cache('unittest_nav');
290 public function __call($method, $arguments) {
291 if (strpos($method,$this->exposedkey
) !== false) {
292 $method = substr($method, strlen($this->exposedkey
));
294 if (method_exists($this, $method)) {
295 return call_user_func_array(array($this, $method), $arguments);
297 throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER
);
299 public function set_initialised() {
300 $this->initialised
= true;
304 class mock_initialise_global_navigation
extends global_navigation
{
308 public function load_for_category() {
309 $this->add('load_for_category', null, null, null, 'initcall'.self
::$count);
314 public function load_for_course() {
315 $this->add('load_for_course', null, null, null, 'initcall'.self
::$count);
320 public function load_for_activity() {
321 $this->add('load_for_activity', null, null, null, 'initcall'.self
::$count);
326 public function load_for_user($user=null, $forceforcontext=false) {
327 $this->add('load_for_user', null, null, null, 'initcall'.self
::$count);
333 class global_navigation_testcase
extends basic_testcase
{
335 * @var global_navigation
339 protected function setUp() {
342 $this->node
= new exposed_global_navigation();
343 // Create an initial tree structure to work with
344 $cat1 = $this->node
->add('category 1', null, navigation_node
::TYPE_CATEGORY
, null, 'cat1');
345 $cat2 = $this->node
->add('category 2', null, navigation_node
::TYPE_CATEGORY
, null, 'cat2');
346 $cat3 = $this->node
->add('category 3', null, navigation_node
::TYPE_CATEGORY
, null, 'cat3');
347 $sub1 = $cat2->add('sub category 1', null, navigation_node
::TYPE_CATEGORY
, null, 'sub1');
348 $sub2 = $cat2->add('sub category 2', null, navigation_node
::TYPE_CATEGORY
, null, 'sub2');
349 $sub3 = $cat2->add('sub category 3', null, navigation_node
::TYPE_CATEGORY
, null, 'sub3');
350 $course1 = $sub2->add('course 1', null, navigation_node
::TYPE_COURSE
, null, 'course1');
351 $course2 = $sub2->add('course 2', null, navigation_node
::TYPE_COURSE
, null, 'course2');
352 $course3 = $sub2->add('course 3', null, navigation_node
::TYPE_COURSE
, null, 'course3');
353 $section1 = $course2->add('section 1', null, navigation_node
::TYPE_SECTION
, null, 'sec1');
354 $section2 = $course2->add('section 2', null, navigation_node
::TYPE_SECTION
, null, 'sec2');
355 $section3 = $course2->add('section 3', null, navigation_node
::TYPE_SECTION
, null, 'sec3');
356 $act1 = $section2->add('activity 1', null, navigation_node
::TYPE_ACTIVITY
, null, 'act1');
357 $act2 = $section2->add('activity 2', null, navigation_node
::TYPE_ACTIVITY
, null, 'act2');
358 $act3 = $section2->add('activity 3', null, navigation_node
::TYPE_ACTIVITY
, null, 'act3');
359 $res1 = $section2->add('resource 1', null, navigation_node
::TYPE_RESOURCE
, null, 'res1');
360 $res2 = $section2->add('resource 2', null, navigation_node
::TYPE_RESOURCE
, null, 'res2');
361 $res3 = $section2->add('resource 3', null, navigation_node
::TYPE_RESOURCE
, null, 'res3');
364 public function test_format_display_course_content() {
365 $this->assertTrue($this->node
->exposed_format_display_course_content('topic'));
366 $this->assertFalse($this->node
->exposed_format_display_course_content('scorm'));
367 $this->assertTrue($this->node
->exposed_format_display_course_content('dummy'));
369 public function test_module_extends_navigation() {
370 $this->assertTrue($this->node
->exposed_module_extends_navigation('data'));
371 $this->assertFalse($this->node
->exposed_module_extends_navigation('test1'));
376 * This is a dummy object that allows us to call protected methods within the
377 * global navigation class by prefixing the methods with `exposed_`
379 class exposed_navbar
extends navbar
{
380 protected $exposedkey = 'exposed_';
381 public function __construct(moodle_page
$page) {
382 parent
::__construct($page);
383 $this->cache
= new navigation_cache('unittest_nav');
385 function __call($method, $arguments) {
386 if (strpos($method,$this->exposedkey
) !== false) {
387 $method = substr($method, strlen($this->exposedkey
));
389 if (method_exists($this, $method)) {
390 return call_user_func_array(array($this, $method), $arguments);
392 throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER
);
396 class navigation_exposed_moodle_page
extends moodle_page
{
397 public function set_navigation(navigation_node
$node) {
398 $this->_navigation
= $node;
402 class navbar_testcase
extends advanced_testcase
{
406 protected function setUp() {
410 $this->resetAfterTest(true);
413 $PAGE->set_course($SITE);
415 $tempnode = new exposed_global_navigation();
416 // Create an initial tree structure to work with
417 $cat1 = $tempnode->add('category 1', null, navigation_node
::TYPE_CATEGORY
, null, 'cat1');
418 $cat2 = $tempnode->add('category 2', null, navigation_node
::TYPE_CATEGORY
, null, 'cat2');
419 $cat3 = $tempnode->add('category 3', null, navigation_node
::TYPE_CATEGORY
, null, 'cat3');
420 $sub1 = $cat2->add('sub category 1', null, navigation_node
::TYPE_CATEGORY
, null, 'sub1');
421 $sub2 = $cat2->add('sub category 2', null, navigation_node
::TYPE_CATEGORY
, null, 'sub2');
422 $sub3 = $cat2->add('sub category 3', null, navigation_node
::TYPE_CATEGORY
, null, 'sub3');
423 $course1 = $sub2->add('course 1', null, navigation_node
::TYPE_COURSE
, null, 'course1');
424 $course2 = $sub2->add('course 2', null, navigation_node
::TYPE_COURSE
, null, 'course2');
425 $course3 = $sub2->add('course 3', null, navigation_node
::TYPE_COURSE
, null, 'course3');
426 $section1 = $course2->add('section 1', null, navigation_node
::TYPE_SECTION
, null, 'sec1');
427 $section2 = $course2->add('section 2', null, navigation_node
::TYPE_SECTION
, null, 'sec2');
428 $section3 = $course2->add('section 3', null, navigation_node
::TYPE_SECTION
, null, 'sec3');
429 $act1 = $section2->add('activity 1', null, navigation_node
::TYPE_ACTIVITY
, null, 'act1');
430 $act2 = $section2->add('activity 2', null, navigation_node
::TYPE_ACTIVITY
, null, 'act2');
431 $act3 = $section2->add('activity 3', null, navigation_node
::TYPE_ACTIVITY
, null, 'act3');
432 $res1 = $section2->add('resource 1', null, navigation_node
::TYPE_RESOURCE
, null, 'res1');
433 $res2 = $section2->add('resource 2', null, navigation_node
::TYPE_RESOURCE
, null, 'res2');
434 $res3 = $section2->add('resource 3', null, navigation_node
::TYPE_RESOURCE
, null, 'res3');
435 $tempnode->find('course2', navigation_node
::TYPE_COURSE
)->make_active();
437 $page = new navigation_exposed_moodle_page();
438 $page->set_url($PAGE->url
);
439 $page->set_context($PAGE->context
);
441 $navigation = new exposed_global_navigation($page);
442 $navigation->children
= $tempnode->children
;
443 $navigation->set_initialised();
444 $page->set_navigation($navigation);
446 $this->cache
= new navigation_cache('unittest_nav');
447 $this->node
= new exposed_navbar($page);
449 public function test_add() {
450 // Add a node with all args set
451 $this->node
->add('test_add_1','http://www.moodle.org/',navigation_node
::TYPE_COURSE
,'testadd1','testadd1',new pix_icon('i/course', ''));
452 // Add a node with the minimum args required
453 $this->node
->add('test_add_2','http://www.moodle.org/',navigation_node
::TYPE_COURSE
,'testadd2','testadd2',new pix_icon('i/course', ''));
454 $this->assertInstanceOf('navigation_node', $this->node
->get('testadd1'));
455 $this->assertInstanceOf('navigation_node', $this->node
->get('testadd2'));
457 public function test_has_items() {
458 $this->assertTrue($this->node
->has_items());
462 class navigation_cache_testcase
extends basic_testcase
{
465 protected function setUp() {
468 $this->cache
= new navigation_cache('unittest_nav');
469 $this->cache
->anysetvariable
= true;
471 public function test___get() {
472 $this->assertTrue($this->cache
->anysetvariable
);
473 $this->assertEquals($this->cache
->notasetvariable
, null);
475 public function test___set() {
476 $this->cache
->myname
= 'Sam Hemelryk';
477 $this->assertTrue($this->cache
->cached('myname'));
478 $this->assertEquals($this->cache
->myname
, 'Sam Hemelryk');
480 public function test_cached() {
481 $this->assertTrue($this->cache
->cached('anysetvariable'));
482 $this->assertFalse($this->cache
->cached('notasetvariable'));
484 public function test_clear() {
485 $cache = clone($this->cache
);
486 $this->assertTrue($cache->cached('anysetvariable'));
488 $this->assertFalse($cache->cached('anysetvariable'));
490 public function test_set() {
491 $this->cache
->set('software', 'Moodle');
492 $this->assertTrue($this->cache
->cached('software'));
493 $this->assertEquals($this->cache
->software
, 'Moodle');
498 * This is a dummy object that allows us to call protected methods within the
499 * global navigation class by prefixing the methods with `exposed_`
501 class exposed_settings_navigation
extends settings_navigation
{
502 protected $exposedkey = 'exposed_';
503 function __construct() {
505 parent
::__construct($PAGE);
506 $this->cache
= new navigation_cache('unittest_nav');
508 function __call($method, $arguments) {
509 if (strpos($method,$this->exposedkey
) !== false) {
510 $method = substr($method, strlen($this->exposedkey
));
512 if (method_exists($this, $method)) {
513 return call_user_func_array(array($this, $method), $arguments);
515 throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER
);
519 class settings_navigation_testcase
extends advanced_testcase
{
523 protected function setUp() {
527 $this->resetAfterTest(true);
530 $PAGE->set_course($SITE);
532 $this->cache
= new navigation_cache('unittest_nav');
533 $this->node
= new exposed_settings_navigation();
535 public function test___construct() {
536 $this->node
= new exposed_settings_navigation();
538 public function test___initialise() {
539 $this->node
->initialise();
540 $this->assertEquals($this->node
->id
, 'settingsnav');
542 public function test_in_alternative_role() {
543 $this->assertFalse($this->node
->exposed_in_alternative_role());