Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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;
18
 
19
use action_link;
20
use global_navigation;
21
use navbar;
22
use navigation_cache;
23
use navigation_node;
24
use navigation_node_collection;
25
use pix_icon;
26
use popup_action;
27
use settings_navigation;
28
 
29
defined('MOODLE_INTERNAL') || die();
30
 
31
global $CFG;
32
require_once($CFG->libdir . '/navigationlib.php');
33
 
34
/**
35
 * Unit tests for lib/navigationlib.php
36
 *
37
 * @package   core
38
 * @category  test
39
 * @copyright 2009 Sam Hemelryk
40
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later (5)
41
 */
1441 ariadna 42
final class navigationlib_test extends \advanced_testcase {
1 efrain 43
    /**
44
     * @var navigation_node
45
     */
46
    public $node;
47
 
48
    protected function setup_node() {
49
        global $PAGE, $SITE;
50
 
1441 ariadna 51
        // Perform a reset between tests to reset the PAGE.
52
        $this->resetAfterTest();
53
 
1 efrain 54
        $PAGE->set_url('/');
55
        $PAGE->set_course($SITE);
56
 
57
        $activeurl = $PAGE->url;
58
        $inactiveurl = new \moodle_url('http://www.moodle.com/');
59
 
60
        navigation_node::override_active_url($PAGE->url);
61
 
62
        $this->node = new navigation_node('Test Node');
63
        $this->node->type = navigation_node::TYPE_SYSTEM;
64
        // We add the first child without key. This way we make sure all keys search by comparison is performed using ===.
65
        $this->node->add('first child without key', null, navigation_node::TYPE_CUSTOM);
66
        $demo1 = $this->node->add('demo1', $inactiveurl, navigation_node::TYPE_COURSE, null, 'demo1', new pix_icon('i/course', ''));
67
        $demo2 = $this->node->add('demo2', $inactiveurl, navigation_node::TYPE_COURSE, null, 'demo2', new pix_icon('i/course', ''));
68
        $demo3 = $this->node->add('demo3', $inactiveurl, navigation_node::TYPE_CATEGORY, null, 'demo3', new pix_icon('i/course', ''));
69
        $demo4 = $demo3->add('demo4', $inactiveurl, navigation_node::TYPE_COURSE,  null, 'demo4', new pix_icon('i/course', ''));
70
        $demo5 = $demo3->add('demo5', $activeurl, navigation_node::TYPE_COURSE, null, 'demo5', new pix_icon('i/course', ''));
71
        $demo5->add('activity1', null, navigation_node::TYPE_ACTIVITY, null, 'activity1')->make_active();
72
        $demo6 = $demo3->add('demo6', null, navigation_node::TYPE_CONTAINER, 'container node test', 'demo6');
73
        $hiddendemo1 = $this->node->add('hiddendemo1', $inactiveurl, navigation_node::TYPE_CATEGORY, null, 'hiddendemo1', new pix_icon('i/course', ''));
74
        $hiddendemo1->hidden = true;
75
        $hiddendemo1->add('hiddendemo2', $inactiveurl, navigation_node::TYPE_COURSE, null, 'hiddendemo2', new pix_icon('i/course', ''))->helpbutton = 'Here is a help button';
76
        $hiddendemo1->add('hiddendemo3', $inactiveurl, navigation_node::TYPE_COURSE, null, 'hiddendemo3', new pix_icon('i/course', ''))->display = false;
77
    }
78
 
11 efrain 79
    public function test_node__construct(): void {
1 efrain 80
        $this->setup_node();
81
 
82
        $fakeproperties = array(
83
            'text' => 'text',
84
            'shorttext' => 'A very silly extra long short text string, more than 25 characters',
85
            'key' => 'key',
86
            'type' => 'navigation_node::TYPE_COURSE',
87
            'action' => new \moodle_url('http://www.moodle.org/'));
88
 
89
        $node = new navigation_node($fakeproperties);
90
        $this->assertSame($fakeproperties['text'], $node->text);
91
        $this->assertTrue(strpos($fakeproperties['shorttext'], substr($node->shorttext, 0, -3)) === 0);
92
        $this->assertSame($fakeproperties['key'], $node->key);
93
        $this->assertSame($fakeproperties['type'], $node->type);
94
        $this->assertSame($fakeproperties['action'], $node->action);
95
    }
96
 
11 efrain 97
    public function test_node_add(): void {
1 efrain 98
        $this->setup_node();
99
 
100
        // Add a node with all args set.
101
        $node1 = $this->node->add('test_add_1', 'http://www.moodle.org/', navigation_node::TYPE_COURSE, 'testadd1', 'key', new pix_icon('i/course', ''));
102
        // Add a node with the minimum args required.
103
        $node2 = $this->node->add('test_add_2', null, navigation_node::TYPE_CUSTOM, 'testadd2');
104
        $node3 = $this->node->add(str_repeat('moodle ', 15), str_repeat('moodle', 15));
105
 
106
        $this->assertInstanceOf('navigation_node', $node1);
107
        $this->assertInstanceOf('navigation_node', $node2);
108
        $this->assertInstanceOf('navigation_node', $node3);
109
 
110
        $ref = $this->node->get('key');
111
        $this->assertSame($node1, $ref);
112
 
113
        $ref = $this->node->get($node2->key);
114
        $this->assertSame($node2, $ref);
115
 
116
        $ref = $this->node->get($node2->key, $node2->type);
117
        $this->assertSame($node2, $ref);
118
 
119
        $ref = $this->node->get($node3->key, $node3->type);
120
        $this->assertSame($node3, $ref);
121
    }
122
 
11 efrain 123
    public function test_node_add_before(): void {
1 efrain 124
        $this->setup_node();
125
 
126
        // Create 3 nodes.
127
        $node1 = navigation_node::create('test_add_1', null, navigation_node::TYPE_CUSTOM,
128
            'test 1', 'testadd1');
129
        $node2 = navigation_node::create('test_add_2', null, navigation_node::TYPE_CUSTOM,
130
            'test 2', 'testadd2');
131
        $node3 = navigation_node::create('test_add_3', null, navigation_node::TYPE_CUSTOM,
132
            'test 3', 'testadd3');
133
        // Add node 2, then node 1 before 2, then node 3 at end.
134
        $this->node->add_node($node2);
135
        $this->node->add_node($node1, 'testadd2');
136
        $this->node->add_node($node3);
137
        // Check the last 3 nodes are in 1, 2, 3 order and have those indexes.
138
        foreach ($this->node->children as $child) {
139
            $keys[] = $child->key;
140
        }
141
        $this->assertSame('testadd1', $keys[count($keys)-3]);
142
        $this->assertSame('testadd2', $keys[count($keys)-2]);
143
        $this->assertSame('testadd3', $keys[count($keys)-1]);
144
    }
145
 
11 efrain 146
    public function test_node_add_class(): void {
1 efrain 147
        $this->setup_node();
148
 
149
        $node = $this->node->get('demo1');
150
        $this->assertInstanceOf('navigation_node', $node);
151
        if ($node !== false) {
152
            $node->add_class('myclass');
153
            $classes = $node->classes;
154
            $this->assertContains('myclass', $classes);
155
        }
156
    }
157
 
158
    /**
159
     * Test the add_attribute method.
160
     * @covers \navigation_node::add_attribute
161
     */
162
    public function test_node_add_attribute(): void {
163
        $this->setup_node();
164
 
165
        $node = $this->node->get('demo1');
166
        $this->assertInstanceOf('navigation_node', $node);
167
        if ($node !== false) {
168
            $node->add_attribute('data-foo', 'bar');
169
            $attribute = reset($node->attributes);
170
            $this->assertEqualsCanonicalizing(['name' => 'data-foo', 'value' => 'bar'], $attribute);
171
        }
172
    }
173
 
11 efrain 174
    public function test_node_check_if_active(): void {
1 efrain 175
        $this->setup_node();
176
 
177
        // First test the string urls
178
        // Demo1 -> action is http://www.moodle.org/, thus should be true.
179
        $demo5 = $this->node->find('demo5', navigation_node::TYPE_COURSE);
180
        if ($this->assertInstanceOf('navigation_node', $demo5)) {
181
            $this->assertTrue($demo5->check_if_active());
182
        }
183
 
184
        // Demo2 -> action is http://www.moodle.com/, thus should be false.
185
        $demo2 = $this->node->get('demo2');
186
        if ($this->assertInstanceOf('navigation_node', $demo2)) {
187
            $this->assertFalse($demo2->check_if_active());
188
        }
189
    }
190
 
11 efrain 191
    public function test_node_contains_active_node(): void {
1 efrain 192
        $this->setup_node();
193
 
194
        // Demo5, and activity1 were set to active during setup.
195
        // Should be true as it contains all nodes.
196
        $this->assertTrue($this->node->contains_active_node());
197
        // Should be true as demo5 is a child of demo3.
198
        $this->assertTrue($this->node->get('demo3')->contains_active_node());
199
        // Obviously duff.
200
        $this->assertFalse($this->node->get('demo1')->contains_active_node());
201
        // Should be true as demo5 contains activity1.
202
        $this->assertTrue($this->node->get('demo3')->get('demo5')->contains_active_node());
203
        // Should be true activity1 is the active node.
204
        $this->assertTrue($this->node->get('demo3')->get('demo5')->get('activity1')->contains_active_node());
205
        // Obviously duff.
206
        $this->assertFalse($this->node->get('demo3')->get('demo4')->contains_active_node());
207
    }
208
 
11 efrain 209
    public function test_node_find_active_node(): void {
1 efrain 210
        $this->setup_node();
211
 
212
        $activenode1 = $this->node->find_active_node();
213
        $activenode2 = $this->node->get('demo1')->find_active_node();
214
 
215
        if ($this->assertInstanceOf('navigation_node', $activenode1)) {
216
            $ref = $this->node->get('demo3')->get('demo5')->get('activity1');
217
            $this->assertSame($activenode1, $ref);
218
        }
219
 
220
        $this->assertNotInstanceOf('navigation_node', $activenode2);
221
    }
222
 
11 efrain 223
    public function test_node_find(): void {
1 efrain 224
        $this->setup_node();
225
 
226
        $node1 = $this->node->find('demo1', navigation_node::TYPE_COURSE);
227
        $node2 = $this->node->find('demo5', navigation_node::TYPE_COURSE);
228
        $node3 = $this->node->find('demo5', navigation_node::TYPE_CATEGORY);
229
        $node4 = $this->node->find('demo0', navigation_node::TYPE_COURSE);
230
        $this->assertInstanceOf('navigation_node', $node1);
231
        $this->assertInstanceOf('navigation_node', $node2);
232
        $this->assertNotInstanceOf('navigation_node', $node3);
233
        $this->assertNotInstanceOf('navigation_node', $node4);
234
    }
235
 
11 efrain 236
    public function test_node_find_expandable(): void {
1 efrain 237
        $this->setup_node();
238
 
239
        $expandable = array();
240
        $this->node->find_expandable($expandable);
241
 
242
        $this->assertCount(0, $expandable);
243
        if (count($expandable) === 4) {
244
            $name = $expandable[0]['key'];
245
            $name .= $expandable[1]['key'];
246
            $name .= $expandable[2]['key'];
247
            $name .= $expandable[3]['key'];
248
            $this->assertSame('demo1demo2demo4hiddendemo2', $name);
249
        }
250
    }
251
 
11 efrain 252
    public function test_node_get(): void {
1 efrain 253
        $this->setup_node();
254
 
255
        $node1 = $this->node->get('demo1'); // Exists.
256
        $node2 = $this->node->get('demo4'); // Doesn't exist for this node.
257
        $node3 = $this->node->get('demo0'); // Doesn't exist at all.
258
        $node4 = $this->node->get(false);   // Sometimes occurs in nature code.
259
        $this->assertInstanceOf('navigation_node', $node1);
260
        $this->assertFalse($node2);
261
        $this->assertFalse($node3);
262
        $this->assertFalse($node4);
263
    }
264
 
11 efrain 265
    public function test_node_get_css_type(): void {
1 efrain 266
        $this->setup_node();
267
 
268
        $csstype1 = $this->node->get('demo3')->get_css_type();
269
        $csstype2 = $this->node->get('demo3')->get('demo5')->get_css_type();
270
        $this->node->get('demo3')->get('demo5')->type = 1000;
271
        $csstype3 = $this->node->get('demo3')->get('demo5')->get_css_type();
272
        $csstype4 = $this->node->get('demo3')->get('demo6')->get_css_type();
273
        $this->assertSame('type_category', $csstype1);
274
        $this->assertSame('type_course', $csstype2);
275
        $this->assertSame('type_unknown', $csstype3);
276
        $this->assertSame('type_container', $csstype4);
277
    }
278
 
11 efrain 279
    public function test_node_make_active(): void {
1 efrain 280
        global $CFG;
281
        $this->setup_node();
282
 
283
        $node1 = $this->node->add('active node 1', null, navigation_node::TYPE_CUSTOM, null, 'anode1');
284
        $node2 = $this->node->add('active node 2', new \moodle_url($CFG->wwwroot), navigation_node::TYPE_COURSE, null, 'anode2');
285
        $node1->make_active();
286
        $this->node->get('anode2')->make_active();
287
        $this->assertTrue($node1->isactive);
288
        $this->assertTrue($this->node->get('anode2')->isactive);
289
    }
290
 
11 efrain 291
    public function test_node_remove(): void {
1 efrain 292
        $this->setup_node();
293
 
294
        $remove1 = $this->node->add('child to remove 1', null, navigation_node::TYPE_CUSTOM, null, 'remove1');
295
        $remove2 = $this->node->add('child to remove 2', null, navigation_node::TYPE_CUSTOM, null, 'remove2');
296
        $remove3 = $remove2->add('child to remove 3', null, navigation_node::TYPE_CUSTOM, null, 'remove3');
297
 
298
        $this->assertInstanceOf('navigation_node', $remove1);
299
        $this->assertInstanceOf('navigation_node', $remove2);
300
        $this->assertInstanceOf('navigation_node', $remove3);
301
 
302
        $this->assertInstanceOf('navigation_node', $this->node->get('remove1'));
303
        $this->assertInstanceOf('navigation_node', $this->node->get('remove2'));
304
        $this->assertInstanceOf('navigation_node', $remove2->get('remove3'));
305
 
306
        // Remove element and make sure this is no longer a child.
307
        $this->assertTrue($remove1->remove());
308
        $this->assertFalse($this->node->get('remove1'));
309
        $this->assertFalse(in_array('remove1', $this->node->get_children_key_list(), true));
310
 
311
        // Make sure that we can insert element after removal.
312
        $insertelement = navigation_node::create('extra element 4', null, navigation_node::TYPE_CUSTOM, null, 'element4');
313
        $this->node->add_node($insertelement, 'remove2');
314
        $this->assertNotEmpty($this->node->get('element4'));
315
 
316
        // Remove more elements.
317
        $this->assertTrue($this->node->get('remove2')->remove());
318
        $this->assertFalse($this->node->get('remove2'));
319
 
320
        // Make sure that we can add element after removal.
321
        $this->node->add('extra element 5', null, navigation_node::TYPE_CUSTOM, null, 'element5');
322
        $this->assertNotEmpty($this->node->get('element5'));
323
 
324
        $this->assertTrue($remove2->get('remove3')->remove());
325
 
326
        $this->assertFalse($this->node->get('remove1'));
327
        $this->assertFalse($this->node->get('remove2'));
328
    }
329
 
11 efrain 330
    public function test_node_remove_class(): void {
1 efrain 331
        $this->setup_node();
332
 
333
        $this->node->add_class('testclass');
334
        $this->assertTrue($this->node->remove_class('testclass'));
335
        $this->assertNotContains('testclass', $this->node->classes);
336
    }
337
 
11 efrain 338
    public function test_module_extends_navigation(): void {
1 efrain 339
        $node = new exposed_global_navigation();
340
        // Create an initial tree structure to work with.
341
        $cat1 = $node->add('category 1', null, navigation_node::TYPE_CATEGORY, null, 'cat1');
342
        $cat2 = $node->add('category 2', null, navigation_node::TYPE_CATEGORY, null, 'cat2');
343
        $cat3 = $node->add('category 3', null, navigation_node::TYPE_CATEGORY, null, 'cat3');
344
        $sub1 = $cat2->add('sub category 1', null, navigation_node::TYPE_CATEGORY, null, 'sub1');
345
        $sub2 = $cat2->add('sub category 2', null, navigation_node::TYPE_CATEGORY, null, 'sub2');
346
        $sub3 = $cat2->add('sub category 3', null, navigation_node::TYPE_CATEGORY, null, 'sub3');
347
        $course1 = $sub2->add('course 1', null, navigation_node::TYPE_COURSE, null, 'course1');
348
        $course2 = $sub2->add('course 2', null, navigation_node::TYPE_COURSE, null, 'course2');
349
        $course3 = $sub2->add('course 3', null, navigation_node::TYPE_COURSE, null, 'course3');
350
        $section1 = $course2->add('section 1', null, navigation_node::TYPE_SECTION, null, 'sec1');
351
        $section2 = $course2->add('section 2', null, navigation_node::TYPE_SECTION, null, 'sec2');
352
        $section3 = $course2->add('section 3', null, navigation_node::TYPE_SECTION, null, 'sec3');
353
        $act1 = $section2->add('activity 1', null, navigation_node::TYPE_ACTIVITY, null, 'act1');
354
        $act2 = $section2->add('activity 2', null, navigation_node::TYPE_ACTIVITY, null, 'act2');
355
        $act3 = $section2->add('activity 3', null, navigation_node::TYPE_ACTIVITY, null, 'act3');
356
        $res1 = $section2->add('resource 1', null, navigation_node::TYPE_RESOURCE, null, 'res1');
357
        $res2 = $section2->add('resource 2', null, navigation_node::TYPE_RESOURCE, null, 'res2');
358
        $res3 = $section2->add('resource 3', null, navigation_node::TYPE_RESOURCE, null, 'res3');
359
 
360
        $this->assertTrue($node->exposed_module_extends_navigation('data'));
361
        $this->assertFalse($node->exposed_module_extends_navigation('test1'));
362
    }
363
 
11 efrain 364
    public function test_navbar_prepend_and_add(): \moodle_page {
1 efrain 365
        global $PAGE;
366
        // Unfortunate hack needed because people use global $PAGE around the place.
367
        $PAGE->set_url('/');
368
 
369
        // We need to reset after this test because we using the generator.
370
        $this->resetAfterTest();
371
 
372
        $generator = self::getDataGenerator();
373
        $cat1 = $generator->create_category();
374
        $cat2 = $generator->create_category(array('parent' => $cat1->id));
375
        $course = $generator->create_course(array('category' => $cat2->id));
376
 
377
        $page = new \moodle_page();
378
        $page->set_course($course);
379
        $page->set_url(new \moodle_url('/course/view.php', array('id' => $course->id)));
380
        $page->navbar->prepend('test 1');
381
        $page->navbar->prepend('test 2');
382
        $page->navbar->add('test 3');
383
        $page->navbar->add('test 4');
384
 
385
        $items = $page->navbar->get_items();
386
        foreach ($items as $item) {
387
            $this->assertInstanceOf('navigation_node', $item);
388
        }
389
 
390
        $i = 0;
391
        $this->assertSame('test 1', $items[$i++]->text);
392
        $this->assertSame('test 2', $items[$i++]->text);
393
        $this->assertSame('home', $items[$i++]->key);
394
        $this->assertSame('courses', $items[$i++]->key);
395
        $this->assertSame($cat1->id, $items[$i++]->key);
396
        $this->assertSame($cat2->id, $items[$i++]->key);
397
        $this->assertSame($course->id, $items[$i++]->key);
398
        $this->assertSame('test 3', $items[$i++]->text);
399
        $this->assertSame('test 4', $items[$i++]->text);
400
 
401
        return $page;
402
    }
403
 
404
    /**
405
     * @depends test_navbar_prepend_and_add
406
     * @param $node
407
     */
11 efrain 408
    public function test_navbar_has_items(\moodle_page $page): void {
1 efrain 409
        $this->resetAfterTest();
410
 
411
        $this->assertTrue($page->navbar->has_items());
412
    }
413
 
11 efrain 414
    public function test_cache__get(): void {
1 efrain 415
        $cache = new navigation_cache('unittest_nav');
416
        $cache->anysetvariable = true;
417
 
418
        $this->assertTrue($cache->anysetvariable);
419
        $this->assertEquals($cache->notasetvariable, null);
420
    }
421
 
11 efrain 422
    public function test_cache__set(): void {
1 efrain 423
        $cache = new navigation_cache('unittest_nav');
424
        $cache->anysetvariable = true;
425
 
426
        $cache->myname = 'Sam Hemelryk';
427
        $this->assertTrue($cache->cached('myname'));
428
        $this->assertSame('Sam Hemelryk', $cache->myname);
429
    }
430
 
11 efrain 431
    public function test_cache_cached(): void {
1 efrain 432
        $cache = new navigation_cache('unittest_nav');
433
        $cache->anysetvariable = true;
434
 
435
        $this->assertTrue($cache->cached('anysetvariable'));
436
        $this->assertFalse($cache->cached('notasetvariable'));
437
    }
438
 
11 efrain 439
    public function test_cache_clear(): void {
1 efrain 440
        $cache = new navigation_cache('unittest_nav');
441
        $cache->anysetvariable = true;
442
 
443
        $cache = clone($cache);
444
        $this->assertTrue($cache->cached('anysetvariable'));
445
        $cache->clear();
446
        $this->assertFalse($cache->cached('anysetvariable'));
447
    }
448
 
11 efrain 449
    public function test_cache_set(): void {
1 efrain 450
        $cache = new navigation_cache('unittest_nav');
451
        $cache->anysetvariable = true;
452
 
453
        $cache->set('software', 'Moodle');
454
        $this->assertTrue($cache->cached('software'));
455
        $this->assertEquals($cache->software, 'Moodle');
456
    }
457
 
11 efrain 458
    public function test_setting___construct(): settings_navigation {
1 efrain 459
        global $PAGE, $SITE;
460
 
461
        $this->resetAfterTest(false);
462
 
463
        $PAGE->set_url('/');
464
        $PAGE->set_course($SITE);
465
 
466
        $node = new exposed_settings_navigation();
467
 
468
        return $node;
469
    }
470
 
471
    /**
472
     * @depends test_setting___construct
473
     * @param mixed $node
474
     * @return mixed
475
     */
11 efrain 476
    public function test_setting__initialise($node): settings_navigation {
1 efrain 477
        $this->resetAfterTest(false);
478
 
479
        $node->initialise();
480
        $this->assertEquals($node->id, 'settingsnav');
481
 
482
        return $node;
483
    }
484
 
485
    /**
486
     * Test that users with the correct permissions can view the preferences page.
487
     */
11 efrain 488
    public function test_can_view_user_preferences(): void {
1 efrain 489
        global $PAGE, $DB, $SITE;
490
        $this->resetAfterTest();
491
 
492
        $persontoview = $this->getDataGenerator()->create_user();
493
        $persondoingtheviewing = $this->getDataGenerator()->create_user();
494
 
495
        $PAGE->set_url('/');
496
        $PAGE->set_course($SITE);
497
 
498
        // Check that a standard user can not view the preferences page.
499
        $studentrole = $DB->get_record('role', array('shortname' => 'student'));
500
        $this->getDataGenerator()->role_assign($studentrole->id, $persondoingtheviewing->id);
501
        $this->setUser($persondoingtheviewing);
502
        $settingsnav = new exposed_settings_navigation();
503
        $settingsnav->initialise();
504
        $settingsnav->extend_for_user($persontoview->id);
505
        $this->assertFalse($settingsnav->can_view_user_preferences($persontoview->id));
506
 
507
        // Set persondoingtheviewing as a manager.
508
        $managerrole = $DB->get_record('role', array('shortname' => 'manager'));
509
        $this->getDataGenerator()->role_assign($managerrole->id, $persondoingtheviewing->id);
510
        $settingsnav = new exposed_settings_navigation();
511
        $settingsnav->initialise();
512
        $settingsnav->extend_for_user($persontoview->id);
513
        $this->assertTrue($settingsnav->can_view_user_preferences($persontoview->id));
514
 
515
        // Check that the admin can view the preferences page.
516
        $this->setAdminUser();
517
        $settingsnav = new exposed_settings_navigation();
518
        $settingsnav->initialise();
519
        $settingsnav->extend_for_user($persontoview->id);
520
        $preferencenode = $settingsnav->find('userviewingsettings' . $persontoview->id, null);
521
        $this->assertTrue($settingsnav->can_view_user_preferences($persontoview->id));
522
    }
523
 
524
    /**
525
     * @depends test_setting__initialise
526
     * @param mixed $node
527
     * @return mixed
528
     */
11 efrain 529
    public function test_setting_in_alternative_role($node): void {
1 efrain 530
        $this->resetAfterTest();
531
 
532
        $this->assertFalse($node->exposed_in_alternative_role());
533
    }
534
 
535
 
11 efrain 536
    public function test_navigation_node_collection_remove_with_no_type(): void {
1 efrain 537
        $navigationnodecollection = new navigation_node_collection();
538
        $this->setup_node();
539
        $this->node->key = 100;
540
 
541
        // Test it's empty
542
        $this->assertEquals(0, count($navigationnodecollection->get_key_list()));
543
 
544
        // Add a node
545
        $navigationnodecollection->add($this->node);
546
 
547
        // Test it's not empty
548
        $this->assertEquals(1, count($navigationnodecollection->get_key_list()));
549
 
550
        // Remove a node - passing key only!
551
        $this->assertTrue($navigationnodecollection->remove(100));
552
 
553
        // Test it's empty again!
554
        $this->assertEquals(0, count($navigationnodecollection->get_key_list()));
555
    }
556
 
11 efrain 557
    public function test_navigation_node_collection_remove_with_type(): void {
1 efrain 558
        $navigationnodecollection = new navigation_node_collection();
559
        $this->setup_node();
560
        $this->node->key = 100;
561
 
562
        // Test it's empty
563
        $this->assertEquals(0, count($navigationnodecollection->get_key_list()));
564
 
565
        // Add a node
566
        $navigationnodecollection->add($this->node);
567
 
568
        // Test it's not empty
569
        $this->assertEquals(1, count($navigationnodecollection->get_key_list()));
570
 
571
        // Remove a node - passing type
572
        $this->assertTrue($navigationnodecollection->remove(100, 1));
573
 
574
        // Test it's empty again!
575
        $this->assertEquals(0, count($navigationnodecollection->get_key_list()));
576
    }
577
 
578
    /**
579
     * Test the set_force_into_more_menu method.
580
     *
581
     * @param bool $haschildren       Whether the navigation node has children nodes
582
     * @param bool $forceintomoremenu Whether to force the navigation node and its children into the "more" menu
583
     * @dataProvider set_force_into_more_menu_provider
584
     */
11 efrain 585
    public function test_set_force_into_more_menu(bool $haschildren, bool $forceintomoremenu): void {
1 efrain 586
        // Create a navigation node.
587
        $node = new navigation_node(['text' => 'Navigation node', 'key' => 'navnode']);
588
 
589
        // If required, add some children nodes to the navigation node.
590
        if ($haschildren) {
591
            for ($i = 1; $i <= 3; $i++) {
592
                $node->add("Child navigation node {$i}");
593
            }
594
        }
595
 
596
        $node->set_force_into_more_menu($forceintomoremenu);
597
        // Assert that the expected value has been assigned to the 'forceintomoremenu' property
598
        // in the navigation node and its children.
599
        $this->assertEquals($forceintomoremenu, $node->forceintomoremenu);
600
        foreach ($node->children as $child) {
601
            $this->assertEquals($forceintomoremenu, $child->forceintomoremenu);
602
        }
603
    }
604
 
605
    /**
606
     * Data provider for the test_set_force_into_more_menu function.
607
     *
608
     * @return array
609
     */
1441 ariadna 610
    public static function set_force_into_more_menu_provider(): array {
1 efrain 611
        return [
612
            'Navigation node without any children nodes; Force into "more" menu => true.' =>
613
                [
614
                    false,
615
                    true,
616
                ],
617
            'Navigation node with children nodes; Force into "more" menu => true.' =>
618
                [
619
                    true,
620
                    true,
621
                ],
622
            'Navigation node with children nodes; Force into "more" menu => false.' =>
623
                [
624
                    true,
625
                    false,
626
                ],
627
        ];
628
    }
629
 
630
    /**
631
     * Test the is_action_link method.
632
     *
633
     * @param navigation_node $node The sample navigation node
634
     * @param bool $expected Whether the navigation node contains an action link
635
     * @dataProvider is_action_link_provider
636
     * @covers navigation_node::is_action_link
637
     */
11 efrain 638
    public function test_is_action_link(navigation_node $node, bool $expected): void {
1 efrain 639
        $this->assertEquals($node->is_action_link(), $expected);
640
    }
641
 
642
    /**
643
     * Data provider for the test_is_action_link function.
644
     *
645
     * @return array
646
     */
1441 ariadna 647
    public static function is_action_link_provider(): array {
1 efrain 648
        return [
649
            'The navigation node has an action link.' =>
650
                [
651
                    navigation_node::create('Node', new action_link(new \moodle_url('/'), '',
652
                        new popup_action('click', new \moodle_url('/'))), navigation_node::TYPE_SETTING),
653
                    true
654
                ],
655
 
656
            'The navigation node does not have an action link.' =>
657
                [
658
                    navigation_node::create('Node', new \moodle_url('/'), navigation_node::TYPE_SETTING),
659
                    false
660
                ],
661
        ];
662
    }
663
 
664
    /**
665
     * Test the action_link_actions method.
666
     *
667
     * @param navigation_node $node The sample navigation node
668
     * @dataProvider action_link_actions_provider
669
     * @covers navigation_node::action_link_actions
670
     */
11 efrain 671
    public function test_action_link_actions(navigation_node $node): void {
1 efrain 672
        // Get the formatted array of action link actions.
673
        $data = $node->action_link_actions();
674
        // The navigation node has an action link.
675
        if ($node->action instanceof action_link) {
676
            if (!empty($node->action->actions)) { // There are actions added to the action link.
677
                $this->assertArrayHasKey('actions', $data);
678
                $this->assertCount(1, $data['actions']);
679
                $expected = (object)[
680
                    'id' => $node->action->attributes['id'],
681
                    'event' => $node->action->actions[0]->event,
682
                    'jsfunction' => $node->action->actions[0]->jsfunction,
683
                    'jsfunctionargs' => json_encode($node->action->actions[0]->jsfunctionargs)
684
                ];
685
                $this->assertEquals($expected, $data['actions'][0]);
686
            } else { // There are no actions added to the action link.
687
                $this->assertArrayHasKey('actions', $data);
688
                $this->assertEmpty($data['actions']);
689
            }
690
        } else { // The navigation node does not have an action link.
691
            $this->assertEmpty($data);
692
        }
693
    }
694
 
695
    /**
696
     * Data provider for the test_action_link_actions function.
697
     *
698
     * @return array
699
     */
1441 ariadna 700
    public static function action_link_actions_provider(): array {
1 efrain 701
        return [
702
            'The navigation node has an action link with an action attached.' =>
703
                [
704
                    navigation_node::create('Node', new action_link(new \moodle_url('/'), '',
705
                        new popup_action('click', new \moodle_url('/'))), navigation_node::TYPE_SETTING),
706
                ],
707
            'The navigation node has an action link without an action.' =>
708
                [
709
                    navigation_node::create('Node', new action_link(new \moodle_url('/'), '', null),
710
                        navigation_node::TYPE_SETTING),
711
                ],
712
            'The navigation node does not have an action link.' =>
713
                [
714
                    navigation_node::create('Node', new \moodle_url('/'), navigation_node::TYPE_SETTING),
715
                ],
716
        ];
717
    }
718
}
719
 
720
 
721
/**
722
 * This is a dummy object that allows us to call protected methods within the
723
 * global navigation class by prefixing the methods with `exposed_`
724
 */
725
class exposed_global_navigation extends global_navigation {
726
    protected $exposedkey = 'exposed_';
1441 ariadna 727
    public function __construct(?\moodle_page $page=null) {
1 efrain 728
        global $PAGE;
729
        if ($page === null) {
730
            $page = $PAGE;
731
        }
732
        parent::__construct($page);
733
    }
734
    public function __call($method, $arguments) {
735
        if (strpos($method, $this->exposedkey) !== false) {
736
            $method = substr($method, strlen($this->exposedkey));
737
        }
738
        if (method_exists($this, $method)) {
739
            return call_user_func_array(array($this, $method), $arguments);
740
        }
741
        throw new \coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
742
    }
743
    public function set_initialised() {
744
        $this->initialised = true;
745
    }
746
}
747
 
748
 
749
class mock_initialise_global_navigation extends global_navigation {
750
 
751
    protected static $count = 1;
752
 
753
    public function load_for_category() {
754
        $this->add('load_for_category', null, null, null, 'initcall'.self::$count);
755
        self::$count++;
756
        return 0;
757
    }
758
 
759
    public function load_for_course() {
760
        $this->add('load_for_course', null, null, null, 'initcall'.self::$count);
761
        self::$count++;
762
        return 0;
763
    }
764
 
765
    public function load_for_activity() {
766
        $this->add('load_for_activity', null, null, null, 'initcall'.self::$count);
767
        self::$count++;
768
        return 0;
769
    }
770
 
771
    public function load_for_user($user=null, $forceforcontext=false) {
772
        $this->add('load_for_user', null, null, null, 'initcall'.self::$count);
773
        self::$count++;
774
        return 0;
775
    }
776
}
777
 
778
/**
779
 * This is a dummy object that allows us to call protected methods within the
780
 * global navigation class by prefixing the methods with `exposed_`.
781
 */
782
class exposed_navbar extends navbar {
783
    protected $exposedkey = 'exposed_';
784
 
785
    public function __construct(\moodle_page $page) {
786
        parent::__construct($page);
787
    }
788
    public function __call($method, $arguments) {
789
        if (strpos($method, $this->exposedkey) !== false) {
790
            $method = substr($method, strlen($this->exposedkey));
791
        }
792
        if (method_exists($this, $method)) {
793
            return call_user_func_array(array($this, $method), $arguments);
794
        }
795
        throw new \coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
796
    }
797
}
798
 
799
class navigation_exposed_moodle_page extends \moodle_page {
800
    public function set_navigation(navigation_node $node) {
801
        $this->_navigation = $node;
802
    }
803
}
804
 
805
/**
806
 * This is a dummy object that allows us to call protected methods within the
807
 * global navigation class by prefixing the methods with `exposed_`.
808
 */
809
class exposed_settings_navigation extends settings_navigation {
810
    protected $exposedkey = 'exposed_';
811
    public function __construct() {
812
        global $PAGE;
813
        parent::__construct($PAGE);
814
    }
815
    public function __call($method, $arguments) {
816
        if (strpos($method, $this->exposedkey) !== false) {
817
            $method = substr($method, strlen($this->exposedkey));
818
        }
819
        if (method_exists($this, $method)) {
820
            return call_user_func_array(array($this, $method), $arguments);
821
        }
822
        throw new \coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
823
    }
824
}