Commit | Line | Data |
---|---|---|
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 | ||
26 | defined('MOODLE_INTERNAL') || die(); | |
27 | ||
28 | global $CFG; | |
29 | require_once($CFG->libdir . '/navigationlib.php'); | |
30 | ||
31 | ||
18c43230 | 32 | class 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 | ||
453 | /** | |
454 | * @depends test_setting__initialise | |
455 | * @param mixed $node | |
456 | * @return mixed | |
457 | */ | |
458 | public function test_setting_in_alternative_role($node) { | |
459 | $this->resetAfterTest(); | |
460 | ||
461 | $this->assertFalse($node->exposed_in_alternative_role()); | |
f4fd0a13 PS |
462 | } |
463 | } | |
464 | ||
18c43230 | 465 | |
f4fd0a13 PS |
466 | /** |
467 | * This is a dummy object that allows us to call protected methods within the | |
468 | * global navigation class by prefixing the methods with `exposed_` | |
469 | */ | |
470 | class exposed_global_navigation extends global_navigation { | |
471 | protected $exposedkey = 'exposed_'; | |
472 | public function __construct(moodle_page $page=null) { | |
473 | global $PAGE; | |
474 | if ($page === null) { | |
475 | $page = $PAGE; | |
476 | } | |
477 | parent::__construct($page); | |
4ca04fb5 | 478 | $this->cache = new navigation_cache('unittest_nav'); |
f4fd0a13 PS |
479 | } |
480 | public function __call($method, $arguments) { | |
18c43230 | 481 | if (strpos($method, $this->exposedkey) !== false) { |
f4fd0a13 PS |
482 | $method = substr($method, strlen($this->exposedkey)); |
483 | } | |
484 | if (method_exists($this, $method)) { | |
485 | return call_user_func_array(array($this, $method), $arguments); | |
486 | } | |
487 | throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER); | |
488 | } | |
489 | public function set_initialised() { | |
490 | $this->initialised = true; | |
491 | } | |
492 | } | |
493 | ||
18c43230 | 494 | |
f4fd0a13 PS |
495 | class mock_initialise_global_navigation extends global_navigation { |
496 | ||
18c43230 | 497 | protected static $count = 1; |
f4fd0a13 PS |
498 | |
499 | public function load_for_category() { | |
500 | $this->add('load_for_category', null, null, null, 'initcall'.self::$count); | |
501 | self::$count++; | |
502 | return 0; | |
503 | } | |
504 | ||
505 | public function load_for_course() { | |
506 | $this->add('load_for_course', null, null, null, 'initcall'.self::$count); | |
507 | self::$count++; | |
508 | return 0; | |
509 | } | |
510 | ||
511 | public function load_for_activity() { | |
512 | $this->add('load_for_activity', null, null, null, 'initcall'.self::$count); | |
513 | self::$count++; | |
514 | return 0; | |
515 | } | |
516 | ||
517 | public function load_for_user($user=null, $forceforcontext=false) { | |
518 | $this->add('load_for_user', null, null, null, 'initcall'.self::$count); | |
519 | self::$count++; | |
520 | return 0; | |
521 | } | |
522 | } | |
523 | ||
f4fd0a13 PS |
524 | /** |
525 | * This is a dummy object that allows us to call protected methods within the | |
18c43230 | 526 | * global navigation class by prefixing the methods with `exposed_`. |
f4fd0a13 PS |
527 | */ |
528 | class exposed_navbar extends navbar { | |
529 | protected $exposedkey = 'exposed_'; | |
530 | public function __construct(moodle_page $page) { | |
531 | parent::__construct($page); | |
4ca04fb5 | 532 | $this->cache = new navigation_cache('unittest_nav'); |
f4fd0a13 | 533 | } |
18c43230 PS |
534 | public function __call($method, $arguments) { |
535 | if (strpos($method, $this->exposedkey) !== false) { | |
f4fd0a13 PS |
536 | $method = substr($method, strlen($this->exposedkey)); |
537 | } | |
538 | if (method_exists($this, $method)) { | |
539 | return call_user_func_array(array($this, $method), $arguments); | |
540 | } | |
541 | throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER); | |
542 | } | |
543 | } | |
544 | ||
545 | class navigation_exposed_moodle_page extends moodle_page { | |
546 | public function set_navigation(navigation_node $node) { | |
547 | $this->_navigation = $node; | |
548 | } | |
549 | } | |
550 | ||
f4fd0a13 PS |
551 | /** |
552 | * This is a dummy object that allows us to call protected methods within the | |
18c43230 | 553 | * global navigation class by prefixing the methods with `exposed_`. |
f4fd0a13 PS |
554 | */ |
555 | class exposed_settings_navigation extends settings_navigation { | |
556 | protected $exposedkey = 'exposed_'; | |
18c43230 | 557 | public function __construct() { |
f4fd0a13 PS |
558 | global $PAGE; |
559 | parent::__construct($PAGE); | |
4ca04fb5 | 560 | $this->cache = new navigation_cache('unittest_nav'); |
f4fd0a13 | 561 | } |
18c43230 PS |
562 | public function __call($method, $arguments) { |
563 | if (strpos($method, $this->exposedkey) !== false) { | |
f4fd0a13 PS |
564 | $method = substr($method, strlen($this->exposedkey)); |
565 | } | |
566 | if (method_exists($this, $method)) { | |
567 | return call_user_func_array(array($this, $method), $arguments); | |
568 | } | |
569 | throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER); | |
570 | } | |
571 | } |