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