Proyectos de Subversion Moodle

Rev

Ir a la última revisión | | 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 theme_boost;
18
 
19
/**
20
 * Test the boostnavbar file
21
 *
22
 * @package    theme_boost
23
 * @covers     \theme_boost\boostnavbar
24
 * @copyright  2021 Peter Dias
25
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 */
27
class boostnavbar_test extends \advanced_testcase {
28
    /**
29
     * Provider for test_remove_no_link_items
30
     * The setup and expected arrays are defined as an array of 'nodekey' => $hasaction
31
     *
32
     * @return array
33
     */
34
    public function remove_no_link_items_provider(): array {
35
        return [
36
            'All nodes have links links including leaf node. Set to remove section nodes.' => [
37
                [
38
                    'node1' => ['hasaction' => true, 'issection' => false],
39
                    'node2' => ['hasaction' => true, 'issection' => false],
40
                    'node3' => ['hasaction' => true, 'issection' => false],
41
                ],
42
                true,
43
                [
44
                    'Home' => true,
45
                    'Courses' => true,
46
                    'tc_1' => true,
47
                    'node1' => true,
48
                    'node2' => true,
49
                    'node3' => true,
50
                ]
51
            ],
52
            'Only some parent nodes have links. Leaf node has a link. Set to remove section nodes.' => [
53
                [
54
                    'node1' => ['hasaction' => false, 'issection' => false],
55
                    'node2' => ['hasaction' => true, 'issection' => false],
56
                    'node3' => ['hasaction' => true, 'issection' => false],
57
                ],
58
                true,
59
                [
60
                    'Home' => true,
61
                    'Courses' => true,
62
                    'tc_1' => true,
63
                    'node2' => true,
64
                    'node3' => true,
65
                ]
66
            ],
67
            'All parent nodes do not have links. Leaf node has a link. Set to remove section nodes.' => [
68
                [
69
                    'node1' => ['hasaction' => false, 'issection' => false],
70
                    'node2' => ['hasaction' => false, 'issection' => false],
71
                    'node3' => ['hasaction' => true, 'issection' => false],
72
                ],
73
                true,
74
                [
75
                    'Home' => true,
76
                    'Courses' => true,
77
                    'tc_1' => true,
78
                    'node3' => true,
79
                ]
80
            ],
81
            'All parent nodes have links. Leaf node does not has a link. Set to remove section nodes.' => [
82
                [
83
                    'node1' => ['hasaction' => true, 'issection' => false],
84
                    'node2' => ['hasaction' => true, 'issection' => false],
85
                    'node3' => ['hasaction' => false, 'issection' => false],
86
                ],
87
                true,
88
                [
89
                    'Home' => true,
90
                    'Courses' => true,
91
                    'tc_1' => true,
92
                    'node1' => true,
93
                    'node2' => true,
94
                    'node3' => false,
95
                ]
96
            ],
97
            'All parent nodes do not have links. Leaf node does not has a link. Set to remove section nodes.' => [
98
                [
99
                    'node1' => ['hasaction' => false, 'issection' => false],
100
                    'node2' => ['hasaction' => false, 'issection' => false],
101
                    'node3' => ['hasaction' => false, 'issection' => false],
102
                ],
103
                true,
104
                [
105
                    'Home' => true,
106
                    'Courses' => true,
107
                    'tc_1' => true,
108
                    'node3' => false,
109
                ]
110
            ],
111
            'Some parent nodes do not have links. Leaf node does not has a link. Set to remove section nodes.' => [
112
                [
113
                    'node1' => ['hasaction' => true, 'issection' => false],
114
                    'node2' => ['hasaction' => false, 'issection' => false],
115
                    'node3' => ['hasaction' => false, 'issection' => false],
116
                ],
117
                true,
118
                [
119
                    'Home' => true,
120
                    'Courses' => true,
121
                    'tc_1' => true,
122
                    'node1' => true,
123
                    'node3' => false,
124
                ]
125
            ],
126
            'All nodes have links links including leaf node and section nodes. Set to remove section nodes.' => [
127
                [
128
                    'node1' => ['hasaction' => true, 'issection' => false],
129
                    'node2' => ['hasaction' => true, 'issection' => false],
130
                    'sectionnode1' => ['hasaction' => true, 'issection' => true],
131
                    'node3' => ['hasaction' => true, 'issection' => false],
132
                ],
133
                true,
134
                [
135
                    'Home' => true,
136
                    'Courses' => true,
137
                    'tc_1' => true,
138
                    'node1' => true,
139
                    'node2' => true,
140
                    'node3' => true,
141
                ]
142
            ],
143
            'All nodes have links links including leaf node and section nodes. Set to not remove section nodes.' => [
144
                [
145
                    'node1' => ['hasaction' => true, 'issection' => false],
146
                    'node2' => ['hasaction' => true, 'issection' => false],
147
                    'sectionnode1' => ['hasaction' => true, 'issection' => true],
148
                    'node3' => ['hasaction' => true, 'issection' => false],
149
                ],
150
                false,
151
                [
152
                    'Home' => true,
153
                    'Courses' => true,
154
                    'tc_1' => true,
155
                    'node1' => true,
156
                    'node2' => true,
157
                    'sectionnode1' => true,
158
                    'node3' => true,
159
                ]
160
            ],
161
            'Only some parent nodes have links. Section node does not have a link. Set to not remove section nodes.' => [
162
                [
163
                    'node1' => ['hasaction' => false, 'issection' => false],
164
                    'node2' => ['hasaction' => true, 'issection' => false],
165
                    'sectionnode1' => ['hasaction' => false, 'issection' => true],
166
                    'node3' => ['hasaction' => true, 'issection' => false],
167
                ],
168
                true,
169
                [
170
                    'Home' => true,
171
                    'Courses' => true,
172
                    'tc_1' => true,
173
                    'node2' => true,
174
                    'node3' => true,
175
                ]
176
            ]
177
        ];
178
    }
179
    /**
180
     * Test the remove_no_link_items function
181
     *
182
     * @dataProvider remove_no_link_items_provider
183
     * @param array $setup
184
     * @param bool $removesectionnodes Whether to remove the section nodes with an associated action.
185
     * @param array $expected
186
     * @throws \ReflectionException
187
     */
188
    public function test_remove_no_link_items(array $setup, bool $removesectionnodes, array $expected) {
189
        global $PAGE;
190
 
191
        $this->resetAfterTest();
192
        // Unfortunate hack needed because people use global $PAGE around the place.
193
        $PAGE->set_url('/');
194
        $course = $this->getDataGenerator()->create_course();
195
        $page = new \moodle_page();
196
        $page->set_course($course);
197
        $page->set_url(new \moodle_url('/course/view.php', array('id' => $course->id)));
198
        // A dummy url to use. We don't care where it's pointing to.
199
        $url = new \moodle_url('/');
200
        foreach ($setup as $key => $value) {
201
            $page->navbar->add($key, $value['hasaction'] ? $url : null,
202
                $value['issection'] ? \navigation_node::TYPE_SECTION : null);
203
        }
204
 
205
        $boostnavbar = $this->getMockBuilder(boostnavbar::class)
206
            ->disableOriginalConstructor()
207
            ->onlyMethods([])
208
            ->getMock();
209
 
210
        $rc = new \ReflectionClass(boostnavbar::class);
211
        $rcp = $rc->getProperty('items');
212
        $rcp->setValue($boostnavbar, $page->navbar->get_items());
213
 
214
        // Make the call to the function.
215
        $rcm = $rc->getMethod('remove_no_link_items');
216
        $rcm->invoke($boostnavbar, $removesectionnodes);
217
 
218
        // Get the value for the class variable that the function modifies.
219
        $values = $rcp->getValue($boostnavbar);
220
        $actual = [];
221
        foreach ($values as $value) {
222
            $actual[$value->text] = $value->has_action();
223
        }
224
        $this->assertEquals($expected, $actual);
225
    }
226
 
227
    /**
228
     * Provider for test_remove_duplicate_items.
229
     *
230
     * @return array
231
     */
232
    public function remove_duplicate_items_provider(): array {
233
        global $CFG;
234
 
235
        return [
236
            'Breadcrumb items with identical text and action url (actions of same type moodle_url).' => [
237
                [
238
                    [
239
                        'text' => 'Node 1',
240
                        'action' => new \moodle_url('/page1.php')
241
                    ],
242
                    [
243
                        'text' => 'Node 2',
244
                        'action' => new \moodle_url('/page2.php', ['id' => 1])
245
                    ],
246
                    [
247
                        'text' => 'Node 4',
248
                        'action' => new \moodle_url('/page4.php', ['id' => 1])
249
                    ],
250
                    [
251
                        'text' => 'Node 2',
252
                        'action' => new \moodle_url('/page2.php', ['id' => 1])
253
                    ],
254
                ],
255
                ['Home', 'Node 1', 'Node 4', 'Node 2']
256
            ],
257
            'Breadcrumb items with identical text and action url (actions of different type moodle_url/text).' => [
258
                [
259
                    [
260
                        'text' => 'Node 1',
261
                        'action' => new \moodle_url('/page1.php')
262
                    ],
263
                    [
264
                        'text' => 'Node 2',
265
                        'action' => new \moodle_url('/page2.php', ['id' => 1])
266
                    ],
267
                    [
268
                        'text' => 'Node 4',
269
                        'action' => new \moodle_url('/page4.php', ['id' => 1])
270
                    ],
271
                    [
272
                        'text' => 'Node 2',
273
                        'action' => "{$CFG->wwwroot}/page2.php?id=1"
274
                    ],
275
                ],
276
                ['Home', 'Node 1', 'Node 4', 'Node 2']
277
            ],
278
            'Breadcrumb items with identical text and action url (actions of different type moodle_url/action_link).' => [
279
                [
280
                    [
281
                        'text' => 'Node 1',
282
                        'action' => new \moodle_url('/page1.php')
283
                    ],
284
                    [
285
                        'text' => 'Node 2',
286
                        'action' => new \moodle_url('/page2.php', ['id' => 1])
287
                    ],
288
                    [
289
                        'text' => 'Node 4',
290
                        'action' => new \moodle_url('/page4.php', ['id' => 1])
291
                    ],
292
                    [
293
                        'text' => 'Node 2',
294
                        'action' => new \action_link(new \moodle_url('/page2.php', ['id' => 1]), 'Action link')
295
                    ],
296
                ],
297
                ['Home', 'Node 1', 'Node 4', 'Node 2']
298
            ],
299
            'Breadcrumbs items with identical text but not identical action url.' => [
300
                [
301
                    [
302
                        'text' => 'Node 1',
303
                        'action' => new \moodle_url('/page1.php')
304
                    ],
305
                    [
306
                        'text' => 'Node 2',
307
                        'action' => new \moodle_url('/page2.php', ['id' => 1])
308
                    ],
309
                    [
310
                        'text' => 'Node 2',
311
                        'action' => new \moodle_url('/page2.php', ['id' => 2])
312
                    ],
313
                    [
314
                        'text' => 'Node 4',
315
                        'action' => new \moodle_url('/page4.php', ['id' => 1])
316
                    ],
317
                ],
318
                ['Home', 'Node 1', 'Node 2', 'Node 2', 'Node 4']
319
            ],
320
            'Breadcrumb items with identical action url but not identical text.' => [
321
                [
322
                    [
323
                        'text' => 'Node 1',
324
                        'action' => new \moodle_url('/page1.php')
325
                    ],
326
                    [
327
                        'text' => 'Node 2',
328
                        'action' => new \moodle_url('/page2.php', ['id' => 1])
329
                    ],
330
                    [
331
                        'text' => 'Node 3',
332
                        'action' => new \moodle_url('/page2.php', ['id' => 1])
333
                    ],
334
                    [
335
                        'text' => 'Node 4',
336
                        'action' => new \moodle_url('/page4.php', ['id' => 1])
337
                    ],
338
                ],
339
                ['Home', 'Node 1', 'Node 2', 'Node 3', 'Node 4']
340
            ],
341
            'Breadcrumb items without any identical action url or text.' => [
342
                [
343
                    [
344
                        'text' => 'Node 1',
345
                        'action' => new \moodle_url('/page1.php')
346
                    ],
347
                    [
348
                        'text' => 'Node 2',
349
                        'action' => new \moodle_url('/page2.php', ['id' => 1])
350
                    ],
351
                    [
352
                        'text' => 'Node 3',
353
                        'action' => new \moodle_url('/page3.php', ['id' => 1])
354
                    ],
355
                    [
356
                        'text' => 'Node 4',
357
                        'action' => new \moodle_url('/page4.php', ['id' => 1])
358
                    ],
359
                ],
360
                ['Home', 'Node 1', 'Node 2', 'Node 3', 'Node 4']
361
            ],
362
        ];
363
    }
364
 
365
    /**
366
     * Test the remove_duplicate_items function.
367
     *
368
     * @dataProvider remove_duplicate_items_provider
369
     * @param array $navbarnodes The array containing the text and action of the nodes to be added to the navbar
370
     * @param array $expected The array containing the text of the expected navbar nodes
371
     */
372
    public function test_remove_duplicate_items(array $navbarnodes, array $expected) {
373
        $this->resetAfterTest();
374
        $page = new \moodle_page();
375
        $page->set_url('/');
376
 
377
        // Add the navbar nodes.
378
        foreach ($navbarnodes as $node) {
379
            $page->navbar->add($node['text'], $node['action'], \navigation_node::TYPE_CUSTOM);
380
        }
381
 
382
        $boostnavbar = $this->getMockBuilder(boostnavbar::class)
383
            ->disableOriginalConstructor()
384
            ->onlyMethods([])
385
            ->getMock();
386
 
387
        $rc = new \ReflectionClass(boostnavbar::class);
388
        $rcp = $rc->getProperty('items');
389
        $rcp->setValue($boostnavbar, $page->navbar->get_items());
390
 
391
        // Make the call to the function.
392
        $rcm = $rc->getMethod('remove_duplicate_items');
393
        $rcm->invoke($boostnavbar);
394
 
395
        // Get the value for the class variable that the function modifies.
396
        $values = $rcp->getValue($boostnavbar);
397
        $actual = [];
398
        foreach ($values as $value) {
399
            $actual[] = $value->text;
400
        }
401
        $this->assertEquals($expected, $actual);
402
    }
403
 
404
 
405
    /**
406
     * Provider for test_remove_items_that_exist_in_navigation.
407
     *
408
     * @return array
409
     */
410
    public function remove_items_that_exist_in_navigation_provider(): array {
411
        global $CFG;
412
 
413
        return [
414
            'Item with identical action url and text exists in the primary navigation menu.' => [
415
                'primary',
416
                [
417
                    [
418
                        'text' => 'Node 1',
419
                        'action' => new \moodle_url('/page1.php')
420
                    ],
421
                ],
422
                [
423
                    'Node 1' => new \moodle_url('/page1.php'),
424
                    'Node 2' => new \moodle_url('/page2.php'),
425
                    'Node 3' => new \moodle_url('/page1.php'),
426
                ],
427
                ['Node 2', 'Node 3']
428
            ],
429
            'Item with identical action url and text exists in the secondary navigation menu.' => [
430
                'secondary',
431
                [
432
                    [
433
                        'text' => 'Node 2',
434
                        'action' => new \moodle_url('/page2.php')
435
                    ],
436
                ],
437
                [
438
                    'Node 1' => new \moodle_url('/page1.php'),
439
                    'Node 2' => new \moodle_url('/page2.php'),
440
                    'Node 3' => new \moodle_url('/page1.php'),
441
                ],
442
                ['Home', 'Node 1', 'Node 3']
443
            ],
444
            'Multiple items with identical action url and text exist in the secondary navigation menu.' => [
445
                'secondary',
446
                [
447
                    [
448
                        'text' => 'Node 2',
449
                        'action' => new \moodle_url('/page2.php')
450
                    ],
451
                    [
452
                        'text' => 'Node 3',
453
                        'action' => new \moodle_url('/page3.php')
454
                    ],
455
                ],
456
                [
457
                    'Node 1' => new \moodle_url('/page1.php'),
458
                    'Node 2' => "{$CFG->wwwroot}/page2.php",
459
                    'Node 3' => new \action_link(new \moodle_url('/page3.php'), 'Action link'),
460
                ],
461
                ['Home', 'Node 1']
462
            ],
463
            'No items with identical action url and text in the secondary navigation menu.' => [
464
                'secondary',
465
                [
466
                    [
467
                        'text' => 'Node 4',
468
                        'action' => new \moodle_url('/page4.php')
469
                    ],
470
                ],
471
                [
472
                    'Node 1' => new \moodle_url('/page1.php'),
473
                    'Node 2' => new \moodle_url('/page2.php'),
474
                    'Node 3' => new \moodle_url('/page1.php'),
475
                ],
476
                ['Home', 'Node 1', 'Node 2', 'Node 3']
477
            ],
478
        ];
479
    }
480
 
481
    /**
482
     * Test the remove_items_that_exist_in_navigation function.
483
     *
484
     * @dataProvider remove_items_that_exist_in_navigation_provider
485
     * @param string $navmenu The name of the navigation menu we would like to use (primary or secondary)
486
     * @param array $navmenunodes The array containing the text and action of the nodes to be added to the navigation menu
487
     * @param array $navbarnodes Array containing the text => action of the nodes to be added to the navbar
488
     * @param array $expected Array containing the text of the expected navbar nodes after the filtering
489
     */
490
    public function test_remove_items_that_exist_in_navigation(string $navmenu, array $navmenunodes, array $navbarnodes,
491
            array $expected) {
492
        global $PAGE;
493
 
494
        // Unfortunate hack needed because people use global $PAGE around the place.
495
        $PAGE->set_url('/');
496
        $this->resetAfterTest();
497
        $page = new \moodle_page();
498
        $page->set_url('/');
499
 
500
        switch ($navmenu) {
501
            case 'primary':
502
                $navigationmenu = new \core\navigation\views\primary($page);
503
                break;
504
            case 'secondary':
505
                $navigationmenu = new \core\navigation\views\secondary($page);
506
        }
507
 
508
        $navigationmenu->initialise();
509
        // Add the additional nodes to the navigation menu.
510
        foreach ($navmenunodes as $navmenunode) {
511
            $navigationmenu->add($navmenunode['text'], $navmenunode['action'], \navigation_node::TYPE_CUSTOM);
512
        }
513
 
514
        // Add the additional navbar nodes.
515
        foreach ($navbarnodes as $text => $action) {
516
            $page->navbar->add($text, $action, \navigation_node::TYPE_CUSTOM);
517
        }
518
 
519
        $boostnavbar = $this->getMockBuilder(boostnavbar::class)
520
            ->disableOriginalConstructor()
521
            ->onlyMethods([])
522
            ->getMock();
523
 
524
        $rc = new \ReflectionClass(boostnavbar::class);
525
        $rcp = $rc->getProperty('items');
526
        $rcp->setValue($boostnavbar, $page->navbar->get_items());
527
 
528
        // Make the call to the function.
529
        $rcm = $rc->getMethod('remove_items_that_exist_in_navigation');
530
        $rcm->invoke($boostnavbar, $navigationmenu);
531
 
532
        // Get the value for the class variable that the function modifies.
533
        $values = $rcp->getValue($boostnavbar);
534
        $actual = [];
535
        foreach ($values as $value) {
536
            $actual[] = $value->text;
537
        }
538
        $this->assertEquals($expected, $actual);
539
    }
540
}