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\output;
|
|
|
18 |
|
|
|
19 |
use ReflectionMethod;
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Primary navigation renderable test
|
|
|
23 |
*
|
|
|
24 |
* @package core
|
|
|
25 |
* @category output
|
|
|
26 |
* @copyright 2021 onwards Peter Dias
|
|
|
27 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
28 |
*/
|
|
|
29 |
class language_menu_test extends \advanced_testcase {
|
|
|
30 |
/**
|
|
|
31 |
* Basic setup to make sure the nav objects gets generated without any issues.
|
|
|
32 |
*/
|
|
|
33 |
public function setUp(): void {
|
|
|
34 |
global $PAGE;
|
|
|
35 |
$this->resetAfterTest();
|
|
|
36 |
$PAGE->set_url('/');
|
|
|
37 |
}
|
|
|
38 |
/**
|
|
|
39 |
* Test the get_lang_menu
|
|
|
40 |
*
|
|
|
41 |
* @dataProvider get_lang_menu_provider
|
|
|
42 |
* @param bool $withadditionallangs
|
|
|
43 |
* @param string $language
|
|
|
44 |
* @param array $expected
|
|
|
45 |
*/
|
11 |
efrain |
46 |
public function test_get_lang_menu(bool $withadditionallangs, string $language, array $expected): void {
|
1 |
efrain |
47 |
global $CFG, $PAGE;
|
|
|
48 |
|
|
|
49 |
// Mimic multiple langs installed. To trigger responses 'get_list_of_translations'.
|
|
|
50 |
// Note: The text/title of the nodes generated will be 'English(fr), English(de)' but we don't care about this.
|
|
|
51 |
// We are testing whether the nodes gets generated when the lang menu is available.
|
|
|
52 |
if ($withadditionallangs) {
|
|
|
53 |
mkdir("$CFG->dataroot/lang/de", 0777, true);
|
|
|
54 |
mkdir("$CFG->dataroot/lang/fr", 0777, true);
|
|
|
55 |
// Ensure the new langs are picked up and not taken from the cache.
|
|
|
56 |
$stringmanager = get_string_manager();
|
|
|
57 |
$stringmanager->reset_caches(true);
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
force_current_language($language);
|
|
|
61 |
|
|
|
62 |
$output = new language_menu($PAGE);
|
|
|
63 |
$method = new ReflectionMethod('\core\output\language_menu', 'export_for_template');
|
|
|
64 |
$renderer = $PAGE->get_renderer('core');
|
|
|
65 |
|
|
|
66 |
$response = $method->invoke($output, $renderer);
|
|
|
67 |
|
|
|
68 |
if ($withadditionallangs) { // If there are multiple languages installed.
|
|
|
69 |
// Assert that the title of the language menu matches the expected one.
|
|
|
70 |
$this->assertEquals($expected['title'], $response['title']);
|
|
|
71 |
// Assert that the number of language menu items matches the number of the expected items.
|
|
|
72 |
$this->assertEquals(count($expected['items']), count($response['items']));
|
|
|
73 |
foreach ($expected['items'] as $expecteditem) {
|
|
|
74 |
$lang = $expecteditem['lang'];
|
|
|
75 |
// We need to manually generate the url key and its value in the expected item array as this cannot
|
|
|
76 |
// be done in the data provider due to the change of the state of $PAGE.
|
|
|
77 |
if ($expecteditem['isactive']) {
|
|
|
78 |
$expecteditem['url'] = new \moodle_url('#');
|
|
|
79 |
} else {
|
|
|
80 |
$expecteditem['url'] = new \moodle_url($PAGE->url, ['lang' => $lang]);
|
|
|
81 |
// When the language menu item is not the current language, it will contain the lang attribute.
|
|
|
82 |
$expecteditem['attributes'][] = [
|
|
|
83 |
'key' => 'lang',
|
|
|
84 |
'value' => $lang
|
|
|
85 |
];
|
|
|
86 |
}
|
|
|
87 |
// The lang value is only used to generate the url, so this key can be removed.
|
|
|
88 |
unset($expecteditem['lang']);
|
|
|
89 |
|
|
|
90 |
// Assert that the given expected item exists in the returned items.
|
|
|
91 |
$this->assertTrue(in_array($expecteditem, $response['items']));
|
|
|
92 |
}
|
|
|
93 |
} else { // No multiple languages.
|
|
|
94 |
$this->assertEquals($expected, $response);
|
|
|
95 |
}
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
/**
|
|
|
99 |
* Provider for test_get_lang_menu
|
|
|
100 |
*
|
|
|
101 |
* @return array
|
|
|
102 |
*/
|
|
|
103 |
public function get_lang_menu_provider(): array {
|
|
|
104 |
return [
|
|
|
105 |
'Lang menu with only the current language' => [
|
|
|
106 |
false, 'en', []
|
|
|
107 |
],
|
|
|
108 |
'Lang menu with only multiple languages installed' => [
|
|
|
109 |
true, 'en', [
|
|
|
110 |
'title' => 'English ‎(en)‎',
|
|
|
111 |
'items' => [
|
|
|
112 |
[
|
|
|
113 |
'title' => 'English ‎(en)‎',
|
|
|
114 |
'text' => 'English ‎(en)‎',
|
|
|
115 |
'link' => true,
|
|
|
116 |
'isactive' => true,
|
|
|
117 |
'lang' => 'en'
|
|
|
118 |
],
|
|
|
119 |
[
|
|
|
120 |
'title' => 'English ‎(de)‎',
|
|
|
121 |
'text' => 'English ‎(de)‎',
|
|
|
122 |
'link' => true,
|
|
|
123 |
'isactive' => false,
|
|
|
124 |
'lang' => 'de'
|
|
|
125 |
],
|
|
|
126 |
|
|
|
127 |
[
|
|
|
128 |
'title' => 'English ‎(fr)‎',
|
|
|
129 |
'text' => 'English ‎(fr)‎',
|
|
|
130 |
'link' => true,
|
|
|
131 |
'isactive' => false,
|
|
|
132 |
'lang' => 'fr'
|
|
|
133 |
],
|
|
|
134 |
],
|
|
|
135 |
],
|
|
|
136 |
],
|
|
|
137 |
'Lang menu with only multiple languages installed and other than EN set active.' => [
|
|
|
138 |
true, 'de', [
|
|
|
139 |
'title' => 'English ‎(de)‎',
|
|
|
140 |
'items' => [
|
|
|
141 |
[
|
|
|
142 |
'title' => 'English ‎(en)‎',
|
|
|
143 |
'text' => 'English ‎(en)‎',
|
|
|
144 |
'link' => true,
|
|
|
145 |
'isactive' => false,
|
|
|
146 |
'lang' => 'en'
|
|
|
147 |
],
|
|
|
148 |
[
|
|
|
149 |
'title' => 'English ‎(de)‎',
|
|
|
150 |
'text' => 'English ‎(de)‎',
|
|
|
151 |
'link' => true,
|
|
|
152 |
'isactive' => true,
|
|
|
153 |
'lang' => 'de'
|
|
|
154 |
],
|
|
|
155 |
[
|
|
|
156 |
'title' => 'English ‎(fr)‎',
|
|
|
157 |
'text' => 'English ‎(fr)‎',
|
|
|
158 |
'link' => true,
|
|
|
159 |
'isactive' => false,
|
|
|
160 |
'lang' => 'fr'
|
|
|
161 |
],
|
|
|
162 |
],
|
|
|
163 |
],
|
|
|
164 |
],
|
|
|
165 |
];
|
|
|
166 |
}
|
|
|
167 |
}
|