2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
20 use global_navigation
;
24 use navigation_node_collection
;
27 use settings_navigation
;
29 defined('MOODLE_INTERNAL') ||
die();
32 require_once($CFG->libdir
. '/navigationlib.php');
35 * Unit tests for lib/navigationlib.php
39 * @copyright 2009 Sam Hemelryk
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later (5)
42 class navigationlib_test
extends \advanced_testcase
{
44 * @var navigation_node
48 protected function setup_node() {
52 $PAGE->set_course($SITE);
54 $activeurl = $PAGE->url
;
55 $inactiveurl = new \
moodle_url('http://www.moodle.com/');
57 navigation_node
::override_active_url($PAGE->url
);
59 $this->node
= new navigation_node('Test Node');
60 $this->node
->type
= navigation_node
::TYPE_SYSTEM
;
61 // We add the first child without key. This way we make sure all keys search by comparison is performed using ===.
62 $this->node
->add('first child without key', null, navigation_node
::TYPE_CUSTOM
);
63 $demo1 = $this->node
->add('demo1', $inactiveurl, navigation_node
::TYPE_COURSE
, null, 'demo1', new pix_icon('i/course', ''));
64 $demo2 = $this->node
->add('demo2', $inactiveurl, navigation_node
::TYPE_COURSE
, null, 'demo2', new pix_icon('i/course', ''));
65 $demo3 = $this->node
->add('demo3', $inactiveurl, navigation_node
::TYPE_CATEGORY
, null, 'demo3', new pix_icon('i/course', ''));
66 $demo4 = $demo3->add('demo4', $inactiveurl, navigation_node
::TYPE_COURSE
, null, 'demo4', new pix_icon('i/course', ''));
67 $demo5 = $demo3->add('demo5', $activeurl, navigation_node
::TYPE_COURSE
, null, 'demo5', new pix_icon('i/course', ''));
68 $demo5->add('activity1', null, navigation_node
::TYPE_ACTIVITY
, null, 'activity1')->make_active();
69 $demo6 = $demo3->add('demo6', null, navigation_node
::TYPE_CONTAINER
, 'container node test', 'demo6');
70 $hiddendemo1 = $this->node
->add('hiddendemo1', $inactiveurl, navigation_node
::TYPE_CATEGORY
, null, 'hiddendemo1', new pix_icon('i/course', ''));
71 $hiddendemo1->hidden
= true;
72 $hiddendemo1->add('hiddendemo2', $inactiveurl, navigation_node
::TYPE_COURSE
, null, 'hiddendemo2', new pix_icon('i/course', ''))->helpbutton
= 'Here is a help button';
73 $hiddendemo1->add('hiddendemo3', $inactiveurl, navigation_node
::TYPE_COURSE
, null, 'hiddendemo3', new pix_icon('i/course', ''))->display
= false;
76 public function test_node__construct() {
79 $fakeproperties = array(
81 'shorttext' => 'A very silly extra long short text string, more than 25 characters',
83 'type' => 'navigation_node::TYPE_COURSE',
84 'action' => new \
moodle_url('http://www.moodle.org/'));
86 $node = new navigation_node($fakeproperties);
87 $this->assertSame($fakeproperties['text'], $node->text
);
88 $this->assertTrue(strpos($fakeproperties['shorttext'], substr($node->shorttext
, 0, -3)) === 0);
89 $this->assertSame($fakeproperties['key'], $node->key
);
90 $this->assertSame($fakeproperties['type'], $node->type
);
91 $this->assertSame($fakeproperties['action'], $node->action
);
94 public function test_node_add() {
97 // Add a node with all args set.
98 $node1 = $this->node
->add('test_add_1', 'http://www.moodle.org/', navigation_node
::TYPE_COURSE
, 'testadd1', 'key', new pix_icon('i/course', ''));
99 // Add a node with the minimum args required.
100 $node2 = $this->node
->add('test_add_2', null, navigation_node
::TYPE_CUSTOM
, 'testadd2');
101 $node3 = $this->node
->add(str_repeat('moodle ', 15), str_repeat('moodle', 15));
103 $this->assertInstanceOf('navigation_node', $node1);
104 $this->assertInstanceOf('navigation_node', $node2);
105 $this->assertInstanceOf('navigation_node', $node3);
107 $ref = $this->node
->get('key');
108 $this->assertSame($node1, $ref);
110 $ref = $this->node
->get($node2->key
);
111 $this->assertSame($node2, $ref);
113 $ref = $this->node
->get($node2->key
, $node2->type
);
114 $this->assertSame($node2, $ref);
116 $ref = $this->node
->get($node3->key
, $node3->type
);
117 $this->assertSame($node3, $ref);
120 public function test_node_add_before() {
124 $node1 = navigation_node
::create('test_add_1', null, navigation_node
::TYPE_CUSTOM
,
125 'test 1', 'testadd1');
126 $node2 = navigation_node
::create('test_add_2', null, navigation_node
::TYPE_CUSTOM
,
127 'test 2', 'testadd2');
128 $node3 = navigation_node
::create('test_add_3', null, navigation_node
::TYPE_CUSTOM
,
129 'test 3', 'testadd3');
130 // Add node 2, then node 1 before 2, then node 3 at end.
131 $this->node
->add_node($node2);
132 $this->node
->add_node($node1, 'testadd2');
133 $this->node
->add_node($node3);
134 // Check the last 3 nodes are in 1, 2, 3 order and have those indexes.
135 foreach ($this->node
->children
as $child) {
136 $keys[] = $child->key
;
138 $this->assertSame('testadd1', $keys[count($keys)-3]);
139 $this->assertSame('testadd2', $keys[count($keys)-2]);
140 $this->assertSame('testadd3', $keys[count($keys)-1]);
143 public function test_node_add_class() {
146 $node = $this->node
->get('demo1');
147 $this->assertInstanceOf('navigation_node', $node);
148 if ($node !== false) {
149 $node->add_class('myclass');
150 $classes = $node->classes
;
151 $this->assertContains('myclass', $classes);
156 * Test the add_attribute method.
157 * @covers \navigation_node::add_attribute
159 public function test_node_add_attribute(): void
{
162 $node = $this->node
->get('demo1');
163 $this->assertInstanceOf('navigation_node', $node);
164 if ($node !== false) {
165 $node->add_attribute('data-foo', 'bar');
166 $attribute = reset($node->attributes
);
167 $this->assertEqualsCanonicalizing(['name' => 'data-foo', 'value' => 'bar'], $attribute);
171 public function test_node_check_if_active() {
174 // First test the string urls
175 // Demo1 -> action is http://www.moodle.org/, thus should be true.
176 $demo5 = $this->node
->find('demo5', navigation_node
::TYPE_COURSE
);
177 if ($this->assertInstanceOf('navigation_node', $demo5)) {
178 $this->assertTrue($demo5->check_if_active());
181 // Demo2 -> action is http://www.moodle.com/, thus should be false.
182 $demo2 = $this->node
->get('demo2');
183 if ($this->assertInstanceOf('navigation_node', $demo2)) {
184 $this->assertFalse($demo2->check_if_active());
188 public function test_node_contains_active_node() {
191 // Demo5, and activity1 were set to active during setup.
192 // Should be true as it contains all nodes.
193 $this->assertTrue($this->node
->contains_active_node());
194 // Should be true as demo5 is a child of demo3.
195 $this->assertTrue($this->node
->get('demo3')->contains_active_node());
197 $this->assertFalse($this->node
->get('demo1')->contains_active_node());
198 // Should be true as demo5 contains activity1.
199 $this->assertTrue($this->node
->get('demo3')->get('demo5')->contains_active_node());
200 // Should be true activity1 is the active node.
201 $this->assertTrue($this->node
->get('demo3')->get('demo5')->get('activity1')->contains_active_node());
203 $this->assertFalse($this->node
->get('demo3')->get('demo4')->contains_active_node());
206 public function test_node_find_active_node() {
209 $activenode1 = $this->node
->find_active_node();
210 $activenode2 = $this->node
->get('demo1')->find_active_node();
212 if ($this->assertInstanceOf('navigation_node', $activenode1)) {
213 $ref = $this->node
->get('demo3')->get('demo5')->get('activity1');
214 $this->assertSame($activenode1, $ref);
217 $this->assertNotInstanceOf('navigation_node', $activenode2);
220 public function test_node_find() {
223 $node1 = $this->node
->find('demo1', navigation_node
::TYPE_COURSE
);
224 $node2 = $this->node
->find('demo5', navigation_node
::TYPE_COURSE
);
225 $node3 = $this->node
->find('demo5', navigation_node
::TYPE_CATEGORY
);
226 $node4 = $this->node
->find('demo0', navigation_node
::TYPE_COURSE
);
227 $this->assertInstanceOf('navigation_node', $node1);
228 $this->assertInstanceOf('navigation_node', $node2);
229 $this->assertNotInstanceOf('navigation_node', $node3);
230 $this->assertNotInstanceOf('navigation_node', $node4);
233 public function test_node_find_expandable() {
236 $expandable = array();
237 $this->node
->find_expandable($expandable);
239 $this->assertCount(0, $expandable);
240 if (count($expandable) === 4) {
241 $name = $expandable[0]['key'];
242 $name .= $expandable[1]['key'];
243 $name .= $expandable[2]['key'];
244 $name .= $expandable[3]['key'];
245 $this->assertSame('demo1demo2demo4hiddendemo2', $name);
249 public function test_node_get() {
252 $node1 = $this->node
->get('demo1'); // Exists.
253 $node2 = $this->node
->get('demo4'); // Doesn't exist for this node.
254 $node3 = $this->node
->get('demo0'); // Doesn't exist at all.
255 $node4 = $this->node
->get(false); // Sometimes occurs in nature code.
256 $this->assertInstanceOf('navigation_node', $node1);
257 $this->assertFalse($node2);
258 $this->assertFalse($node3);
259 $this->assertFalse($node4);
262 public function test_node_get_css_type() {
265 $csstype1 = $this->node
->get('demo3')->get_css_type();
266 $csstype2 = $this->node
->get('demo3')->get('demo5')->get_css_type();
267 $this->node
->get('demo3')->get('demo5')->type
= 1000;
268 $csstype3 = $this->node
->get('demo3')->get('demo5')->get_css_type();
269 $csstype4 = $this->node
->get('demo3')->get('demo6')->get_css_type();
270 $this->assertSame('type_category', $csstype1);
271 $this->assertSame('type_course', $csstype2);
272 $this->assertSame('type_unknown', $csstype3);
273 $this->assertSame('type_container', $csstype4);
276 public function test_node_make_active() {
280 $node1 = $this->node
->add('active node 1', null, navigation_node
::TYPE_CUSTOM
, null, 'anode1');
281 $node2 = $this->node
->add('active node 2', new \
moodle_url($CFG->wwwroot
), navigation_node
::TYPE_COURSE
, null, 'anode2');
282 $node1->make_active();
283 $this->node
->get('anode2')->make_active();
284 $this->assertTrue($node1->isactive
);
285 $this->assertTrue($this->node
->get('anode2')->isactive
);
288 public function test_node_remove() {
291 $remove1 = $this->node
->add('child to remove 1', null, navigation_node
::TYPE_CUSTOM
, null, 'remove1');
292 $remove2 = $this->node
->add('child to remove 2', null, navigation_node
::TYPE_CUSTOM
, null, 'remove2');
293 $remove3 = $remove2->add('child to remove 3', null, navigation_node
::TYPE_CUSTOM
, null, 'remove3');
295 $this->assertInstanceOf('navigation_node', $remove1);
296 $this->assertInstanceOf('navigation_node', $remove2);
297 $this->assertInstanceOf('navigation_node', $remove3);
299 $this->assertInstanceOf('navigation_node', $this->node
->get('remove1'));
300 $this->assertInstanceOf('navigation_node', $this->node
->get('remove2'));
301 $this->assertInstanceOf('navigation_node', $remove2->get('remove3'));
303 // Remove element and make sure this is no longer a child.
304 $this->assertTrue($remove1->remove());
305 $this->assertFalse($this->node
->get('remove1'));
306 $this->assertFalse(in_array('remove1', $this->node
->get_children_key_list(), true));
308 // Make sure that we can insert element after removal.
309 $insertelement = navigation_node
::create('extra element 4', null, navigation_node
::TYPE_CUSTOM
, null, 'element4');
310 $this->node
->add_node($insertelement, 'remove2');
311 $this->assertNotEmpty($this->node
->get('element4'));
313 // Remove more elements.
314 $this->assertTrue($this->node
->get('remove2')->remove());
315 $this->assertFalse($this->node
->get('remove2'));
317 // Make sure that we can add element after removal.
318 $this->node
->add('extra element 5', null, navigation_node
::TYPE_CUSTOM
, null, 'element5');
319 $this->assertNotEmpty($this->node
->get('element5'));
321 $this->assertTrue($remove2->get('remove3')->remove());
323 $this->assertFalse($this->node
->get('remove1'));
324 $this->assertFalse($this->node
->get('remove2'));
327 public function test_node_remove_class() {
330 $this->node
->add_class('testclass');
331 $this->assertTrue($this->node
->remove_class('testclass'));
332 $this->assertNotContains('testclass', $this->node
->classes
);
335 public function test_module_extends_navigation() {
336 $node = new exposed_global_navigation();
337 // Create an initial tree structure to work with.
338 $cat1 = $node->add('category 1', null, navigation_node
::TYPE_CATEGORY
, null, 'cat1');
339 $cat2 = $node->add('category 2', null, navigation_node
::TYPE_CATEGORY
, null, 'cat2');
340 $cat3 = $node->add('category 3', null, navigation_node
::TYPE_CATEGORY
, null, 'cat3');
341 $sub1 = $cat2->add('sub category 1', null, navigation_node
::TYPE_CATEGORY
, null, 'sub1');
342 $sub2 = $cat2->add('sub category 2', null, navigation_node
::TYPE_CATEGORY
, null, 'sub2');
343 $sub3 = $cat2->add('sub category 3', null, navigation_node
::TYPE_CATEGORY
, null, 'sub3');
344 $course1 = $sub2->add('course 1', null, navigation_node
::TYPE_COURSE
, null, 'course1');
345 $course2 = $sub2->add('course 2', null, navigation_node
::TYPE_COURSE
, null, 'course2');
346 $course3 = $sub2->add('course 3', null, navigation_node
::TYPE_COURSE
, null, 'course3');
347 $section1 = $course2->add('section 1', null, navigation_node
::TYPE_SECTION
, null, 'sec1');
348 $section2 = $course2->add('section 2', null, navigation_node
::TYPE_SECTION
, null, 'sec2');
349 $section3 = $course2->add('section 3', null, navigation_node
::TYPE_SECTION
, null, 'sec3');
350 $act1 = $section2->add('activity 1', null, navigation_node
::TYPE_ACTIVITY
, null, 'act1');
351 $act2 = $section2->add('activity 2', null, navigation_node
::TYPE_ACTIVITY
, null, 'act2');
352 $act3 = $section2->add('activity 3', null, navigation_node
::TYPE_ACTIVITY
, null, 'act3');
353 $res1 = $section2->add('resource 1', null, navigation_node
::TYPE_RESOURCE
, null, 'res1');
354 $res2 = $section2->add('resource 2', null, navigation_node
::TYPE_RESOURCE
, null, 'res2');
355 $res3 = $section2->add('resource 3', null, navigation_node
::TYPE_RESOURCE
, null, 'res3');
357 $this->assertTrue($node->exposed_module_extends_navigation('data'));
358 $this->assertFalse($node->exposed_module_extends_navigation('test1'));
361 public function test_navbar_prepend_and_add() {
363 // Unfortunate hack needed because people use global $PAGE around the place.
366 // We need to reset after this test because we using the generator.
367 $this->resetAfterTest();
369 $generator = self
::getDataGenerator();
370 $cat1 = $generator->create_category();
371 $cat2 = $generator->create_category(array('parent' => $cat1->id
));
372 $course = $generator->create_course(array('category' => $cat2->id
));
374 $page = new \
moodle_page();
375 $page->set_course($course);
376 $page->set_url(new \
moodle_url('/course/view.php', array('id' => $course->id
)));
377 $page->navbar
->prepend('test 1');
378 $page->navbar
->prepend('test 2');
379 $page->navbar
->add('test 3');
380 $page->navbar
->add('test 4');
382 $items = $page->navbar
->get_items();
383 foreach ($items as $item) {
384 $this->assertInstanceOf('navigation_node', $item);
388 $this->assertSame('test 1', $items[$i++
]->text
);
389 $this->assertSame('test 2', $items[$i++
]->text
);
390 $this->assertSame('home', $items[$i++
]->key
);
391 $this->assertSame('courses', $items[$i++
]->key
);
392 $this->assertSame($cat1->id
, $items[$i++
]->key
);
393 $this->assertSame($cat2->id
, $items[$i++
]->key
);
394 $this->assertSame($course->id
, $items[$i++
]->key
);
395 $this->assertSame('test 3', $items[$i++
]->text
);
396 $this->assertSame('test 4', $items[$i++
]->text
);
402 * @depends test_navbar_prepend_and_add
405 public function test_navbar_has_items(\moodle_page
$page) {
406 $this->resetAfterTest();
408 $this->assertTrue($page->navbar
->has_items());
411 public function test_cache__get() {
412 $cache = new navigation_cache('unittest_nav');
413 $cache->anysetvariable
= true;
415 $this->assertTrue($cache->anysetvariable
);
416 $this->assertEquals($cache->notasetvariable
, null);
419 public function test_cache__set() {
420 $cache = new navigation_cache('unittest_nav');
421 $cache->anysetvariable
= true;
423 $cache->myname
= 'Sam Hemelryk';
424 $this->assertTrue($cache->cached('myname'));
425 $this->assertSame('Sam Hemelryk', $cache->myname
);
428 public function test_cache_cached() {
429 $cache = new navigation_cache('unittest_nav');
430 $cache->anysetvariable
= true;
432 $this->assertTrue($cache->cached('anysetvariable'));
433 $this->assertFalse($cache->cached('notasetvariable'));
436 public function test_cache_clear() {
437 $cache = new navigation_cache('unittest_nav');
438 $cache->anysetvariable
= true;
440 $cache = clone($cache);
441 $this->assertTrue($cache->cached('anysetvariable'));
443 $this->assertFalse($cache->cached('anysetvariable'));
446 public function test_cache_set() {
447 $cache = new navigation_cache('unittest_nav');
448 $cache->anysetvariable
= true;
450 $cache->set('software', 'Moodle');
451 $this->assertTrue($cache->cached('software'));
452 $this->assertEquals($cache->software
, 'Moodle');
455 public function test_setting___construct() {
458 $this->resetAfterTest(false);
461 $PAGE->set_course($SITE);
463 $node = new exposed_settings_navigation();
469 * @depends test_setting___construct
473 public function test_setting__initialise($node) {
474 $this->resetAfterTest(false);
477 $this->assertEquals($node->id
, 'settingsnav');
483 * Test that users with the correct permissions can view the preferences page.
485 public function test_can_view_user_preferences() {
486 global $PAGE, $DB, $SITE;
487 $this->resetAfterTest();
489 $persontoview = $this->getDataGenerator()->create_user();
490 $persondoingtheviewing = $this->getDataGenerator()->create_user();
493 $PAGE->set_course($SITE);
495 // Check that a standard user can not view the preferences page.
496 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
497 $this->getDataGenerator()->role_assign($studentrole->id
, $persondoingtheviewing->id
);
498 $this->setUser($persondoingtheviewing);
499 $settingsnav = new exposed_settings_navigation();
500 $settingsnav->initialise();
501 $settingsnav->extend_for_user($persontoview->id
);
502 $this->assertFalse($settingsnav->can_view_user_preferences($persontoview->id
));
504 // Set persondoingtheviewing as a manager.
505 $managerrole = $DB->get_record('role', array('shortname' => 'manager'));
506 $this->getDataGenerator()->role_assign($managerrole->id
, $persondoingtheviewing->id
);
507 $settingsnav = new exposed_settings_navigation();
508 $settingsnav->initialise();
509 $settingsnav->extend_for_user($persontoview->id
);
510 $this->assertTrue($settingsnav->can_view_user_preferences($persontoview->id
));
512 // Check that the admin can view the preferences page.
513 $this->setAdminUser();
514 $settingsnav = new exposed_settings_navigation();
515 $settingsnav->initialise();
516 $settingsnav->extend_for_user($persontoview->id
);
517 $preferencenode = $settingsnav->find('userviewingsettings' . $persontoview->id
, null);
518 $this->assertTrue($settingsnav->can_view_user_preferences($persontoview->id
));
522 * @depends test_setting__initialise
526 public function test_setting_in_alternative_role($node) {
527 $this->resetAfterTest();
529 $this->assertFalse($node->exposed_in_alternative_role());
533 public function test_navigation_node_collection_remove_with_no_type() {
534 $navigationnodecollection = new navigation_node_collection();
536 $this->node
->key
= 100;
539 $this->assertEquals(0, count($navigationnodecollection->get_key_list()));
542 $navigationnodecollection->add($this->node
);
544 // Test it's not empty
545 $this->assertEquals(1, count($navigationnodecollection->get_key_list()));
547 // Remove a node - passing key only!
548 $this->assertTrue($navigationnodecollection->remove(100));
550 // Test it's empty again!
551 $this->assertEquals(0, count($navigationnodecollection->get_key_list()));
554 public function test_navigation_node_collection_remove_with_type() {
555 $navigationnodecollection = new navigation_node_collection();
557 $this->node
->key
= 100;
560 $this->assertEquals(0, count($navigationnodecollection->get_key_list()));
563 $navigationnodecollection->add($this->node
);
565 // Test it's not empty
566 $this->assertEquals(1, count($navigationnodecollection->get_key_list()));
568 // Remove a node - passing type
569 $this->assertTrue($navigationnodecollection->remove(100, 1));
571 // Test it's empty again!
572 $this->assertEquals(0, count($navigationnodecollection->get_key_list()));
576 * Test the set_force_into_more_menu method.
578 * @param bool $haschildren Whether the navigation node has children nodes
579 * @param bool $forceintomoremenu Whether to force the navigation node and its children into the "more" menu
580 * @dataProvider set_force_into_more_menu_provider
582 public function test_set_force_into_more_menu(bool $haschildren, bool $forceintomoremenu) {
583 // Create a navigation node.
584 $node = new navigation_node(['text' => 'Navigation node', 'key' => 'navnode']);
586 // If required, add some children nodes to the navigation node.
588 for ($i = 1; $i <= 3; $i++
) {
589 $node->add("Child navigation node {$i}");
593 $node->set_force_into_more_menu($forceintomoremenu);
594 // Assert that the expected value has been assigned to the 'forceintomoremenu' property
595 // in the navigation node and its children.
596 $this->assertEquals($forceintomoremenu, $node->forceintomoremenu
);
597 foreach ($node->children
as $child) {
598 $this->assertEquals($forceintomoremenu, $child->forceintomoremenu
);
603 * Data provider for the test_set_force_into_more_menu function.
607 public function set_force_into_more_menu_provider(): array {
609 'Navigation node without any children nodes; Force into "more" menu => true.' =>
614 'Navigation node with children nodes; Force into "more" menu => true.' =>
619 'Navigation node with children nodes; Force into "more" menu => false.' =>
628 * Test the is_action_link method.
630 * @param navigation_node $node The sample navigation node
631 * @param bool $expected Whether the navigation node contains an action link
632 * @dataProvider is_action_link_provider
633 * @covers navigation_node::is_action_link
635 public function test_is_action_link(navigation_node
$node, bool $expected) {
636 $this->assertEquals($node->is_action_link(), $expected);
640 * Data provider for the test_is_action_link function.
644 public function is_action_link_provider(): array {
646 'The navigation node has an action link.' =>
648 navigation_node
::create('Node', new action_link(new \
moodle_url('/'), '',
649 new popup_action('click', new \
moodle_url('/'))), navigation_node
::TYPE_SETTING
),
653 'The navigation node does not have an action link.' =>
655 navigation_node
::create('Node', new \
moodle_url('/'), navigation_node
::TYPE_SETTING
),
662 * Test the action_link_actions method.
664 * @param navigation_node $node The sample navigation node
665 * @dataProvider action_link_actions_provider
666 * @covers navigation_node::action_link_actions
668 public function test_action_link_actions(navigation_node
$node) {
669 // Get the formatted array of action link actions.
670 $data = $node->action_link_actions();
671 // The navigation node has an action link.
672 if ($node->action
instanceof action_link
) {
673 if (!empty($node->action
->actions
)) { // There are actions added to the action link.
674 $this->assertArrayHasKey('actions', $data);
675 $this->assertCount(1, $data['actions']);
676 $expected = (object)[
677 'id' => $node->action
->attributes
['id'],
678 'event' => $node->action
->actions
[0]->event
,
679 'jsfunction' => $node->action
->actions
[0]->jsfunction
,
680 'jsfunctionargs' => json_encode($node->action
->actions
[0]->jsfunctionargs
)
682 $this->assertEquals($expected, $data['actions'][0]);
683 } else { // There are no actions added to the action link.
684 $this->assertArrayHasKey('actions', $data);
685 $this->assertEmpty($data['actions']);
687 } else { // The navigation node does not have an action link.
688 $this->assertEmpty($data);
693 * Data provider for the test_action_link_actions function.
697 public function action_link_actions_provider(): array {
699 'The navigation node has an action link with an action attached.' =>
701 navigation_node
::create('Node', new action_link(new \
moodle_url('/'), '',
702 new popup_action('click', new \
moodle_url('/'))), navigation_node
::TYPE_SETTING
),
704 'The navigation node has an action link without an action.' =>
706 navigation_node
::create('Node', new action_link(new \
moodle_url('/'), '', null),
707 navigation_node
::TYPE_SETTING
),
709 'The navigation node does not have an action link.' =>
711 navigation_node
::create('Node', new \
moodle_url('/'), navigation_node
::TYPE_SETTING
),
719 * This is a dummy object that allows us to call protected methods within the
720 * global navigation class by prefixing the methods with `exposed_`
722 class exposed_global_navigation
extends global_navigation
{
723 protected $exposedkey = 'exposed_';
724 public function __construct(\moodle_page
$page=null) {
726 if ($page === null) {
729 parent
::__construct($page);
731 public function __call($method, $arguments) {
732 if (strpos($method, $this->exposedkey
) !== false) {
733 $method = substr($method, strlen($this->exposedkey
));
735 if (method_exists($this, $method)) {
736 return call_user_func_array(array($this, $method), $arguments);
738 throw new \
coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER
);
740 public function set_initialised() {
741 $this->initialised
= true;
746 class mock_initialise_global_navigation
extends global_navigation
{
748 protected static $count = 1;
750 public function load_for_category() {
751 $this->add('load_for_category', null, null, null, 'initcall'.self
::$count);
756 public function load_for_course() {
757 $this->add('load_for_course', null, null, null, 'initcall'.self
::$count);
762 public function load_for_activity() {
763 $this->add('load_for_activity', null, null, null, 'initcall'.self
::$count);
768 public function load_for_user($user=null, $forceforcontext=false) {
769 $this->add('load_for_user', null, null, null, 'initcall'.self
::$count);
776 * This is a dummy object that allows us to call protected methods within the
777 * global navigation class by prefixing the methods with `exposed_`.
779 class exposed_navbar
extends navbar
{
780 protected $exposedkey = 'exposed_';
782 public function __construct(\moodle_page
$page) {
783 parent
::__construct($page);
785 public function __call($method, $arguments) {
786 if (strpos($method, $this->exposedkey
) !== false) {
787 $method = substr($method, strlen($this->exposedkey
));
789 if (method_exists($this, $method)) {
790 return call_user_func_array(array($this, $method), $arguments);
792 throw new \
coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER
);
796 class navigation_exposed_moodle_page
extends \moodle_page
{
797 public function set_navigation(navigation_node
$node) {
798 $this->_navigation
= $node;
803 * This is a dummy object that allows us to call protected methods within the
804 * global navigation class by prefixing the methods with `exposed_`.
806 class exposed_settings_navigation
extends settings_navigation
{
807 protected $exposedkey = 'exposed_';
808 public function __construct() {
810 parent
::__construct($PAGE);
812 public function __call($method, $arguments) {
813 if (strpos($method, $this->exposedkey
) !== false) {
814 $method = substr($method, strlen($this->exposedkey
));
816 if (method_exists($this, $method)) {
817 return call_user_func_array(array($this, $method), $arguments);
819 throw new \
coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER
);