1 |
efrain |
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 |
namespace core\navigation\views;
|
|
|
18 |
|
|
|
19 |
use navigation_node;
|
|
|
20 |
use ReflectionMethod;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Class core_primary_testcase
|
|
|
24 |
*
|
|
|
25 |
* Unit test for the primary nav view.
|
|
|
26 |
*
|
|
|
27 |
* @package core
|
|
|
28 |
* @category navigation
|
|
|
29 |
* @copyright 2021 onwards Peter Dias
|
|
|
30 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
31 |
*/
|
|
|
32 |
class primary_test extends \advanced_testcase {
|
|
|
33 |
/**
|
|
|
34 |
* Test the initialise in different contexts
|
|
|
35 |
*
|
|
|
36 |
* @param string $usertype The user to setup for - admin, guest, regular user
|
|
|
37 |
* @param string $expected The expected nodes
|
|
|
38 |
* @dataProvider setting_initialise_provider
|
|
|
39 |
*/
|
|
|
40 |
public function test_setting_initialise($usertype, $expected) {
|
|
|
41 |
global $PAGE;
|
|
|
42 |
$PAGE->set_url("/");
|
|
|
43 |
$this->resetAfterTest();
|
|
|
44 |
if ($usertype == 'admin') {
|
|
|
45 |
$this->setAdminUser();
|
|
|
46 |
} else if ($usertype == 'guest') {
|
|
|
47 |
$this->setGuestUser();
|
|
|
48 |
} else {
|
|
|
49 |
$user = $this->getDataGenerator()->create_user();
|
|
|
50 |
$this->setUser($user);
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
$node = new primary($PAGE);
|
|
|
54 |
$node->initialise();
|
|
|
55 |
$children = $node->get_children_key_list();
|
|
|
56 |
$this->assertEquals($expected, $children);
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* Data provider for the test_setting_initialise function
|
|
|
61 |
*/
|
|
|
62 |
public function setting_initialise_provider() {
|
|
|
63 |
return [
|
|
|
64 |
'Testing as a guest user' => ['guest', ['home']],
|
|
|
65 |
'Testing as an admin' => ['admin', ['home', 'myhome', 'mycourses', 'siteadminnode']],
|
|
|
66 |
'Testing as a regular user' => ['user', ['home', 'myhome', 'mycourses']]
|
|
|
67 |
];
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
/**
|
|
|
71 |
* Get the nav tree initialised to test search_and_set_active_node.
|
|
|
72 |
*
|
|
|
73 |
* @param string|null $seturl The url set for $PAGE.
|
|
|
74 |
* @return navigation_node The initialised nav tree.
|
|
|
75 |
*/
|
|
|
76 |
private function get_tree_initilised_to_set_activate(?string $seturl = null): navigation_node {
|
|
|
77 |
$node = new navigation_node('My test node');
|
|
|
78 |
$node->type = navigation_node::TYPE_SYSTEM;
|
|
|
79 |
$node->add('first child', null, navigation_node::TYPE_CUSTOM, 'firstchld', 'firstchild');
|
|
|
80 |
$child2 = $node->add('second child', null, navigation_node::TYPE_COURSE, 'secondchld', 'secondchild');
|
|
|
81 |
$child3 = $node->add('third child', null, navigation_node::TYPE_CONTAINER, 'thirdchld', 'thirdchild');
|
|
|
82 |
$node->add('fourth child', null, navigation_node::TYPE_ACTIVITY, 'fourthchld', 'fourthchld');
|
|
|
83 |
$node->add('fifth child', '/my', navigation_node::TYPE_CATEGORY, 'fifthchld', 'fifthchild');
|
|
|
84 |
|
|
|
85 |
// If seturl is null then set actionurl of child6 to '/'.
|
|
|
86 |
if ($seturl === null) {
|
|
|
87 |
$child6actionurl = new \moodle_url('/');
|
|
|
88 |
} else {
|
|
|
89 |
// If seturl is provided then set actionurl of child6 to '/foo'.
|
|
|
90 |
$child6actionurl = new \moodle_url('/foo');
|
|
|
91 |
}
|
|
|
92 |
$child6 = $child2->add('sixth child', $child6actionurl, navigation_node::TYPE_COURSE, 'sixthchld', 'sixthchild');
|
|
|
93 |
// Activate the sixthchild node.
|
|
|
94 |
$child6->make_active();
|
|
|
95 |
$child2->add('seventh child', null, navigation_node::TYPE_COURSE, 'seventhchld', 'seventhchild');
|
|
|
96 |
$child8 = $child2->add('eighth child', null, navigation_node::TYPE_CUSTOM, 'eighthchld', 'eighthchild');
|
|
|
97 |
$child8->add('nineth child', null, navigation_node::TYPE_CUSTOM, 'ninethchld', 'ninethchild');
|
|
|
98 |
$child3->add('tenth child', null, navigation_node::TYPE_CUSTOM, 'tenthchld', 'tenthchild');
|
|
|
99 |
|
|
|
100 |
return $node;
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
/**
|
|
|
104 |
* Testing search_and_set_active_node.
|
|
|
105 |
*
|
|
|
106 |
* @param string $expectedkey Expected key of the node, if set.
|
|
|
107 |
* @param string|null $key The key of the node to activate.
|
|
|
108 |
* @param string|null $seturl Set the url for $PAGE.
|
|
|
109 |
* @return void
|
|
|
110 |
* @dataProvider search_and_set_active_node_provider
|
|
|
111 |
*/
|
|
|
112 |
public function test_search_and_set_active_node(string $expectedkey, ?string $key = null, ?string $seturl = null): void {
|
|
|
113 |
global $PAGE;
|
|
|
114 |
|
|
|
115 |
if ($seturl !== null) {
|
|
|
116 |
navigation_node::override_active_url(new \moodle_url($seturl));
|
|
|
117 |
} else {
|
|
|
118 |
$PAGE->set_url('/');
|
|
|
119 |
navigation_node::override_active_url(new \moodle_url('/'));
|
|
|
120 |
}
|
|
|
121 |
if ($key !== null) {
|
|
|
122 |
$PAGE->set_primary_active_tab($key);
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
$node = $this->get_tree_initilised_to_set_activate($seturl);
|
|
|
126 |
|
|
|
127 |
$primary = new primary($PAGE);
|
|
|
128 |
$method = new ReflectionMethod('core\navigation\views\primary', 'search_and_set_active_node');
|
|
|
129 |
|
|
|
130 |
$result = $method->invoke($primary, $node);
|
|
|
131 |
|
|
|
132 |
$sixthchildnode = $node->find('sixthchild', navigation_node::TYPE_COURSE);
|
|
|
133 |
if ($expectedkey !== '') {
|
|
|
134 |
$this->assertInstanceOf('navigation_node', $result);
|
|
|
135 |
$this->assertEquals($result->isactive, true);
|
|
|
136 |
$this->assertEquals($result->key, $expectedkey);
|
|
|
137 |
|
|
|
138 |
// Test the state of sixthchild, based on $expectedkey.
|
|
|
139 |
if ($expectedkey !== 'sixthchild') {
|
|
|
140 |
$this->assertFalse($sixthchildnode->isactive);
|
|
|
141 |
} else {
|
|
|
142 |
$this->assertTrue($sixthchildnode->isactive);
|
|
|
143 |
}
|
|
|
144 |
} else {
|
|
|
145 |
$this->assertNull($result);
|
|
|
146 |
$this->assertTrue($sixthchildnode->isactive);
|
|
|
147 |
}
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
/**
|
|
|
151 |
* Data provider for test_search_and_set_active_node
|
|
|
152 |
*
|
|
|
153 |
* @return array
|
|
|
154 |
*/
|
|
|
155 |
public function search_and_set_active_node_provider(): array {
|
|
|
156 |
return [
|
|
|
157 |
'Test by activating node which is part of the tree'
|
|
|
158 |
=> ['tenthchild', 'tenthchild'],
|
|
|
159 |
'Do not change the state of any nodes of the tree'
|
|
|
160 |
=> ['sixthchild'],
|
|
|
161 |
'Test by setting an empty string as node key to activate' => ['sixthchild', ''],
|
|
|
162 |
'Activate a node which does not exist in the tree'
|
|
|
163 |
=> ['', 'foobar'],
|
|
|
164 |
'Activate the leaf node of the tree' => ['ninethchild', 'ninethchild'],
|
|
|
165 |
'Test by changing the $PAGE url which is different from action url of child6'
|
|
|
166 |
=> ['', null, '/foobar'],
|
|
|
167 |
'Test by having $PAGE url and child6 action url same'
|
|
|
168 |
=> ['sixthchild', null, '/foo'],
|
|
|
169 |
];
|
|
|
170 |
}
|
|
|
171 |
}
|