Updated the HEAD build version to 20090910
[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;
d972bad1 43 $this->node->add('demo1', 'http://www.moodle.org/', navigation_node::TYPE_COURSE, null, 'demo1', $CFG->httpswwwroot . '/pix/i/course.gif');
44 $this->node->add('demo2', 'http://www.moodle.com/', navigation_node::TYPE_COURSE, null, 'demo2', $CFG->httpswwwroot . '/pix/i/course.gif');
45 $this->node->add('demo3', 'http://www.moodle.org/', navigation_node::TYPE_CATEGORY, null, 'demo3',$CFG->httpswwwroot . '/pix/i/course.gif');
46 $this->node->get('demo3')->add('demo4', new moodle_url('http://www.moodle.org/'),navigation_node::TYPE_COURSE, null, 'demo4', $CFG->httpswwwroot . '/pix/i/course.gif');
47 $this->node->get('demo3')->add('demo5', 'http://www.moodle.org/test.php', navigation_node::TYPE_COURSE, null, 'demo5',$CFG->httpswwwroot . '/pix/i/course.gif');
7d2a0492 48 $this->node->get('demo3')->get('demo5')->make_active();
d972bad1 49 $this->node->get('demo3')->get('demo5')->add('activity1', null, navigation_node::TYPE_ACTIVITY, null, 'activity1');
7d2a0492 50 $this->node->get('demo3')->get('demo5')->get('activity1')->make_active();
d972bad1 51 $this->node->add('hiddendemo1', 'http://www.moodle.org/', navigation_node::TYPE_CATEGORY, null, 'hiddendemo1', $CFG->httpswwwroot . '/pix/i/course.gif');
7d2a0492 52 $this->node->get('hiddendemo1')->hidden = true;
d972bad1 53 $this->node->get('hiddendemo1')->add('hiddendemo2', new moodle_url('http://www.moodle.org/'), navigation_node::TYPE_COURSE, null, 'hiddendemo2', $CFG->httpswwwroot . '/pix/i/course.gif');
54 $this->node->get('hiddendemo1')->add('hiddendemo3', new moodle_url('http://www.moodle.org/'), navigation_node::TYPE_COURSE,null, 'hiddendemo3', $CFG->httpswwwroot . '/pix/i/course.gif');
7d2a0492 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
d972bad1 80 $key1 = $this->node->add('test_add_1','http://www.moodle.org/',navigation_node::TYPE_COURSE,'testadd1','key',$CFG->httpswwwroot . '/pix/i/course.gif');
7d2a0492 81 // Add a node with the minimum args required
d972bad1 82 $key2 = $this->node->add('test_add_2',null, navigation_node::TYPE_CUSTOM,'testadd2');
7d2a0492 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');
bdb0a369 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');
7d2a0492 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;
d972bad1 251 $key1 = $this->node->add('active node 1', null, navigation_node::TYPE_CUSTOM, null, 'anode1');
252 $key2 = $this->node->add('active node 2', new moodle_url($CFG->wwwroot), navigation_node::TYPE_COURSE, null, 'anode2');
7d2a0492 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() {
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
319class global_navigation_test extends UnitTestCase {
320 /**
321 * @var global_navigation
322 */
323 public $node;
324 protected $cache;
3b7a763c 325 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;}';
326 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 327 public static $includecoverage = array('./lib/navigationlib.php');
328 public static $excludecoverage = array();
329
330 public function setUp() {
331 $this->cache = new navigation_cache('simpletest_nav');
332 $this->node = new exposed_global_navigation();
333 // Create an initial tree structure to work with
d972bad1 334 $this->node->add('category 1', null, navigation_node::TYPE_CATEGORY, null, 'cat1');
335 $this->node->add('category 2', null, navigation_node::TYPE_CATEGORY, null, 'cat2');
336 $this->node->add('category 3', null, navigation_node::TYPE_CATEGORY, null, 'cat3');
337 $this->node->get('cat2')->add('sub category 1', null, navigation_node::TYPE_CATEGORY, null, 'sub1');
338 $this->node->get('cat2')->add('sub category 2', null, navigation_node::TYPE_CATEGORY, null, 'sub2');
339 $this->node->get('cat2')->add('sub category 3', null, navigation_node::TYPE_CATEGORY, null, 'sub3');
340 $this->node->get('cat2')->get('sub2')->add('course 1', null, navigation_node::TYPE_COURSE, null, 'course1');
341 $this->node->get('cat2')->get('sub2')->add('course 2', null, navigation_node::TYPE_COURSE, null, 'course2');
342 $this->node->get('cat2')->get('sub2')->add('course 3', null, navigation_node::TYPE_COURSE, null, 'course3');
343 $this->node->get('cat2')->get('sub2')->get('course2')->add('section 1', null, navigation_node::TYPE_COURSE, null, 'sec1');
344 $this->node->get('cat2')->get('sub2')->get('course2')->add('section 2', null, navigation_node::TYPE_COURSE, null, 'sec2');
345 $this->node->get('cat2')->get('sub2')->get('course2')->add('section 3', null, navigation_node::TYPE_COURSE, null, 'sec3');
346 $this->node->get('cat2')->get('sub2')->get('course2')->get('sec2')->add('activity 1', null, navigation_node::TYPE_ACTIVITY, null, 'act1');
347 $this->node->get('cat2')->get('sub2')->get('course2')->get('sec2')->add('activity 2', null, navigation_node::TYPE_ACTIVITY, null, 'act2');
348 $this->node->get('cat2')->get('sub2')->get('course2')->get('sec2')->add('activity 3', null, navigation_node::TYPE_ACTIVITY, null, 'act3');
349 $this->node->get('cat2')->get('sub2')->get('course2')->get('sec2')->add('resource 1', null, navigation_node::TYPE_RESOURCE, null, 'res1');
350 $this->node->get('cat2')->get('sub2')->get('course2')->get('sec2')->add('resource 2', null, navigation_node::TYPE_RESOURCE, null, 'res2');
351 $this->node->get('cat2')->get('sub2')->get('course2')->get('sec2')->add('resource 3', null, navigation_node::TYPE_RESOURCE, null, 'res3');
7d2a0492 352
353 $this->cache->clear();
3b7a763c 354 $this->cache->modinfo5 = unserialize($this->modinfo5);
355 $this->cache->coursesections5 = unserialize($this->coursesections5);
7d2a0492 356 $this->cache->canviewhiddenactivities = true;
357 $this->cache->canviewhiddensections = true;
358 $this->cache->canviewhiddencourses = true;
d972bad1 359 $this->node->get('cat2')->get('sub2')->add('Test Course 5', new moodle_url('http://moodle.org'),navigation_node::TYPE_COURSE,null,'5');
7d2a0492 360 }
361 public function test_add_categories() {
362 $categories = array();
363 for ($i=0;$i<3;$i++) {
364 $categories[$i] = new stdClass;
365 $categories[$i]->id = 'sub4_'.$i;
366 $categories[$i]->name = 'add_categories '.$i;
367 }
368 $this->node->exposed_add_categories(array('cat3'), $categories);
369 $this->assertEqual(count($this->node->get('cat3')->children), 3);
370 $this->assertIsA($this->node->get('cat3')->get('sub4_1'), 'navigation_node');
371 $this->node->get('cat3')->children = array();
372 }
373 public function test_add_course_section_generic() {
374 $keys = array('cat2', 'sub2', '5');
375 $course = new stdClass;
376 $course->id = '5';
3b7a763c 377 $course->numsections = 10;
378 $course->modinfo = $this->modinfo5;
7d2a0492 379 $this->node->add_course_section_generic($keys, $course, 'topic', 'topic');
380 $this->assertEqual(count($this->node->get_by_path($keys)->children),4);
381 }
382 public function test_add_category_by_path() {
383 $category = new stdClass;
384 $category->id = 'sub3';
385 $category->name = 'Sub category 3';
386 $category->path = '/cat2/sub3';
387 $this->node->exposed_add_category_by_path($category);
388 $this->assertIsA($this->node->get('cat2')->get('sub3'), 'navigation_node');
389 }
390 public function test_add_courses() {
391 $courses = array();
392 for ($i=0;$i<5;$i++) {
393 $course = new stdClass;
394 $course->id = $i;
395 $course->visible = true;
396 $course->category = 'cat3';
397 $course->fullname = "Test Course $i";
398 $course->shortname = "tcourse$i";
3b7a763c 399 $course->numsections = 10;
400 $course->modinfo = $this->modinfo5;
7d2a0492 401 $courses[$i] = $course;
402 }
403
404 $this->node->add_courses($courses);
405 $this->assertIsA($this->node->get('cat3')->get(0), 'navigation_node');
406 $this->assertIsA($this->node->get('cat3')->get(1), 'navigation_node');
407 $this->assertIsA($this->node->get('cat3')->get(2), 'navigation_node');
408 $this->assertIsA($this->node->get('cat3')->get(3), 'navigation_node');
409 $this->assertIsA($this->node->get('cat3')->get(4), 'navigation_node');
410 $this->node->get('cat3')->children = array();
411 }
412 public function test_can_display_type() {
413 $this->node->expansionlimit = navigation_node::TYPE_COURSE;
414 $this->assertTrue($this->node->exposed_can_display_type(navigation_node::TYPE_CATEGORY));
415 $this->assertTrue($this->node->exposed_can_display_type(navigation_node::TYPE_COURSE));
416 $this->assertFalse($this->node->exposed_can_display_type(navigation_node::TYPE_SECTION));
417 $this->node->expansionlimit = null;
418 }
419 public function test_content() {
420 $html1 = $this->node->content();
421 $this->node->expansionlimit = navigation_node::TYPE_CATEGORY;
422 $html2 = $this->node->content();
423 $this->node->expansionlimit = null;
424 $this->assert(new ContainsTagWithAttribute('a','href',$this->node->action->out()), $html1);
425 $this->assert(new ContainsTagWithAttribute('a','href',$this->node->action->out()), $html2);
426 }
427 public function test_format_display_course_content() {
428 $this->assertTrue($this->node->exposed_format_display_course_content('topic'));
429 $this->assertFalse($this->node->exposed_format_display_course_content('scorm'));
430 $this->assertTrue($this->node->exposed_format_display_course_content('dummy'));
431 }
432 public function test_load_course() {
433 $course = new stdClass;
434 $course->id = 'tcourse10';
435 $course->fullname = 'Test Course 10';
436 $course->shortname = 'tcourse10';
437 $course->visible = true;
3b7a763c 438 $course->numsections = 10;
439 $course->modinfo = $this->modinfo5;
7d2a0492 440 $this->node->exposed_load_course(array('cat2','sub3'), $course);
441 $this->assertIsA($this->node->get('cat2')->get('sub3')->get('tcourse10'), 'navigation_node');
442 }
443 public function test_load_course_activities() {
444 $keys = array('cat2', 'sub2', '5');
445 $course = new stdClass;
446 $course->id = '5';
3b7a763c 447 $course->numsections = 10;
7d2a0492 448 $modinfo = $this->cache->modinfo5;
449 $modinfo->cms[290] = clone($modinfo->cms[288]);
450 $modinfo->cms[290]->id = 290;
451 $modinfo->cms[290]->modname = 'resource';
452 $modinfo->cms[290]->instance = 21;
453 $modinfo->instances['resource'] = array();
454 $modinfo->instances['resource'][21] = clone($modinfo->instances['forum'][19]);
455 $modinfo->instances['resource'][21]->id = 21;
456 $this->cache->modinfo5 = $modinfo;
3b7a763c 457 $course->modinfo = serialize($modinfo);
7d2a0492 458 $this->node->exposed_load_course_activities($keys, $course);
3b7a763c 459
7d2a0492 460 $this->assertIsA($this->node->get_by_path(array_merge($keys, array(288))), 'navigation_node');
461 $this->assertEqual($this->node->get_by_path(array_merge($keys, array(288)))->type, navigation_node::TYPE_ACTIVITY);
462 $this->assertIsA($this->node->get_by_path(array_merge($keys, array(290))), 'navigation_node');
463 $this->assertEqual($this->node->get_by_path(array_merge($keys, array(290)))->type, navigation_node::TYPE_RESOURCE);
464 }
465 public function test_load_course_sections() {
466 $keys = array('cat2', 'sub2', '5');
467 $course = new stdClass;
468 $course->id = '5';
469 $course->format = 'topics';
3b7a763c 470 $course->numsections = 10;
471 $course->modinfo = $this->modinfo5;
7d2a0492 472 $coursechildren = $this->node->get_by_path($keys)->children;
473
474 $this->node->get_by_path(array('cat2', 'sub2', '5'))->children = array();
475 $this->node->exposed_load_course_sections($keys, $course);
476
477 $course->format = 'topics';
478 $this->node->get_by_path(array('cat2', 'sub2', '5'))->children = array();
479 $this->node->exposed_load_course_sections($keys, $course);
480
481 $course->format = 'scorm';
482 $this->node->get_by_path(array('cat2', 'sub2', '5'))->children = array();
483 $this->node->exposed_load_course_sections($keys, $course);
484
485 $course->format = 'sillywilly';
486 $this->node->get_by_path(array('cat2', 'sub2', '5'))->children = array();
487 $this->node->exposed_load_course_sections($keys, $course);
488
489 $this->node->get_by_path($keys)->children = $coursechildren;
490 }
491 public function test_load_for_user() {
492 $this->node->exposed_load_for_user();
493 }
494 public function test_load_section_activities() {
495 $keys = array('cat2', 'sub2', '5');
496 $course = new stdClass;
497 $course->id = '5';
3b7a763c 498 $course->numsections = 10;
d972bad1 499 $this->node->get_by_path($keys)->add('Test Section 1', null, navigation_node::TYPE_SECTION, null, $this->cache->coursesections5[1]->id);
7d2a0492 500 $modinfo = $this->cache->modinfo5;
501 $modinfo->sections[1] = array(289, 290);
502 $modinfo->cms[289] = clone($modinfo->cms[288]);
503 $modinfo->cms[289]->id = 289;
504 $modinfo->cms[289]->sectionnum = 1;
505 $modinfo->cms[290]->modname = 'forum';
506 $modinfo->cms[289]->instance = 20;
507 $modinfo->cms[290] = clone($modinfo->cms[288]);
508 $modinfo->cms[290]->id = 290;
509 $modinfo->cms[290]->modname = 'resource';
510 $modinfo->cms[290]->sectionnum = 1;
511 $modinfo->cms[290]->instance = 21;
512 $modinfo->instances['forum'][20] = clone($modinfo->instances['forum'][19]);
513 $modinfo->instances['forum'][20]->id = 20;
514 $modinfo->instances['resource'] = array();
515 $modinfo->instances['resource'][21] = clone($modinfo->instances['forum'][19]);
516 $modinfo->instances['resource'][21]->id = 21;
517 $this->cache->modinfo5 = $modinfo;
3b7a763c 518 $course->modinfo = serialize($modinfo);
7d2a0492 519 $this->node->exposed_load_section_activities($keys, 1, $course);
520 $keys[] = 97;
521 $this->assertIsA($this->node->get_by_path(array_merge($keys, array(289))),'navigation_node');
522 $this->assertEqual($this->node->get_by_path(array_merge($keys, array(289)))->type, navigation_node::TYPE_ACTIVITY);
523 $this->assertIsA($this->node->get_by_path(array_merge($keys, array(290))),'navigation_node');
524 $this->assertEqual($this->node->get_by_path(array_merge($keys, array(290)))->type, navigation_node::TYPE_RESOURCE);
525 }
526 public function test_module_extends_navigation() {
527 $this->cache->test1_extends_navigation = true;
528 $this->cache->test2_extends_navigation = false;
529 $this->assertTrue($this->node->exposed_module_extends_navigation('forum'));
530 $this->assertTrue($this->node->exposed_module_extends_navigation('test1'));
531 $this->assertFalse($this->node->exposed_module_extends_navigation('test2'));
532 $this->assertFalse($this->node->exposed_module_extends_navigation('test3'));
533 }
534}
535
536/**
537 * This is a dummy object that allows us to call protected methods within the
538 * global navigation class by prefixing the methods with `exposed_`
539 */
540class exposed_navbar extends navbar {
541 protected $exposedkey = 'exposed_';
542 function __construct() {
543 global $PAGE;
544 parent::__construct($PAGE);
545 $this->cache = new navigation_cache('simpletest_nav');
546 }
547 function __call($method, $arguments) {
548 if (strpos($method,$this->exposedkey) !== false) {
549 $method = substr($method, strlen($this->exposedkey));
550 }
551 if (method_exists($this, $method)) {
552 return call_user_func_array(array($this, $method), $arguments);
553 }
554 throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
555 }
556}
557
558class navbar_test extends UnitTestCase {
559 protected $node;
560 protected $oldnav;
561
562 public static $includecoverage = array('./lib/navigationlib.php');
563 public static $excludecoverage = array();
564
565 public function setUp() {
566 global $PAGE;
567 $this->oldnav = $PAGE->navigation;
568 $this->cache = new navigation_cache('simpletest_nav');
569 $this->node = new exposed_navbar();
570 $temptree = new global_navigation_test();
571 $temptree->setUp();
572 $temptree->node->get_by_path(array('cat2','sub2', 'course2'))->make_active();
573 $PAGE->navigation = $temptree->node;
574 }
575 public function tearDown() {
576 global $PAGE;
577 $PAGE->navigation = $this->oldnav;
578 }
579 public function test_add() {
580 global $CFG;
581 // Add a node with all args set
d972bad1 582 $this->node->add('test_add_1','http://www.moodle.org/',navigation_node::TYPE_COURSE,'testadd1','testadd1',$CFG->httpswwwroot . '/pix/i/course.gif');
7d2a0492 583 // Add a node with the minimum args required
584 $key2 = $this->node->add('test_add_2');
585 $this->assertIsA($this->node->get('testadd1'), 'navigation_node');
586 $this->assertIsA($this->node->get('testadd1')->get($key2), 'navigation_node');
587 }
588 public function test_content() {
589 $html = $this->node->content();
590 $this->assert(new ContainsTagWithAttribute('a','href',$this->node->action->out()), $html);
591 }
592 public function test_has_items() {
593 global $PAGE;
594 $this->assertTrue($this->node->has_items());
595 $PAGE->navigation->get_by_path(array('cat2','sub2', 'course2'))->remove_class('active_tree_node');
596 $PAGE->navigation->get_by_path(array('cat2','sub2', 'course2'))->isactive = false;
597 $this->assertFalse($this->node->has_items());
598 $PAGE->navigation->get_by_path(array('cat2','sub2', 'course2'))->make_active();
599 }
600 public function test_parse_branch_to_html() {
601 global $CFG;
d972bad1 602 $key = $this->node->add('test_add_1','http://www.moodle.org/',navigation_node::TYPE_COURSE,'testadd1','testadd1',$CFG->httpswwwroot . '/pix/i/course.gif');
7d2a0492 603 $this->node->get($key)->make_active();
b3b3eace 604 $html = $this->node->exposed_parse_branch_to_html($this->node->children, true, true);
7d2a0492 605 $this->assert(new ContainsTagWithAttribute('a','href',$this->node->action->out()), $html);
606 }
607}
608
609class navigation_cache_test extends UnitTestCase {
610 protected $cache;
611
612 public static $includecoverage = array('./lib/navigationlib.php');
613 public static $excludecoverage = array();
614
615 public function setUp() {
616 $this->cache = new navigation_cache('simpletest_nav');
617 $this->cache->anysetvariable = true;
618 }
619 public function test___get() {
620 $this->assertTrue($this->cache->anysetvariable);
621 $this->assertEqual($this->cache->notasetvariable, null);
622 }
623 public function test___set() {
624 $this->cache->myname = 'Sam Hemelryk';
625 $this->assertTrue($this->cache->cached('myname'));
626 $this->assertEqual($this->cache->myname, 'Sam Hemelryk');
627 }
628 public function test_cached() {
629 $this->assertTrue($this->cache->cached('anysetvariable'));
630 $this->assertFalse($this->cache->cached('notasetvariable'));
631 }
632 public function test_clear() {
633 $cache = clone($this->cache);
634 $this->assertTrue($cache->cached('anysetvariable'));
635 $cache->clear();
636 $this->assertFalse($cache->cached('anysetvariable'));
637 }
638 public function test_set() {
639 $this->cache->set('software', 'Moodle');
640 $this->assertTrue($this->cache->cached('software'));
641 $this->assertEqual($this->cache->software, 'Moodle');
642 }
643}
644
645/**
646 * This is a dummy object that allows us to call protected methods within the
647 * global navigation class by prefixing the methods with `exposed_`
648 */
649class exposed_settings_navigation extends settings_navigation {
650 protected $exposedkey = 'exposed_';
651 function __construct() {
652 global $PAGE;
653 parent::__construct($PAGE);
654 $this->cache = new navigation_cache('simpletest_nav');
655 }
656 function __call($method, $arguments) {
657 if (strpos($method,$this->exposedkey) !== false) {
658 $method = substr($method, strlen($this->exposedkey));
659 }
660 if (method_exists($this, $method)) {
661 return call_user_func_array(array($this, $method), $arguments);
662 }
663 throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
664 }
665}
666
667class settings_navigation_test extends UnitTestCase {
668 protected $node;
669 protected $cache;
670
671 public static $includecoverage = array('./lib/navigationlib.php');
672 public static $excludecoverage = array();
673
674 public function setUp() {
675 global $PAGE;
676 $this->cache = new navigation_cache('simpletest_nav');
677 $this->node = new exposed_settings_navigation();
678 }
679 public function test___construct() {
680 $this->node = new exposed_settings_navigation();
681 }
682 public function test___initialise() {
683 $this->node->initialise();
684 $this->assertEqual($this->node->id, 'settingsnav');
685 }
686 public function test_load_front_page_settings() {
687 $this->node->exposed_load_front_page_settings();
688 $settings = false;
689 foreach ($this->node->children as $child) {
690 if ($child->id === 'frontpagesettings') {
691 $settings = $child;
692 }
693 }
694 $this->assertIsA($settings, 'navigation_node');
695 }
696 public function test_in_alternative_role() {
697 $this->assertFalse($this->node->exposed_in_alternative_role());
698 }
699 public function test_remove_empty_root_branches() {
d972bad1 700 $this->node->add('rootbranch1', null, navigation_node::TYPE_SETTING, null, 'rootbranch1');
701 $this->node->add('rootbranch2', null, navigation_node::TYPE_SETTING, null, 'rootbranch2');
702 $this->node->add('rootbranch3', null, navigation_node::TYPE_SETTING, null, 'rootbranch3');
703 $this->node->get('rootbranch2')->add('something', null, navigation_node::TYPE_SETTING);
7d2a0492 704 $this->node->remove_empty_root_branches();
705 $this->assertFalse($this->node->get('rootbranch1'));
706 $this->assertIsA($this->node->get('rootbranch2'), 'navigation_node');
707 $this->assertFalse($this->node->get('rootbranch3'));
708 }
709}