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 |
defined('MOODLE_INTERNAL') || die();
|
|
|
20 |
|
|
|
21 |
global $CFG;
|
|
|
22 |
require_once($CFG->libdir . '/outputrequirementslib.php');
|
|
|
23 |
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* Unit tests for lib/outputrequirementslibphp.
|
|
|
27 |
*
|
|
|
28 |
* @package core
|
|
|
29 |
* @category test
|
|
|
30 |
* @copyright 2012 Petr Škoda
|
|
|
31 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
32 |
*/
|
|
|
33 |
class outputrequirementslib_test extends \advanced_testcase {
|
11 |
efrain |
34 |
public function test_string_for_js(): void {
|
1 |
efrain |
35 |
$this->resetAfterTest();
|
|
|
36 |
|
|
|
37 |
$page = new \moodle_page();
|
|
|
38 |
$page->requires->string_for_js('course', 'moodle', 1);
|
|
|
39 |
$page->requires->string_for_js('course', 'moodle', 1);
|
|
|
40 |
$this->expectException('coding_exception');
|
|
|
41 |
$page->requires->string_for_js('course', 'moodle', 2);
|
|
|
42 |
|
|
|
43 |
// Note: we can not switch languages in phpunit yet,
|
|
|
44 |
// it would be nice to test that the strings are actually fetched in the footer.
|
|
|
45 |
}
|
|
|
46 |
|
11 |
efrain |
47 |
public function test_one_time_output_normal_case(): void {
|
1 |
efrain |
48 |
$page = new \moodle_page();
|
|
|
49 |
$this->assertTrue($page->requires->should_create_one_time_item_now('test_item'));
|
|
|
50 |
$this->assertFalse($page->requires->should_create_one_time_item_now('test_item'));
|
|
|
51 |
}
|
|
|
52 |
|
11 |
efrain |
53 |
public function test_one_time_output_repeat_output_throws(): void {
|
1 |
efrain |
54 |
$page = new \moodle_page();
|
|
|
55 |
$page->requires->set_one_time_item_created('test_item');
|
|
|
56 |
$this->expectException('coding_exception');
|
|
|
57 |
$page->requires->set_one_time_item_created('test_item');
|
|
|
58 |
}
|
|
|
59 |
|
11 |
efrain |
60 |
public function test_one_time_output_different_pages_independent(): void {
|
1 |
efrain |
61 |
$firstpage = new \moodle_page();
|
|
|
62 |
$secondpage = new \moodle_page();
|
|
|
63 |
$this->assertTrue($firstpage->requires->should_create_one_time_item_now('test_item'));
|
|
|
64 |
$this->assertTrue($secondpage->requires->should_create_one_time_item_now('test_item'));
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* Test for the jquery_plugin method.
|
|
|
69 |
*
|
|
|
70 |
* Test to make sure that backslashes are not generated with either slasharguments set to on or off.
|
|
|
71 |
*/
|
11 |
efrain |
72 |
public function test_jquery_plugin(): void {
|
1 |
efrain |
73 |
global $CFG, $PAGE;
|
|
|
74 |
|
|
|
75 |
$this->resetAfterTest();
|
|
|
76 |
|
|
|
77 |
// With slasharguments on.
|
|
|
78 |
$CFG->slasharguments = 1;
|
|
|
79 |
|
|
|
80 |
$page = new \moodle_page();
|
|
|
81 |
$requirements = $page->requires;
|
|
|
82 |
// Assert successful method call.
|
|
|
83 |
$this->assertTrue($requirements->jquery_plugin('jquery'));
|
|
|
84 |
$this->assertTrue($requirements->jquery_plugin('ui'));
|
|
|
85 |
|
|
|
86 |
// Get the code containing the required jquery plugins.
|
|
|
87 |
$renderer = $PAGE->get_renderer('core', null, RENDERER_TARGET_MAINTENANCE);
|
|
|
88 |
$requirecode = $requirements->get_top_of_body_code($renderer);
|
|
|
89 |
// Make sure that the generated code does not contain backslashes.
|
|
|
90 |
$this->assertFalse(strpos($requirecode, '\\'), "Output contains backslashes: " . $requirecode);
|
|
|
91 |
|
|
|
92 |
// With slasharguments off.
|
|
|
93 |
$CFG->slasharguments = 0;
|
|
|
94 |
|
|
|
95 |
$page = new \moodle_page();
|
|
|
96 |
$requirements = $page->requires;
|
|
|
97 |
// Assert successful method call.
|
|
|
98 |
$this->assertTrue($requirements->jquery_plugin('jquery'));
|
|
|
99 |
$this->assertTrue($requirements->jquery_plugin('ui'));
|
|
|
100 |
|
|
|
101 |
// Get the code containing the required jquery plugins.
|
|
|
102 |
$requirecode = $requirements->get_top_of_body_code($renderer);
|
|
|
103 |
// Make sure that the generated code does not contain backslashes.
|
|
|
104 |
$this->assertFalse(strpos($requirecode, '\\'), "Output contains backslashes: " . $requirecode);
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
/**
|
|
|
108 |
* Test AMD modules loading.
|
|
|
109 |
*/
|
11 |
efrain |
110 |
public function test_js_call_amd(): void {
|
1 |
efrain |
111 |
|
|
|
112 |
$page = new \moodle_page();
|
|
|
113 |
|
|
|
114 |
// Load an AMD module without a function call.
|
|
|
115 |
$page->requires->js_call_amd('theme_foobar/lightbox');
|
|
|
116 |
|
|
|
117 |
// Load an AMD module and call its function without parameters.
|
|
|
118 |
$page->requires->js_call_amd('theme_foobar/demo_one', 'init');
|
|
|
119 |
|
|
|
120 |
// Load an AMD module and call its function with some parameters.
|
|
|
121 |
$page->requires->js_call_amd('theme_foobar/demo_two', 'init', [
|
|
|
122 |
'foo',
|
|
|
123 |
'keyWillIgnored' => 'baz',
|
|
|
124 |
[42, 'xyz'],
|
|
|
125 |
]);
|
|
|
126 |
|
|
|
127 |
$html = $page->requires->get_end_code();
|
|
|
128 |
|
|
|
129 |
$modname = 'theme_foobar/lightbox';
|
|
|
130 |
$this->assertStringContainsString("M.util.js_pending('{$modname}'); require(['{$modname}'], function(amd) {M.util.js_complete('{$modname}');});", $html);
|
|
|
131 |
|
|
|
132 |
$modname = 'theme_foobar/demo_one';
|
|
|
133 |
$this->assertStringContainsString("M.util.js_pending('{$modname}'); require(['{$modname}'], function(amd) {amd.init(); M.util.js_complete('{$modname}');});", $html);
|
|
|
134 |
|
|
|
135 |
$modname = 'theme_foobar/demo_two';
|
|
|
136 |
$this->assertStringContainsString("M.util.js_pending('{$modname}'); require(['{$modname}'], function(amd) {amd.init(\"foo\", \"baz\", [42,\"xyz\"]); M.util.js_complete('{$modname}');});", $html);
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
/**
|
|
|
140 |
* Test the actual URL through which a JavaScript file is served.
|
|
|
141 |
*
|
|
|
142 |
* @param \moodle_url $moodleurl The <u>moodle_url</u> instance pointing to a web resource.
|
|
|
143 |
* @param int $cfgslashargs The value to force $CFG->slasharguments.
|
|
|
144 |
* @param string $expected The expected output URL.
|
|
|
145 |
* @throws ReflectionException if the class does not exist.
|
|
|
146 |
* @see \page_requirements_manager::js_fix_url()
|
|
|
147 |
* @see \moodle_url
|
|
|
148 |
* @covers \page_requirements_manager::js_fix_url
|
|
|
149 |
* @dataProvider js_fix_url_moodle_url_provider
|
|
|
150 |
*/
|
11 |
efrain |
151 |
public function test_js_fix_url_moodle_url(\moodle_url $moodleurl, int $cfgslashargs, string $expected): void {
|
1 |
efrain |
152 |
global $CFG;
|
|
|
153 |
$defaultslashargs = $CFG->slasharguments;
|
|
|
154 |
|
|
|
155 |
$CFG->slasharguments = $cfgslashargs;
|
|
|
156 |
$rc = new \ReflectionClass(\page_requirements_manager::class);
|
|
|
157 |
$rcm = $rc->getMethod('js_fix_url');
|
|
|
158 |
$requires = new \page_requirements_manager();
|
|
|
159 |
$actualmoodleurl = $rcm->invokeArgs($requires, [$moodleurl]);
|
|
|
160 |
$this->assertEquals($expected, $actualmoodleurl->out(false));
|
|
|
161 |
|
|
|
162 |
$CFG->slasharguments = $defaultslashargs;
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
/**
|
|
|
166 |
* Data provider for JavaScript proper Handler using a <u>\moodle_url</url>.
|
|
|
167 |
*
|
|
|
168 |
* @return array
|
|
|
169 |
* @see \page_requirements_manager::js_fix_url()
|
|
|
170 |
* @see \moodle_url
|
|
|
171 |
*/
|
|
|
172 |
public function js_fix_url_moodle_url_provider() {
|
|
|
173 |
global $CFG;
|
|
|
174 |
$wwwroot = rtrim($CFG->wwwroot, '/');
|
|
|
175 |
$libdir = rtrim($CFG->libdir, '/');
|
|
|
176 |
$admin = "/{$CFG->admin}/"; // Deprecated, just for coverage purposes.
|
|
|
177 |
|
|
|
178 |
// Note: $CFG->slasharguments is enabled by default; it will be a forced setting one day (MDL-62640).
|
|
|
179 |
return [
|
|
|
180 |
'Environment XML file' => [
|
|
|
181 |
new \moodle_url('/admin/environment.xml'),
|
|
|
182 |
0,
|
|
|
183 |
$wwwroot . $admin . 'environment.xml'
|
|
|
184 |
],
|
|
|
185 |
'Google Maps CDN (HTTPS)' => [
|
|
|
186 |
new \moodle_url('https://maps.googleapis.com/maps/api/js', ['key' => 'googlemapkey3', 'sensor' => 'false']),
|
|
|
187 |
1,
|
|
|
188 |
'https://maps.googleapis.com/maps/api/js?key=googlemapkey3&sensor=false'
|
|
|
189 |
],
|
|
|
190 |
'Google Maps CDN (HTTP)' => [
|
|
|
191 |
new \moodle_url('http://maps.googleapis.com/maps/api/js', ['key' => 'googlemapkey3', 'sensor' => 'false']),
|
|
|
192 |
0,
|
|
|
193 |
'http://maps.googleapis.com/maps/api/js?key=googlemapkey3&sensor=false'
|
|
|
194 |
],
|
|
|
195 |
'H5P JS internal resource (slasharguments on)' => [
|
|
|
196 |
new \moodle_url('/h5p/js/embed.js'),
|
|
|
197 |
1,
|
|
|
198 |
$wwwroot . '/lib/javascript.php/1/h5p/js/embed.js'
|
|
|
199 |
],
|
|
|
200 |
'H5P JS internal resource (slasharguments off)' => [
|
|
|
201 |
new \moodle_url('/h5p/js/embed.js'),
|
|
|
202 |
0,
|
|
|
203 |
$wwwroot . '/lib/javascript.php?rev=1&jsfile=%2Fh5p%2Fjs%2Fembed.js'
|
|
|
204 |
],
|
|
|
205 |
'A custom Moodle CSS Handler' => [
|
|
|
206 |
new \moodle_url('/mod/data/css.php?d=1234567890'),
|
|
|
207 |
1,
|
|
|
208 |
$wwwroot . '/mod/data/css.php?d=1234567890'
|
|
|
209 |
],
|
|
|
210 |
'A custom Moodle JS Handler (slasharguments on)' => [
|
|
|
211 |
new \moodle_url('/mod/data/js.php?d=1234567890'),
|
|
|
212 |
1,
|
|
|
213 |
$wwwroot . '/mod/data/js.php?d=1234567890'
|
|
|
214 |
],
|
|
|
215 |
'A custom Moodle JS Handler (slasharguments off)' => [
|
|
|
216 |
new \moodle_url('/mod/data/js.php?d=1234567890'),
|
|
|
217 |
0,
|
|
|
218 |
$wwwroot . '/mod/data/js.php?d=1234567890'
|
|
|
219 |
],
|
|
|
220 |
];
|
|
|
221 |
}
|
|
|
222 |
|
|
|
223 |
/**
|
|
|
224 |
* Test the actual url through which a JavaScript file is served.
|
|
|
225 |
*
|
|
|
226 |
* @param string $url The URL pointing to a web resource.
|
|
|
227 |
* @param int $cfgslashargs The value to force $CFG->slasharguments.
|
|
|
228 |
* @param string $expected The expected output URL.
|
|
|
229 |
* @throws ReflectionException if the class does not exist.
|
|
|
230 |
* @see \page_requirements_manager::js_fix_url()
|
|
|
231 |
* @covers \page_requirements_manager::js_fix_url
|
|
|
232 |
* @dataProvider js_fix_url_plain_string_provider
|
|
|
233 |
*/
|
11 |
efrain |
234 |
public function test_js_fix_url_plain_string(string $url, int $cfgslashargs, string $expected): void {
|
1 |
efrain |
235 |
global $CFG;
|
|
|
236 |
$defaultslashargs = $CFG->slasharguments;
|
|
|
237 |
|
|
|
238 |
$CFG->slasharguments = $cfgslashargs;
|
|
|
239 |
$rc = new \ReflectionClass(\page_requirements_manager::class);
|
|
|
240 |
$rcm = $rc->getMethod('js_fix_url');
|
|
|
241 |
$requires = new \page_requirements_manager();
|
|
|
242 |
$actualmoodleurl = $rcm->invokeArgs($requires, [$url]);
|
|
|
243 |
$this->assertEquals($expected, $actualmoodleurl->out(false));
|
|
|
244 |
|
|
|
245 |
$CFG->slasharguments = $defaultslashargs;
|
|
|
246 |
}
|
|
|
247 |
|
|
|
248 |
/**
|
|
|
249 |
* Data provider for JavaScript proper Handler using a plain relative string.
|
|
|
250 |
*
|
|
|
251 |
* @return array
|
|
|
252 |
* @see \page_requirements_manager::js_fix_url()
|
|
|
253 |
*/
|
|
|
254 |
public function js_fix_url_plain_string_provider() {
|
|
|
255 |
global $CFG;
|
|
|
256 |
$wwwroot = rtrim($CFG->wwwroot, '/');
|
|
|
257 |
$admin = "/{$CFG->admin}/"; // Deprecated, just for coverage purposes.
|
|
|
258 |
|
|
|
259 |
// Note: $CFG->slasharguments is enabled by default; it will be a forced setting one day (MDL-62640).
|
|
|
260 |
return [
|
|
|
261 |
'Environment XML file' => [
|
|
|
262 |
'/admin/environment.xml',
|
|
|
263 |
0,
|
|
|
264 |
$wwwroot . $admin . 'environment.xml'
|
|
|
265 |
],
|
|
|
266 |
'Data JS' => [
|
|
|
267 |
'/mod/data/data.js',
|
|
|
268 |
1,
|
|
|
269 |
$wwwroot . '/lib/javascript.php/1/mod/data/data.js'
|
|
|
270 |
],
|
|
|
271 |
'SCORM Request JS' => [
|
|
|
272 |
'/mod/scorm/request.js',
|
|
|
273 |
1,
|
|
|
274 |
$wwwroot . '/lib/javascript.php/1/mod/scorm/request.js'
|
|
|
275 |
],
|
|
|
276 |
'Wiki Editors Buttons JS' => [
|
|
|
277 |
'/mod/wiki/editors/wiki/buttons.js',
|
|
|
278 |
1,
|
|
|
279 |
$wwwroot . '/lib/javascript.php/1/mod/wiki/editors/wiki/buttons.js'
|
|
|
280 |
],
|
|
|
281 |
'A non-JS internal resource' => [
|
|
|
282 |
'/theme/boost/pix/favicon.ico',
|
|
|
283 |
0,
|
|
|
284 |
$wwwroot . '/theme/boost/pix/favicon.ico'
|
|
|
285 |
],
|
|
|
286 |
'A custom Moodle CSS Handler' => [
|
|
|
287 |
'/mod/data/css.php?d=1234567890',
|
|
|
288 |
1,
|
|
|
289 |
$wwwroot . '/mod/data/css.php?d=1234567890'
|
|
|
290 |
],
|
|
|
291 |
'A custom Moodle JS Handler (slasharguments on)' => [
|
|
|
292 |
'/mod/data/js.php?d=1234567890',
|
|
|
293 |
1,
|
|
|
294 |
$wwwroot . '/mod/data/js.php?d=1234567890'
|
|
|
295 |
],
|
|
|
296 |
'A custom Moodle JS Handler (slasharguments off)' => [
|
|
|
297 |
'/mod/data/js.php?d=1234567890',
|
|
|
298 |
0,
|
|
|
299 |
$wwwroot . '/mod/data/js.php?d=1234567890'
|
|
|
300 |
],
|
|
|
301 |
];
|
|
|
302 |
}
|
|
|
303 |
|
|
|
304 |
/**
|
|
|
305 |
* Test the coding exceptions when trying to get the actual URL through which a JavaScript file is served.
|
|
|
306 |
*
|
|
|
307 |
* @param moodle_url|string|null $url The URL pointing to a web resource.
|
|
|
308 |
* @param string $exmessage The expected output URL.
|
|
|
309 |
* @throws ReflectionException if the class does not exist.
|
|
|
310 |
* @see \page_requirements_manager::js_fix_url()
|
|
|
311 |
* @covers \page_requirements_manager::js_fix_url
|
|
|
312 |
* @dataProvider js_fix_url_coding_exception_provider
|
|
|
313 |
*/
|
11 |
efrain |
314 |
public function test_js_fix_url_coding_exception($url, string $exmessage): void {
|
1 |
efrain |
315 |
$rc = new \ReflectionClass(\page_requirements_manager::class);
|
|
|
316 |
$rcm = $rc->getMethod('js_fix_url');
|
|
|
317 |
$requires = new \page_requirements_manager();
|
|
|
318 |
$this->expectException(\coding_exception::class);
|
|
|
319 |
$this->expectExceptionMessage($exmessage);
|
|
|
320 |
$actualmoodleurl = $rcm->invokeArgs($requires, [$url]);
|
|
|
321 |
}
|
|
|
322 |
|
|
|
323 |
/**
|
|
|
324 |
* Data provider for throwing coding exceptions in <u>\page_requirements_manager::js_fix_url()</u>.
|
|
|
325 |
*
|
|
|
326 |
* @return array
|
|
|
327 |
* @see \page_requirements_manager::js_fix_url()
|
|
|
328 |
*/
|
|
|
329 |
public function js_fix_url_coding_exception_provider() {
|
|
|
330 |
global $CFG;
|
|
|
331 |
$wwwroot = rtrim($CFG->wwwroot, '/');
|
|
|
332 |
|
|
|
333 |
return [
|
|
|
334 |
'Provide a null argument' => [
|
|
|
335 |
null,
|
|
|
336 |
'Coding error detected, it must be fixed by a programmer: '
|
|
|
337 |
. 'Invalid JS url, it has to be shortened url starting with / or moodle_url instance.'
|
|
|
338 |
],
|
|
|
339 |
'Provide an internal absolute URL' => [
|
|
|
340 |
$wwwroot . '/lib/javascript.php/1/h5p/js/embed.js',
|
|
|
341 |
'Coding error detected, it must be fixed by a programmer: '
|
|
|
342 |
. 'Invalid JS url, it has to be shortened url starting with / or moodle_url instance. '
|
|
|
343 |
. '(' . $wwwroot . '/lib/javascript.php/1/h5p/js/embed.js)'
|
|
|
344 |
],
|
|
|
345 |
'Provide an external absolute URL' => [
|
|
|
346 |
'https://maps.googleapis.com/maps/api/js?key=googlemapkey3&sensor=false',
|
|
|
347 |
'Coding error detected, it must be fixed by a programmer: '
|
|
|
348 |
. 'Invalid JS url, it has to be shortened url starting with / or moodle_url instance. '
|
|
|
349 |
. '(https://maps.googleapis.com/maps/api/js?key=googlemapkey3&sensor=false)'
|
|
|
350 |
],
|
|
|
351 |
'A non-JS internal resource using an absolute URL' => [
|
|
|
352 |
$wwwroot . '/theme/boost/pix/favicon.ico',
|
|
|
353 |
'Coding error detected, it must be fixed by a programmer: '
|
|
|
354 |
. 'Invalid JS url, it has to be shortened url starting with / or moodle_url instance. ('
|
|
|
355 |
. $wwwroot . '/theme/boost/pix/favicon.ico)'
|
|
|
356 |
],
|
|
|
357 |
'A non-existant internal resource using an absolute URL' => [
|
|
|
358 |
$wwwroot . '/path/to/file.ext',
|
|
|
359 |
'Coding error detected, it must be fixed by a programmer: '
|
|
|
360 |
. 'Invalid JS url, it has to be shortened url starting with / or moodle_url instance. ('
|
|
|
361 |
. $wwwroot . '/path/to/file.ext)'
|
|
|
362 |
],
|
|
|
363 |
'A non-existant internal resource. WARN the developer!' => [
|
|
|
364 |
'/path/to/file1.ext',
|
|
|
365 |
'Coding error detected, it must be fixed by a programmer: '
|
|
|
366 |
. 'Attempt to require a JavaScript file that does not exist. (/path/to/file1.ext)'
|
|
|
367 |
],
|
|
|
368 |
'A non-existant internal resource using moodle_url. WARN the developer!' => [
|
|
|
369 |
new \moodle_url('/path/to/file2.ext'),
|
|
|
370 |
'Coding error detected, it must be fixed by a programmer: '
|
|
|
371 |
. 'Attempt to require a JavaScript file that does not exist. (/path/to/file2.ext)'
|
|
|
372 |
],
|
|
|
373 |
];
|
|
|
374 |
}
|
|
|
375 |
}
|