MDL-50917 navigation: Unit test for viewing preference page.
[moodle.git] / lib / tests / navigationlib_test.php
CommitLineData
f4fd0a13
PS
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Unit tests for lib/navigationlib.php
19 *
20 * @package core
21 * @category phpunit
22 * @copyright 2009 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later (5)
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29require_once($CFG->libdir . '/navigationlib.php');
30
31
18c43230 32class core_navigationlib_testcase extends advanced_testcase {
f4fd0a13
PS
33 /**
34 * @var navigation_node
35 */
36 public $node;
37
18c43230
PS
38 protected function setup_node() {
39 global $PAGE, $SITE;
f4fd0a13
PS
40
41 $PAGE->set_url('/');
42 $PAGE->set_course($SITE);
43
18c43230
PS
44 $activeurl = $PAGE->url;
45 $inactiveurl = new moodle_url('http://www.moodle.com/');
f4fd0a13 46
18c43230 47 navigation_node::override_active_url($PAGE->url);
f4fd0a13
PS
48
49 $this->node = new navigation_node('Test Node');
50 $this->node->type = navigation_node::TYPE_SYSTEM;
18c43230 51 // We add the first child without key. This way we make sure all keys search by comparison is performed using ===.
e3b11a6e 52 $this->node->add('first child without key', null, navigation_node::TYPE_CUSTOM);
18c43230
PS
53 $demo1 = $this->node->add('demo1', $inactiveurl, navigation_node::TYPE_COURSE, null, 'demo1', new pix_icon('i/course', ''));
54 $demo2 = $this->node->add('demo2', $inactiveurl, navigation_node::TYPE_COURSE, null, 'demo2', new pix_icon('i/course', ''));
55 $demo3 = $this->node->add('demo3', $inactiveurl, navigation_node::TYPE_CATEGORY, null, 'demo3', new pix_icon('i/course', ''));
56 $demo4 = $demo3->add('demo4', $inactiveurl, navigation_node::TYPE_COURSE, null, 'demo4', new pix_icon('i/course', ''));
57 $demo5 = $demo3->add('demo5', $activeurl, navigation_node::TYPE_COURSE, null, 'demo5', new pix_icon('i/course', ''));
f4fd0a13 58 $demo5->add('activity1', null, navigation_node::TYPE_ACTIVITY, null, 'activity1')->make_active();
18c43230 59 $hiddendemo1 = $this->node->add('hiddendemo1', $inactiveurl, navigation_node::TYPE_CATEGORY, null, 'hiddendemo1', new pix_icon('i/course', ''));
f4fd0a13 60 $hiddendemo1->hidden = true;
18c43230
PS
61 $hiddendemo1->add('hiddendemo2', $inactiveurl, navigation_node::TYPE_COURSE, null, 'hiddendemo2', new pix_icon('i/course', ''))->helpbutton = 'Here is a help button';
62 $hiddendemo1->add('hiddendemo3', $inactiveurl, navigation_node::TYPE_COURSE, null, 'hiddendemo3', new pix_icon('i/course', ''))->display = false;
f4fd0a13
PS
63 }
64
18c43230
PS
65 public function test_node__construct() {
66 $this->setup_node();
67
68 $fakeproperties = array(
69 'text' => 'text',
70 'shorttext' => 'A very silly extra long short text string, more than 25 characters',
71 'key' => 'key',
72 'type' => 'navigation_node::TYPE_COURSE',
c78c8aaa 73 'action' => new moodle_url('http://www.moodle.org/'));
18c43230
PS
74
75 $node = new navigation_node($fakeproperties);
76 $this->assertSame($fakeproperties['text'], $node->text);
77 $this->assertTrue(strpos($fakeproperties['shorttext'], substr($node->shorttext, 0, -3)) === 0);
78 $this->assertSame($fakeproperties['key'], $node->key);
79 $this->assertSame($fakeproperties['type'], $node->type);
80 $this->assertSame($fakeproperties['action'], $node->action);
81 }
82
83 public function test_node_add() {
84 $this->setup_node();
85
86 // Add a node with all args set.
87 $node1 = $this->node->add('test_add_1', 'http://www.moodle.org/', navigation_node::TYPE_COURSE, 'testadd1', 'key', new pix_icon('i/course', ''));
88 // Add a node with the minimum args required.
89 $node2 = $this->node->add('test_add_2', null, navigation_node::TYPE_CUSTOM, 'testadd2');
90 $node3 = $this->node->add(str_repeat('moodle ', 15), str_repeat('moodle', 15));
f4fd0a13
PS
91
92 $this->assertInstanceOf('navigation_node', $node1);
93 $this->assertInstanceOf('navigation_node', $node2);
94 $this->assertInstanceOf('navigation_node', $node3);
95
96 $ref = $this->node->get('key');
97 $this->assertSame($node1, $ref);
98
99 $ref = $this->node->get($node2->key);
100 $this->assertSame($node2, $ref);
101
102 $ref = $this->node->get($node2->key, $node2->type);
103 $this->assertSame($node2, $ref);
104
105 $ref = $this->node->get($node3->key, $node3->type);
106 $this->assertSame($node3, $ref);
107 }
108
18c43230
PS
109 public function test_node_add_before() {
110 $this->setup_node();
111
112 // Create 3 nodes.
f4fd0a13
PS
113 $node1 = navigation_node::create('test_add_1', null, navigation_node::TYPE_CUSTOM,
114 'test 1', 'testadd1');
115 $node2 = navigation_node::create('test_add_2', null, navigation_node::TYPE_CUSTOM,
116 'test 2', 'testadd2');
117 $node3 = navigation_node::create('test_add_3', null, navigation_node::TYPE_CUSTOM,
118 'test 3', 'testadd3');
18c43230 119 // Add node 2, then node 1 before 2, then node 3 at end.
f4fd0a13
PS
120 $this->node->add_node($node2);
121 $this->node->add_node($node1, 'testadd2');
122 $this->node->add_node($node3);
18c43230
PS
123 // Check the last 3 nodes are in 1, 2, 3 order and have those indexes.
124 foreach ($this->node->children as $child) {
f4fd0a13
PS
125 $keys[] = $child->key;
126 }
18c43230
PS
127 $this->assertSame('testadd1', $keys[count($keys)-3]);
128 $this->assertSame('testadd2', $keys[count($keys)-2]);
129 $this->assertSame('testadd3', $keys[count($keys)-1]);
f4fd0a13
PS
130 }
131
18c43230
PS
132 public function test_node_add_class() {
133 $this->setup_node();
134
f4fd0a13
PS
135 $node = $this->node->get('demo1');
136 $this->assertInstanceOf('navigation_node', $node);
137 if ($node !== false) {
138 $node->add_class('myclass');
139 $classes = $node->classes;
18c43230 140 $this->assertContains('myclass', $classes);
f4fd0a13
PS
141 }
142 }
143
18c43230
PS
144 public function test_node_check_if_active() {
145 $this->setup_node();
f4fd0a13 146
f4fd0a13 147 // First test the string urls
18c43230 148 // Demo1 -> action is http://www.moodle.org/, thus should be true.
f4fd0a13
PS
149 $demo5 = $this->node->find('demo5', navigation_node::TYPE_COURSE);
150 if ($this->assertInstanceOf('navigation_node', $demo5)) {
151 $this->assertTrue($demo5->check_if_active());
152 }
153
18c43230 154 // Demo2 -> action is http://www.moodle.com/, thus should be false.
f4fd0a13
PS
155 $demo2 = $this->node->get('demo2');
156 if ($this->assertInstanceOf('navigation_node', $demo2)) {
157 $this->assertFalse($demo2->check_if_active());
158 }
159 }
160
18c43230
PS
161 public function test_node_contains_active_node() {
162 $this->setup_node();
163
164 // Demo5, and activity1 were set to active during setup.
165 // Should be true as it contains all nodes.
f4fd0a13 166 $this->assertTrue($this->node->contains_active_node());
18c43230 167 // Should be true as demo5 is a child of demo3.
f4fd0a13 168 $this->assertTrue($this->node->get('demo3')->contains_active_node());
18c43230 169 // Obviously duff.
f4fd0a13 170 $this->assertFalse($this->node->get('demo1')->contains_active_node());
18c43230 171 // Should be true as demo5 contains activity1.
f4fd0a13 172 $this->assertTrue($this->node->get('demo3')->get('demo5')->contains_active_node());
18c43230 173 // Should be true activity1 is the active node.
f4fd0a13 174 $this->assertTrue($this->node->get('demo3')->get('demo5')->get('activity1')->contains_active_node());
18c43230 175 // Obviously duff.
f4fd0a13
PS
176 $this->assertFalse($this->node->get('demo3')->get('demo4')->contains_active_node());
177 }
178
18c43230
PS
179 public function test_node_find_active_node() {
180 $this->setup_node();
181
f4fd0a13
PS
182 $activenode1 = $this->node->find_active_node();
183 $activenode2 = $this->node->get('demo1')->find_active_node();
184
185 if ($this->assertInstanceOf('navigation_node', $activenode1)) {
186 $ref = $this->node->get('demo3')->get('demo5')->get('activity1');
187 $this->assertSame($activenode1, $ref);
188 }
189
190 $this->assertNotInstanceOf('navigation_node', $activenode2);
191 }
192
18c43230
PS
193 public function test_node_find() {
194 $this->setup_node();
195
f4fd0a13
PS
196 $node1 = $this->node->find('demo1', navigation_node::TYPE_COURSE);
197 $node2 = $this->node->find('demo5', navigation_node::TYPE_COURSE);
198 $node3 = $this->node->find('demo5', navigation_node::TYPE_CATEGORY);
199 $node4 = $this->node->find('demo0', navigation_node::TYPE_COURSE);
200 $this->assertInstanceOf('navigation_node', $node1);
201 $this->assertInstanceOf('navigation_node', $node2);
202 $this->assertNotInstanceOf('navigation_node', $node3);
203 $this->assertNotInstanceOf('navigation_node', $node4);
204 }
205
18c43230
PS
206 public function test_node_find_expandable() {
207 $this->setup_node();
208
f4fd0a13
PS
209 $expandable = array();
210 $this->node->find_expandable($expandable);
18c43230
PS
211
212 $this->assertCount(0, $expandable);
f4fd0a13
PS
213 if (count($expandable) === 4) {
214 $name = $expandable[0]['key'];
215 $name .= $expandable[1]['key'];
216 $name .= $expandable[2]['key'];
217 $name .= $expandable[3]['key'];
18c43230 218 $this->assertSame('demo1demo2demo4hiddendemo2', $name);
f4fd0a13
PS
219 }
220 }
221
18c43230
PS
222 public function test_node_get() {
223 $this->setup_node();
224
225 $node1 = $this->node->get('demo1'); // Exists.
226 $node2 = $this->node->get('demo4'); // Doesn't exist for this node.
227 $node3 = $this->node->get('demo0'); // Doesn't exist at all.
228 $node4 = $this->node->get(false); // Sometimes occurs in nature code.
f4fd0a13
PS
229 $this->assertInstanceOf('navigation_node', $node1);
230 $this->assertFalse($node2);
231 $this->assertFalse($node3);
232 $this->assertFalse($node4);
233 }
234
18c43230
PS
235 public function test_node_get_css_type() {
236 $this->setup_node();
237
f4fd0a13
PS
238 $csstype1 = $this->node->get('demo3')->get_css_type();
239 $csstype2 = $this->node->get('demo3')->get('demo5')->get_css_type();
240 $this->node->get('demo3')->get('demo5')->type = 1000;
241 $csstype3 = $this->node->get('demo3')->get('demo5')->get_css_type();
18c43230
PS
242 $this->assertSame('type_category', $csstype1);
243 $this->assertSame('type_course', $csstype2);
244 $this->assertSame('type_unknown', $csstype3);
f4fd0a13
PS
245 }
246
18c43230 247 public function test_node_make_active() {
f4fd0a13 248 global $CFG;
18c43230
PS
249 $this->setup_node();
250
f4fd0a13
PS
251 $node1 = $this->node->add('active node 1', null, navigation_node::TYPE_CUSTOM, null, 'anode1');
252 $node2 = $this->node->add('active node 2', new moodle_url($CFG->wwwroot), navigation_node::TYPE_COURSE, null, 'anode2');
253 $node1->make_active();
254 $this->node->get('anode2')->make_active();
255 $this->assertTrue($node1->isactive);
256 $this->assertTrue($this->node->get('anode2')->isactive);
257 }
18c43230
PS
258
259 public function test_node_remove() {
260 $this->setup_node();
261
f4fd0a13
PS
262 $remove1 = $this->node->add('child to remove 1', null, navigation_node::TYPE_CUSTOM, null, 'remove1');
263 $remove2 = $this->node->add('child to remove 2', null, navigation_node::TYPE_CUSTOM, null, 'remove2');
264 $remove3 = $remove2->add('child to remove 3', null, navigation_node::TYPE_CUSTOM, null, 'remove3');
265
266 $this->assertInstanceOf('navigation_node', $remove1);
267 $this->assertInstanceOf('navigation_node', $remove2);
268 $this->assertInstanceOf('navigation_node', $remove3);
269
270 $this->assertInstanceOf('navigation_node', $this->node->get('remove1'));
271 $this->assertInstanceOf('navigation_node', $this->node->get('remove2'));
272 $this->assertInstanceOf('navigation_node', $remove2->get('remove3'));
273
e3b11a6e 274 // Remove element and make sure this is no longer a child.
f4fd0a13 275 $this->assertTrue($remove1->remove());
e3b11a6e
MG
276 $this->assertFalse($this->node->get('remove1'));
277 $this->assertFalse(in_array('remove1', $this->node->get_children_key_list(), true));
278
18c43230 279 // Make sure that we can insert element after removal.
5150c302
MG
280 $insertelement = navigation_node::create('extra element 4', null, navigation_node::TYPE_CUSTOM, null, 'element4');
281 $this->node->add_node($insertelement, 'remove2');
282 $this->assertNotEmpty($this->node->get('element4'));
283
18c43230 284 // Remove more elements.
f4fd0a13 285 $this->assertTrue($this->node->get('remove2')->remove());
e3b11a6e 286 $this->assertFalse($this->node->get('remove2'));
5150c302 287
18c43230 288 // Make sure that we can add element after removal.
5150c302
MG
289 $this->node->add('extra element 5', null, navigation_node::TYPE_CUSTOM, null, 'element5');
290 $this->assertNotEmpty($this->node->get('element5'));
291
f4fd0a13
PS
292 $this->assertTrue($remove2->get('remove3')->remove());
293
294 $this->assertFalse($this->node->get('remove1'));
295 $this->assertFalse($this->node->get('remove2'));
296 }
18c43230
PS
297
298 public function test_node_remove_class() {
299 $this->setup_node();
300
f4fd0a13
PS
301 $this->node->add_class('testclass');
302 $this->assertTrue($this->node->remove_class('testclass'));
18c43230
PS
303 $this->assertNotContains('testclass', $this->node->classes);
304 }
305
306 public function test_module_extends_navigation() {
307 $node = new exposed_global_navigation();
308 // Create an initial tree structure to work with.
309 $cat1 = $node->add('category 1', null, navigation_node::TYPE_CATEGORY, null, 'cat1');
310 $cat2 = $node->add('category 2', null, navigation_node::TYPE_CATEGORY, null, 'cat2');
311 $cat3 = $node->add('category 3', null, navigation_node::TYPE_CATEGORY, null, 'cat3');
312 $sub1 = $cat2->add('sub category 1', null, navigation_node::TYPE_CATEGORY, null, 'sub1');
313 $sub2 = $cat2->add('sub category 2', null, navigation_node::TYPE_CATEGORY, null, 'sub2');
314 $sub3 = $cat2->add('sub category 3', null, navigation_node::TYPE_CATEGORY, null, 'sub3');
315 $course1 = $sub2->add('course 1', null, navigation_node::TYPE_COURSE, null, 'course1');
316 $course2 = $sub2->add('course 2', null, navigation_node::TYPE_COURSE, null, 'course2');
317 $course3 = $sub2->add('course 3', null, navigation_node::TYPE_COURSE, null, 'course3');
318 $section1 = $course2->add('section 1', null, navigation_node::TYPE_SECTION, null, 'sec1');
319 $section2 = $course2->add('section 2', null, navigation_node::TYPE_SECTION, null, 'sec2');
320 $section3 = $course2->add('section 3', null, navigation_node::TYPE_SECTION, null, 'sec3');
321 $act1 = $section2->add('activity 1', null, navigation_node::TYPE_ACTIVITY, null, 'act1');
322 $act2 = $section2->add('activity 2', null, navigation_node::TYPE_ACTIVITY, null, 'act2');
323 $act3 = $section2->add('activity 3', null, navigation_node::TYPE_ACTIVITY, null, 'act3');
324 $res1 = $section2->add('resource 1', null, navigation_node::TYPE_RESOURCE, null, 'res1');
325 $res2 = $section2->add('resource 2', null, navigation_node::TYPE_RESOURCE, null, 'res2');
326 $res3 = $section2->add('resource 3', null, navigation_node::TYPE_RESOURCE, null, 'res3');
327
328 $this->assertTrue($node->exposed_module_extends_navigation('data'));
329 $this->assertFalse($node->exposed_module_extends_navigation('test1'));
330 }
331
4d5c7cb0
SH
332 public function test_navbar_prepend_and_add() {
333 global $PAGE;
334 // Unfortunate hack needed because people use global $PAGE around the place.
18c43230 335 $PAGE->set_url('/');
18c43230 336
4d5c7cb0
SH
337 // We need to reset after this test because we using the generator.
338 $this->resetAfterTest();
18c43230 339
4d5c7cb0
SH
340 $generator = self::getDataGenerator();
341 $cat1 = $generator->create_category();
342 $cat2 = $generator->create_category(array('parent' => $cat1->id));
343 $course = $generator->create_course(array('category' => $cat2->id));
344
345 $page = new moodle_page();
346 $page->set_course($course);
347 $page->set_url(new moodle_url('/course/view.php', array('id' => $course->id)));
348 $page->navbar->prepend('test 1');
349 $page->navbar->prepend('test 2');
350 $page->navbar->add('test 3');
351 $page->navbar->add('test 4');
352
353 $items = $page->navbar->get_items();
354 foreach ($items as $item) {
355 $this->assertInstanceOf('navigation_node', $item);
356 }
18c43230 357
4d5c7cb0
SH
358 $i = 0;
359 $this->assertSame('test 1', $items[$i++]->text);
360 $this->assertSame('test 2', $items[$i++]->text);
361 $this->assertSame('home', $items[$i++]->key);
362 $this->assertSame('courses', $items[$i++]->key);
363 $this->assertSame($cat1->id, $items[$i++]->key);
364 $this->assertSame($cat2->id, $items[$i++]->key);
365 $this->assertSame($course->id, $items[$i++]->key);
366 $this->assertSame('test 3', $items[$i++]->text);
367 $this->assertSame('test 4', $items[$i++]->text);
368
369 return $page;
18c43230
PS
370 }
371
372 /**
4d5c7cb0 373 * @depends test_navbar_prepend_and_add
18c43230
PS
374 * @param $node
375 */
4d5c7cb0 376 public function test_navbar_has_items(moodle_page $page) {
18c43230
PS
377 $this->resetAfterTest();
378
4d5c7cb0 379 $this->assertTrue($page->navbar->has_items());
18c43230
PS
380 }
381
382 public function test_cache__get() {
383 $cache = new navigation_cache('unittest_nav');
384 $cache->anysetvariable = true;
385
386 $this->assertTrue($cache->anysetvariable);
387 $this->assertEquals($cache->notasetvariable, null);
388 }
389
390 public function test_cache__set() {
391 $cache = new navigation_cache('unittest_nav');
392 $cache->anysetvariable = true;
393
394 $cache->myname = 'Sam Hemelryk';
395 $this->assertTrue($cache->cached('myname'));
396 $this->assertSame('Sam Hemelryk', $cache->myname);
397 }
398
399 public function test_cache_cached() {
400 $cache = new navigation_cache('unittest_nav');
401 $cache->anysetvariable = true;
402
403 $this->assertTrue($cache->cached('anysetvariable'));
404 $this->assertFalse($cache->cached('notasetvariable'));
405 }
406
407 public function test_cache_clear() {
408 $cache = new navigation_cache('unittest_nav');
409 $cache->anysetvariable = true;
410
411 $cache = clone($cache);
412 $this->assertTrue($cache->cached('anysetvariable'));
413 $cache->clear();
414 $this->assertFalse($cache->cached('anysetvariable'));
415 }
416
417 public function test_cache_set() {
418 $cache = new navigation_cache('unittest_nav');
419 $cache->anysetvariable = true;
420
421 $cache->set('software', 'Moodle');
422 $this->assertTrue($cache->cached('software'));
423 $this->assertEquals($cache->software, 'Moodle');
424 }
425
426 public function test_setting___construct() {
427 global $PAGE, $SITE;
428
429 $this->resetAfterTest(false);
430
431 $PAGE->set_url('/');
432 $PAGE->set_course($SITE);
433
434 $node = new exposed_settings_navigation();
435
436 return $node;
437 }
438
439 /**
440 * @depends test_setting___construct
441 * @param mixed $node
442 * @return mixed
443 */
444 public function test_setting__initialise($node) {
445 $this->resetAfterTest(false);
446
447 $node->initialise();
448 $this->assertEquals($node->id, 'settingsnav');
449
450 return $node;
451 }
452
508e364f
AG
453 /**
454 * Test that users with the correct permissions can view the preferences page.
455 */
456 public function test_can_view_user_preferences() {
457 global $PAGE, $DB, $SITE;
458 $this->resetAfterTest();
459
460 $persontoview = $this->getDataGenerator()->create_user();
461 $persondoingtheviewing = $this->getDataGenerator()->create_user();
462
463 $PAGE->set_url('/');
464 $PAGE->set_course($SITE);
465
466 // Check that a standard user can not view the preferences page.
467 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
468 $this->getDataGenerator()->role_assign($studentrole->id, $persondoingtheviewing->id);
469 $this->setUser($persondoingtheviewing);
470 $settingsnav = new exposed_settings_navigation();
471 $settingsnav->initialise();
472 $settingsnav->extend_for_user($persontoview->id);
473 $this->assertFalse($settingsnav->can_view_user_preferences($persontoview->id));
474
475 // Set persondoingtheviewing as a manager.
476 $managerrole = $DB->get_record('role', array('shortname' => 'manager'));
477 $this->getDataGenerator()->role_assign($managerrole->id, $persondoingtheviewing->id);
478 $settingsnav = new exposed_settings_navigation();
479 $settingsnav->initialise();
480 $settingsnav->extend_for_user($persontoview->id);
481 $this->assertTrue($settingsnav->can_view_user_preferences($persontoview->id));
482
483 // Check that the admin can view the preferences page.
484 $this->setAdminUser();
485 $settingsnav = new exposed_settings_navigation();
486 $settingsnav->initialise();
487 $settingsnav->extend_for_user($persontoview->id);
488 $preferencenode = $settingsnav->find('userviewingsettings' . $persontoview->id, null);
489 $this->assertTrue($settingsnav->can_view_user_preferences($persontoview->id));
490 }
491
18c43230
PS
492 /**
493 * @depends test_setting__initialise
494 * @param mixed $node
495 * @return mixed
496 */
497 public function test_setting_in_alternative_role($node) {
498 $this->resetAfterTest();
499
500 $this->assertFalse($node->exposed_in_alternative_role());
f4fd0a13
PS
501 }
502}
503
18c43230 504
f4fd0a13
PS
505/**
506 * This is a dummy object that allows us to call protected methods within the
507 * global navigation class by prefixing the methods with `exposed_`
508 */
509class exposed_global_navigation extends global_navigation {
510 protected $exposedkey = 'exposed_';
511 public function __construct(moodle_page $page=null) {
512 global $PAGE;
513 if ($page === null) {
514 $page = $PAGE;
515 }
516 parent::__construct($page);
4ca04fb5 517 $this->cache = new navigation_cache('unittest_nav');
f4fd0a13
PS
518 }
519 public function __call($method, $arguments) {
18c43230 520 if (strpos($method, $this->exposedkey) !== false) {
f4fd0a13
PS
521 $method = substr($method, strlen($this->exposedkey));
522 }
523 if (method_exists($this, $method)) {
524 return call_user_func_array(array($this, $method), $arguments);
525 }
526 throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
527 }
528 public function set_initialised() {
529 $this->initialised = true;
530 }
531}
532
18c43230 533
f4fd0a13
PS
534class mock_initialise_global_navigation extends global_navigation {
535
18c43230 536 protected static $count = 1;
f4fd0a13
PS
537
538 public function load_for_category() {
539 $this->add('load_for_category', null, null, null, 'initcall'.self::$count);
540 self::$count++;
541 return 0;
542 }
543
544 public function load_for_course() {
545 $this->add('load_for_course', null, null, null, 'initcall'.self::$count);
546 self::$count++;
547 return 0;
548 }
549
550 public function load_for_activity() {
551 $this->add('load_for_activity', null, null, null, 'initcall'.self::$count);
552 self::$count++;
553 return 0;
554 }
555
556 public function load_for_user($user=null, $forceforcontext=false) {
557 $this->add('load_for_user', null, null, null, 'initcall'.self::$count);
558 self::$count++;
559 return 0;
560 }
561}
562
f4fd0a13
PS
563/**
564 * This is a dummy object that allows us to call protected methods within the
18c43230 565 * global navigation class by prefixing the methods with `exposed_`.
f4fd0a13
PS
566 */
567class exposed_navbar extends navbar {
568 protected $exposedkey = 'exposed_';
569 public function __construct(moodle_page $page) {
570 parent::__construct($page);
4ca04fb5 571 $this->cache = new navigation_cache('unittest_nav');
f4fd0a13 572 }
18c43230
PS
573 public function __call($method, $arguments) {
574 if (strpos($method, $this->exposedkey) !== false) {
f4fd0a13
PS
575 $method = substr($method, strlen($this->exposedkey));
576 }
577 if (method_exists($this, $method)) {
578 return call_user_func_array(array($this, $method), $arguments);
579 }
580 throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
581 }
582}
583
584class navigation_exposed_moodle_page extends moodle_page {
585 public function set_navigation(navigation_node $node) {
586 $this->_navigation = $node;
587 }
588}
589
f4fd0a13
PS
590/**
591 * This is a dummy object that allows us to call protected methods within the
18c43230 592 * global navigation class by prefixing the methods with `exposed_`.
f4fd0a13
PS
593 */
594class exposed_settings_navigation extends settings_navigation {
595 protected $exposedkey = 'exposed_';
18c43230 596 public function __construct() {
f4fd0a13
PS
597 global $PAGE;
598 parent::__construct($PAGE);
4ca04fb5 599 $this->cache = new navigation_cache('unittest_nav');
f4fd0a13 600 }
18c43230
PS
601 public function __call($method, $arguments) {
602 if (strpos($method, $this->exposedkey) !== false) {
f4fd0a13
PS
603 $method = substr($method, strlen($this->exposedkey));
604 }
605 if (method_exists($this, $method)) {
606 return call_user_func_array(array($this, $method), $arguments);
607 }
608 throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
609 }
610}