Proyectos de Subversion Moodle

Rev

Rev 1 | | 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
 */
42
class navigationlib_test extends \advanced_testcase {
43
    /**
44
     * @var navigation_node
45
     */
46
    public $node;
47
 
48
    protected function setup_node() {
49
        global $PAGE, $SITE;
50
 
51
        $PAGE->set_url('/');
52
        $PAGE->set_course($SITE);
53
 
54
        $activeurl = $PAGE->url;
55
        $inactiveurl = new \moodle_url('http://www.moodle.com/');
56
 
57
        navigation_node::override_active_url($PAGE->url);
58
 
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;
74
    }
75
 
11 efrain 76
    public function test_node__construct(): void {
1 efrain 77
        $this->setup_node();
78
 
79
        $fakeproperties = array(
80
            'text' => 'text',
81
            'shorttext' => 'A very silly extra long short text string, more than 25 characters',
82
            'key' => 'key',
83
            'type' => 'navigation_node::TYPE_COURSE',
84
            'action' => new \moodle_url('http://www.moodle.org/'));
85
 
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);
92
    }
93
 
11 efrain 94
    public function test_node_add(): void {
1 efrain 95
        $this->setup_node();
96
 
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));
102
 
103
        $this->assertInstanceOf('navigation_node', $node1);
104
        $this->assertInstanceOf('navigation_node', $node2);
105
        $this->assertInstanceOf('navigation_node', $node3);
106
 
107
        $ref = $this->node->get('key');
108
        $this->assertSame($node1, $ref);
109
 
110
        $ref = $this->node->get($node2->key);
111
        $this->assertSame($node2, $ref);
112
 
113
        $ref = $this->node->get($node2->key, $node2->type);
114
        $this->assertSame($node2, $ref);
115
 
116
        $ref = $this->node->get($node3->key, $node3->type);
117
        $this->assertSame($node3, $ref);
118
    }
119
 
11 efrain 120
    public function test_node_add_before(): void {
1 efrain 121
        $this->setup_node();
122
 
123
        // Create 3 nodes.
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;
137
        }
138
        $this->assertSame('testadd1', $keys[count($keys)-3]);
139
        $this->assertSame('testadd2', $keys[count($keys)-2]);
140
        $this->assertSame('testadd3', $keys[count($keys)-1]);
141
    }
142
 
11 efrain 143
    public function test_node_add_class(): void {
1 efrain 144
        $this->setup_node();
145
 
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);
152
        }
153
    }
154
 
155
    /**
156
     * Test the add_attribute method.
157
     * @covers \navigation_node::add_attribute
158
     */
159
    public function test_node_add_attribute(): void {
160
        $this->setup_node();
161
 
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);
168
        }
169
    }
170
 
11 efrain 171
    public function test_node_check_if_active(): void {
1 efrain 172
        $this->setup_node();
173
 
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());
179
        }
180
 
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());
185
        }
186
    }
187
 
11 efrain 188
    public function test_node_contains_active_node(): void {
1 efrain 189
        $this->setup_node();
190
 
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());
196
        // Obviously duff.
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());
202
        // Obviously duff.
203
        $this->assertFalse($this->node->get('demo3')->get('demo4')->contains_active_node());
204
    }
205
 
11 efrain 206
    public function test_node_find_active_node(): void {
1 efrain 207
        $this->setup_node();
208
 
209
        $activenode1 = $this->node->find_active_node();
210
        $activenode2 = $this->node->get('demo1')->find_active_node();
211
 
212
        if ($this->assertInstanceOf('navigation_node', $activenode1)) {
213
            $ref = $this->node->get('demo3')->get('demo5')->get('activity1');
214
            $this->assertSame($activenode1, $ref);
215
        }
216
 
217
        $this->assertNotInstanceOf('navigation_node', $activenode2);
218
    }
219
 
11 efrain 220
    public function test_node_find(): void {
1 efrain 221
        $this->setup_node();
222
 
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);
231
    }
232
 
11 efrain 233
    public function test_node_find_expandable(): void {
1 efrain 234
        $this->setup_node();
235
 
236
        $expandable = array();
237
        $this->node->find_expandable($expandable);
238
 
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);
246
        }
247
    }
248
 
11 efrain 249
    public function test_node_get(): void {
1 efrain 250
        $this->setup_node();
251
 
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);
260
    }
261
 
11 efrain 262
    public function test_node_get_css_type(): void {
1 efrain 263
        $this->setup_node();
264
 
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);
274
    }
275
 
11 efrain 276
    public function test_node_make_active(): void {
1 efrain 277
        global $CFG;
278
        $this->setup_node();
279
 
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);
286
    }
287
 
11 efrain 288
    public function test_node_remove(): void {
1 efrain 289
        $this->setup_node();
290
 
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');
294
 
295
        $this->assertInstanceOf('navigation_node', $remove1);
296
        $this->assertInstanceOf('navigation_node', $remove2);
297
        $this->assertInstanceOf('navigation_node', $remove3);
298
 
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'));
302
 
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));
307
 
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'));
312
 
313
        // Remove more elements.
314
        $this->assertTrue($this->node->get('remove2')->remove());
315
        $this->assertFalse($this->node->get('remove2'));
316
 
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'));
320
 
321
        $this->assertTrue($remove2->get('remove3')->remove());
322
 
323
        $this->assertFalse($this->node->get('remove1'));
324
        $this->assertFalse($this->node->get('remove2'));
325
    }
326
 
11 efrain 327
    public function test_node_remove_class(): void {
1 efrain 328
        $this->setup_node();
329
 
330
        $this->node->add_class('testclass');
331
        $this->assertTrue($this->node->remove_class('testclass'));
332
        $this->assertNotContains('testclass', $this->node->classes);
333
    }
334
 
11 efrain 335
    public function test_module_extends_navigation(): void {
1 efrain 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');
356
 
357
        $this->assertTrue($node->exposed_module_extends_navigation('data'));
358
        $this->assertFalse($node->exposed_module_extends_navigation('test1'));
359
    }
360
 
11 efrain 361
    public function test_navbar_prepend_and_add(): \moodle_page {
1 efrain 362
        global $PAGE;
363
        // Unfortunate hack needed because people use global $PAGE around the place.
364
        $PAGE->set_url('/');
365
 
366
        // We need to reset after this test because we using the generator.
367
        $this->resetAfterTest();
368
 
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));
373
 
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');
381
 
382
        $items = $page->navbar->get_items();
383
        foreach ($items as $item) {
384
            $this->assertInstanceOf('navigation_node', $item);
385
        }
386
 
387
        $i = 0;
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);
397
 
398
        return $page;
399
    }
400
 
401
    /**
402
     * @depends test_navbar_prepend_and_add
403
     * @param $node
404
     */
11 efrain 405
    public function test_navbar_has_items(\moodle_page $page): void {
1 efrain 406
        $this->resetAfterTest();
407
 
408
        $this->assertTrue($page->navbar->has_items());
409
    }
410
 
11 efrain 411
    public function test_cache__get(): void {
1 efrain 412
        $cache = new navigation_cache('unittest_nav');
413
        $cache->anysetvariable = true;
414
 
415
        $this->assertTrue($cache->anysetvariable);
416
        $this->assertEquals($cache->notasetvariable, null);
417
    }
418
 
11 efrain 419
    public function test_cache__set(): void {
1 efrain 420
        $cache = new navigation_cache('unittest_nav');
421
        $cache->anysetvariable = true;
422
 
423
        $cache->myname = 'Sam Hemelryk';
424
        $this->assertTrue($cache->cached('myname'));
425
        $this->assertSame('Sam Hemelryk', $cache->myname);
426
    }
427
 
11 efrain 428
    public function test_cache_cached(): void {
1 efrain 429
        $cache = new navigation_cache('unittest_nav');
430
        $cache->anysetvariable = true;
431
 
432
        $this->assertTrue($cache->cached('anysetvariable'));
433
        $this->assertFalse($cache->cached('notasetvariable'));
434
    }
435
 
11 efrain 436
    public function test_cache_clear(): void {
1 efrain 437
        $cache = new navigation_cache('unittest_nav');
438
        $cache->anysetvariable = true;
439
 
440
        $cache = clone($cache);
441
        $this->assertTrue($cache->cached('anysetvariable'));
442
        $cache->clear();
443
        $this->assertFalse($cache->cached('anysetvariable'));
444
    }
445
 
11 efrain 446
    public function test_cache_set(): void {
1 efrain 447
        $cache = new navigation_cache('unittest_nav');
448
        $cache->anysetvariable = true;
449
 
450
        $cache->set('software', 'Moodle');
451
        $this->assertTrue($cache->cached('software'));
452
        $this->assertEquals($cache->software, 'Moodle');
453
    }
454
 
11 efrain 455
    public function test_setting___construct(): settings_navigation {
1 efrain 456
        global $PAGE, $SITE;
457
 
458
        $this->resetAfterTest(false);
459
 
460
        $PAGE->set_url('/');
461
        $PAGE->set_course($SITE);
462
 
463
        $node = new exposed_settings_navigation();
464
 
465
        return $node;
466
    }
467
 
468
    /**
469
     * @depends test_setting___construct
470
     * @param mixed $node
471
     * @return mixed
472
     */
11 efrain 473
    public function test_setting__initialise($node): settings_navigation {
1 efrain 474
        $this->resetAfterTest(false);
475
 
476
        $node->initialise();
477
        $this->assertEquals($node->id, 'settingsnav');
478
 
479
        return $node;
480
    }
481
 
482
    /**
483
     * Test that users with the correct permissions can view the preferences page.
484
     */
11 efrain 485
    public function test_can_view_user_preferences(): void {
1 efrain 486
        global $PAGE, $DB, $SITE;
487
        $this->resetAfterTest();
488
 
489
        $persontoview = $this->getDataGenerator()->create_user();
490
        $persondoingtheviewing = $this->getDataGenerator()->create_user();
491
 
492
        $PAGE->set_url('/');
493
        $PAGE->set_course($SITE);
494
 
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));
503
 
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));
511
 
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));
519
    }
520
 
521
    /**
522
     * @depends test_setting__initialise
523
     * @param mixed $node
524
     * @return mixed
525
     */
11 efrain 526
    public function test_setting_in_alternative_role($node): void {
1 efrain 527
        $this->resetAfterTest();
528
 
529
        $this->assertFalse($node->exposed_in_alternative_role());
530
    }
531
 
532
 
11 efrain 533
    public function test_navigation_node_collection_remove_with_no_type(): void {
1 efrain 534
        $navigationnodecollection = new navigation_node_collection();
535
        $this->setup_node();
536
        $this->node->key = 100;
537
 
538
        // Test it's empty
539
        $this->assertEquals(0, count($navigationnodecollection->get_key_list()));
540
 
541
        // Add a node
542
        $navigationnodecollection->add($this->node);
543
 
544
        // Test it's not empty
545
        $this->assertEquals(1, count($navigationnodecollection->get_key_list()));
546
 
547
        // Remove a node - passing key only!
548
        $this->assertTrue($navigationnodecollection->remove(100));
549
 
550
        // Test it's empty again!
551
        $this->assertEquals(0, count($navigationnodecollection->get_key_list()));
552
    }
553
 
11 efrain 554
    public function test_navigation_node_collection_remove_with_type(): void {
1 efrain 555
        $navigationnodecollection = new navigation_node_collection();
556
        $this->setup_node();
557
        $this->node->key = 100;
558
 
559
        // Test it's empty
560
        $this->assertEquals(0, count($navigationnodecollection->get_key_list()));
561
 
562
        // Add a node
563
        $navigationnodecollection->add($this->node);
564
 
565
        // Test it's not empty
566
        $this->assertEquals(1, count($navigationnodecollection->get_key_list()));
567
 
568
        // Remove a node - passing type
569
        $this->assertTrue($navigationnodecollection->remove(100, 1));
570
 
571
        // Test it's empty again!
572
        $this->assertEquals(0, count($navigationnodecollection->get_key_list()));
573
    }
574
 
575
    /**
576
     * Test the set_force_into_more_menu method.
577
     *
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
581
     */
11 efrain 582
    public function test_set_force_into_more_menu(bool $haschildren, bool $forceintomoremenu): void {
1 efrain 583
        // Create a navigation node.
584
        $node = new navigation_node(['text' => 'Navigation node', 'key' => 'navnode']);
585
 
586
        // If required, add some children nodes to the navigation node.
587
        if ($haschildren) {
588
            for ($i = 1; $i <= 3; $i++) {
589
                $node->add("Child navigation node {$i}");
590
            }
591
        }
592
 
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);
599
        }
600
    }
601
 
602
    /**
603
     * Data provider for the test_set_force_into_more_menu function.
604
     *
605
     * @return array
606
     */
607
    public function set_force_into_more_menu_provider(): array {
608
        return [
609
            'Navigation node without any children nodes; Force into "more" menu => true.' =>
610
                [
611
                    false,
612
                    true,
613
                ],
614
            'Navigation node with children nodes; Force into "more" menu => true.' =>
615
                [
616
                    true,
617
                    true,
618
                ],
619
            'Navigation node with children nodes; Force into "more" menu => false.' =>
620
                [
621
                    true,
622
                    false,
623
                ],
624
        ];
625
    }
626
 
627
    /**
628
     * Test the is_action_link method.
629
     *
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
634
     */
11 efrain 635
    public function test_is_action_link(navigation_node $node, bool $expected): void {
1 efrain 636
        $this->assertEquals($node->is_action_link(), $expected);
637
    }
638
 
639
    /**
640
     * Data provider for the test_is_action_link function.
641
     *
642
     * @return array
643
     */
644
    public function is_action_link_provider(): array {
645
        return [
646
            'The navigation node has an action link.' =>
647
                [
648
                    navigation_node::create('Node', new action_link(new \moodle_url('/'), '',
649
                        new popup_action('click', new \moodle_url('/'))), navigation_node::TYPE_SETTING),
650
                    true
651
                ],
652
 
653
            'The navigation node does not have an action link.' =>
654
                [
655
                    navigation_node::create('Node', new \moodle_url('/'), navigation_node::TYPE_SETTING),
656
                    false
657
                ],
658
        ];
659
    }
660
 
661
    /**
662
     * Test the action_link_actions method.
663
     *
664
     * @param navigation_node $node The sample navigation node
665
     * @dataProvider action_link_actions_provider
666
     * @covers navigation_node::action_link_actions
667
     */
11 efrain 668
    public function test_action_link_actions(navigation_node $node): void {
1 efrain 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)
681
                ];
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']);
686
            }
687
        } else { // The navigation node does not have an action link.
688
            $this->assertEmpty($data);
689
        }
690
    }
691
 
692
    /**
693
     * Data provider for the test_action_link_actions function.
694
     *
695
     * @return array
696
     */
697
    public function action_link_actions_provider(): array {
698
        return [
699
            'The navigation node has an action link with an action attached.' =>
700
                [
701
                    navigation_node::create('Node', new action_link(new \moodle_url('/'), '',
702
                        new popup_action('click', new \moodle_url('/'))), navigation_node::TYPE_SETTING),
703
                ],
704
            'The navigation node has an action link without an action.' =>
705
                [
706
                    navigation_node::create('Node', new action_link(new \moodle_url('/'), '', null),
707
                        navigation_node::TYPE_SETTING),
708
                ],
709
            'The navigation node does not have an action link.' =>
710
                [
711
                    navigation_node::create('Node', new \moodle_url('/'), navigation_node::TYPE_SETTING),
712
                ],
713
        ];
714
    }
715
}
716
 
717
 
718
/**
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_`
721
 */
722
class exposed_global_navigation extends global_navigation {
723
    protected $exposedkey = 'exposed_';
724
    public function __construct(\moodle_page $page=null) {
725
        global $PAGE;
726
        if ($page === null) {
727
            $page = $PAGE;
728
        }
729
        parent::__construct($page);
730
    }
731
    public function __call($method, $arguments) {
732
        if (strpos($method, $this->exposedkey) !== false) {
733
            $method = substr($method, strlen($this->exposedkey));
734
        }
735
        if (method_exists($this, $method)) {
736
            return call_user_func_array(array($this, $method), $arguments);
737
        }
738
        throw new \coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
739
    }
740
    public function set_initialised() {
741
        $this->initialised = true;
742
    }
743
}
744
 
745
 
746
class mock_initialise_global_navigation extends global_navigation {
747
 
748
    protected static $count = 1;
749
 
750
    public function load_for_category() {
751
        $this->add('load_for_category', null, null, null, 'initcall'.self::$count);
752
        self::$count++;
753
        return 0;
754
    }
755
 
756
    public function load_for_course() {
757
        $this->add('load_for_course', null, null, null, 'initcall'.self::$count);
758
        self::$count++;
759
        return 0;
760
    }
761
 
762
    public function load_for_activity() {
763
        $this->add('load_for_activity', null, null, null, 'initcall'.self::$count);
764
        self::$count++;
765
        return 0;
766
    }
767
 
768
    public function load_for_user($user=null, $forceforcontext=false) {
769
        $this->add('load_for_user', null, null, null, 'initcall'.self::$count);
770
        self::$count++;
771
        return 0;
772
    }
773
}
774
 
775
/**
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_`.
778
 */
779
class exposed_navbar extends navbar {
780
    protected $exposedkey = 'exposed_';
781
 
782
    public function __construct(\moodle_page $page) {
783
        parent::__construct($page);
784
    }
785
    public function __call($method, $arguments) {
786
        if (strpos($method, $this->exposedkey) !== false) {
787
            $method = substr($method, strlen($this->exposedkey));
788
        }
789
        if (method_exists($this, $method)) {
790
            return call_user_func_array(array($this, $method), $arguments);
791
        }
792
        throw new \coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
793
    }
794
}
795
 
796
class navigation_exposed_moodle_page extends \moodle_page {
797
    public function set_navigation(navigation_node $node) {
798
        $this->_navigation = $node;
799
    }
800
}
801
 
802
/**
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_`.
805
 */
806
class exposed_settings_navigation extends settings_navigation {
807
    protected $exposedkey = 'exposed_';
808
    public function __construct() {
809
        global $PAGE;
810
        parent::__construct($PAGE);
811
    }
812
    public function __call($method, $arguments) {
813
        if (strpos($method, $this->exposedkey) !== false) {
814
            $method = substr($method, strlen($this->exposedkey));
815
        }
816
        if (method_exists($this, $method)) {
817
            return call_user_func_array(array($this, $method), $arguments);
818
        }
819
        throw new \coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
820
    }
821
}