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