navigation MDL-14632 Very significant navigation commit
[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();
35
36 public function setUp() {
37 global $CFG, $FULLME;
38 parent::setUp();
39 $oldfullme = $FULLME;
40 $FULLME = 'http://www.moodle.org/test.php';
41 $this->node = new navigation_node('Test Node');
42 $this->node->type = navigation_node::TYPE_SYSTEM;
43 $this->node->add('demo1', null, 'demo1', navigation_node::TYPE_COURSE, 'http://www.moodle.org/',$CFG->httpswwwroot . '/pix/i/course.gif');
44 $this->node->add('demo2', null, 'demo2', navigation_node::TYPE_COURSE, 'http://www.moodle.com/',$CFG->httpswwwroot . '/pix/i/course.gif');
45 $this->node->add('demo3', null, 'demo3', navigation_node::TYPE_CATEGORY, 'http://www.moodle.org/',$CFG->httpswwwroot . '/pix/i/course.gif');
46 $this->node->get('demo3')->add('demo4', null, 'demo4', navigation_node::TYPE_COURSE, new moodle_url('http://www.moodle.org/'),$CFG->httpswwwroot . '/pix/i/course.gif');
47 $this->node->get('demo3')->add('demo5', null, 'demo5', navigation_node::TYPE_COURSE, 'http://www.moodle.org/test.php',$CFG->httpswwwroot . '/pix/i/course.gif');
48 $this->node->get('demo3')->get('demo5')->make_active();
49 $this->node->get('demo3')->get('demo5')->add('activity1', null, 'activity1',navigation_node::TYPE_ACTIVITY);
50 $this->node->get('demo3')->get('demo5')->get('activity1')->make_active();
51 $this->node->add('hiddendemo1', null, 'hiddendemo1', navigation_node::TYPE_CATEGORY, 'http://www.moodle.org/',$CFG->httpswwwroot . '/pix/i/course.gif');
52 $this->node->get('hiddendemo1')->hidden = true;
53 $this->node->get('hiddendemo1')->add('hiddendemo2', null, 'hiddendemo2', navigation_node::TYPE_COURSE, new moodle_url('http://www.moodle.org/'),$CFG->httpswwwroot . '/pix/i/course.gif');
54 $this->node->get('hiddendemo1')->add('hiddendemo3', null, 'hiddendemo3', navigation_node::TYPE_COURSE, new moodle_url('http://www.moodle.org/'),$CFG->httpswwwroot . '/pix/i/course.gif');
55 $this->node->get('hiddendemo1')->get('hiddendemo2')->helpbutton = 'Here is a help button';
56 $this->node->get('hiddendemo1')->get('hiddendemo3')->display = false;
57 $FULLME = $oldfullme;
58 }
59 public function test___construct() {
60 global $CFG;
61 $properties = array();
62 $properties['text'] = 'text';
63 $properties['shorttext'] = 'shorttext';
64 $properties['key'] = 'key';
65 $properties['type'] = 'navigation_node::TYPE_COURSE';
66 $properties['action'] = 'http://www.moodle.org/';
67 $properties['icon'] = $CFG->httpswwwroot . '/pix/i/course.gif';
68 $node = new navigation_node($properties);
69 $this->assertEqual($node->text, $properties['text']);
70 $this->assertEqual($node->title, $properties['text']);
71 $this->assertEqual($node->shorttext, $properties['shorttext']);
72 $this->assertEqual($node->key, $properties['key']);
73 $this->assertEqual($node->type, $properties['type']);
74 $this->assertEqual($node->action, $properties['action']);
75 $this->assertEqual($node->icon, $properties['icon']);
76 }
77 public function test_add() {
78 global $CFG;
79 // Add a node with all args set
80 $key1 = $this->node->add('test_add_1','testadd1','key',navigation_node::TYPE_COURSE,'http://www.moodle.org/',$CFG->httpswwwroot . '/pix/i/course.gif');
81 // Add a node with the minimum args required
82 $key2 = $this->node->add('test_add_2','testadd2');
83 $key3 = $this->node->add(str_repeat('moodle ', 15),str_repeat('moodle', 15));
84 $this->assertEqual('key',$key1);
85 $this->assertEqual($key2, $this->node->get($key2)->key);
86 $this->assertEqual($key3, $this->node->get($key3)->key);
87 $this->assertIsA($this->node->get('key'), 'navigation_node');
88 $this->assertIsA($this->node->get($key2), 'navigation_node');
89 $this->assertIsA($this->node->get($key3), 'navigation_node');
90 }
91
92 public function test_add_class() {
93 $node = $this->node->get('demo1');
94 $this->assertIsA($node, 'navigation_node');
95 if ($node !== false) {
96 $node->add_class('myclass');
97 $classes = $node->classes;
98 $this->assertTrue(in_array('myclass', $classes));
99 }
100 }
101
102 public function test_add_to_path() {
103 global $CFG;
104 $path = array('demo3','demo5');
105 $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');
106 $this->assertEqual($key1, 'testatp1');
107
108 // This should generate an exception as we have not provided any text for
109 // the node
110 $this->expectException();
111 $key3 = $this->node->add_to_path(array('demo3','dud1','dud2'), 'text', 'shorttext');
112 $this->assertFalse($key3);
113
114 // This should generate an exception as we have not provided any text for
115 // the node
116 $this->expectException(new coding_exception('You must set the text for the node when you create it.'));
117 $key2 = $this->node->add_to_path($path);
118 }
119
120 public function test_check_if_active() {
121 global $FULLME;
122 $oldfullme = $FULLME;
123
124 // First test the string urls
125 $FULLME = 'http://www.moodle.org/';
126 // demo1 -> action is http://www.moodle.org/, thus should be true
127 $this->assertTrue($this->node->get('demo1')->check_if_active());
128 // demo2 -> action is http://www.moodle.com/, thus should be false
129 $this->assertFalse($this->node->get('demo2')->check_if_active());
130
131 $FULLME = $oldfullme;
132 }
133
134 public function test_contains_active_node() {
135 // demo5, and activity1 were set to active during setup
136 // Should be true as it contains all nodes
137 $this->assertTrue($this->node->contains_active_node());
138 // Should be true as demo5 is a child of demo3
139 $this->assertTrue($this->node->get('demo3')->contains_active_node());
140 // Obviously duff
141 $this->assertFalse($this->node->get('demo1')->contains_active_node());
142 // Should be true as demo5 contains activity1
143 $this->assertTrue($this->node->get('demo3')->get('demo5')->contains_active_node());
144 // Should be false activity1 doesnt contain the active node... it is the active node
145 $this->assertFalse($this->node->get('demo3')->get('demo5')->get('activity1')->contains_active_node());
146 // Obviously duff
147 $this->assertFalse($this->node->get('demo3')->get('demo4')->contains_active_node());
148 }
149
150 public function test_content() {
151 $content1 = $this->node->get('demo1')->content();
152 $content2 = $this->node->get('demo3')->content();
153 $content3 = $this->node->get('demo3')->get('demo5')->content();
154 $content4 = $this->node->get('hiddendemo1')->get('hiddendemo2')->content();
155 $content5 = $this->node->get('hiddendemo1')->get('hiddendemo3')->content();
156 $this->assert(new ContainsTagWithAttribute('a','href',$this->node->get('demo1')->action), $content1);
157 $this->assert(new ContainsTagWithAttribute('a','href',$this->node->get('demo3')->action), $content2);
158 $this->assert(new ContainsTagWithAttribute('a','href',$this->node->get('demo3')->get('demo5')->action), $content3);
159 $this->assert(new ContainsTagWithAttribute('a','href',$this->node->get('hiddendemo1')->get('hiddendemo2')->action->out()), $content4);
160 $this->assertTrue(empty($content5));
161 }
162
163 public function test_find_active_node() {
164 $activenode1 = $this->node->find_active_node();
165 $activenode2 = $this->node->find_active_node(navigation_node::TYPE_COURSE);
166 $activenode3 = $this->node->find_active_node(navigation_node::TYPE_CATEGORY);
167 $activenode4 = $this->node->get('demo1')->find_active_node(navigation_node::TYPE_COURSE);
168 $this->assertIsA($activenode1, 'navigation_node');
169 if ($activenode1 instanceof navigation_node) {
170 $this->assertEqual($activenode1, $this->node->get('demo3')->get('demo5'));
171 }
172 $this->assertIsA($activenode2, 'navigation_node');
173 if ($activenode1 instanceof navigation_node) {
174 $this->assertEqual($activenode2, $this->node->get('demo3')->get('demo5'));
175 }
176 $this->assertIsA($activenode3, 'navigation_node');
177 if ($activenode1 instanceof navigation_node) {
178 $this->assertEqual($activenode3, $this->node->get('demo3'));
179 }
180 $this->assertNotA($activenode4, 'navigation_node');
181 }
182
183 public function test_find_child() {
184 $node1 = $this->node->find_child('demo1', navigation_node::TYPE_COURSE);
185 $node2 = $this->node->find_child('demo5', navigation_node::TYPE_COURSE);
186 $node3 = $this->node->find_child('demo5', navigation_node::TYPE_CATEGORY);
187 $node4 = $this->node->find_child('demo0', navigation_node::TYPE_COURSE);
188 $this->assertIsA($node1, 'navigation_node');
189 $this->assertIsA($node2, 'navigation_node');
190 $this->assertNotA($node3, 'navigation_node');
191 $this->assertNotA($node4, 'navigation_node');
192 }
193
194 public function test_find_child_depth() {
195 $depth1 = $this->node->find_child_depth('demo1',navigation_node::TYPE_COURSE);
196 $depth2 = $this->node->find_child_depth('demo5',navigation_node::TYPE_COURSE);
197 $depth3 = $this->node->find_child_depth('demo5',navigation_node::TYPE_CATEGORY);
198 $depth4 = $this->node->find_child_depth('demo0',navigation_node::TYPE_COURSE);
199 $this->assertEqual(1, $depth1);
200 $this->assertEqual(1, $depth2);
201 $this->assertFalse($depth3);
202 $this->assertFalse($depth4);
203 }
204
205 public function test_find_expandable() {
206 $expandable = array();
207 $this->node->find_expandable($expandable);
208 $this->assertEqual(count($expandable), 5);
209 if (count($expandable) === 5) {
210 $name = $expandable[0]['branchid'];
211 $name .= $expandable[1]['branchid'];
212 $name .= $expandable[2]['branchid'];
213 $name .= $expandable[3]['branchid'];
214 $name .= $expandable[4]['branchid'];
215 $this->assertEqual($name, 'demo1demo2demo4hiddendemo2hiddendemo3');
216 }
217 }
218
219 public function test_get() {
220 $node1 = $this->node->get('demo1'); // Exists
221 $node2 = $this->node->get('demo4'); // Doesn't exist for this node
222 $node3 = $this->node->get('demo0'); // Doesn't exist at all
223 $node4 = $this->node->get(false); // Sometimes occurs in nature code
224 $this->assertIsA($node1, 'navigation_node');
225 $this->assertFalse($node2);
226 $this->assertFalse($node3);
227 $this->assertFalse($node4);
228 }
229
230 public function test_get_by_path() {
231 $node1 = $this->node->get_by_path(array('demo3', 'demo4')); // This path exists and should return a node
232 $node2 = $this->node->get_by_path(array('demo1', 'demo2')); // Both elements exist but demo2 is not a child of demo1
233 $node3 = $this->node->get_by_path(array('demo0', 'demo6')); // This path is totally bogus
234 $this->assertIsA($node1, 'navigation_node');
235 $this->assertFalse($node2);
236 $this->assertFalse($node3);
237 }
238
239 public function test_get_css_type() {
240 $csstype1 = $this->node->get('demo3')->get_css_type();
241 $csstype2 = $this->node->get('demo3')->get('demo5')->get_css_type();
242 $this->node->get('demo3')->get('demo5')->type = 1000;
243 $csstype3 = $this->node->get('demo3')->get('demo5')->get_css_type();
244 $this->assertEqual($csstype1, 'type_category');
245 $this->assertEqual($csstype2, 'type_course');
246 $this->assertEqual($csstype3, 'type_unknown');
247 }
248
249 public function test_make_active() {
250 global $CFG;
251 $key1 = $this->node->add('active node 1', null, 'anode1');
252 $key2 = $this->node->add('active node 2', null, 'anode2', navigation_node::TYPE_COURSE, new moodle_url($CFG->wwwroot));
253 $this->node->get($key1)->make_active();
254 $this->node->get($key2)->make_active();
255 $this->assertTrue($this->node->get($key1)->isactive);
256 $this->assertTrue($this->node->get($key2)->isactive);
257 }
258
259 public function test_reiterate_active_nodes() {
260 global $FULLME;
261 $oldfullme = $FULLME;
262 $FULLME = 'http://www.moodle.org/test.php';
263 $cachenode = serialize($this->node);
264 $cachenode = unserialize($cachenode);
265 $this->assertFalse($cachenode->get('demo3')->get('demo5')->isactive);
266 $this->assertTrue($cachenode->reiterate_active_nodes());
267 $this->assertTrue($cachenode->get('demo3')->get('demo5')->isactive);
268 $FULLME = $oldfullme;
269 }
270 public function test_remove_child() {
271 $this->node->add('child to remove 1', null, 'remove1');
272 $this->node->add('child to remove 2', null, 'remove2');
273 $this->node->get('remove2')->add('child to remove 3', null, 'remove3');
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
319class global_navigation_test extends UnitTestCase {
320 /**
321 * @var global_navigation
322 */
323 public $node;
324 protected $cache;
325 public static $includecoverage = array('./lib/navigationlib.php');
326 public static $excludecoverage = array();
327
328 public function setUp() {
329 $this->cache = new navigation_cache('simpletest_nav');
330 $this->node = new exposed_global_navigation();
331 // Create an initial tree structure to work with
332 $this->node->add('category 1', null, 'cat1', navigation_node::TYPE_CATEGORY);
333 $this->node->add('category 2', null, 'cat2', navigation_node::TYPE_CATEGORY);
334 $this->node->add('category 3', null, 'cat3', navigation_node::TYPE_CATEGORY);
335 $this->node->get('cat2')->add('sub category 1', null, 'sub1', navigation_node::TYPE_CATEGORY);
336 $this->node->get('cat2')->add('sub category 2', null, 'sub2', navigation_node::TYPE_CATEGORY);
337 $this->node->get('cat2')->add('sub category 3', null, 'sub3', navigation_node::TYPE_CATEGORY);
338 $this->node->get('cat2')->get('sub2')->add('course 1', null, 'course1', navigation_node::TYPE_COURSE);
339 $this->node->get('cat2')->get('sub2')->add('course 2', null, 'course2', navigation_node::TYPE_COURSE);
340 $this->node->get('cat2')->get('sub2')->add('course 3', null, 'course3', navigation_node::TYPE_COURSE);
341 $this->node->get('cat2')->get('sub2')->get('course2')->add('section 1', null, 'sec1', navigation_node::TYPE_COURSE);
342 $this->node->get('cat2')->get('sub2')->get('course2')->add('section 2', null, 'sec2', navigation_node::TYPE_COURSE);
343 $this->node->get('cat2')->get('sub2')->get('course2')->add('section 3', null, 'sec3', navigation_node::TYPE_COURSE);
344 $this->node->get('cat2')->get('sub2')->get('course2')->get('sec2')->add('activity 1', null, 'act1', navigation_node::TYPE_ACTIVITY);
345 $this->node->get('cat2')->get('sub2')->get('course2')->get('sec2')->add('activity 2', null, 'act2', navigation_node::TYPE_ACTIVITY);
346 $this->node->get('cat2')->get('sub2')->get('course2')->get('sec2')->add('activity 3', null, 'act3', navigation_node::TYPE_ACTIVITY);
347 $this->node->get('cat2')->get('sub2')->get('course2')->get('sec2')->add('resource 1', null, 'res1', navigation_node::TYPE_RESOURCE);
348 $this->node->get('cat2')->get('sub2')->get('course2')->get('sec2')->add('resource 2', null, 'res2', navigation_node::TYPE_RESOURCE);
349 $this->node->get('cat2')->get('sub2')->get('course2')->get('sec2')->add('resource 3', null, 'res3', navigation_node::TYPE_RESOURCE);
350
351 $this->cache->clear();
352 $this->cache->modinfo5 = unserialize('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;}');
353 $this->cache->coursesections5 = unserialize('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";}}');
354 $this->cache->canviewhiddenactivities = true;
355 $this->cache->canviewhiddensections = true;
356 $this->cache->canviewhiddencourses = true;
357 $this->node->get('cat2')->get('sub2')->add('Test Course 5',null,'5',navigation_node::TYPE_COURSE, new moodle_url('http://moodle.org'));
358 }
359 public function test_add_categories() {
360 $categories = array();
361 for ($i=0;$i<3;$i++) {
362 $categories[$i] = new stdClass;
363 $categories[$i]->id = 'sub4_'.$i;
364 $categories[$i]->name = 'add_categories '.$i;
365 }
366 $this->node->exposed_add_categories(array('cat3'), $categories);
367 $this->assertEqual(count($this->node->get('cat3')->children), 3);
368 $this->assertIsA($this->node->get('cat3')->get('sub4_1'), 'navigation_node');
369 $this->node->get('cat3')->children = array();
370 }
371 public function test_add_course_section_generic() {
372 $keys = array('cat2', 'sub2', '5');
373 $course = new stdClass;
374 $course->id = '5';
375 $this->node->add_course_section_generic($keys, $course, 'topic', 'topic');
376 $this->assertEqual(count($this->node->get_by_path($keys)->children),4);
377 }
378 public function test_add_category_by_path() {
379 $category = new stdClass;
380 $category->id = 'sub3';
381 $category->name = 'Sub category 3';
382 $category->path = '/cat2/sub3';
383 $this->node->exposed_add_category_by_path($category);
384 $this->assertIsA($this->node->get('cat2')->get('sub3'), 'navigation_node');
385 }
386 public function test_add_courses() {
387 $courses = array();
388 for ($i=0;$i<5;$i++) {
389 $course = new stdClass;
390 $course->id = $i;
391 $course->visible = true;
392 $course->category = 'cat3';
393 $course->fullname = "Test Course $i";
394 $course->shortname = "tcourse$i";
395 $courses[$i] = $course;
396 }
397
398 $this->node->add_courses($courses);
399 $this->assertIsA($this->node->get('cat3')->get(0), 'navigation_node');
400 $this->assertIsA($this->node->get('cat3')->get(1), 'navigation_node');
401 $this->assertIsA($this->node->get('cat3')->get(2), 'navigation_node');
402 $this->assertIsA($this->node->get('cat3')->get(3), 'navigation_node');
403 $this->assertIsA($this->node->get('cat3')->get(4), 'navigation_node');
404 $this->node->get('cat3')->children = array();
405 }
406 public function test_can_display_type() {
407 $this->node->expansionlimit = navigation_node::TYPE_COURSE;
408 $this->assertTrue($this->node->exposed_can_display_type(navigation_node::TYPE_CATEGORY));
409 $this->assertTrue($this->node->exposed_can_display_type(navigation_node::TYPE_COURSE));
410 $this->assertFalse($this->node->exposed_can_display_type(navigation_node::TYPE_SECTION));
411 $this->node->expansionlimit = null;
412 }
413 public function test_content() {
414 $html1 = $this->node->content();
415 $this->node->expansionlimit = navigation_node::TYPE_CATEGORY;
416 $html2 = $this->node->content();
417 $this->node->expansionlimit = null;
418 $this->assert(new ContainsTagWithAttribute('a','href',$this->node->action->out()), $html1);
419 $this->assert(new ContainsTagWithAttribute('a','href',$this->node->action->out()), $html2);
420 }
421 public function test_format_display_course_content() {
422 $this->assertTrue($this->node->exposed_format_display_course_content('topic'));
423 $this->assertFalse($this->node->exposed_format_display_course_content('scorm'));
424 $this->assertTrue($this->node->exposed_format_display_course_content('dummy'));
425 }
426 public function test_load_course() {
427 $course = new stdClass;
428 $course->id = 'tcourse10';
429 $course->fullname = 'Test Course 10';
430 $course->shortname = 'tcourse10';
431 $course->visible = true;
432 $this->node->exposed_load_course(array('cat2','sub3'), $course);
433 $this->assertIsA($this->node->get('cat2')->get('sub3')->get('tcourse10'), 'navigation_node');
434 }
435 public function test_load_course_activities() {
436 $keys = array('cat2', 'sub2', '5');
437 $course = new stdClass;
438 $course->id = '5';
439 $modinfo = $this->cache->modinfo5;
440 $modinfo->cms[290] = clone($modinfo->cms[288]);
441 $modinfo->cms[290]->id = 290;
442 $modinfo->cms[290]->modname = 'resource';
443 $modinfo->cms[290]->instance = 21;
444 $modinfo->instances['resource'] = array();
445 $modinfo->instances['resource'][21] = clone($modinfo->instances['forum'][19]);
446 $modinfo->instances['resource'][21]->id = 21;
447 $this->cache->modinfo5 = $modinfo;
448 $this->node->exposed_load_course_activities($keys, $course);
449 $this->assertIsA($this->node->get_by_path(array_merge($keys, array(288))), 'navigation_node');
450 $this->assertEqual($this->node->get_by_path(array_merge($keys, array(288)))->type, navigation_node::TYPE_ACTIVITY);
451 $this->assertIsA($this->node->get_by_path(array_merge($keys, array(290))), 'navigation_node');
452 $this->assertEqual($this->node->get_by_path(array_merge($keys, array(290)))->type, navigation_node::TYPE_RESOURCE);
453 }
454 public function test_load_course_sections() {
455 $keys = array('cat2', 'sub2', '5');
456 $course = new stdClass;
457 $course->id = '5';
458 $course->format = 'topics';
459 $coursechildren = $this->node->get_by_path($keys)->children;
460
461 $this->node->get_by_path(array('cat2', 'sub2', '5'))->children = array();
462 $this->node->exposed_load_course_sections($keys, $course);
463
464 $course->format = 'topics';
465 $this->node->get_by_path(array('cat2', 'sub2', '5'))->children = array();
466 $this->node->exposed_load_course_sections($keys, $course);
467
468 $course->format = 'scorm';
469 $this->node->get_by_path(array('cat2', 'sub2', '5'))->children = array();
470 $this->node->exposed_load_course_sections($keys, $course);
471
472 $course->format = 'sillywilly';
473 $this->node->get_by_path(array('cat2', 'sub2', '5'))->children = array();
474 $this->node->exposed_load_course_sections($keys, $course);
475
476 $this->node->get_by_path($keys)->children = $coursechildren;
477 }
478 public function test_load_for_user() {
479 $this->node->exposed_load_for_user();
480 }
481 public function test_load_section_activities() {
482 $keys = array('cat2', 'sub2', '5');
483 $course = new stdClass;
484 $course->id = '5';
485 $this->node->get_by_path($keys)->add('Test Section 1', null, $this->cache->coursesections5[1]->id, navigation_node::TYPE_SECTION);
486 $modinfo = $this->cache->modinfo5;
487 $modinfo->sections[1] = array(289, 290);
488 $modinfo->cms[289] = clone($modinfo->cms[288]);
489 $modinfo->cms[289]->id = 289;
490 $modinfo->cms[289]->sectionnum = 1;
491 $modinfo->cms[290]->modname = 'forum';
492 $modinfo->cms[289]->instance = 20;
493 $modinfo->cms[290] = clone($modinfo->cms[288]);
494 $modinfo->cms[290]->id = 290;
495 $modinfo->cms[290]->modname = 'resource';
496 $modinfo->cms[290]->sectionnum = 1;
497 $modinfo->cms[290]->instance = 21;
498 $modinfo->instances['forum'][20] = clone($modinfo->instances['forum'][19]);
499 $modinfo->instances['forum'][20]->id = 20;
500 $modinfo->instances['resource'] = array();
501 $modinfo->instances['resource'][21] = clone($modinfo->instances['forum'][19]);
502 $modinfo->instances['resource'][21]->id = 21;
503 $this->cache->modinfo5 = $modinfo;
504 $this->node->exposed_load_section_activities($keys, 1, $course);
505 $keys[] = 97;
506 $this->assertIsA($this->node->get_by_path(array_merge($keys, array(289))),'navigation_node');
507 $this->assertEqual($this->node->get_by_path(array_merge($keys, array(289)))->type, navigation_node::TYPE_ACTIVITY);
508 $this->assertIsA($this->node->get_by_path(array_merge($keys, array(290))),'navigation_node');
509 $this->assertEqual($this->node->get_by_path(array_merge($keys, array(290)))->type, navigation_node::TYPE_RESOURCE);
510 }
511 public function test_module_extends_navigation() {
512 $this->cache->test1_extends_navigation = true;
513 $this->cache->test2_extends_navigation = false;
514 $this->assertTrue($this->node->exposed_module_extends_navigation('forum'));
515 $this->assertTrue($this->node->exposed_module_extends_navigation('test1'));
516 $this->assertFalse($this->node->exposed_module_extends_navigation('test2'));
517 $this->assertFalse($this->node->exposed_module_extends_navigation('test3'));
518 }
519}
520
521/**
522 * This is a dummy object that allows us to call protected methods within the
523 * global navigation class by prefixing the methods with `exposed_`
524 */
525class exposed_navbar extends navbar {
526 protected $exposedkey = 'exposed_';
527 function __construct() {
528 global $PAGE;
529 parent::__construct($PAGE);
530 $this->cache = new navigation_cache('simpletest_nav');
531 }
532 function __call($method, $arguments) {
533 if (strpos($method,$this->exposedkey) !== false) {
534 $method = substr($method, strlen($this->exposedkey));
535 }
536 if (method_exists($this, $method)) {
537 return call_user_func_array(array($this, $method), $arguments);
538 }
539 throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
540 }
541}
542
543class navbar_test extends UnitTestCase {
544 protected $node;
545 protected $oldnav;
546
547 public static $includecoverage = array('./lib/navigationlib.php');
548 public static $excludecoverage = array();
549
550 public function setUp() {
551 global $PAGE;
552 $this->oldnav = $PAGE->navigation;
553 $this->cache = new navigation_cache('simpletest_nav');
554 $this->node = new exposed_navbar();
555 $temptree = new global_navigation_test();
556 $temptree->setUp();
557 $temptree->node->get_by_path(array('cat2','sub2', 'course2'))->make_active();
558 $PAGE->navigation = $temptree->node;
559 }
560 public function tearDown() {
561 global $PAGE;
562 $PAGE->navigation = $this->oldnav;
563 }
564 public function test_add() {
565 global $CFG;
566 // Add a node with all args set
567 $this->node->add('test_add_1','testadd1','testadd1',navigation_node::TYPE_COURSE,'http://www.moodle.org/',$CFG->httpswwwroot . '/pix/i/course.gif');
568 // Add a node with the minimum args required
569 $key2 = $this->node->add('test_add_2');
570 $this->assertIsA($this->node->get('testadd1'), 'navigation_node');
571 $this->assertIsA($this->node->get('testadd1')->get($key2), 'navigation_node');
572 }
573 public function test_content() {
574 $html = $this->node->content();
575 $this->assert(new ContainsTagWithAttribute('a','href',$this->node->action->out()), $html);
576 }
577 public function test_has_items() {
578 global $PAGE;
579 $this->assertTrue($this->node->has_items());
580 $PAGE->navigation->get_by_path(array('cat2','sub2', 'course2'))->remove_class('active_tree_node');
581 $PAGE->navigation->get_by_path(array('cat2','sub2', 'course2'))->isactive = false;
582 $this->assertFalse($this->node->has_items());
583 $PAGE->navigation->get_by_path(array('cat2','sub2', 'course2'))->make_active();
584 }
585 public function test_parse_branch_to_html() {
586 global $CFG;
587 $key = $this->node->add('test_add_1','testadd1','testadd1',navigation_node::TYPE_COURSE,'http://www.moodle.org/',$CFG->httpswwwroot . '/pix/i/course.gif');
588 $this->node->get($key)->make_active();
589 $html = $this->node->exposed_parse_branch_to_html($this->node->children, true);
590 $this->assert(new ContainsTagWithAttribute('a','href',$this->node->action->out()), $html);
591 }
592}
593
594class navigation_cache_test extends UnitTestCase {
595 protected $cache;
596
597 public static $includecoverage = array('./lib/navigationlib.php');
598 public static $excludecoverage = array();
599
600 public function setUp() {
601 $this->cache = new navigation_cache('simpletest_nav');
602 $this->cache->anysetvariable = true;
603 }
604 public function test___get() {
605 $this->assertTrue($this->cache->anysetvariable);
606 $this->assertEqual($this->cache->notasetvariable, null);
607 }
608 public function test___set() {
609 $this->cache->myname = 'Sam Hemelryk';
610 $this->assertTrue($this->cache->cached('myname'));
611 $this->assertEqual($this->cache->myname, 'Sam Hemelryk');
612 }
613 public function test_cached() {
614 $this->assertTrue($this->cache->cached('anysetvariable'));
615 $this->assertFalse($this->cache->cached('notasetvariable'));
616 }
617 public function test_clear() {
618 $cache = clone($this->cache);
619 $this->assertTrue($cache->cached('anysetvariable'));
620 $cache->clear();
621 $this->assertFalse($cache->cached('anysetvariable'));
622 }
623 public function test_set() {
624 $this->cache->set('software', 'Moodle');
625 $this->assertTrue($this->cache->cached('software'));
626 $this->assertEqual($this->cache->software, 'Moodle');
627 }
628}
629
630/**
631 * This is a dummy object that allows us to call protected methods within the
632 * global navigation class by prefixing the methods with `exposed_`
633 */
634class exposed_settings_navigation extends settings_navigation {
635 protected $exposedkey = 'exposed_';
636 function __construct() {
637 global $PAGE;
638 parent::__construct($PAGE);
639 $this->cache = new navigation_cache('simpletest_nav');
640 }
641 function __call($method, $arguments) {
642 if (strpos($method,$this->exposedkey) !== false) {
643 $method = substr($method, strlen($this->exposedkey));
644 }
645 if (method_exists($this, $method)) {
646 return call_user_func_array(array($this, $method), $arguments);
647 }
648 throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
649 }
650}
651
652class settings_navigation_test extends UnitTestCase {
653 protected $node;
654 protected $cache;
655
656 public static $includecoverage = array('./lib/navigationlib.php');
657 public static $excludecoverage = array();
658
659 public function setUp() {
660 global $PAGE;
661 $this->cache = new navigation_cache('simpletest_nav');
662 $this->node = new exposed_settings_navigation();
663 }
664 public function test___construct() {
665 $this->node = new exposed_settings_navigation();
666 }
667 public function test___initialise() {
668 $this->node->initialise();
669 $this->assertEqual($this->node->id, 'settingsnav');
670 }
671 public function test_load_front_page_settings() {
672 $this->node->exposed_load_front_page_settings();
673 $settings = false;
674 foreach ($this->node->children as $child) {
675 if ($child->id === 'frontpagesettings') {
676 $settings = $child;
677 }
678 }
679 $this->assertIsA($settings, 'navigation_node');
680 }
681 public function test_in_alternative_role() {
682 $this->assertFalse($this->node->exposed_in_alternative_role());
683 }
684 public function test_remove_empty_root_branches() {
685 $this->node->add('rootbranch1', null, 'rootbranch1');
686 $this->node->add('rootbranch2', null, 'rootbranch2');
687 $this->node->add('rootbranch3', null, 'rootbranch3');
688 $this->node->get('rootbranch2')->add('something', null, null, navigation_node::TYPE_SETTING);
689 $this->node->remove_empty_root_branches();
690 $this->assertFalse($this->node->get('rootbranch1'));
691 $this->assertIsA($this->node->get('rootbranch2'), 'navigation_node');
692 $this->assertFalse($this->node->get('rootbranch3'));
693 }
694}