Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

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