navigation MDL-21604 Fixed AJAX security issue + the following changes
[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
44 public function setUp() {
c705a24e 45 global $CFG, $PAGE;
7d2a0492 46 parent::setUp();
c705a24e 47
48 $this->activeurl = $PAGE->url;
460232f3
SH
49 navigation_node::override_active_url($this->activeurl);
50
c705a24e 51 $this->inactiveurl = new moodle_url('http://www.moodle.com/');
52 $this->fakeproperties['action'] = $this->inactiveurl;
53
7d2a0492 54 $this->node = new navigation_node('Test Node');
55 $this->node->type = navigation_node::TYPE_SYSTEM;
c705a24e 56 $this->node->add('demo1', $this->inactiveurl, navigation_node::TYPE_COURSE, null, 'demo1', $CFG->httpswwwroot . '/pix/i/course.gif');
57 $this->node->add('demo2', $this->inactiveurl, navigation_node::TYPE_COURSE, null, 'demo2', $CFG->httpswwwroot . '/pix/i/course.gif');
58 $this->node->add('demo3', $this->inactiveurl, navigation_node::TYPE_CATEGORY, null, 'demo3',$CFG->httpswwwroot . '/pix/i/course.gif');
59 $this->node->get('demo3')->add('demo4', $this->inactiveurl,navigation_node::TYPE_COURSE, null, 'demo4', $CFG->httpswwwroot . '/pix/i/course.gif');
60 $this->node->get('demo3')->add('demo5', $this->activeurl, navigation_node::TYPE_COURSE, null, 'demo5',$CFG->httpswwwroot . '/pix/i/course.gif');
61 #$this->node->get('demo3')->get('demo5')->make_active();
d972bad1 62 $this->node->get('demo3')->get('demo5')->add('activity1', null, navigation_node::TYPE_ACTIVITY, null, 'activity1');
7d2a0492 63 $this->node->get('demo3')->get('demo5')->get('activity1')->make_active();
c705a24e 64 $this->node->add('hiddendemo1', $this->inactiveurl, navigation_node::TYPE_CATEGORY, null, 'hiddendemo1', $CFG->httpswwwroot . '/pix/i/course.gif');
7d2a0492 65 $this->node->get('hiddendemo1')->hidden = true;
c705a24e 66 $this->node->get('hiddendemo1')->add('hiddendemo2', $this->inactiveurl, navigation_node::TYPE_COURSE, null, 'hiddendemo2', $CFG->httpswwwroot . '/pix/i/course.gif');
67 $this->node->get('hiddendemo1')->add('hiddendemo3', $this->inactiveurl, navigation_node::TYPE_COURSE,null, 'hiddendemo3', $CFG->httpswwwroot . '/pix/i/course.gif');
7d2a0492 68 $this->node->get('hiddendemo1')->get('hiddendemo2')->helpbutton = 'Here is a help button';
69 $this->node->get('hiddendemo1')->get('hiddendemo3')->display = false;
7d2a0492 70 }
8926f844 71
7d2a0492 72 public function test___construct() {
73 global $CFG;
95b97515 74 $node = new navigation_node($this->fakeproperties);
75 $this->assertEqual($node->text, $this->fakeproperties['text']);
76 $this->assertEqual($node->title, $this->fakeproperties['text']);
77 $this->assertTrue(strpos($this->fakeproperties['shorttext'], substr($node->shorttext,0, -3))===0);
78 $this->assertEqual($node->key, $this->fakeproperties['key']);
79 $this->assertEqual($node->type, $this->fakeproperties['type']);
80 $this->assertEqual($node->action, $this->fakeproperties['action']);
81 }
7d2a0492 82 public function test_add() {
83 global $CFG;
84 // Add a node with all args set
d972bad1 85 $key1 = $this->node->add('test_add_1','http://www.moodle.org/',navigation_node::TYPE_COURSE,'testadd1','key',$CFG->httpswwwroot . '/pix/i/course.gif');
7d2a0492 86 // Add a node with the minimum args required
d972bad1 87 $key2 = $this->node->add('test_add_2',null, navigation_node::TYPE_CUSTOM,'testadd2');
7d2a0492 88 $key3 = $this->node->add(str_repeat('moodle ', 15),str_repeat('moodle', 15));
3d97797b 89 $this->assertEqual('key:'.navigation_node::TYPE_COURSE,$key1);
7d2a0492 90 $this->assertEqual($key2, $this->node->get($key2)->key);
91 $this->assertEqual($key3, $this->node->get($key3)->key);
92 $this->assertIsA($this->node->get('key'), 'navigation_node');
93 $this->assertIsA($this->node->get($key2), 'navigation_node');
94 $this->assertIsA($this->node->get($key3), 'navigation_node');
95 }
96
97 public function test_add_class() {
98 $node = $this->node->get('demo1');
99 $this->assertIsA($node, 'navigation_node');
100 if ($node !== false) {
101 $node->add_class('myclass');
102 $classes = $node->classes;
103 $this->assertTrue(in_array('myclass', $classes));
104 }
105 }
106
107 public function test_add_to_path() {
108 global $CFG;
109 $path = array('demo3','demo5');
bdb0a369 110 $key1 = $this->node->add_to_path($path,'testatp1', 'Test add to path 1', 'testatp1', navigation_node::TYPE_COURSE, 'http://www.moodle.org/', $CFG->httpswwwroot . '/pix/i/course.gif');
3d97797b 111 $this->assertEqual($key1, 'testatp1:'.navigation_node::TYPE_COURSE);
7d2a0492 112
113 // This should generate an exception as we have not provided any text for
114 // the node
115 $this->expectException();
116 $key3 = $this->node->add_to_path(array('demo3','dud1','dud2'), 'text', 'shorttext');
117 $this->assertFalse($key3);
118
119 // This should generate an exception as we have not provided any text for
120 // the node
121 $this->expectException(new coding_exception('You must set the text for the node when you create it.'));
122 $key2 = $this->node->add_to_path($path);
123 }
124
125 public function test_check_if_active() {
7d2a0492 126 // First test the string urls
7d2a0492 127 // demo1 -> action is http://www.moodle.org/, thus should be true
c705a24e 128 $this->assertTrue($this->node->get('demo3')->get('demo5')->check_if_active());
7d2a0492 129 // demo2 -> action is http://www.moodle.com/, thus should be false
130 $this->assertFalse($this->node->get('demo2')->check_if_active());
7d2a0492 131 }
132
133 public function test_contains_active_node() {
134 // demo5, and activity1 were set to active during setup
135 // Should be true as it contains all nodes
136 $this->assertTrue($this->node->contains_active_node());
137 // Should be true as demo5 is a child of demo3
138 $this->assertTrue($this->node->get('demo3')->contains_active_node());
139 // Obviously duff
140 $this->assertFalse($this->node->get('demo1')->contains_active_node());
141 // Should be true as demo5 contains activity1
142 $this->assertTrue($this->node->get('demo3')->get('demo5')->contains_active_node());
143 // Should be false activity1 doesnt contain the active node... it is the active node
144 $this->assertFalse($this->node->get('demo3')->get('demo5')->get('activity1')->contains_active_node());
145 // Obviously duff
146 $this->assertFalse($this->node->get('demo3')->get('demo4')->contains_active_node());
147 }
148
149 public function test_content() {
95b97515 150 $this->node->get('demo3')->get('demo5')->action = null;
151 $this->node->get('demo3')->get('demo5')->title('This is a title');
152 $this->node->get('demo3')->get('demo5')->hidden = true;
153 $this->node->get('demo3')->get('demo5')->icon = null;
154 $this->node->get('demo3')->get('demo5')->helpbutton = 'A fake help button';
7d2a0492 155 $content1 = $this->node->get('demo1')->content();
156 $content2 = $this->node->get('demo3')->content();
157 $content3 = $this->node->get('demo3')->get('demo5')->content();
158 $content4 = $this->node->get('hiddendemo1')->get('hiddendemo2')->content();
159 $content5 = $this->node->get('hiddendemo1')->get('hiddendemo3')->content();
c705a24e 160 $this->assert(new ContainsTagWithAttribute('a','href',$this->node->get('demo1')->action->out()), $content1);
161 $this->assert(new ContainsTagWithAttribute('a','href',$this->node->get('demo3')->action->out()), $content2);
3d97797b
SH
162 $this->assert(new ContainsTagWithAttribute('span','class','dimmed_text'), $content3);
163 #$this->assertEqual($content3, 'A fake help button<span class="clearhelpbutton"><span class="dimmed_text" title="This is a title">demo5</span></span>');
7d2a0492 164 $this->assert(new ContainsTagWithAttribute('a','href',$this->node->get('hiddendemo1')->get('hiddendemo2')->action->out()), $content4);
165 $this->assertTrue(empty($content5));
166 }
167
95b97515 168 public function test_get_active_node() {
169 $node1 = $this->node->get_active_node();
170 $node2 = $this->node->get('demo3')->get_active_node();
171 $this->assertFalse($node1);
172 $this->assertIsA($node2, 'navigation_node');
173 }
174
7d2a0492 175 public function test_find_active_node() {
176 $activenode1 = $this->node->find_active_node();
177 $activenode2 = $this->node->find_active_node(navigation_node::TYPE_COURSE);
178 $activenode3 = $this->node->find_active_node(navigation_node::TYPE_CATEGORY);
179 $activenode4 = $this->node->get('demo1')->find_active_node(navigation_node::TYPE_COURSE);
180 $this->assertIsA($activenode1, 'navigation_node');
181 if ($activenode1 instanceof navigation_node) {
182 $this->assertEqual($activenode1, $this->node->get('demo3')->get('demo5'));
183 }
184 $this->assertIsA($activenode2, 'navigation_node');
185 if ($activenode1 instanceof navigation_node) {
186 $this->assertEqual($activenode2, $this->node->get('demo3')->get('demo5'));
187 }
188 $this->assertIsA($activenode3, 'navigation_node');
189 if ($activenode1 instanceof navigation_node) {
190 $this->assertEqual($activenode3, $this->node->get('demo3'));
191 }
192 $this->assertNotA($activenode4, 'navigation_node');
193 }
194
195 public function test_find_child() {
196 $node1 = $this->node->find_child('demo1', navigation_node::TYPE_COURSE);
197 $node2 = $this->node->find_child('demo5', navigation_node::TYPE_COURSE);
198 $node3 = $this->node->find_child('demo5', navigation_node::TYPE_CATEGORY);
199 $node4 = $this->node->find_child('demo0', navigation_node::TYPE_COURSE);
200 $this->assertIsA($node1, 'navigation_node');
201 $this->assertIsA($node2, 'navigation_node');
202 $this->assertNotA($node3, 'navigation_node');
203 $this->assertNotA($node4, 'navigation_node');
204 }
205
206 public function test_find_child_depth() {
207 $depth1 = $this->node->find_child_depth('demo1',navigation_node::TYPE_COURSE);
208 $depth2 = $this->node->find_child_depth('demo5',navigation_node::TYPE_COURSE);
209 $depth3 = $this->node->find_child_depth('demo5',navigation_node::TYPE_CATEGORY);
210 $depth4 = $this->node->find_child_depth('demo0',navigation_node::TYPE_COURSE);
211 $this->assertEqual(1, $depth1);
212 $this->assertEqual(1, $depth2);
213 $this->assertFalse($depth3);
214 $this->assertFalse($depth4);
215 }
216
217 public function test_find_expandable() {
218 $expandable = array();
219 $this->node->find_expandable($expandable);
220 $this->assertEqual(count($expandable), 5);
221 if (count($expandable) === 5) {
222 $name = $expandable[0]['branchid'];
223 $name .= $expandable[1]['branchid'];
224 $name .= $expandable[2]['branchid'];
225 $name .= $expandable[3]['branchid'];
226 $name .= $expandable[4]['branchid'];
3d97797b 227 $this->assertEqual($name, 'demo1:20demo2:20demo4:20hiddendemo2:20hiddendemo3:20');
7d2a0492 228 }
229 }
230
231 public function test_get() {
232 $node1 = $this->node->get('demo1'); // Exists
233 $node2 = $this->node->get('demo4'); // Doesn't exist for this node
234 $node3 = $this->node->get('demo0'); // Doesn't exist at all
235 $node4 = $this->node->get(false); // Sometimes occurs in nature code
236 $this->assertIsA($node1, 'navigation_node');
237 $this->assertFalse($node2);
238 $this->assertFalse($node3);
239 $this->assertFalse($node4);
240 }
241
242 public function test_get_by_path() {
243 $node1 = $this->node->get_by_path(array('demo3', 'demo4')); // This path exists and should return a node
244 $node2 = $this->node->get_by_path(array('demo1', 'demo2')); // Both elements exist but demo2 is not a child of demo1
245 $node3 = $this->node->get_by_path(array('demo0', 'demo6')); // This path is totally bogus
246 $this->assertIsA($node1, 'navigation_node');
247 $this->assertFalse($node2);
248 $this->assertFalse($node3);
249 }
250
251 public function test_get_css_type() {
252 $csstype1 = $this->node->get('demo3')->get_css_type();
253 $csstype2 = $this->node->get('demo3')->get('demo5')->get_css_type();
254 $this->node->get('demo3')->get('demo5')->type = 1000;
255 $csstype3 = $this->node->get('demo3')->get('demo5')->get_css_type();
256 $this->assertEqual($csstype1, 'type_category');
257 $this->assertEqual($csstype2, 'type_course');
258 $this->assertEqual($csstype3, 'type_unknown');
259 }
260
261 public function test_make_active() {
262 global $CFG;
d972bad1 263 $key1 = $this->node->add('active node 1', null, navigation_node::TYPE_CUSTOM, null, 'anode1');
264 $key2 = $this->node->add('active node 2', new moodle_url($CFG->wwwroot), navigation_node::TYPE_COURSE, null, 'anode2');
7d2a0492 265 $this->node->get($key1)->make_active();
266 $this->node->get($key2)->make_active();
267 $this->assertTrue($this->node->get($key1)->isactive);
268 $this->assertTrue($this->node->get($key2)->isactive);
269 }
7d2a0492 270 public function test_remove_child() {
d972bad1 271 $this->node->add('child to remove 1', null, navigation_node::TYPE_CUSTOM, null, 'remove1');
272 $this->node->add('child to remove 2', null, navigation_node::TYPE_CUSTOM, null, 'remove2');
273 $this->node->get('remove2')->add('child to remove 3', null, navigation_node::TYPE_CUSTOM, null, 'remove3');
7d2a0492 274 $this->assertIsA($this->node->get('remove1'), 'navigation_node');
275 $this->assertTrue($this->node->remove_child('remove1'));
276 $this->assertFalse($this->node->remove_child('remove3'));
277 $this->assertFalse($this->node->remove_child('remove0'));
278 $this->assertTrue($this->node->remove_child('remove2'));
279 }
280 public function test_remove_class() {
281 $this->node->add_class('testclass');
282 $this->assertTrue($this->node->remove_class('testclass'));
283 $this->assertFalse(in_array('testclass', $this->node->classes));
284 }
285 public function test_respect_forced_open() {
286 $this->node->respect_forced_open();
287 $this->assertTrue($this->node->forceopen);
288 }
289 public function test_toggle_type_display() {
290 $this->node->toggle_type_display(navigation_node::TYPE_CATEGORY);
291 $this->assertFalse($this->node->get('demo1')->display);
292 $this->assertFalse($this->node->get('demo3')->get('demo5')->display);
293 $this->assertTrue($this->node->get('demo3')->display);
294 $this->node->toggle_type_display(navigation_node::TYPE_CATEGORY, true);
295 }
296}
297
298/**
299 * This is a dummy object that allows us to call protected methods within the
300 * global navigation class by prefixing the methods with `exposed_`
301 */
302class exposed_global_navigation extends global_navigation {
303 protected $exposedkey = 'exposed_';
304 function __construct() {
305 parent::__construct();
306 $this->cache = new navigation_cache('simpletest_nav');
307 }
308 function __call($method, $arguments) {
309 if (strpos($method,$this->exposedkey) !== false) {
310 $method = substr($method, strlen($this->exposedkey));
311 }
312 if (method_exists($this, $method)) {
313 return call_user_func_array(array($this, $method), $arguments);
314 }
315 throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
316 }
317}
318
95b97515 319class mock_initialise_global_navigation extends global_navigation {
320
321 static $count = 1;
322
323 public function load_for_category() {
324 $this->add('load_for_category', null, null, null, 'initcall'.self::$count);
325 self::$count++;
326 return 0;
327 }
328
329 public function load_for_course() {
330 $this->add('load_for_course', null, null, null, 'initcall'.self::$count);
331 self::$count++;
332 return 0;
333 }
334
335 public function load_for_activity() {
336 $this->add('load_for_activity', null, null, null, 'initcall'.self::$count);
337 self::$count++;
338 return 0;
339 }
340
341 public function load_for_user() {
342 $this->add('load_for_user', null, null, null, 'initcall'.self::$count);
343 self::$count++;
344 return 0;
345 }
346}
347
7d2a0492 348class global_navigation_test extends UnitTestCase {
349 /**
350 * @var global_navigation
351 */
352 public $node;
353 protected $cache;
3b7a763c 354 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;}';
355 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";}}';
7d2a0492 356 public static $includecoverage = array('./lib/navigationlib.php');
357 public static $excludecoverage = array();
8926f844 358
7d2a0492 359 public function setUp() {
360 $this->cache = new navigation_cache('simpletest_nav');
361 $this->node = new exposed_global_navigation();
362 // Create an initial tree structure to work with
d972bad1 363 $this->node->add('category 1', null, navigation_node::TYPE_CATEGORY, null, 'cat1');
364 $this->node->add('category 2', null, navigation_node::TYPE_CATEGORY, null, 'cat2');
365 $this->node->add('category 3', null, navigation_node::TYPE_CATEGORY, null, 'cat3');
366 $this->node->get('cat2')->add('sub category 1', null, navigation_node::TYPE_CATEGORY, null, 'sub1');
367 $this->node->get('cat2')->add('sub category 2', null, navigation_node::TYPE_CATEGORY, null, 'sub2');
368 $this->node->get('cat2')->add('sub category 3', null, navigation_node::TYPE_CATEGORY, null, 'sub3');
369 $this->node->get('cat2')->get('sub2')->add('course 1', null, navigation_node::TYPE_COURSE, null, 'course1');
370 $this->node->get('cat2')->get('sub2')->add('course 2', null, navigation_node::TYPE_COURSE, null, 'course2');
371 $this->node->get('cat2')->get('sub2')->add('course 3', null, navigation_node::TYPE_COURSE, null, 'course3');
372 $this->node->get('cat2')->get('sub2')->get('course2')->add('section 1', null, navigation_node::TYPE_COURSE, null, 'sec1');
373 $this->node->get('cat2')->get('sub2')->get('course2')->add('section 2', null, navigation_node::TYPE_COURSE, null, 'sec2');
374 $this->node->get('cat2')->get('sub2')->get('course2')->add('section 3', null, navigation_node::TYPE_COURSE, null, 'sec3');
375 $this->node->get('cat2')->get('sub2')->get('course2')->get('sec2')->add('activity 1', null, navigation_node::TYPE_ACTIVITY, null, 'act1');
376 $this->node->get('cat2')->get('sub2')->get('course2')->get('sec2')->add('activity 2', null, navigation_node::TYPE_ACTIVITY, null, 'act2');
377 $this->node->get('cat2')->get('sub2')->get('course2')->get('sec2')->add('activity 3', null, navigation_node::TYPE_ACTIVITY, null, 'act3');
378 $this->node->get('cat2')->get('sub2')->get('course2')->get('sec2')->add('resource 1', null, navigation_node::TYPE_RESOURCE, null, 'res1');
379 $this->node->get('cat2')->get('sub2')->get('course2')->get('sec2')->add('resource 2', null, navigation_node::TYPE_RESOURCE, null, 'res2');
380 $this->node->get('cat2')->get('sub2')->get('course2')->get('sec2')->add('resource 3', null, navigation_node::TYPE_RESOURCE, null, 'res3');
7d2a0492 381
382 $this->cache->clear();
3b7a763c 383 $this->cache->modinfo5 = unserialize($this->modinfo5);
384 $this->cache->coursesections5 = unserialize($this->coursesections5);
7d2a0492 385 $this->cache->canviewhiddenactivities = true;
386 $this->cache->canviewhiddensections = true;
387 $this->cache->canviewhiddencourses = true;
d972bad1 388 $this->node->get('cat2')->get('sub2')->add('Test Course 5', new moodle_url('http://moodle.org'),navigation_node::TYPE_COURSE,null,'5');
7d2a0492 389 }
390 public function test_add_categories() {
391 $categories = array();
392 for ($i=0;$i<3;$i++) {
393 $categories[$i] = new stdClass;
394 $categories[$i]->id = 'sub4_'.$i;
395 $categories[$i]->name = 'add_categories '.$i;
396 }
397 $this->node->exposed_add_categories(array('cat3'), $categories);
398 $this->assertEqual(count($this->node->get('cat3')->children), 3);
399 $this->assertIsA($this->node->get('cat3')->get('sub4_1'), 'navigation_node');
400 $this->node->get('cat3')->children = array();
401 }
402 public function test_add_course_section_generic() {
403 $keys = array('cat2', 'sub2', '5');
404 $course = new stdClass;
405 $course->id = '5';
3b7a763c 406 $course->numsections = 10;
407 $course->modinfo = $this->modinfo5;
7d2a0492 408 $this->node->add_course_section_generic($keys, $course, 'topic', 'topic');
409 $this->assertEqual(count($this->node->get_by_path($keys)->children),4);
410 }
411 public function test_add_category_by_path() {
412 $category = new stdClass;
413 $category->id = 'sub3';
414 $category->name = 'Sub category 3';
415 $category->path = '/cat2/sub3';
416 $this->node->exposed_add_category_by_path($category);
417 $this->assertIsA($this->node->get('cat2')->get('sub3'), 'navigation_node');
418 }
95b97515 419
420 public function test_load_course_categories() {
421 global $PAGE;
422 $originalcategories = $PAGE->categories;
423 $PAGE->categories = array();
424 $PAGE->categories[0] = new stdClass;
425 $PAGE->categories[0]->id = 130;
426 $PAGE->categories[0]->name = 'category1';
427 $PAGE->categories[1] = new stdClass;
428 $PAGE->categories[1]->id = 131;
429 $PAGE->categories[1]->name = 'category0';
430 $test = new exposed_global_navigation();
431 $keys = array();
432 $keys[] = $test->add('base level', null, null, null, 'base');
433 $catcount = $test->exposed_load_course_categories($keys);
434 $this->assertIsA($test->get('base'), 'navigation_node');
435 $this->assertIsA($test->get('base')->get(131), 'navigation_node');
436 $this->assertIsA($test->get('base')->get(131)->get(130), 'navigation_node');
437 $PAGE->categories = $originalcategories;
438 }
439
440 public function test_init() {
441 global $PAGE, $SITE;
442 $originalcontext = $PAGE->context;
443 $test = new mock_initialise_global_navigation();
444 // System
445 $PAGE->context->contextlevel = CONTEXT_SYSTEM;
446 $node1 = clone($test);
447 $node1->initialise();
448 $this->assertIsA($node1->get('initcall1'), 'navigation_node');
449 if ($node1->get('initcall1')) {
450 $this->assertEqual($node1->get('initcall1')->text, 'load_for_category');
451 }
452 // Course category
453 $PAGE->context->contextlevel = CONTEXT_COURSECAT;
454 $node2 = clone($test);
455 $node2->initialise();
456 $this->assertIsA($node2->get('initcall2'), 'navigation_node');
457 if ($node2->get('initcall2')) {
458 $this->assertEqual($node2->get('initcall2')->text, 'load_for_category');
459 }
460 $PAGE->context->contextlevel = CONTEXT_COURSE;
461 // For course (we need to adjust the site id so we look like a normal course
462 $SITE->id++;
463 $node3 = clone($test);
464 $node3->initialise();
465 $this->assertIsA($node3->get('initcall3'), 'navigation_node');
466 if ($node3->get('initcall3')) {
467 $this->assertEqual($node3->get('initcall3')->text, 'load_for_course');
468 }
469 $SITE->id--;
470 // Course is site
471 $node4 = clone($test);
472 $node4->initialise();
473 $this->assertIsA($node4->get('initcall4'), 'navigation_node');
474 if ($node4->get('initcall4')) {
475 $this->assertEqual($node4->get('initcall4')->text, 'load_for_category');
476 }
477 $PAGE->context->contextlevel = CONTEXT_MODULE;
478 $node5 = clone($test);
479 $node5->initialise();
480 $this->assertIsA($node5->get('initcall5'), 'navigation_node');
481 if ($node5->get('initcall5')) {
482 $this->assertEqual($node5->get('initcall5')->text, 'load_for_activity');
483 }
484 $PAGE->context->contextlevel = CONTEXT_BLOCK;
485 $node6 = clone($test);
486 $node6->initialise();
487 $this->assertIsA($node6->get('initcall6'), 'navigation_node');
488 if ($node6->get('initcall6')) {
489 $this->assertEqual($node6->get('initcall6')->text, 'load_for_course');
490 }
491 $PAGE->context->contextlevel = CONTEXT_USER;
492 $node7 = clone($test);
493 $node7->initialise();
494 $this->assertIsA($node7->get('initcall7'), 'navigation_node');
495 if ($node7->get('initcall7')) {
496 $this->assertEqual($node7->get('initcall7')->text, 'load_for_user');
497 }
498 $PAGE->context = $originalcontext;
499 }
500
7d2a0492 501 public function test_add_courses() {
502 $courses = array();
503 for ($i=0;$i<5;$i++) {
504 $course = new stdClass;
505 $course->id = $i;
506 $course->visible = true;
507 $course->category = 'cat3';
508 $course->fullname = "Test Course $i";
509 $course->shortname = "tcourse$i";
3b7a763c 510 $course->numsections = 10;
511 $course->modinfo = $this->modinfo5;
7d2a0492 512 $courses[$i] = $course;
513 }
8926f844 514
7d2a0492 515 $this->node->add_courses($courses);
516 $this->assertIsA($this->node->get('cat3')->get(0), 'navigation_node');
517 $this->assertIsA($this->node->get('cat3')->get(1), 'navigation_node');
518 $this->assertIsA($this->node->get('cat3')->get(2), 'navigation_node');
519 $this->assertIsA($this->node->get('cat3')->get(3), 'navigation_node');
520 $this->assertIsA($this->node->get('cat3')->get(4), 'navigation_node');
521 $this->node->get('cat3')->children = array();
522 }
523 public function test_can_display_type() {
524 $this->node->expansionlimit = navigation_node::TYPE_COURSE;
525 $this->assertTrue($this->node->exposed_can_display_type(navigation_node::TYPE_CATEGORY));
526 $this->assertTrue($this->node->exposed_can_display_type(navigation_node::TYPE_COURSE));
527 $this->assertFalse($this->node->exposed_can_display_type(navigation_node::TYPE_SECTION));
528 $this->node->expansionlimit = null;
529 }
530 public function test_content() {
531 $html1 = $this->node->content();
532 $this->node->expansionlimit = navigation_node::TYPE_CATEGORY;
533 $html2 = $this->node->content();
534 $this->node->expansionlimit = null;
535 $this->assert(new ContainsTagWithAttribute('a','href',$this->node->action->out()), $html1);
536 $this->assert(new ContainsTagWithAttribute('a','href',$this->node->action->out()), $html2);
537 }
538 public function test_format_display_course_content() {
539 $this->assertTrue($this->node->exposed_format_display_course_content('topic'));
540 $this->assertFalse($this->node->exposed_format_display_course_content('scorm'));
541 $this->assertTrue($this->node->exposed_format_display_course_content('dummy'));
542 }
7d2a0492 543 public function test_load_course_activities() {
544 $keys = array('cat2', 'sub2', '5');
545 $course = new stdClass;
546 $course->id = '5';
3b7a763c 547 $course->numsections = 10;
7d2a0492 548 $modinfo = $this->cache->modinfo5;
549 $modinfo->cms[290] = clone($modinfo->cms[288]);
550 $modinfo->cms[290]->id = 290;
551 $modinfo->cms[290]->modname = 'resource';
552 $modinfo->cms[290]->instance = 21;
553 $modinfo->instances['resource'] = array();
554 $modinfo->instances['resource'][21] = clone($modinfo->instances['forum'][19]);
555 $modinfo->instances['resource'][21]->id = 21;
556 $this->cache->modinfo5 = $modinfo;
3b7a763c 557 $course->modinfo = serialize($modinfo);
7d2a0492 558 $this->node->exposed_load_course_activities($keys, $course);
3b7a763c 559
7d2a0492 560 $this->assertIsA($this->node->get_by_path(array_merge($keys, array(288))), 'navigation_node');
561 $this->assertEqual($this->node->get_by_path(array_merge($keys, array(288)))->type, navigation_node::TYPE_ACTIVITY);
562 $this->assertIsA($this->node->get_by_path(array_merge($keys, array(290))), 'navigation_node');
563 $this->assertEqual($this->node->get_by_path(array_merge($keys, array(290)))->type, navigation_node::TYPE_RESOURCE);
564 }
565 public function test_load_course_sections() {
566 $keys = array('cat2', 'sub2', '5');
567 $course = new stdClass;
568 $course->id = '5';
569 $course->format = 'topics';
3b7a763c 570 $course->numsections = 10;
571 $course->modinfo = $this->modinfo5;
7d2a0492 572 $coursechildren = $this->node->get_by_path($keys)->children;
8926f844 573
7d2a0492 574 $this->node->get_by_path(array('cat2', 'sub2', '5'))->children = array();
575 $this->node->exposed_load_course_sections($keys, $course);
576
577 $course->format = 'topics';
578 $this->node->get_by_path(array('cat2', 'sub2', '5'))->children = array();
579 $this->node->exposed_load_course_sections($keys, $course);
580
581 $course->format = 'scorm';
582 $this->node->get_by_path(array('cat2', 'sub2', '5'))->children = array();
583 $this->node->exposed_load_course_sections($keys, $course);
584
585 $course->format = 'sillywilly';
586 $this->node->get_by_path(array('cat2', 'sub2', '5'))->children = array();
587 $this->node->exposed_load_course_sections($keys, $course);
588
589 $this->node->get_by_path($keys)->children = $coursechildren;
590 }
591 public function test_load_for_user() {
592 $this->node->exposed_load_for_user();
593 }
594 public function test_load_section_activities() {
595 $keys = array('cat2', 'sub2', '5');
596 $course = new stdClass;
597 $course->id = '5';
3b7a763c 598 $course->numsections = 10;
d972bad1 599 $this->node->get_by_path($keys)->add('Test Section 1', null, navigation_node::TYPE_SECTION, null, $this->cache->coursesections5[1]->id);
7d2a0492 600 $modinfo = $this->cache->modinfo5;
601 $modinfo->sections[1] = array(289, 290);
602 $modinfo->cms[289] = clone($modinfo->cms[288]);
603 $modinfo->cms[289]->id = 289;
604 $modinfo->cms[289]->sectionnum = 1;
605 $modinfo->cms[290]->modname = 'forum';
606 $modinfo->cms[289]->instance = 20;
607 $modinfo->cms[290] = clone($modinfo->cms[288]);
608 $modinfo->cms[290]->id = 290;
609 $modinfo->cms[290]->modname = 'resource';
610 $modinfo->cms[290]->sectionnum = 1;
611 $modinfo->cms[290]->instance = 21;
612 $modinfo->instances['forum'][20] = clone($modinfo->instances['forum'][19]);
613 $modinfo->instances['forum'][20]->id = 20;
614 $modinfo->instances['resource'] = array();
615 $modinfo->instances['resource'][21] = clone($modinfo->instances['forum'][19]);
616 $modinfo->instances['resource'][21]->id = 21;
617 $this->cache->modinfo5 = $modinfo;
3b7a763c 618 $course->modinfo = serialize($modinfo);
7d2a0492 619 $this->node->exposed_load_section_activities($keys, 1, $course);
620 $keys[] = 97;
621 $this->assertIsA($this->node->get_by_path(array_merge($keys, array(289))),'navigation_node');
622 $this->assertEqual($this->node->get_by_path(array_merge($keys, array(289)))->type, navigation_node::TYPE_ACTIVITY);
623 $this->assertIsA($this->node->get_by_path(array_merge($keys, array(290))),'navigation_node');
624 $this->assertEqual($this->node->get_by_path(array_merge($keys, array(290)))->type, navigation_node::TYPE_RESOURCE);
625 }
626 public function test_module_extends_navigation() {
627 $this->cache->test1_extends_navigation = true;
628 $this->cache->test2_extends_navigation = false;
629 $this->assertTrue($this->node->exposed_module_extends_navigation('forum'));
630 $this->assertTrue($this->node->exposed_module_extends_navigation('test1'));
631 $this->assertFalse($this->node->exposed_module_extends_navigation('test2'));
632 $this->assertFalse($this->node->exposed_module_extends_navigation('test3'));
633 }
634}
635
95b97515 636class mock_initialise_limited_global_navigation extends limited_global_navigation {
637
638 public function load_categories($id) {
639 $this->add('load_for_category_'.$id, null, null, null, 'initcall');
640 return 0;
641 }
642
643 public function load_course($id) {
644 $this->add('load_for_course_'.$id, null, null, null, 'initcall');
645 return 0;
646 }
647
648 public function load_activity($id) {
649 $this->add('load_for_activity_'.$id, null, null, null, 'initcall');
650 return 0;
651 }
652
653 public function load_section($id) {
654 $this->add('load_for_section_'.$id, null, null, null, 'initcall');
655 return 0;
656 }
657}
658
659/**
660 * This is a dummy object that allows us to call protected methods within the
661 * global navigation class by prefixing the methods with `exposed_`
662 */
663class exposed_limited_global_navigation extends limited_global_navigation {
664 protected $exposedkey = 'exposed_';
665 function __construct() {
666 parent::__construct();
667 $this->cache = new navigation_cache('simpletest_nav');
668 }
669 function __call($method, $arguments) {
670 if (strpos($method,$this->exposedkey) !== false) {
671 $method = substr($method, strlen($this->exposedkey));
672 }
673 if (method_exists($this, $method)) {
674 return call_user_func_array(array($this, $method), $arguments);
675 }
676 throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
677 }
678}
679
680class limited_global_navigation_test extends UnitTestCase {
681
682 public $mocknode;
683 public $exposednode;
684
685 public function setUp() {
686 $this->mocknode = new mock_initialise_limited_global_navigation();
687 $this->exposednode = new exposed_limited_global_navigation();
688 }
689
690 public function test_initialise() {
691 $node1 = clone($this->mocknode);
692 $node1->initialise(navigation_node::TYPE_ACTIVITY, 1);
693 $this->assertIsA($node1->get('initcall'), 'navigation_node');
694 if ($node1->get('initcall')) {
695 $this->assertEqual($node1->get('initcall')->text, 'load_for_activity_1');
696 }
697
2ffd5c35 698 /* Disabled as there is a context check occuring
95b97515 699 $node2 = clone($this->mocknode);
ab5bd34d 700 $fakecontext = new stdClass;
701 $this->cache->coursecontext2 = $fakecontext;
95b97515 702 $node2->initialise(navigation_node::TYPE_CATEGORY, 2);
703 $this->assertIsA($node2->get('initcall'), 'navigation_node');
704 if ($node2->get('initcall')) {
705 $this->assertEqual($node2->get('initcall')->text, 'load_for_category_2');
706 }
2ffd5c35 707 */
95b97515 708
709 $node3 = clone($this->mocknode);
710 $node3->initialise(navigation_node::TYPE_COURSE, 3);
711 $this->assertIsA($node3->get('initcall'), 'navigation_node');
712 if ($node3->get('initcall')) {
713 $this->assertEqual($node3->get('initcall')->text, 'load_for_course_3');
714 }
715
716 $node4 = clone($this->mocknode);
717 $node4->initialise(navigation_node::TYPE_SECTION, 4);
718 $this->assertIsA($node4->get('initcall'), 'navigation_node');
719 if ($node4->get('initcall')) {
720 $this->assertEqual($node4->get('initcall')->text, 'load_for_section_4');
721 }
8926f844 722
95b97515 723 $node5 = clone($this->mocknode);
724 $node5->initialise(navigation_node::TYPE_RESOURCE, 5);
725 $this->assertFalse($node5->get('initcall'));
726
727 $node6 = clone($this->mocknode);
728 $node6->initialise(navigation_node::TYPE_SETTING, 6);
729 $this->assertFalse($node5->get('initcall'));
730
731 $node7 = clone($this->mocknode);
732 $node7->initialise(navigation_node::TYPE_SYSTEM, 7);
733 $this->assertFalse($node5->get('initcall'));
734 }
735
736}
737
7d2a0492 738/**
739 * This is a dummy object that allows us to call protected methods within the
740 * global navigation class by prefixing the methods with `exposed_`
741 */
742class exposed_navbar extends navbar {
743 protected $exposedkey = 'exposed_';
744 function __construct() {
745 global $PAGE;
746 parent::__construct($PAGE);
747 $this->cache = new navigation_cache('simpletest_nav');
748 }
749 function __call($method, $arguments) {
750 if (strpos($method,$this->exposedkey) !== false) {
751 $method = substr($method, strlen($this->exposedkey));
752 }
753 if (method_exists($this, $method)) {
754 return call_user_func_array(array($this, $method), $arguments);
755 }
756 throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
757 }
758}
759
760class navbar_test extends UnitTestCase {
761 protected $node;
762 protected $oldnav;
763
764 public static $includecoverage = array('./lib/navigationlib.php');
765 public static $excludecoverage = array();
766
767 public function setUp() {
768 global $PAGE;
769 $this->oldnav = $PAGE->navigation;
770 $this->cache = new navigation_cache('simpletest_nav');
771 $this->node = new exposed_navbar();
772 $temptree = new global_navigation_test();
773 $temptree->setUp();
774 $temptree->node->get_by_path(array('cat2','sub2', 'course2'))->make_active();
775 $PAGE->navigation = $temptree->node;
776 }
777 public function tearDown() {
778 global $PAGE;
779 $PAGE->navigation = $this->oldnav;
780 }
781 public function test_add() {
782 global $CFG;
783 // Add a node with all args set
d972bad1 784 $this->node->add('test_add_1','http://www.moodle.org/',navigation_node::TYPE_COURSE,'testadd1','testadd1',$CFG->httpswwwroot . '/pix/i/course.gif');
7d2a0492 785 // Add a node with the minimum args required
786 $key2 = $this->node->add('test_add_2');
787 $this->assertIsA($this->node->get('testadd1'), 'navigation_node');
788 $this->assertIsA($this->node->get('testadd1')->get($key2), 'navigation_node');
789 }
790 public function test_content() {
c705a24e 791 $this->assertTrue( (strpos($this->node->content(), $this->node->action->out()) !== false) );
7d2a0492 792 }
793 public function test_has_items() {
794 global $PAGE;
795 $this->assertTrue($this->node->has_items());
7d2a0492 796 }
797 public function test_parse_branch_to_html() {
798 global $CFG;
d972bad1 799 $key = $this->node->add('test_add_1','http://www.moodle.org/',navigation_node::TYPE_COURSE,'testadd1','testadd1',$CFG->httpswwwroot . '/pix/i/course.gif');
7d2a0492 800 $this->node->get($key)->make_active();
b3b3eace 801 $html = $this->node->exposed_parse_branch_to_html($this->node->children, true, true);
c705a24e 802 $this->assertTrue( (strpos($html, $this->node->action->out()) !== false) );
7d2a0492 803 }
804}
805
806class navigation_cache_test extends UnitTestCase {
807 protected $cache;
808
809 public static $includecoverage = array('./lib/navigationlib.php');
810 public static $excludecoverage = array();
811
812 public function setUp() {
813 $this->cache = new navigation_cache('simpletest_nav');
814 $this->cache->anysetvariable = true;
815 }
816 public function test___get() {
817 $this->assertTrue($this->cache->anysetvariable);
818 $this->assertEqual($this->cache->notasetvariable, null);
819 }
820 public function test___set() {
821 $this->cache->myname = 'Sam Hemelryk';
822 $this->assertTrue($this->cache->cached('myname'));
823 $this->assertEqual($this->cache->myname, 'Sam Hemelryk');
824 }
825 public function test_cached() {
826 $this->assertTrue($this->cache->cached('anysetvariable'));
827 $this->assertFalse($this->cache->cached('notasetvariable'));
828 }
829 public function test_clear() {
830 $cache = clone($this->cache);
831 $this->assertTrue($cache->cached('anysetvariable'));
832 $cache->clear();
833 $this->assertFalse($cache->cached('anysetvariable'));
834 }
835 public function test_set() {
836 $this->cache->set('software', 'Moodle');
837 $this->assertTrue($this->cache->cached('software'));
838 $this->assertEqual($this->cache->software, 'Moodle');
839 }
840}
841
842/**
843 * This is a dummy object that allows us to call protected methods within the
844 * global navigation class by prefixing the methods with `exposed_`
845 */
846class exposed_settings_navigation extends settings_navigation {
847 protected $exposedkey = 'exposed_';
848 function __construct() {
849 global $PAGE;
850 parent::__construct($PAGE);
851 $this->cache = new navigation_cache('simpletest_nav');
852 }
853 function __call($method, $arguments) {
854 if (strpos($method,$this->exposedkey) !== false) {
855 $method = substr($method, strlen($this->exposedkey));
856 }
857 if (method_exists($this, $method)) {
858 return call_user_func_array(array($this, $method), $arguments);
859 }
860 throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
861 }
862}
863
864class settings_navigation_test extends UnitTestCase {
865 protected $node;
866 protected $cache;
867
868 public static $includecoverage = array('./lib/navigationlib.php');
869 public static $excludecoverage = array();
870
871 public function setUp() {
872 global $PAGE;
873 $this->cache = new navigation_cache('simpletest_nav');
874 $this->node = new exposed_settings_navigation();
875 }
876 public function test___construct() {
877 $this->node = new exposed_settings_navigation();
878 }
879 public function test___initialise() {
880 $this->node->initialise();
881 $this->assertEqual($this->node->id, 'settingsnav');
882 }
883 public function test_load_front_page_settings() {
884 $this->node->exposed_load_front_page_settings();
885 $settings = false;
886 foreach ($this->node->children as $child) {
887 if ($child->id === 'frontpagesettings') {
888 $settings = $child;
889 }
890 }
891 $this->assertIsA($settings, 'navigation_node');
892 }
893 public function test_in_alternative_role() {
894 $this->assertFalse($this->node->exposed_in_alternative_role());
895 }
896 public function test_remove_empty_root_branches() {
d972bad1 897 $this->node->add('rootbranch1', null, navigation_node::TYPE_SETTING, null, 'rootbranch1');
898 $this->node->add('rootbranch2', null, navigation_node::TYPE_SETTING, null, 'rootbranch2');
899 $this->node->add('rootbranch3', null, navigation_node::TYPE_SETTING, null, 'rootbranch3');
900 $this->node->get('rootbranch2')->add('something', null, navigation_node::TYPE_SETTING);
7d2a0492 901 $this->node->remove_empty_root_branches();
902 $this->assertFalse($this->node->get('rootbranch1'));
903 $this->assertIsA($this->node->get('rootbranch2'), 'navigation_node');
904 $this->assertFalse($this->node->get('rootbranch3'));
905 }
95b97515 906}