MDL-27809 navigation: Reworked the caching within navigation and improved the orderin...
[moodle.git] / lib / simpletest / testnavigationlib.php
CommitLineData
7d2a0492 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Unit tests for lib/navigationlib.php
20 *
21 * @package moodlecore
22 * @copyright 2009 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later (5)
24 */
25
26if (!defined('MOODLE_INTERNAL')) {
27 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
28}
29require_once($CFG->libdir . '/navigationlib.php');
30
31class navigation_node_test extends UnitTestCase {
32 protected $tree;
33 public static $includecoverage = array('./lib/navigationlib.php');
34 public static $excludecoverage = array();
95b97515 35 protected $fakeproperties = array(
36 'text' => 'text',
37 'shorttext' => 'A very silly extra long short text string, more than 25 characters',
38 'key' => 'key',
39 'type' => 'navigation_node::TYPE_COURSE',
40 'action' => 'http://www.moodle.org/');
c705a24e 41 protected $activeurl = null;
42 protected $inactivenode = null;
7d2a0492 43
3406acde
SH
44 /**
45 * @var navigation_node
46 */
47 public $node;
48
7d2a0492 49 public function setUp() {
c705a24e 50 global $CFG, $PAGE;
7d2a0492 51 parent::setUp();
c705a24e 52
53 $this->activeurl = $PAGE->url;
460232f3
SH
54 navigation_node::override_active_url($this->activeurl);
55
c705a24e 56 $this->inactiveurl = new moodle_url('http://www.moodle.com/');
57 $this->fakeproperties['action'] = $this->inactiveurl;
58
7d2a0492 59 $this->node = new navigation_node('Test Node');
60 $this->node->type = navigation_node::TYPE_SYSTEM;
3406acde
SH
61 $demo1 = $this->node->add('demo1', $this->inactiveurl, navigation_node::TYPE_COURSE, null, 'demo1', new pix_icon('i/course', ''));
62 $demo2 = $this->node->add('demo2', $this->inactiveurl, navigation_node::TYPE_COURSE, null, 'demo2', new pix_icon('i/course', ''));
63 $demo3 = $this->node->add('demo3', $this->inactiveurl, navigation_node::TYPE_CATEGORY, null, 'demo3',new pix_icon('i/course', ''));
64 $demo4 = $demo3->add('demo4', $this->inactiveurl,navigation_node::TYPE_COURSE, null, 'demo4', new pix_icon('i/course', ''));
65 $demo5 = $demo3->add('demo5', $this->activeurl, navigation_node::TYPE_COURSE, null, 'demo5',new pix_icon('i/course', ''));
66 $demo5->add('activity1', null, navigation_node::TYPE_ACTIVITY, null, 'activity1')->make_active();
67 $hiddendemo1 = $this->node->add('hiddendemo1', $this->inactiveurl, navigation_node::TYPE_CATEGORY, null, 'hiddendemo1', new pix_icon('i/course', ''));
68 $hiddendemo1->hidden = true;
69 $hiddendemo1->add('hiddendemo2', $this->inactiveurl, navigation_node::TYPE_COURSE, null, 'hiddendemo2', new pix_icon('i/course', ''))->helpbutton = 'Here is a help button';;
70 $hiddendemo1->add('hiddendemo3', $this->inactiveurl, navigation_node::TYPE_COURSE,null, 'hiddendemo3', new pix_icon('i/course', ''))->display = false;
7d2a0492 71 }
8926f844 72
7d2a0492 73 public function test___construct() {
74 global $CFG;
95b97515 75 $node = new navigation_node($this->fakeproperties);
76 $this->assertEqual($node->text, $this->fakeproperties['text']);
77 $this->assertEqual($node->title, $this->fakeproperties['text']);
78 $this->assertTrue(strpos($this->fakeproperties['shorttext'], substr($node->shorttext,0, -3))===0);
79 $this->assertEqual($node->key, $this->fakeproperties['key']);
80 $this->assertEqual($node->type, $this->fakeproperties['type']);
81 $this->assertEqual($node->action, $this->fakeproperties['action']);
82 }
7d2a0492 83 public function test_add() {
84 global $CFG;
85 // Add a node with all args set
3406acde 86 $node1 = $this->node->add('test_add_1','http://www.moodle.org/',navigation_node::TYPE_COURSE,'testadd1','key',new pix_icon('i/course', ''));
7d2a0492 87 // Add a node with the minimum args required
3406acde
SH
88 $node2 = $this->node->add('test_add_2',null, navigation_node::TYPE_CUSTOM,'testadd2');
89 $node3 = $this->node->add(str_repeat('moodle ', 15),str_repeat('moodle', 15));
90
91 $this->assertIsA($node1, 'navigation_node');
92 $this->assertIsA($node2, 'navigation_node');
93 $this->assertIsA($node3, 'navigation_node');
94
95 $this->assertReference($node1, $this->node->get('key'));
96 $this->assertReference($node2, $this->node->get($node2->key));
97 $this->assertReference($node2, $this->node->get($node2->key, $node2->type));
98 $this->assertReference($node3, $this->node->get($node3->key, $node3->type));
7d2a0492 99 }
100
188a8127 101 public function test_add_before() {
102 global $CFG;
103 // Create 3 nodes
104 $node1 = navigation_node::create('test_add_1', null, navigation_node::TYPE_CUSTOM,
105 'test 1', 'testadd1');
106 $node2 = navigation_node::create('test_add_2', null, navigation_node::TYPE_CUSTOM,
107 'test 2', 'testadd2');
108 $node3 = navigation_node::create('test_add_3', null, navigation_node::TYPE_CUSTOM,
109 'test 3', 'testadd3');
110 // Add node 2, then node 1 before 2, then node 3 at end
111 $this->node->add_node($node2);
112 $this->node->add_node($node1, 'testadd2');
113 $this->node->add_node($node3);
114 // Check the last 3 nodes are in 1, 2, 3 order and have those indexes
115 foreach($this->node->children as $child) {
116 $keys[] = $child->key;
117 }
118 $this->assertEqual('testadd1', $keys[count($keys)-3]);
119 $this->assertEqual('testadd2', $keys[count($keys)-2]);
120 $this->assertEqual('testadd3', $keys[count($keys)-1]);
121 }
122
7d2a0492 123 public function test_add_class() {
124 $node = $this->node->get('demo1');
125 $this->assertIsA($node, 'navigation_node');
126 if ($node !== false) {
127 $node->add_class('myclass');
128 $classes = $node->classes;
129 $this->assertTrue(in_array('myclass', $classes));
130 }
131 }
132
7d2a0492 133
134 public function test_check_if_active() {
7d2a0492 135 // First test the string urls
7d2a0492 136 // demo1 -> action is http://www.moodle.org/, thus should be true
3406acde
SH
137 $demo5 = $this->node->find('demo5', navigation_node::TYPE_COURSE);
138 if ($this->assertIsA($demo5, 'navigation_node')) {
139 $this->assertTrue($demo5->check_if_active());
140 }
141
7d2a0492 142 // demo2 -> action is http://www.moodle.com/, thus should be false
3406acde
SH
143 $demo2 = $this->node->get('demo2');
144 if ($this->assertIsA($demo2, 'navigation_node')) {
145 $this->assertFalse($demo2->check_if_active());
146 }
7d2a0492 147 }
148
149 public function test_contains_active_node() {
150 // demo5, and activity1 were set to active during setup
151 // Should be true as it contains all nodes
152 $this->assertTrue($this->node->contains_active_node());
153 // Should be true as demo5 is a child of demo3
154 $this->assertTrue($this->node->get('demo3')->contains_active_node());
155 // Obviously duff
156 $this->assertFalse($this->node->get('demo1')->contains_active_node());
157 // Should be true as demo5 contains activity1
158 $this->assertTrue($this->node->get('demo3')->get('demo5')->contains_active_node());
3406acde
SH
159 // Should be true activity1 is the active node
160 $this->assertTrue($this->node->get('demo3')->get('demo5')->get('activity1')->contains_active_node());
7d2a0492 161 // Obviously duff
162 $this->assertFalse($this->node->get('demo3')->get('demo4')->contains_active_node());
163 }
164
7d2a0492 165 public function test_find_active_node() {
166 $activenode1 = $this->node->find_active_node();
3406acde
SH
167 $activenode2 = $this->node->get('demo1')->find_active_node();
168
169 if ($this->assertIsA($activenode1, 'navigation_node')) {
170 $this->assertReference($activenode1, $this->node->get('demo3')->get('demo5')->get('activity1'));
7d2a0492 171 }
3406acde
SH
172
173 $this->assertNotA($activenode2, 'navigation_node');
7d2a0492 174 }
175
3406acde
SH
176 public function test_find() {
177 $node1 = $this->node->find('demo1', navigation_node::TYPE_COURSE);
178 $node2 = $this->node->find('demo5', navigation_node::TYPE_COURSE);
179 $node3 = $this->node->find('demo5', navigation_node::TYPE_CATEGORY);
180 $node4 = $this->node->find('demo0', navigation_node::TYPE_COURSE);
7d2a0492 181 $this->assertIsA($node1, 'navigation_node');
182 $this->assertIsA($node2, 'navigation_node');
183 $this->assertNotA($node3, 'navigation_node');
184 $this->assertNotA($node4, 'navigation_node');
185 }
186
7d2a0492 187 public function test_find_expandable() {
188 $expandable = array();
189 $this->node->find_expandable($expandable);
abaece1e
SH
190 $this->assertEqual(count($expandable), 4);
191 if (count($expandable) === 4) {
c755c874
SH
192 $name = $expandable[0]['key'];
193 $name .= $expandable[1]['key'];
194 $name .= $expandable[2]['key'];
195 $name .= $expandable[3]['key'];
abaece1e 196 $this->assertEqual($name, 'demo1demo2demo4hiddendemo2');
7d2a0492 197 }
198 }
199
200 public function test_get() {
201 $node1 = $this->node->get('demo1'); // Exists
202 $node2 = $this->node->get('demo4'); // Doesn't exist for this node
203 $node3 = $this->node->get('demo0'); // Doesn't exist at all
204 $node4 = $this->node->get(false); // Sometimes occurs in nature code
205 $this->assertIsA($node1, 'navigation_node');
206 $this->assertFalse($node2);
207 $this->assertFalse($node3);
208 $this->assertFalse($node4);
209 }
210
7d2a0492 211 public function test_get_css_type() {
212 $csstype1 = $this->node->get('demo3')->get_css_type();
213 $csstype2 = $this->node->get('demo3')->get('demo5')->get_css_type();
214 $this->node->get('demo3')->get('demo5')->type = 1000;
215 $csstype3 = $this->node->get('demo3')->get('demo5')->get_css_type();
216 $this->assertEqual($csstype1, 'type_category');
217 $this->assertEqual($csstype2, 'type_course');
218 $this->assertEqual($csstype3, 'type_unknown');
219 }
220
221 public function test_make_active() {
222 global $CFG;
3406acde
SH
223 $node1 = $this->node->add('active node 1', null, navigation_node::TYPE_CUSTOM, null, 'anode1');
224 $node2 = $this->node->add('active node 2', new moodle_url($CFG->wwwroot), navigation_node::TYPE_COURSE, null, 'anode2');
225 $node1->make_active();
226 $this->node->get('anode2')->make_active();
227 $this->assertTrue($node1->isactive);
228 $this->assertTrue($this->node->get('anode2')->isactive);
229 }
230 public function test_remove() {
231 $remove1 = $this->node->add('child to remove 1', null, navigation_node::TYPE_CUSTOM, null, 'remove1');
232 $remove2 = $this->node->add('child to remove 2', null, navigation_node::TYPE_CUSTOM, null, 'remove2');
233 $remove3 = $remove2->add('child to remove 3', null, navigation_node::TYPE_CUSTOM, null, 'remove3');
234
235 $this->assertIsA($remove1, 'navigation_node');
236 $this->assertIsA($remove2, 'navigation_node');
237 $this->assertIsA($remove3, 'navigation_node');
238
7d2a0492 239 $this->assertIsA($this->node->get('remove1'), 'navigation_node');
3406acde
SH
240 $this->assertIsA($this->node->get('remove2'), 'navigation_node');
241 $this->assertIsA($remove2->get('remove3'), 'navigation_node');
242
243 $this->assertTrue($remove1->remove());
244 $this->assertTrue($this->node->get('remove2')->remove());
245 $this->assertTrue($remove2->get('remove3')->remove());
246
247 $this->assertFalse($this->node->get('remove1'));
248 $this->assertFalse($this->node->get('remove2'));
7d2a0492 249 }
250 public function test_remove_class() {
251 $this->node->add_class('testclass');
252 $this->assertTrue($this->node->remove_class('testclass'));
253 $this->assertFalse(in_array('testclass', $this->node->classes));
254 }
7d2a0492 255}
256
257/**
258 * This is a dummy object that allows us to call protected methods within the
259 * global navigation class by prefixing the methods with `exposed_`
260 */
261class exposed_global_navigation extends global_navigation {
262 protected $exposedkey = 'exposed_';
abaece1e 263 public function __construct(moodle_page $page=null) {
3406acde 264 global $PAGE;
abaece1e
SH
265 if ($page === null) {
266 $page = $PAGE;
267 }
268 parent::__construct($page);
7d2a0492 269 $this->cache = new navigation_cache('simpletest_nav');
270 }
abaece1e 271 public function __call($method, $arguments) {
7d2a0492 272 if (strpos($method,$this->exposedkey) !== false) {
273 $method = substr($method, strlen($this->exposedkey));
274 }
275 if (method_exists($this, $method)) {
276 return call_user_func_array(array($this, $method), $arguments);
277 }
278 throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
279 }
abaece1e
SH
280 public function set_initialised() {
281 $this->initialised = true;
282 }
7d2a0492 283}
284
95b97515 285class mock_initialise_global_navigation extends global_navigation {
286
287 static $count = 1;
288
289 public function load_for_category() {
290 $this->add('load_for_category', null, null, null, 'initcall'.self::$count);
291 self::$count++;
292 return 0;
293 }
294
295 public function load_for_course() {
296 $this->add('load_for_course', null, null, null, 'initcall'.self::$count);
297 self::$count++;
298 return 0;
299 }
300
301 public function load_for_activity() {
302 $this->add('load_for_activity', null, null, null, 'initcall'.self::$count);
303 self::$count++;
304 return 0;
305 }
306
307 public function load_for_user() {
308 $this->add('load_for_user', null, null, null, 'initcall'.self::$count);
309 self::$count++;
310 return 0;
311 }
312}
313
7d2a0492 314class global_navigation_test extends UnitTestCase {
315 /**
316 * @var global_navigation
317 */
318 public $node;
319 protected $cache;
3b7a763c 320 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;}';
321 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 322 public static $includecoverage = array('./lib/navigationlib.php');
323 public static $excludecoverage = array();
8926f844 324
7d2a0492 325 public function setUp() {
326 $this->cache = new navigation_cache('simpletest_nav');
327 $this->node = new exposed_global_navigation();
328 // Create an initial tree structure to work with
3406acde
SH
329 $cat1 = $this->node->add('category 1', null, navigation_node::TYPE_CATEGORY, null, 'cat1');
330 $cat2 = $this->node->add('category 2', null, navigation_node::TYPE_CATEGORY, null, 'cat2');
331 $cat3 = $this->node->add('category 3', null, navigation_node::TYPE_CATEGORY, null, 'cat3');
332 $sub1 = $cat2->add('sub category 1', null, navigation_node::TYPE_CATEGORY, null, 'sub1');
333 $sub2 = $cat2->add('sub category 2', null, navigation_node::TYPE_CATEGORY, null, 'sub2');
334 $sub3 = $cat2->add('sub category 3', null, navigation_node::TYPE_CATEGORY, null, 'sub3');
335 $course1 = $sub2->add('course 1', null, navigation_node::TYPE_COURSE, null, 'course1');
336 $course2 = $sub2->add('course 2', null, navigation_node::TYPE_COURSE, null, 'course2');
337 $course3 = $sub2->add('course 3', null, navigation_node::TYPE_COURSE, null, 'course3');
338 $section1 = $course2->add('section 1', null, navigation_node::TYPE_COURSE, null, 'sec1');
339 $section2 = $course2->add('section 2', null, navigation_node::TYPE_COURSE, null, 'sec2');
340 $section3 = $course2->add('section 3', null, navigation_node::TYPE_COURSE, null, 'sec3');
341 $act1 = $section2->add('activity 1', null, navigation_node::TYPE_ACTIVITY, null, 'act1');
342 $act2 = $section2->add('activity 2', null, navigation_node::TYPE_ACTIVITY, null, 'act2');
343 $act3 = $section2->add('activity 3', null, navigation_node::TYPE_ACTIVITY, null, 'act3');
344 $res1 = $section2->add('resource 1', null, navigation_node::TYPE_RESOURCE, null, 'res1');
345 $res2 = $section2->add('resource 2', null, navigation_node::TYPE_RESOURCE, null, 'res2');
346 $res3 = $section2->add('resource 3', null, navigation_node::TYPE_RESOURCE, null, 'res3');
7d2a0492 347
348 $this->cache->clear();
3b7a763c 349 $this->cache->modinfo5 = unserialize($this->modinfo5);
350 $this->cache->coursesections5 = unserialize($this->coursesections5);
7d2a0492 351 $this->cache->canviewhiddenactivities = true;
352 $this->cache->canviewhiddensections = true;
353 $this->cache->canviewhiddencourses = true;
3406acde 354 $sub2->add('Test Course 5', new moodle_url('http://moodle.org'),navigation_node::TYPE_COURSE,null,'5');
7d2a0492 355 }
3406acde
SH
356 public function test_load_generic_course_sections() {
357 $coursenode = $this->node->find('5', navigation_node::TYPE_COURSE);
7d2a0492 358 $course = new stdClass;
359 $course->id = '5';
3b7a763c 360 $course->numsections = 10;
361 $course->modinfo = $this->modinfo5;
3406acde 362 $this->node->load_generic_course_sections($course, $coursenode, 'topic', 'topic');
abaece1e 363 $this->assertEqual($coursenode->children->count(),1);
7d2a0492 364 }
365 public function test_format_display_course_content() {
366 $this->assertTrue($this->node->exposed_format_display_course_content('topic'));
367 $this->assertFalse($this->node->exposed_format_display_course_content('scorm'));
368 $this->assertTrue($this->node->exposed_format_display_course_content('dummy'));
369 }
7d2a0492 370 public function test_module_extends_navigation() {
47379255 371 $this->assertTrue($this->node->exposed_module_extends_navigation('data'));
e26507b3 372 $this->assertFalse($this->node->exposed_module_extends_navigation('test1'));
7d2a0492 373 }
374}
375
376/**
377 * This is a dummy object that allows us to call protected methods within the
378 * global navigation class by prefixing the methods with `exposed_`
379 */
380class exposed_navbar extends navbar {
381 protected $exposedkey = 'exposed_';
abaece1e
SH
382 public function __construct(moodle_page $page) {
383 parent::__construct($page);
7d2a0492 384 $this->cache = new navigation_cache('simpletest_nav');
385 }
386 function __call($method, $arguments) {
387 if (strpos($method,$this->exposedkey) !== false) {
388 $method = substr($method, strlen($this->exposedkey));
389 }
390 if (method_exists($this, $method)) {
391 return call_user_func_array(array($this, $method), $arguments);
392 }
393 throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
394 }
395}
396
abaece1e
SH
397class navigation_exposed_moodle_page extends moodle_page {
398 public function set_navigation(navigation_node $node) {
399 $this->_navigation = $node;
400 }
401}
402
7d2a0492 403class navbar_test extends UnitTestCase {
404 protected $node;
405 protected $oldnav;
406
407 public static $includecoverage = array('./lib/navigationlib.php');
408 public static $excludecoverage = array();
409
410 public function setUp() {
411 global $PAGE;
abaece1e 412
7d2a0492 413 $temptree = new global_navigation_test();
414 $temptree->setUp();
3406acde 415 $temptree->node->find('course2', navigation_node::TYPE_COURSE)->make_active();
abaece1e
SH
416
417 $page = new navigation_exposed_moodle_page();
418 $page->set_url($PAGE->url);
419 $page->set_context($PAGE->context);
420
421 $navigation = new exposed_global_navigation($page);
422 $navigation->children = $temptree->node->children;
423 $navigation->set_initialised();
424 $page->set_navigation($navigation);
425
426 $this->cache = new navigation_cache('simpletest_nav');
427 $this->node = new exposed_navbar($page);
7d2a0492 428 }
429 public function test_add() {
7d2a0492 430 // Add a node with all args set
a3bbac8b 431 $this->node->add('test_add_1','http://www.moodle.org/',navigation_node::TYPE_COURSE,'testadd1','testadd1',new pix_icon('i/course', ''));
7d2a0492 432 // Add a node with the minimum args required
3406acde 433 $this->node->add('test_add_2','http://www.moodle.org/',navigation_node::TYPE_COURSE,'testadd2','testadd2',new pix_icon('i/course', ''));
7d2a0492 434 $this->assertIsA($this->node->get('testadd1'), 'navigation_node');
3406acde 435 $this->assertIsA($this->node->get('testadd2'), 'navigation_node');
7d2a0492 436 }
437 public function test_has_items() {
7d2a0492 438 $this->assertTrue($this->node->has_items());
7d2a0492 439 }
7d2a0492 440}
441
442class navigation_cache_test extends UnitTestCase {
443 protected $cache;
444
445 public static $includecoverage = array('./lib/navigationlib.php');
446 public static $excludecoverage = array();
447
448 public function setUp() {
449 $this->cache = new navigation_cache('simpletest_nav');
450 $this->cache->anysetvariable = true;
451 }
452 public function test___get() {
453 $this->assertTrue($this->cache->anysetvariable);
454 $this->assertEqual($this->cache->notasetvariable, null);
455 }
456 public function test___set() {
457 $this->cache->myname = 'Sam Hemelryk';
458 $this->assertTrue($this->cache->cached('myname'));
459 $this->assertEqual($this->cache->myname, 'Sam Hemelryk');
460 }
461 public function test_cached() {
462 $this->assertTrue($this->cache->cached('anysetvariable'));
463 $this->assertFalse($this->cache->cached('notasetvariable'));
464 }
465 public function test_clear() {
466 $cache = clone($this->cache);
467 $this->assertTrue($cache->cached('anysetvariable'));
468 $cache->clear();
469 $this->assertFalse($cache->cached('anysetvariable'));
470 }
471 public function test_set() {
472 $this->cache->set('software', 'Moodle');
473 $this->assertTrue($this->cache->cached('software'));
474 $this->assertEqual($this->cache->software, 'Moodle');
475 }
476}
477
478/**
479 * This is a dummy object that allows us to call protected methods within the
480 * global navigation class by prefixing the methods with `exposed_`
481 */
482class exposed_settings_navigation extends settings_navigation {
483 protected $exposedkey = 'exposed_';
484 function __construct() {
485 global $PAGE;
486 parent::__construct($PAGE);
487 $this->cache = new navigation_cache('simpletest_nav');
488 }
489 function __call($method, $arguments) {
490 if (strpos($method,$this->exposedkey) !== false) {
491 $method = substr($method, strlen($this->exposedkey));
492 }
493 if (method_exists($this, $method)) {
494 return call_user_func_array(array($this, $method), $arguments);
495 }
496 throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
497 }
498}
499
500class settings_navigation_test extends UnitTestCase {
501 protected $node;
502 protected $cache;
503
504 public static $includecoverage = array('./lib/navigationlib.php');
505 public static $excludecoverage = array();
506
507 public function setUp() {
508 global $PAGE;
509 $this->cache = new navigation_cache('simpletest_nav');
510 $this->node = new exposed_settings_navigation();
511 }
512 public function test___construct() {
513 $this->node = new exposed_settings_navigation();
514 }
515 public function test___initialise() {
516 $this->node->initialise();
517 $this->assertEqual($this->node->id, 'settingsnav');
518 }
7d2a0492 519 public function test_in_alternative_role() {
520 $this->assertFalse($this->node->exposed_in_alternative_role());
521 }
95b97515 522}