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