MDL-27809 navigation: Fixed up a couple of minor issues after peer-review
[moodle.git] / lib / simpletest / testnavigationlib.php
CommitLineData
7d2a0492 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
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.
9//
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.
14//
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/>.
17
18/**
19 * Unit tests for lib/navigationlib.php
20 *
21 * @package moodlecore
22 * @copyright 2009 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later (5)
24 */
25
26if (!defined('MOODLE_INTERNAL')) {
27 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
28}
29require_once($CFG->libdir . '/navigationlib.php');
30
31class navigation_node_test extends UnitTestCase {
32 protected $tree;
33 public static $includecoverage = array('./lib/navigationlib.php');
34 public static $excludecoverage = array();
95b97515 35 protected $fakeproperties = array(
36 'text' => 'text',
37 'shorttext' => 'A very silly extra long short text string, more than 25 characters',
38 'key' => 'key',
39 'type' => 'navigation_node::TYPE_COURSE',
40 'action' => 'http://www.moodle.org/');
c705a24e 41 protected $activeurl = null;
42 protected $inactivenode = null;
7d2a0492 43
3406acde
SH
44 /**
45 * @var navigation_node
46 */
47 public $node;
48
7d2a0492 49 public function setUp() {
c705a24e 50 global $CFG, $PAGE;
7d2a0492 51 parent::setUp();
c705a24e 52
53 $this->activeurl = $PAGE->url;
460232f3
SH
54 navigation_node::override_active_url($this->activeurl);
55
c705a24e 56 $this->inactiveurl = new moodle_url('http://www.moodle.com/');
57 $this->fakeproperties['action'] = $this->inactiveurl;
58
7d2a0492 59 $this->node = new navigation_node('Test Node');
60 $this->node->type = navigation_node::TYPE_SYSTEM;
3406acde
SH
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;
7d2a0492 71 }
8926f844 72
7d2a0492 73 public function test___construct() {
74 global $CFG;
95b97515 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']);
82 }
7d2a0492 83 public function test_add() {
84 global $CFG;
85 // Add a node with all args set
3406acde 86 $node1 = $this->node->add('test_add_1','http://www.moodle.org/',navigation_node::TYPE_COURSE,'testadd1','key',new pix_icon('i/course', ''));
7d2a0492 87 // Add a node with the minimum args required
3406acde
SH
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));
90
91 $this->assertIsA($node1, 'navigation_node');
92 $this->assertIsA($node2, 'navigation_node');
93 $this->assertIsA($node3, 'navigation_node');
94
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));
7d2a0492 99 }
100
188a8127 101 public function test_add_before() {
102 global $CFG;
103 // Create 3 nodes
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;
117 }
118 $this->assertEqual('testadd1', $keys[count($keys)-3]);
119 $this->assertEqual('testadd2', $keys[count($keys)-2]);
120 $this->assertEqual('testadd3', $keys[count($keys)-1]);
121 }
122
7d2a0492 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));
130 }
131 }
132
7d2a0492 133
134 public function test_check_if_active() {
7d2a0492 135 // First test the string urls
7d2a0492 136 // demo1 -> action is http://www.moodle.org/, thus should be true
3406acde
SH
137 $demo5 = $this->node->find('demo5', navigation_node::TYPE_COURSE);
138 if ($this->assertIsA($demo5, 'navigation_node')) {
139 $this->assertTrue($demo5->check_if_active());
140 }
141
7d2a0492 142 // demo2 -> action is http://www.moodle.com/, thus should be false
3406acde
SH
143 $demo2 = $this->node->get('demo2');
144 if ($this->assertIsA($demo2, 'navigation_node')) {
145 $this->assertFalse($demo2->check_if_active());
146 }
7d2a0492 147 }
148
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());
155 // Obviously duff
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());
3406acde
SH
159 // Should be true activity1 is the active node
160 $this->assertTrue($this->node->get('demo3')->get('demo5')->get('activity1')->contains_active_node());
7d2a0492 161 // Obviously duff
162 $this->assertFalse($this->node->get('demo3')->get('demo4')->contains_active_node());
163 }
164
7d2a0492 165 public function test_find_active_node() {
166 $activenode1 = $this->node->find_active_node();
3406acde
SH
167 $activenode2 = $this->node->get('demo1')->find_active_node();
168
169 if ($this->assertIsA($activenode1, 'navigation_node')) {
170 $this->assertReference($activenode1, $this->node->get('demo3')->get('demo5')->get('activity1'));
7d2a0492 171 }
3406acde
SH
172
173 $this->assertNotA($activenode2, 'navigation_node');
7d2a0492 174 }
175
3406acde
SH
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);
7d2a0492 181 $this->assertIsA($node1, 'navigation_node');
182 $this->assertIsA($node2, 'navigation_node');
183 $this->assertNotA($node3, 'navigation_node');
184 $this->assertNotA($node4, 'navigation_node');
185 }
186
7d2a0492 187 public function test_find_expandable() {
188 $expandable = array();
189 $this->node->find_expandable($expandable);
abaece1e
SH
190 $this->assertEqual(count($expandable), 4);
191 if (count($expandable) === 4) {
c755c874
SH
192 $name = $expandable[0]['key'];
193 $name .= $expandable[1]['key'];
194 $name .= $expandable[2]['key'];
195 $name .= $expandable[3]['key'];
abaece1e 196 $this->assertEqual($name, 'demo1demo2demo4hiddendemo2');
7d2a0492 197 }
198 }
199
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);
209 }
210
7d2a0492 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');
219 }
220
221 public function test_make_active() {
222 global $CFG;
3406acde
SH
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);
229 }
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');
234
235 $this->assertIsA($remove1, 'navigation_node');
236 $this->assertIsA($remove2, 'navigation_node');
237 $this->assertIsA($remove3, 'navigation_node');
238
7d2a0492 239 $this->assertIsA($this->node->get('remove1'), 'navigation_node');
3406acde
SH
240 $this->assertIsA($this->node->get('remove2'), 'navigation_node');
241 $this->assertIsA($remove2->get('remove3'), 'navigation_node');
242
243 $this->assertTrue($remove1->remove());
244 $this->assertTrue($this->node->get('remove2')->remove());
245 $this->assertTrue($remove2->get('remove3')->remove());
246
247 $this->assertFalse($this->node->get('remove1'));
248 $this->assertFalse($this->node->get('remove2'));
7d2a0492 249 }
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));
254 }
7d2a0492 255}
256
257/**
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_`
260 */
261class exposed_global_navigation extends global_navigation {
262 protected $exposedkey = 'exposed_';
abaece1e 263 public function __construct(moodle_page $page=null) {
3406acde 264 global $PAGE;
abaece1e
SH
265 if ($page === null) {
266 $page = $PAGE;
267 }
268 parent::__construct($page);
7d2a0492 269 $this->cache = new navigation_cache('simpletest_nav');
270 }
abaece1e 271 public function __call($method, $arguments) {
7d2a0492 272 if (strpos($method,$this->exposedkey) !== false) {
273 $method = substr($method, strlen($this->exposedkey));
274 }
275 if (method_exists($this, $method)) {
276 return call_user_func_array(array($this, $method), $arguments);
277 }
278 throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
279 }
abaece1e
SH
280 public function set_initialised() {
281 $this->initialised = true;
282 }
7d2a0492 283}
284
95b97515 285class mock_initialise_global_navigation extends global_navigation {
286
287 static $count = 1;
288
289 public function load_for_category() {
290 $this->add('load_for_category', null, null, null, 'initcall'.self::$count);
291 self::$count++;
292 return 0;
293 }
294
295 public function load_for_course() {
296 $this->add('load_for_course', null, null, null, 'initcall'.self::$count);
297 self::$count++;
298 return 0;
299 }
300
301 public function load_for_activity() {
302 $this->add('load_for_activity', null, null, null, 'initcall'.self::$count);
303 self::$count++;
304 return 0;
305 }
306
307 public function load_for_user() {
308 $this->add('load_for_user', null, null, null, 'initcall'.self::$count);
309 self::$count++;
310 return 0;
311 }
312}
313
7d2a0492 314class global_navigation_test extends UnitTestCase {
315 /**
316 * @var global_navigation
317 */
318 public $node;
7d2a0492 319 public static $includecoverage = array('./lib/navigationlib.php');
320 public static $excludecoverage = array();
8926f844 321
7d2a0492 322 public function setUp() {
7d2a0492 323 $this->node = new exposed_global_navigation();
324 // Create an initial tree structure to work with
3406acde
SH
325 $cat1 = $this->node->add('category 1', null, navigation_node::TYPE_CATEGORY, null, 'cat1');
326 $cat2 = $this->node->add('category 2', null, navigation_node::TYPE_CATEGORY, null, 'cat2');
327 $cat3 = $this->node->add('category 3', null, navigation_node::TYPE_CATEGORY, null, 'cat3');
328 $sub1 = $cat2->add('sub category 1', null, navigation_node::TYPE_CATEGORY, null, 'sub1');
329 $sub2 = $cat2->add('sub category 2', null, navigation_node::TYPE_CATEGORY, null, 'sub2');
330 $sub3 = $cat2->add('sub category 3', null, navigation_node::TYPE_CATEGORY, null, 'sub3');
331 $course1 = $sub2->add('course 1', null, navigation_node::TYPE_COURSE, null, 'course1');
332 $course2 = $sub2->add('course 2', null, navigation_node::TYPE_COURSE, null, 'course2');
333 $course3 = $sub2->add('course 3', null, navigation_node::TYPE_COURSE, null, 'course3');
3992a46e
SH
334 $section1 = $course2->add('section 1', null, navigation_node::TYPE_SECTION, null, 'sec1');
335 $section2 = $course2->add('section 2', null, navigation_node::TYPE_SECTION, null, 'sec2');
336 $section3 = $course2->add('section 3', null, navigation_node::TYPE_SECTION, null, 'sec3');
3406acde
SH
337 $act1 = $section2->add('activity 1', null, navigation_node::TYPE_ACTIVITY, null, 'act1');
338 $act2 = $section2->add('activity 2', null, navigation_node::TYPE_ACTIVITY, null, 'act2');
339 $act3 = $section2->add('activity 3', null, navigation_node::TYPE_ACTIVITY, null, 'act3');
340 $res1 = $section2->add('resource 1', null, navigation_node::TYPE_RESOURCE, null, 'res1');
341 $res2 = $section2->add('resource 2', null, navigation_node::TYPE_RESOURCE, null, 'res2');
342 $res3 = $section2->add('resource 3', null, navigation_node::TYPE_RESOURCE, null, 'res3');
7d2a0492 343 }
3992a46e 344
7d2a0492 345 public function test_format_display_course_content() {
346 $this->assertTrue($this->node->exposed_format_display_course_content('topic'));
347 $this->assertFalse($this->node->exposed_format_display_course_content('scorm'));
348 $this->assertTrue($this->node->exposed_format_display_course_content('dummy'));
349 }
7d2a0492 350 public function test_module_extends_navigation() {
47379255 351 $this->assertTrue($this->node->exposed_module_extends_navigation('data'));
e26507b3 352 $this->assertFalse($this->node->exposed_module_extends_navigation('test1'));
7d2a0492 353 }
354}
355
356/**
357 * This is a dummy object that allows us to call protected methods within the
358 * global navigation class by prefixing the methods with `exposed_`
359 */
360class exposed_navbar extends navbar {
361 protected $exposedkey = 'exposed_';
abaece1e
SH
362 public function __construct(moodle_page $page) {
363 parent::__construct($page);
7d2a0492 364 $this->cache = new navigation_cache('simpletest_nav');
365 }
366 function __call($method, $arguments) {
367 if (strpos($method,$this->exposedkey) !== false) {
368 $method = substr($method, strlen($this->exposedkey));
369 }
370 if (method_exists($this, $method)) {
371 return call_user_func_array(array($this, $method), $arguments);
372 }
373 throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
374 }
375}
376
abaece1e
SH
377class navigation_exposed_moodle_page extends moodle_page {
378 public function set_navigation(navigation_node $node) {
379 $this->_navigation = $node;
380 }
381}
382
7d2a0492 383class navbar_test extends UnitTestCase {
384 protected $node;
385 protected $oldnav;
386
387 public static $includecoverage = array('./lib/navigationlib.php');
388 public static $excludecoverage = array();
389
390 public function setUp() {
391 global $PAGE;
abaece1e 392
7d2a0492 393 $temptree = new global_navigation_test();
394 $temptree->setUp();
3406acde 395 $temptree->node->find('course2', navigation_node::TYPE_COURSE)->make_active();
abaece1e
SH
396
397 $page = new navigation_exposed_moodle_page();
398 $page->set_url($PAGE->url);
399 $page->set_context($PAGE->context);
400
401 $navigation = new exposed_global_navigation($page);
402 $navigation->children = $temptree->node->children;
403 $navigation->set_initialised();
404 $page->set_navigation($navigation);
405
406 $this->cache = new navigation_cache('simpletest_nav');
407 $this->node = new exposed_navbar($page);
7d2a0492 408 }
409 public function test_add() {
7d2a0492 410 // Add a node with all args set
a3bbac8b 411 $this->node->add('test_add_1','http://www.moodle.org/',navigation_node::TYPE_COURSE,'testadd1','testadd1',new pix_icon('i/course', ''));
7d2a0492 412 // Add a node with the minimum args required
3406acde 413 $this->node->add('test_add_2','http://www.moodle.org/',navigation_node::TYPE_COURSE,'testadd2','testadd2',new pix_icon('i/course', ''));
7d2a0492 414 $this->assertIsA($this->node->get('testadd1'), 'navigation_node');
3406acde 415 $this->assertIsA($this->node->get('testadd2'), 'navigation_node');
7d2a0492 416 }
417 public function test_has_items() {
7d2a0492 418 $this->assertTrue($this->node->has_items());
7d2a0492 419 }
7d2a0492 420}
421
422class navigation_cache_test extends UnitTestCase {
423 protected $cache;
424
425 public static $includecoverage = array('./lib/navigationlib.php');
426 public static $excludecoverage = array();
427
428 public function setUp() {
429 $this->cache = new navigation_cache('simpletest_nav');
430 $this->cache->anysetvariable = true;
431 }
432 public function test___get() {
433 $this->assertTrue($this->cache->anysetvariable);
434 $this->assertEqual($this->cache->notasetvariable, null);
435 }
436 public function test___set() {
437 $this->cache->myname = 'Sam Hemelryk';
438 $this->assertTrue($this->cache->cached('myname'));
439 $this->assertEqual($this->cache->myname, 'Sam Hemelryk');
440 }
441 public function test_cached() {
442 $this->assertTrue($this->cache->cached('anysetvariable'));
443 $this->assertFalse($this->cache->cached('notasetvariable'));
444 }
445 public function test_clear() {
446 $cache = clone($this->cache);
447 $this->assertTrue($cache->cached('anysetvariable'));
448 $cache->clear();
449 $this->assertFalse($cache->cached('anysetvariable'));
450 }
451 public function test_set() {
452 $this->cache->set('software', 'Moodle');
453 $this->assertTrue($this->cache->cached('software'));
454 $this->assertEqual($this->cache->software, 'Moodle');
455 }
456}
457
458/**
459 * This is a dummy object that allows us to call protected methods within the
460 * global navigation class by prefixing the methods with `exposed_`
461 */
462class exposed_settings_navigation extends settings_navigation {
463 protected $exposedkey = 'exposed_';
464 function __construct() {
465 global $PAGE;
466 parent::__construct($PAGE);
467 $this->cache = new navigation_cache('simpletest_nav');
468 }
469 function __call($method, $arguments) {
470 if (strpos($method,$this->exposedkey) !== false) {
471 $method = substr($method, strlen($this->exposedkey));
472 }
473 if (method_exists($this, $method)) {
474 return call_user_func_array(array($this, $method), $arguments);
475 }
476 throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
477 }
478}
479
480class settings_navigation_test extends UnitTestCase {
481 protected $node;
482 protected $cache;
483
484 public static $includecoverage = array('./lib/navigationlib.php');
485 public static $excludecoverage = array();
486
487 public function setUp() {
488 global $PAGE;
489 $this->cache = new navigation_cache('simpletest_nav');
490 $this->node = new exposed_settings_navigation();
491 }
492 public function test___construct() {
493 $this->node = new exposed_settings_navigation();
494 }
495 public function test___initialise() {
496 $this->node->initialise();
497 $this->assertEqual($this->node->id, 'settingsnav');
498 }
7d2a0492 499 public function test_in_alternative_role() {
500 $this->assertFalse($this->node->exposed_in_alternative_role());
501 }
95b97515 502}