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 |
/**
|
|
|
20 |
* Tests user menu functionality.
|
|
|
21 |
*
|
|
|
22 |
* @package core
|
|
|
23 |
* @copyright 2015 Jetha Chan <jetha@moodle.com>
|
|
|
24 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
25 |
*/
|
|
|
26 |
class user_menu_test extends \advanced_testcase {
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Custom user menu data for the test_custom_user_menu test.
|
|
|
30 |
*
|
|
|
31 |
* @return array containing testing data
|
|
|
32 |
*/
|
|
|
33 |
public function custom_user_menu_data() {
|
|
|
34 |
return array(
|
|
|
35 |
// These are fillers only.
|
|
|
36 |
array('###', 0, 1),
|
|
|
37 |
array('#####', 0, 1),
|
|
|
38 |
|
|
|
39 |
// These are invalid and will not generate any entry or filler.
|
|
|
40 |
array('-----', 0, 0),
|
|
|
41 |
array('_____', 0, 0),
|
|
|
42 |
array('test', 0, 0),
|
|
|
43 |
array('#Garbage#', 0, 0),
|
|
|
44 |
array('privatefiles,/user/files.php', 0, 0),
|
|
|
45 |
|
|
|
46 |
// These are valid but have an invalid string identifiers or components. They will still produce a menu
|
|
|
47 |
// item, and no exception should be thrown.
|
|
|
48 |
array('#my1files,moodle|/user/files.php', 1, 1),
|
|
|
49 |
array('#my1files,moodleakjladf|/user/files.php', 1, 1),
|
|
|
50 |
array('#my1files,a/b|/user/files.php', 1, 1),
|
|
|
51 |
array('#my1files,#b|/user/files.php', 1, 1),
|
|
|
52 |
|
|
|
53 |
// These are unusual, but valid and will generate a menu entry (no filler).
|
|
|
54 |
array('-|-|-|-', 1, 1),
|
|
|
55 |
array('-|-|-', 1, 1),
|
|
|
56 |
array('-|-', 1, 1),
|
|
|
57 |
array('#f234|2', 1, 1),
|
|
|
58 |
|
|
|
59 |
// This is a pretty typical entry.
|
|
|
60 |
array('messages,message|/message/index.php', 1, 1),
|
|
|
61 |
|
|
|
62 |
// And these are combinations containing both valid and invalid.
|
|
|
63 |
array('messages,message|/message/index.php
|
|
|
64 |
privatefiles,moodle|/user/files.php
|
|
|
65 |
###
|
|
|
66 |
badges,badges|/badges/mybadges.php
|
|
|
67 |
-|-
|
|
|
68 |
test
|
|
|
69 |
-
|
|
|
70 |
#####
|
|
|
71 |
#f234|2', 5, 3),
|
|
|
72 |
);
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* Test the custom user menu.
|
|
|
77 |
*
|
|
|
78 |
* @dataProvider custom_user_menu_data
|
|
|
79 |
* @param string $input The menu text to test
|
|
|
80 |
* @param int $entrycount The numbers of entries expected
|
|
|
81 |
*/
|
11 |
efrain |
82 |
public function test_custom_user_menu($data, $entrycount, $dividercount): void {
|
1 |
efrain |
83 |
global $CFG, $OUTPUT, $USER, $PAGE;
|
|
|
84 |
|
|
|
85 |
// Must reset because of config and user modifications.
|
|
|
86 |
$this->resetAfterTest(true);
|
|
|
87 |
|
|
|
88 |
// Test using an admin user at the root of Moodle; this way we don't have to create a test user with avatar.
|
|
|
89 |
$this->setAdminUser();
|
|
|
90 |
$PAGE->set_url('/');
|
|
|
91 |
$CFG->theme = 'classic';
|
|
|
92 |
$PAGE->reset_theme_and_output();
|
|
|
93 |
$PAGE->initialise_theme_and_output();
|
|
|
94 |
|
|
|
95 |
// Set the configuration.
|
|
|
96 |
set_config('customusermenuitems', $data);
|
|
|
97 |
|
|
|
98 |
// We always add two dividers as standard.
|
|
|
99 |
$dividercount += 2;
|
|
|
100 |
|
|
|
101 |
// The basic entry count will additionally include the wrapper menu, Preferences, Logout and switch roles link.
|
|
|
102 |
$entrycount += 3;
|
|
|
103 |
|
|
|
104 |
$output = $OUTPUT->user_menu($USER);
|
|
|
105 |
preg_match_all('/<a [^>]+role="menuitem"[^>]+>/', $output, $results);
|
|
|
106 |
$this->assertCount($entrycount, $results[0]);
|
|
|
107 |
|
|
|
108 |
preg_match_all('/<span class="filler">/', $output, $results);
|
|
|
109 |
$this->assertCount($dividercount, $results[0]);
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
}
|