3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * 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 if (!defined('MOODLE_INTERNAL')) {
27 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
29 require_once($CFG->libdir . '/navigationlib.php');
31 class navigation_node_test extends UnitTestCase {
33 public static $includecoverage = array('./lib/navigationlib.php');
34 public static $excludecoverage = array();
35 protected $fakeproperties = array(
37 'shorttext' => 'A very silly extra long short text string, more than 25 characters',
39 'type' => 'navigation_node::TYPE_COURSE',
40 'action' => 'http://www.moodle.org/');
41 protected $activeurl = null;
42 protected $inactivenode = null;
45 * @var navigation_node
49 public function setUp() {
53 $this->activeurl = $PAGE->url;
54 navigation_node::override_active_url($this->activeurl);
56 $this->inactiveurl = new moodle_url('http://www.moodle.com/');
57 $this->fakeproperties['action'] = $this->inactiveurl;
59 $this->node = new navigation_node('Test Node');
60 $this->node->type = navigation_node::TYPE_SYSTEM;
61 $demo1 = $this->node->add('demo1', $this->inactiveurl, navigation_node::TYPE_COURSE, null, 'demo1', new pix_icon('i/course', ''));
62 $demo2 = $this->node->add('demo2', $this->inactiveurl, navigation_node::TYPE_COURSE, null, 'demo2', new pix_icon('i/course', ''));
63 $demo3 = $this->node->add('demo3', $this->inactiveurl, navigation_node::TYPE_CATEGORY, null, 'demo3',new pix_icon('i/course', ''));
64 $demo4 = $demo3->add('demo4', $this->inactiveurl,navigation_node::TYPE_COURSE, null, 'demo4', new pix_icon('i/course', ''));
65 $demo5 = $demo3->add('demo5', $this->activeurl, navigation_node::TYPE_COURSE, null, 'demo5',new pix_icon('i/course', ''));
66 $demo5->add('activity1', null, navigation_node::TYPE_ACTIVITY, null, 'activity1')->make_active();
67 $hiddendemo1 = $this->node->add('hiddendemo1', $this->inactiveurl, navigation_node::TYPE_CATEGORY, null, 'hiddendemo1', new pix_icon('i/course', ''));
68 $hiddendemo1->hidden = true;
69 $hiddendemo1->add('hiddendemo2', $this->inactiveurl, navigation_node::TYPE_COURSE, null, 'hiddendemo2', new pix_icon('i/course', ''))->helpbutton = 'Here is a help button';;
70 $hiddendemo1->add('hiddendemo3', $this->inactiveurl, navigation_node::TYPE_COURSE,null, 'hiddendemo3', new pix_icon('i/course', ''))->display = false;
73 public function test___construct() {
75 $node = new navigation_node($this->fakeproperties);
76 $this->assertEqual($node->text, $this->fakeproperties['text']);
77 $this->assertEqual($node->title, $this->fakeproperties['text']);
78 $this->assertTrue(strpos($this->fakeproperties['shorttext'], substr($node->shorttext,0, -3))===0);
79 $this->assertEqual($node->key, $this->fakeproperties['key']);
80 $this->assertEqual($node->type, $this->fakeproperties['type']);
81 $this->assertEqual($node->action, $this->fakeproperties['action']);
83 public function test_add() {
85 // Add a node with all args set
86 $node1 = $this->node->add('test_add_1','http://www.moodle.org/',navigation_node::TYPE_COURSE,'testadd1','key',new pix_icon('i/course', ''));
87 // Add a node with the minimum args required
88 $node2 = $this->node->add('test_add_2',null, navigation_node::TYPE_CUSTOM,'testadd2');
89 $node3 = $this->node->add(str_repeat('moodle ', 15),str_repeat('moodle', 15));
91 $this->assertIsA($node1, 'navigation_node');
92 $this->assertIsA($node2, 'navigation_node');
93 $this->assertIsA($node3, 'navigation_node');
95 $this->assertReference($node1, $this->node->get('key'));
96 $this->assertReference($node2, $this->node->get($node2->key));
97 $this->assertReference($node2, $this->node->get($node2->key, $node2->type));
98 $this->assertReference($node3, $this->node->get($node3->key, $node3->type));
101 public function test_add_before() {
104 $node1 = navigation_node::create('test_add_1', null, navigation_node::TYPE_CUSTOM,
105 'test 1', 'testadd1');
106 $node2 = navigation_node::create('test_add_2', null, navigation_node::TYPE_CUSTOM,
107 'test 2', 'testadd2');
108 $node3 = navigation_node::create('test_add_3', null, navigation_node::TYPE_CUSTOM,
109 'test 3', 'testadd3');
110 // Add node 2, then node 1 before 2, then node 3 at end
111 $this->node->add_node($node2);
112 $this->node->add_node($node1, 'testadd2');
113 $this->node->add_node($node3);
114 // Check the last 3 nodes are in 1, 2, 3 order and have those indexes
115 foreach($this->node->children as $child) {
116 $keys[] = $child->key;
118 $this->assertEqual('testadd1', $keys[count($keys)-3]);
119 $this->assertEqual('testadd2', $keys[count($keys)-2]);
120 $this->assertEqual('testadd3', $keys[count($keys)-1]);
123 public function test_add_class() {
124 $node = $this->node->get('demo1');
125 $this->assertIsA($node, 'navigation_node');
126 if ($node !== false) {
127 $node->add_class('myclass');
128 $classes = $node->classes;
129 $this->assertTrue(in_array('myclass', $classes));
134 public function test_check_if_active() {
135 // First test the string urls
136 // demo1 -> action is http://www.moodle.org/, thus should be true
137 $demo5 = $this->node->find('demo5', navigation_node::TYPE_COURSE);
138 if ($this->assertIsA($demo5, 'navigation_node')) {
139 $this->assertTrue($demo5->check_if_active());
142 // demo2 -> action is http://www.moodle.com/, thus should be false
143 $demo2 = $this->node->get('demo2');
144 if ($this->assertIsA($demo2, 'navigation_node')) {
145 $this->assertFalse($demo2->check_if_active());
149 public function test_contains_active_node() {
150 // demo5, and activity1 were set to active during setup
151 // Should be true as it contains all nodes
152 $this->assertTrue($this->node->contains_active_node());
153 // Should be true as demo5 is a child of demo3
154 $this->assertTrue($this->node->get('demo3')->contains_active_node());
156 $this->assertFalse($this->node->get('demo1')->contains_active_node());
157 // Should be true as demo5 contains activity1
158 $this->assertTrue($this->node->get('demo3')->get('demo5')->contains_active_node());
159 // Should be true activity1 is the active node
160 $this->assertTrue($this->node->get('demo3')->get('demo5')->get('activity1')->contains_active_node());
162 $this->assertFalse($this->node->get('demo3')->get('demo4')->contains_active_node());
165 public function test_find_active_node() {
166 $activenode1 = $this->node->find_active_node();
167 $activenode2 = $this->node->get('demo1')->find_active_node();
169 if ($this->assertIsA($activenode1, 'navigation_node')) {
170 $this->assertReference($activenode1, $this->node->get('demo3')->get('demo5')->get('activity1'));
173 $this->assertNotA($activenode2, 'navigation_node');
176 public function test_find() {
177 $node1 = $this->node->find('demo1', navigation_node::TYPE_COURSE);
178 $node2 = $this->node->find('demo5', navigation_node::TYPE_COURSE);
179 $node3 = $this->node->find('demo5', navigation_node::TYPE_CATEGORY);
180 $node4 = $this->node->find('demo0', navigation_node::TYPE_COURSE);
181 $this->assertIsA($node1, 'navigation_node');
182 $this->assertIsA($node2, 'navigation_node');
183 $this->assertNotA($node3, 'navigation_node');
184 $this->assertNotA($node4, 'navigation_node');
187 public function test_find_expandable() {
188 $expandable = array();
189 $this->node->find_expandable($expandable);
190 $this->assertEqual(count($expandable), 4);
191 if (count($expandable) === 4) {
192 $name = $expandable[0]['key'];
193 $name .= $expandable[1]['key'];
194 $name .= $expandable[2]['key'];
195 $name .= $expandable[3]['key'];
196 $this->assertEqual($name, 'demo1demo2demo4hiddendemo2');
200 public function test_get() {
201 $node1 = $this->node->get('demo1'); // Exists
202 $node2 = $this->node->get('demo4'); // Doesn't exist for this node
203 $node3 = $this->node->get('demo0'); // Doesn't exist at all
204 $node4 = $this->node->get(false); // Sometimes occurs in nature code
205 $this->assertIsA($node1, 'navigation_node');
206 $this->assertFalse($node2);
207 $this->assertFalse($node3);
208 $this->assertFalse($node4);
211 public function test_get_css_type() {
212 $csstype1 = $this->node->get('demo3')->get_css_type();
213 $csstype2 = $this->node->get('demo3')->get('demo5')->get_css_type();
214 $this->node->get('demo3')->get('demo5')->type = 1000;
215 $csstype3 = $this->node->get('demo3')->get('demo5')->get_css_type();
216 $this->assertEqual($csstype1, 'type_category');
217 $this->assertEqual($csstype2, 'type_course');
218 $this->assertEqual($csstype3, 'type_unknown');
221 public function test_make_active() {
223 $node1 = $this->node->add('active node 1', null, navigation_node::TYPE_CUSTOM, null, 'anode1');
224 $node2 = $this->node->add('active node 2', new moodle_url($CFG->wwwroot), navigation_node::TYPE_COURSE, null, 'anode2');
225 $node1->make_active();
226 $this->node->get('anode2')->make_active();
227 $this->assertTrue($node1->isactive);
228 $this->assertTrue($this->node->get('anode2')->isactive);
230 public function test_remove() {
231 $remove1 = $this->node->add('child to remove 1', null, navigation_node::TYPE_CUSTOM, null, 'remove1');
232 $remove2 = $this->node->add('child to remove 2', null, navigation_node::TYPE_CUSTOM, null, 'remove2');
233 $remove3 = $remove2->add('child to remove 3', null, navigation_node::TYPE_CUSTOM, null, 'remove3');
235 $this->assertIsA($remove1, 'navigation_node');
236 $this->assertIsA($remove2, 'navigation_node');
237 $this->assertIsA($remove3, 'navigation_node');
239 $this->assertIsA($this->node->get('remove1'), 'navigation_node');
240 $this->assertIsA($this->node->get('remove2'), 'navigation_node');
241 $this->assertIsA($remove2->get('remove3'), 'navigation_node');
243 $this->assertTrue($remove1->remove());
244 $this->assertTrue($this->node->get('remove2')->remove());
245 $this->assertTrue($remove2->get('remove3')->remove());
247 $this->assertFalse($this->node->get('remove1'));
248 $this->assertFalse($this->node->get('remove2'));
250 public function test_remove_class() {
251 $this->node->add_class('testclass');
252 $this->assertTrue($this->node->remove_class('testclass'));
253 $this->assertFalse(in_array('testclass', $this->node->classes));
258 * This is a dummy object that allows us to call protected methods within the
259 * global navigation class by prefixing the methods with `exposed_`
261 class exposed_global_navigation extends global_navigation {
262 protected $exposedkey = 'exposed_';
263 public function __construct(moodle_page $page=null) {
265 if ($page === null) {
268 parent::__construct($page);
269 $this->cache = new navigation_cache('simpletest_nav');
271 public function __call($method, $arguments) {
272 if (strpos($method,$this->exposedkey) !== false) {
273 $method = substr($method, strlen($this->exposedkey));
275 if (method_exists($this, $method)) {
276 return call_user_func_array(array($this, $method), $arguments);
278 throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
280 public function set_initialised() {
281 $this->initialised = true;
285 class mock_initialise_global_navigation extends global_navigation {
289 public function load_for_category() {
290 $this->add('load_for_category', null, null, null, 'initcall'.self::$count);
295 public function load_for_course() {
296 $this->add('load_for_course', null, null, null, 'initcall'.self::$count);
301 public function load_for_activity() {
302 $this->add('load_for_activity', null, null, null, 'initcall'.self::$count);
307 public function load_for_user() {
308 $this->add('load_for_user', null, null, null, 'initcall'.self::$count);
314 class global_navigation_test extends UnitTestCase {
316 * @var global_navigation
320 protected $modinfo5 = 'O:6:"object":6:{s:8:"courseid";s:1:"5";s:6:"userid";s:1:"2";s:8:"sections";a:1:{i:0;a:1:{i:0;s:3:"288";}}s:3:"cms";a:1:{i:288;O:6:"object":17:{s:2:"id";s:3:"288";s:8:"instance";s:2:"19";s:6:"course";s:1:"5";s:7:"modname";s:5:"forum";s:4:"name";s:10:"News forum";s:7:"visible";s:1:"1";s:10:"sectionnum";s:1:"0";s:9:"groupmode";s:1:"0";s:10:"groupingid";s:1:"0";s:16:"groupmembersonly";s:1:"0";s:6:"indent";s:1:"0";s:10:"completion";s:1:"0";s:5:"extra";s:0:"";s:4:"icon";s:0:"";s:11:"uservisible";b:1;s:9:"modplural";s:6:"Forums";s:9:"available";b:1;}}s:9:"instances";a:1:{s:5:"forum";a:1:{i:19;R:8;}}s:6:"groups";N;}';
321 protected $coursesections5 = 'a:5:{i:0;O:8:"stdClass":6:{s:7:"section";s:1:"0";s:2:"id";s:2:"14";s:6:"course";s:1:"5";s:7:"summary";N;s:8:"sequence";s:3:"288";s:7:"visible";s:1:"1";}i:1;O:8:"stdClass":6:{s:7:"section";s:1:"1";s:2:"id";s:2:"97";s:6:"course";s:1:"5";s:7:"summary";s:0:"";s:8:"sequence";N;s:7:"visible";s:1:"1";}i:2;O:8:"stdClass":6:{s:7:"section";s:1:"2";s:2:"id";s:2:"98";s:6:"course";s:1:"5";s:7:"summary";s:0:"";s:8:"sequence";N;s:7:"visible";s:1:"1";}i:3;O:8:"stdClass":6:{s:7:"section";s:1:"3";s:2:"id";s:2:"99";s:6:"course";s:1:"5";s:7:"summary";s:0:"";s:8:"sequence";N;s:7:"visible";s:1:"1";}i:4;O:8:"stdClass":6:{s:7:"section";s:1:"4";s:2:"id";s:3:"100";s:6:"course";s:1:"5";s:7:"summary";s:0:"";s:8:"sequence";N;s:7:"visible";s:1:"1";}}';
322 public static $includecoverage = array('./lib/navigationlib.php');
323 public static $excludecoverage = array();
325 public function setUp() {
326 $this->cache = new navigation_cache('simpletest_nav');
327 $this->node = new exposed_global_navigation();
328 // Create an initial tree structure to work with
329 $cat1 = $this->node->add('category 1', null, navigation_node::TYPE_CATEGORY, null, 'cat1');
330 $cat2 = $this->node->add('category 2', null, navigation_node::TYPE_CATEGORY, null, 'cat2');
331 $cat3 = $this->node->add('category 3', null, navigation_node::TYPE_CATEGORY, null, 'cat3');
332 $sub1 = $cat2->add('sub category 1', null, navigation_node::TYPE_CATEGORY, null, 'sub1');
333 $sub2 = $cat2->add('sub category 2', null, navigation_node::TYPE_CATEGORY, null, 'sub2');
334 $sub3 = $cat2->add('sub category 3', null, navigation_node::TYPE_CATEGORY, null, 'sub3');
335 $course1 = $sub2->add('course 1', null, navigation_node::TYPE_COURSE, null, 'course1');
336 $course2 = $sub2->add('course 2', null, navigation_node::TYPE_COURSE, null, 'course2');
337 $course3 = $sub2->add('course 3', null, navigation_node::TYPE_COURSE, null, 'course3');
338 $section1 = $course2->add('section 1', null, navigation_node::TYPE_COURSE, null, 'sec1');
339 $section2 = $course2->add('section 2', null, navigation_node::TYPE_COURSE, null, 'sec2');
340 $section3 = $course2->add('section 3', null, navigation_node::TYPE_COURSE, null, 'sec3');
341 $act1 = $section2->add('activity 1', null, navigation_node::TYPE_ACTIVITY, null, 'act1');
342 $act2 = $section2->add('activity 2', null, navigation_node::TYPE_ACTIVITY, null, 'act2');
343 $act3 = $section2->add('activity 3', null, navigation_node::TYPE_ACTIVITY, null, 'act3');
344 $res1 = $section2->add('resource 1', null, navigation_node::TYPE_RESOURCE, null, 'res1');
345 $res2 = $section2->add('resource 2', null, navigation_node::TYPE_RESOURCE, null, 'res2');
346 $res3 = $section2->add('resource 3', null, navigation_node::TYPE_RESOURCE, null, 'res3');
348 $this->cache->clear();
349 $this->cache->modinfo5 = unserialize($this->modinfo5);
350 $this->cache->coursesections5 = unserialize($this->coursesections5);
351 $this->cache->canviewhiddenactivities = true;
352 $this->cache->canviewhiddensections = true;
353 $this->cache->canviewhiddencourses = true;
354 $sub2->add('Test Course 5', new moodle_url('http://moodle.org'),navigation_node::TYPE_COURSE,null,'5');
356 public function test_load_generic_course_sections() {
357 $coursenode = $this->node->find('5', navigation_node::TYPE_COURSE);
358 $course = new stdClass;
360 $course->numsections = 10;
361 $course->modinfo = $this->modinfo5;
362 $this->node->load_generic_course_sections($course, $coursenode, 'topic', 'topic');
363 $this->assertEqual($coursenode->children->count(),1);
365 public function test_format_display_course_content() {
366 $this->assertTrue($this->node->exposed_format_display_course_content('topic'));
367 $this->assertFalse($this->node->exposed_format_display_course_content('scorm'));
368 $this->assertTrue($this->node->exposed_format_display_course_content('dummy'));
370 public function test_module_extends_navigation() {
371 $this->assertTrue($this->node->exposed_module_extends_navigation('data'));
372 $this->assertFalse($this->node->exposed_module_extends_navigation('test1'));
377 * This is a dummy object that allows us to call protected methods within the
378 * global navigation class by prefixing the methods with `exposed_`
380 class exposed_navbar extends navbar {
381 protected $exposedkey = 'exposed_';
382 public function __construct(moodle_page $page) {
383 parent::__construct($page);
384 $this->cache = new navigation_cache('simpletest_nav');
386 function __call($method, $arguments) {
387 if (strpos($method,$this->exposedkey) !== false) {
388 $method = substr($method, strlen($this->exposedkey));
390 if (method_exists($this, $method)) {
391 return call_user_func_array(array($this, $method), $arguments);
393 throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
397 class navigation_exposed_moodle_page extends moodle_page {
398 public function set_navigation(navigation_node $node) {
399 $this->_navigation = $node;
403 class navbar_test extends UnitTestCase {
407 public static $includecoverage = array('./lib/navigationlib.php');
408 public static $excludecoverage = array();
410 public function setUp() {
413 $temptree = new global_navigation_test();
415 $temptree->node->find('course2', navigation_node::TYPE_COURSE)->make_active();
417 $page = new navigation_exposed_moodle_page();
418 $page->set_url($PAGE->url);
419 $page->set_context($PAGE->context);
421 $navigation = new exposed_global_navigation($page);
422 $navigation->children = $temptree->node->children;
423 $navigation->set_initialised();
424 $page->set_navigation($navigation);
426 $this->cache = new navigation_cache('simpletest_nav');
427 $this->node = new exposed_navbar($page);
429 public function test_add() {
430 // Add a node with all args set
431 $this->node->add('test_add_1','http://www.moodle.org/',navigation_node::TYPE_COURSE,'testadd1','testadd1',new pix_icon('i/course', ''));
432 // Add a node with the minimum args required
433 $this->node->add('test_add_2','http://www.moodle.org/',navigation_node::TYPE_COURSE,'testadd2','testadd2',new pix_icon('i/course', ''));
434 $this->assertIsA($this->node->get('testadd1'), 'navigation_node');
435 $this->assertIsA($this->node->get('testadd2'), 'navigation_node');
437 public function test_has_items() {
438 $this->assertTrue($this->node->has_items());
442 class navigation_cache_test extends UnitTestCase {
445 public static $includecoverage = array('./lib/navigationlib.php');
446 public static $excludecoverage = array();
448 public function setUp() {
449 $this->cache = new navigation_cache('simpletest_nav');
450 $this->cache->anysetvariable = true;
452 public function test___get() {
453 $this->assertTrue($this->cache->anysetvariable);
454 $this->assertEqual($this->cache->notasetvariable, null);
456 public function test___set() {
457 $this->cache->myname = 'Sam Hemelryk';
458 $this->assertTrue($this->cache->cached('myname'));
459 $this->assertEqual($this->cache->myname, 'Sam Hemelryk');
461 public function test_cached() {
462 $this->assertTrue($this->cache->cached('anysetvariable'));
463 $this->assertFalse($this->cache->cached('notasetvariable'));
465 public function test_clear() {
466 $cache = clone($this->cache);
467 $this->assertTrue($cache->cached('anysetvariable'));
469 $this->assertFalse($cache->cached('anysetvariable'));
471 public function test_set() {
472 $this->cache->set('software', 'Moodle');
473 $this->assertTrue($this->cache->cached('software'));
474 $this->assertEqual($this->cache->software, 'Moodle');
479 * This is a dummy object that allows us to call protected methods within the
480 * global navigation class by prefixing the methods with `exposed_`
482 class exposed_settings_navigation extends settings_navigation {
483 protected $exposedkey = 'exposed_';
484 function __construct() {
486 parent::__construct($PAGE);
487 $this->cache = new navigation_cache('simpletest_nav');
489 function __call($method, $arguments) {
490 if (strpos($method,$this->exposedkey) !== false) {
491 $method = substr($method, strlen($this->exposedkey));
493 if (method_exists($this, $method)) {
494 return call_user_func_array(array($this, $method), $arguments);
496 throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
500 class settings_navigation_test extends UnitTestCase {
504 public static $includecoverage = array('./lib/navigationlib.php');
505 public static $excludecoverage = array();
507 public function setUp() {
509 $this->cache = new navigation_cache('simpletest_nav');
510 $this->node = new exposed_settings_navigation();
512 public function test___construct() {
513 $this->node = new exposed_settings_navigation();
515 public function test___initialise() {
516 $this->node->initialise();
517 $this->assertEqual($this->node->id, 'settingsnav');
519 public function test_in_alternative_role() {
520 $this->assertFalse($this->node->exposed_in_alternative_role());