MDL-64506 phpunit: Update unit tests to use classic
[moodle.git] / lib / tests / user_menu_test.php
CommitLineData
c589b7be
JC
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/**
18 * Tests user menu functionality.
19 *
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
23 */
24class core_user_menu_testcase extends advanced_testcase {
25
8b7ca27d
AN
26 /**
27 * Custom user menu data for the test_custom_user_menu test.
28 *
29 * @return array containing testing data
30 */
31 public function custom_user_menu_data() {
32 return array(
33 // These are fillers only.
34 array('###', 0, 1),
35 array('#####', 0, 1),
c589b7be 36
8b7ca27d
AN
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),
06c27531
AN
41 array('#Garbage#', 0, 0),
42
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),
c589b7be 49
8b7ca27d
AN
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),
c589b7be 55
8b7ca27d
AN
56 // This is a pretty typical entry.
57 array('messages,message|/message/index.php|message', 1, 0),
c589b7be 58
8b7ca27d
AN
59 // And these are combinations containing both valid and invalid.
60 array('messages,message|/message/index.php|message
4887d152 61privatefiles,moodle|/user/files.php|download
c589b7be 62###
4887d152 63badges,badges|/badges/mybadges.php|award
c589b7be
JC
64-|-|-
65test
66-
67#####
8b7ca27d
AN
68#f234|2', 5, 2),
69 );
70 }
71
72 /**
73 * Test the custom user menu.
74 *
75 * @dataProvider custom_user_menu_data
76 * @param string $input The menu text to test
77 * @param int $entrycount The numbers of entries expected
78 */
79 public function test_custom_user_menu($data, $entrycount, $dividercount) {
80 global $CFG, $OUTPUT, $USER, $PAGE;
81
82 // Must reset because of config and user modifications.
83 $this->resetAfterTest(true);
84
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('/');
e00f1c66 88 $CFG->theme = 'classic';
191a1c7c
DW
89 $PAGE->reset_theme_and_output();
90 $PAGE->initialise_theme_and_output();
8b7ca27d
AN
91
92 // Set the configuration.
93 set_config('customusermenuitems', $data);
94
95 // We always add two dividers as standard.
96 $dividercount += 2;
97
22b8c5b8 98 // The basic entry count will additionally include the wrapper menu, Dashboard, Profile, Logout and switch roles link.
a3d57af8 99 $entrycount += 4;
c589b7be 100
8b7ca27d
AN
101 $output = $OUTPUT->user_menu($USER);
102 preg_match_all('/<a [^>]+role="menuitem"[^>]+>/', $output, $results);
103 $this->assertCount($entrycount, $results[0]);
c589b7be 104
8b7ca27d
AN
105 preg_match_all('/<span class="filler">/', $output, $results);
106 $this->assertCount($dividercount, $results[0]);
c589b7be
JC
107 }
108
109}