MDL-67271 core: Add test to find missing SVG icons
[moodle.git] / lib / tests / weblib_test.php
blobf34b81e5066ff3350861f4ce5c647a23bdd1d4c9
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 * Weblib tests.
20 * @package core
21 * @category phpunit
22 * @copyright &copy; 2006 The Open University
23 * @author T.J.Hunt@open.ac.uk
24 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
26 class weblib_test extends advanced_testcase {
27 /**
28 * @covers ::format_string
30 public function test_format_string() {
31 global $CFG;
33 // Ampersands.
34 $this->assertSame("&amp; &amp;&amp;&amp;&amp;&amp; &amp;&amp;", format_string("& &&&&& &&"));
35 $this->assertSame("ANother &amp; &amp;&amp;&amp;&amp;&amp; Category", format_string("ANother & &&&&& Category"));
36 $this->assertSame("ANother &amp; &amp;&amp;&amp;&amp;&amp; Category", format_string("ANother & &&&&& Category", true));
37 $this->assertSame("Nick's Test Site &amp; Other things", format_string("Nick's Test Site & Other things", true));
38 $this->assertSame("& < > \" '", format_string("& < > \" '", true, ['escape' => false]));
40 // String entities.
41 $this->assertSame("&quot;", format_string("&quot;"));
43 // Digital entities.
44 $this->assertSame("&11234;", format_string("&11234;"));
46 // Unicode entities.
47 $this->assertSame("&#4475;", format_string("&#4475;"));
49 // Nulls.
50 $this->assertSame('', format_string(null));
51 $this->assertSame('', format_string(null, true, ['escape' => false]));
53 // < and > signs.
54 $originalformatstringstriptags = $CFG->formatstringstriptags;
56 $CFG->formatstringstriptags = false;
57 $this->assertSame('x &lt; 1', format_string('x < 1'));
58 $this->assertSame('x &gt; 1', format_string('x > 1'));
59 $this->assertSame('x &lt; 1 and x &gt; 0', format_string('x < 1 and x > 0'));
61 $CFG->formatstringstriptags = true;
62 $this->assertSame('x &lt; 1', format_string('x < 1'));
63 $this->assertSame('x &gt; 1', format_string('x > 1'));
64 $this->assertSame('x &lt; 1 and x &gt; 0', format_string('x < 1 and x > 0'));
66 $CFG->formatstringstriptags = $originalformatstringstriptags;
69 /**
70 * The format string static caching should include the filters option to make
71 * sure filters are correctly applied when requested.
73 public function test_format_string_static_caching_with_filters() {
74 global $CFG;
76 $this->resetAfterTest(true);
77 $this->setAdminUser();
78 $generator = $this->getDataGenerator();
79 $course = $generator->create_course();
80 $user = $generator->create_user();
81 $rawstring = '<span lang="en" class="multilang">English</span><span lang="ca" class="multilang">Catalan</span>';
82 $expectednofilter = strip_tags($rawstring);
83 $expectedfilter = 'English';
84 $striplinks = true;
85 $context = context_course::instance($course->id);
86 $options = [
87 'context' => $context,
88 'escape' => true,
89 'filter' => false
92 $this->setUser($user);
94 // Format the string without filters. It should just strip the
95 // links.
96 $nofilterresult = format_string($rawstring, $striplinks, $options);
97 $this->assertEquals($expectednofilter, $nofilterresult);
99 // Add the multilang filter. Make sure it's enabled globally.
100 $CFG->filterall = true;
101 $CFG->stringfilters = 'multilang';
102 filter_set_global_state('multilang', TEXTFILTER_ON);
103 filter_set_local_state('multilang', $context->id, TEXTFILTER_ON);
104 // This time we want to apply the filters.
105 $options['filter'] = true;
106 $filterresult = format_string($rawstring, $striplinks, $options);
107 $this->assertMatchesRegularExpression("/$expectedfilter/", $filterresult);
109 filter_set_local_state('multilang', $context->id, TEXTFILTER_OFF);
111 // Confirm that we get back the cached string. The result should be
112 // the same as the filtered text above even though we've disabled the
113 // multilang filter in between.
114 $cachedresult = format_string($rawstring, $striplinks, $options);
115 $this->assertMatchesRegularExpression("/$expectedfilter/", $cachedresult);
119 * @covers ::s
121 public function test_s() {
122 // Special cases.
123 $this->assertSame('0', s(0));
124 $this->assertSame('0', s('0'));
125 $this->assertSame('0', s(false));
126 $this->assertSame('', s(null));
128 // Normal cases.
129 $this->assertSame('This Breaks &quot; Strict', s('This Breaks " Strict'));
130 $this->assertSame('This Breaks &lt;a&gt;&quot; Strict&lt;/a&gt;', s('This Breaks <a>" Strict</a>'));
132 // Unicode characters.
133 $this->assertSame('Café', s('Café'));
134 $this->assertSame('一, 二, 三', s('一, 二, 三'));
136 // Don't escape already-escaped numeric entities. (Note, this behaviour
137 // may not be desirable. Perhaps we should remove these tests and that
138 // functionality, but we can only do that if we understand why it was added.)
139 $this->assertSame('An entity: &#x09ff;.', s('An entity: &#x09ff;.'));
140 $this->assertSame('An entity: &#1073;.', s('An entity: &#1073;.'));
141 $this->assertSame('An entity: &amp;amp;.', s('An entity: &amp;.'));
142 $this->assertSame('Not an entity: &amp;amp;#x09ff;.', s('Not an entity: &amp;#x09ff;.'));
144 // Test all ASCII characters (0-127).
145 for ($i = 0; $i <= 127; $i++) {
146 $character = chr($i);
147 $result = s($character);
148 switch ($character) {
149 case '"' :
150 $this->assertSame('&quot;', $result);
151 break;
152 case '&' :
153 $this->assertSame('&amp;', $result);
154 break;
155 case "'" :
156 $this->assertSame('&#039;', $result);
157 break;
158 case '<' :
159 $this->assertSame('&lt;', $result);
160 break;
161 case '>' :
162 $this->assertSame('&gt;', $result);
163 break;
164 default:
165 $this->assertSame($character, $result);
166 break;
172 * @covers ::format_text_email
174 public function test_format_text_email() {
175 $this->assertSame("This is a TEST\n",
176 format_text_email('<p>This is a <strong>test</strong></p>', FORMAT_HTML));
177 $this->assertSame("This is a TEST\n",
178 format_text_email('<p class="frogs">This is a <strong class=\'fishes\'>test</strong></p>', FORMAT_HTML));
179 $this->assertSame('& so is this',
180 format_text_email('&amp; so is this', FORMAT_HTML));
181 $this->assertSame('Two bullets: ' . core_text::code2utf8(8226) . ' ' . core_text::code2utf8(8226),
182 format_text_email('Two bullets: &#x2022; &#8226;', FORMAT_HTML));
183 $this->assertSame(core_text::code2utf8(0x7fd2).core_text::code2utf8(0x7fd2),
184 format_text_email('&#x7fd2;&#x7FD2;', FORMAT_HTML));
188 * @covers ::obfuscate_email
190 public function test_obfuscate_email() {
191 $email = 'some.user@example.com';
192 $obfuscated = obfuscate_email($email);
193 $this->assertNotSame($email, $obfuscated);
194 $back = core_text::entities_to_utf8(urldecode($email), true);
195 $this->assertSame($email, $back);
199 * @covers ::obfuscate_text
201 public function test_obfuscate_text() {
202 $text = 'Žluťoučký koníček 32131';
203 $obfuscated = obfuscate_text($text);
204 $this->assertNotSame($text, $obfuscated);
205 $back = core_text::entities_to_utf8($obfuscated, true);
206 $this->assertSame($text, $back);
210 * @covers ::highlight
212 public function test_highlight() {
213 $this->assertSame('This is <span class="highlight">good</span>',
214 highlight('good', 'This is good'));
216 $this->assertSame('<span class="highlight">span</span>',
217 highlight('SpaN', 'span'));
219 $this->assertSame('<span class="highlight">SpaN</span>',
220 highlight('span', 'SpaN'));
222 $this->assertSame('<span><span class="highlight">span</span></span>',
223 highlight('span', '<span>span</span>'));
225 $this->assertSame('He <span class="highlight">is</span> <span class="highlight">good</span>',
226 highlight('good is', 'He is good'));
228 $this->assertSame('This is <span class="highlight">good</span>',
229 highlight('+good', 'This is good'));
231 $this->assertSame('This is good',
232 highlight('-good', 'This is good'));
234 $this->assertSame('This is goodness',
235 highlight('+good', 'This is goodness'));
237 $this->assertSame('This is <span class="highlight">good</span>ness',
238 highlight('good', 'This is goodness'));
240 $this->assertSame('<p><b>test</b> <b>1</b></p><p><b>1</b></p>',
241 highlight('test 1', '<p>test 1</p><p>1</p>', false, '<b>', '</b>'));
243 $this->assertSame('<p><b>test</b> <b>1</b></p><p><b>1</b></p>',
244 highlight('test +1', '<p>test 1</p><p>1</p>', false, '<b>', '</b>'));
246 $this->assertSame('<p><b>test</b> 1</p><p>1</p>',
247 highlight('test -1', '<p>test 1</p><p>1</p>', false, '<b>', '</b>'));
251 * @covers ::replace_ampersands_not_followed_by_entity
253 public function test_replace_ampersands() {
254 $this->assertSame("This &amp; that &nbsp;", replace_ampersands_not_followed_by_entity("This & that &nbsp;"));
255 $this->assertSame("This &amp;nbsp that &nbsp;", replace_ampersands_not_followed_by_entity("This &nbsp that &nbsp;"));
259 * @covers ::strip_links
261 public function test_strip_links() {
262 $this->assertSame('this is a link', strip_links('this is a <a href="http://someaddress.com/query">link</a>'));
266 * @covers ::wikify_links
268 public function test_wikify_links() {
269 $this->assertSame('this is a link [ http://someaddress.com/query ]', wikify_links('this is a <a href="http://someaddress.com/query">link</a>'));
273 * @covers ::clean_text
275 public function test_clean_text() {
276 $text = "lala <applet>xx</applet>";
277 $this->assertSame($text, clean_text($text, FORMAT_PLAIN));
278 $this->assertSame('lala xx', clean_text($text, FORMAT_MARKDOWN));
279 $this->assertSame('lala xx', clean_text($text, FORMAT_MOODLE));
280 $this->assertSame('lala xx', clean_text($text, FORMAT_HTML));
284 * Test trusttext enabling.
286 * @covers ::trusttext_active
288 public function test_trusttext_active() {
289 global $CFG;
290 $this->resetAfterTest();
292 $this->assertFalse(trusttext_active());
293 $CFG->enabletrusttext = '1';
294 $this->assertTrue(trusttext_active());
298 * Test trusttext detection.
300 * @covers ::trusttext_trusted
302 public function test_trusttext_trusted() {
303 global $CFG;
304 $this->resetAfterTest();
306 $syscontext = context_system::instance();
307 $course = $this->getDataGenerator()->create_course();
308 $coursecontext = context_course::instance($course->id);
309 $user1 = $this->getDataGenerator()->create_user();
310 $user2 = $this->getDataGenerator()->create_user();
311 $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'editingteacher');
313 $this->setAdminUser();
315 $CFG->enabletrusttext = '0';
316 $this->assertFalse(trusttext_trusted($syscontext));
317 $this->assertFalse(trusttext_trusted($coursecontext));
319 $CFG->enabletrusttext = '1';
320 $this->assertTrue(trusttext_trusted($syscontext));
321 $this->assertTrue(trusttext_trusted($coursecontext));
323 $this->setUser($user1);
325 $CFG->enabletrusttext = '0';
326 $this->assertFalse(trusttext_trusted($syscontext));
327 $this->assertFalse(trusttext_trusted($coursecontext));
329 $CFG->enabletrusttext = '1';
330 $this->assertFalse(trusttext_trusted($syscontext));
331 $this->assertFalse(trusttext_trusted($coursecontext));
333 $this->setUser($user2);
335 $CFG->enabletrusttext = '0';
336 $this->assertFalse(trusttext_trusted($syscontext));
337 $this->assertFalse(trusttext_trusted($coursecontext));
339 $CFG->enabletrusttext = '1';
340 $this->assertFalse(trusttext_trusted($syscontext));
341 $this->assertTrue(trusttext_trusted($coursecontext));
345 * Data provider for trusttext_pre_edit() tests.
347 public function trusttext_pre_edit_provider(): array {
348 return [
349 [true, 0, 'editingteacher', FORMAT_HTML, 1],
350 [true, 0, 'editingteacher', FORMAT_MOODLE, 1],
351 [false, 0, 'editingteacher', FORMAT_MARKDOWN, 1],
352 [false, 0, 'editingteacher', FORMAT_PLAIN, 1],
354 [false, 1, 'editingteacher', FORMAT_HTML, 1],
355 [false, 1, 'editingteacher', FORMAT_MOODLE, 1],
356 [false, 1, 'editingteacher', FORMAT_MARKDOWN, 1],
357 [false, 1, 'editingteacher', FORMAT_PLAIN, 1],
359 [true, 0, 'student', FORMAT_HTML, 1],
360 [true, 0, 'student', FORMAT_MOODLE, 1],
361 [false, 0, 'student', FORMAT_MARKDOWN, 1],
362 [false, 0, 'student', FORMAT_PLAIN, 1],
364 [true, 1, 'student', FORMAT_HTML, 1],
365 [true, 1, 'student', FORMAT_MOODLE, 1],
366 [false, 1, 'student', FORMAT_MARKDOWN, 1],
367 [false, 1, 'student', FORMAT_PLAIN, 1],
372 * Test text cleaning before editing.
374 * @dataProvider trusttext_pre_edit_provider
375 * @covers ::trusttext_pre_edit
377 * @param bool $expectedsanitised
378 * @param int $enabled
379 * @param string $rolename
380 * @param string $format
381 * @param int $trust
383 public function test_trusttext_pre_edit(bool $expectedsanitised, int $enabled, string $rolename,
384 string $format, int $trust) {
385 global $CFG, $DB;
386 $this->resetAfterTest();
388 $exploit = "abc<script>alert('xss')</script> < > &";
389 $sanitised = purify_html($exploit);
391 $course = $this->getDataGenerator()->create_course();
392 $context = context_course::instance($course->id);
394 $user = $this->getDataGenerator()->create_user();
395 $this->getDataGenerator()->enrol_user($user->id, $course->id, $rolename);
397 $this->setUser($user);
399 $CFG->enabletrusttext = (string)$enabled;
401 $object = new stdClass();
402 $object->some = $exploit;
403 $object->someformat = $format;
404 $object->sometrust = (string)$trust;
405 $result = trusttext_pre_edit(clone($object), 'some', $context);
407 if ($expectedsanitised) {
408 $message = "sanitisation is expected for: $enabled, $rolename, $format, $trust";
409 $this->assertSame($sanitised, $result->some, $message);
410 } else {
411 $message = "sanitisation is not expected for: $enabled, $rolename, $format, $trust";
412 $this->assertSame($exploit, $result->some, $message);
417 * Test removal of legacy trusttext flag.
418 * @covers ::trusttext_strip
420 public function test_trusttext_strip() {
421 $this->assertSame('abc', trusttext_strip('abc'));
422 $this->assertSame('abc', trusttext_strip('ab#####TRUSTTEXT#####c'));
426 * Test trust option of format_text().
427 * @covers ::format_text
429 public function test_format_text_trusted() {
430 global $CFG;
431 $this->resetAfterTest();
433 $text = "lala <object>xx</object>";
435 $CFG->enabletrusttext = 0;
437 $this->assertSame(s($text),
438 format_text($text, FORMAT_PLAIN, ['trusted' => true]));
439 $this->assertSame("<p>lala xx</p>\n",
440 format_text($text, FORMAT_MARKDOWN, ['trusted' => true]));
441 $this->assertSame('<div class="text_to_html">lala xx</div>',
442 format_text($text, FORMAT_MOODLE, ['trusted' => true]));
443 $this->assertSame('lala xx',
444 format_text($text, FORMAT_HTML, ['trusted' => true]));
446 $this->assertSame(s($text),
447 format_text($text, FORMAT_PLAIN, ['trusted' => false]));
448 $this->assertSame("<p>lala xx</p>\n",
449 format_text($text, FORMAT_MARKDOWN, ['trusted' => false]));
450 $this->assertSame('<div class="text_to_html">lala xx</div>',
451 format_text($text, FORMAT_MOODLE, ['trusted' => false]));
452 $this->assertSame('lala xx',
453 format_text($text, FORMAT_HTML, ['trusted' => false]));
455 $CFG->enabletrusttext = 1;
457 $this->assertSame(s($text),
458 format_text($text, FORMAT_PLAIN, ['trusted' => true]));
459 $this->assertSame("<p>lala xx</p>\n",
460 format_text($text, FORMAT_MARKDOWN, ['trusted' => true]));
461 $this->assertSame('<div class="text_to_html">lala <object>xx</object></div>',
462 format_text($text, FORMAT_MOODLE, ['trusted' => true]));
463 $this->assertSame('lala <object>xx</object>',
464 format_text($text, FORMAT_HTML, ['trusted' => true]));
466 $this->assertSame(s($text),
467 format_text($text, FORMAT_PLAIN, ['trusted' => false]));
468 $this->assertSame("<p>lala xx</p>\n",
469 format_text($text, FORMAT_MARKDOWN, ['trusted' => false]));
470 $this->assertSame('<div class="text_to_html">lala xx</div>',
471 format_text($text, FORMAT_MOODLE, ['trusted' => false]));
472 $this->assertSame('lala xx',
473 format_text($text, FORMAT_HTML, ['trusted' => false]));
475 $this->assertSame("<p>lala <object>xx</object></p>\n",
476 format_text($text, FORMAT_MARKDOWN, ['trusted' => true, 'noclean' => true]));
477 $this->assertSame("<p>lala <object>xx</object></p>\n",
478 format_text($text, FORMAT_MARKDOWN, ['trusted' => false, 'noclean' => true]));
482 * @covers ::qualified_me
484 public function test_qualified_me() {
485 global $PAGE, $FULLME, $CFG;
486 $this->resetAfterTest();
488 $PAGE = new moodle_page();
490 $FULLME = $CFG->wwwroot.'/course/view.php?id=1&xx=yy';
491 $this->assertSame($FULLME, qualified_me());
493 $PAGE->set_url('/course/view.php', array('id'=>1));
494 $this->assertSame($CFG->wwwroot.'/course/view.php?id=1', qualified_me());
498 * @covers \null_progress_trace
500 public function test_null_progress_trace() {
501 $this->resetAfterTest(false);
503 $trace = new null_progress_trace();
504 $trace->output('do');
505 $trace->output('re', 1);
506 $trace->output('mi', 2);
507 $trace->finished();
508 $output = ob_get_contents();
509 $this->assertSame('', $output);
510 $this->expectOutputString('');
514 * @covers \null_progress_trace
516 public function test_text_progress_trace() {
517 $this->resetAfterTest(false);
519 $trace = new text_progress_trace();
520 $trace->output('do');
521 $trace->output('re', 1);
522 $trace->output('mi', 2);
523 $trace->finished();
524 $this->expectOutputString("do\n re\n mi\n");
528 * @covers \html_progress_trace
530 public function test_html_progress_trace() {
531 $this->resetAfterTest(false);
533 $trace = new html_progress_trace();
534 $trace->output('do');
535 $trace->output('re', 1);
536 $trace->output('mi', 2);
537 $trace->finished();
538 $this->expectOutputString("<p>do</p>\n<p>&#160;&#160;re</p>\n<p>&#160;&#160;&#160;&#160;mi</p>\n");
542 * @covers \html_list_progress_trace
544 public function test_html_list_progress_trace() {
545 $this->resetAfterTest(false);
547 $trace = new html_list_progress_trace();
548 $trace->output('do');
549 $trace->output('re', 1);
550 $trace->output('mi', 2);
551 $trace->finished();
552 $this->expectOutputString("<ul>\n<li>do<ul>\n<li>re<ul>\n<li>mi</li>\n</ul>\n</li>\n</ul>\n</li>\n</ul>\n");
556 * @covers \progress_trace_buffer
558 public function test_progress_trace_buffer() {
559 $this->resetAfterTest(false);
561 $trace = new progress_trace_buffer(new html_progress_trace());
562 ob_start();
563 $trace->output('do');
564 $trace->output('re', 1);
565 $trace->output('mi', 2);
566 $trace->finished();
567 $output = ob_get_contents();
568 ob_end_clean();
569 $this->assertSame("<p>do</p>\n<p>&#160;&#160;re</p>\n<p>&#160;&#160;&#160;&#160;mi</p>\n", $output);
570 $this->assertSame($output, $trace->get_buffer());
572 $trace = new progress_trace_buffer(new html_progress_trace(), false);
573 $trace->output('do');
574 $trace->output('re', 1);
575 $trace->output('mi', 2);
576 $trace->finished();
577 $this->assertSame("<p>do</p>\n<p>&#160;&#160;re</p>\n<p>&#160;&#160;&#160;&#160;mi</p>\n", $trace->get_buffer());
578 $this->assertSame("<p>do</p>\n<p>&#160;&#160;re</p>\n<p>&#160;&#160;&#160;&#160;mi</p>\n", $trace->get_buffer());
579 $trace->reset_buffer();
580 $this->assertSame('', $trace->get_buffer());
581 $this->expectOutputString('');
585 * @covers \combined_progress_trace
587 public function test_combined_progress_trace() {
588 $this->resetAfterTest(false);
590 $trace1 = new progress_trace_buffer(new html_progress_trace(), false);
591 $trace2 = new progress_trace_buffer(new text_progress_trace(), false);
593 $trace = new combined_progress_trace(array($trace1, $trace2));
594 $trace->output('do');
595 $trace->output('re', 1);
596 $trace->output('mi', 2);
597 $trace->finished();
598 $this->assertSame("<p>do</p>\n<p>&#160;&#160;re</p>\n<p>&#160;&#160;&#160;&#160;mi</p>\n", $trace1->get_buffer());
599 $this->assertSame("do\n re\n mi\n", $trace2->get_buffer());
600 $this->expectOutputString('');
604 * @covers ::set_debugging
606 public function test_set_debugging() {
607 global $CFG;
609 $this->resetAfterTest();
611 $this->assertEquals(DEBUG_DEVELOPER, $CFG->debug);
612 $this->assertTrue($CFG->debugdeveloper);
613 $this->assertNotEmpty($CFG->debugdisplay);
615 set_debugging(DEBUG_DEVELOPER, true);
616 $this->assertEquals(DEBUG_DEVELOPER, $CFG->debug);
617 $this->assertTrue($CFG->debugdeveloper);
618 $this->assertNotEmpty($CFG->debugdisplay);
620 set_debugging(DEBUG_DEVELOPER, false);
621 $this->assertEquals(DEBUG_DEVELOPER, $CFG->debug);
622 $this->assertTrue($CFG->debugdeveloper);
623 $this->assertEmpty($CFG->debugdisplay);
625 set_debugging(-1);
626 $this->assertEquals(-1, $CFG->debug);
627 $this->assertTrue($CFG->debugdeveloper);
629 set_debugging(DEBUG_ALL);
630 $this->assertEquals(DEBUG_ALL, $CFG->debug);
631 $this->assertFalse($CFG->debugdeveloper);
633 set_debugging(DEBUG_NORMAL);
634 $this->assertEquals(DEBUG_NORMAL, $CFG->debug);
635 $this->assertFalse($CFG->debugdeveloper);
637 set_debugging(DEBUG_MINIMAL);
638 $this->assertEquals(DEBUG_MINIMAL, $CFG->debug);
639 $this->assertFalse($CFG->debugdeveloper);
641 set_debugging(DEBUG_NONE);
642 $this->assertEquals(DEBUG_NONE, $CFG->debug);
643 $this->assertFalse($CFG->debugdeveloper);
647 * @covers ::strip_pluginfile_content
649 public function test_strip_pluginfile_content() {
650 $source = <<<SOURCE
651 Hello!
653 I'm writing to you from the Moodle Majlis in Muscat, Oman, where we just had several days of Moodle community goodness.
655 URL outside a tag: https://moodle.org/logo/logo-240x60.gif
656 Plugin url outside a tag: @@PLUGINFILE@@/logo-240x60.gif
658 External link 1:<img src='https://moodle.org/logo/logo-240x60.gif' alt='Moodle'/>
659 External link 2:<img alt="Moodle" src="https://moodle.org/logo/logo-240x60.gif"/>
660 Internal link 1:<img src='@@PLUGINFILE@@/logo-240x60.gif' alt='Moodle'/>
661 Internal link 2:<img alt="Moodle" src="@@PLUGINFILE@@logo-240x60.gif"/>
662 Anchor link 1:<a href="@@PLUGINFILE@@logo-240x60.gif" alt="bananas">Link text</a>
663 Anchor link 2:<a title="bananas" href="../logo-240x60.gif">Link text</a>
664 Anchor + ext. img:<a title="bananas" href="../logo-240x60.gif"><img alt="Moodle" src="@@PLUGINFILE@@logo-240x60.gif"/></a>
665 Ext. anchor + img:<a href="@@PLUGINFILE@@logo-240x60.gif"><img alt="Moodle" src="https://moodle.org/logo/logo-240x60.gif"/></a>
666 SOURCE;
667 $expected = <<<EXPECTED
668 Hello!
670 I'm writing to you from the Moodle Majlis in Muscat, Oman, where we just had several days of Moodle community goodness.
672 URL outside a tag: https://moodle.org/logo/logo-240x60.gif
673 Plugin url outside a tag: @@PLUGINFILE@@/logo-240x60.gif
675 External link 1:<img src="https://moodle.org/logo/logo-240x60.gif" alt="Moodle" />
676 External link 2:<img alt="Moodle" src="https://moodle.org/logo/logo-240x60.gif" />
677 Internal link 1:
678 Internal link 2:
679 Anchor link 1:Link text
680 Anchor link 2:<a title="bananas" href="../logo-240x60.gif">Link text</a>
681 Anchor + ext. img:<a title="bananas" href="../logo-240x60.gif"></a>
682 Ext. anchor + img:<img alt="Moodle" src="https://moodle.org/logo/logo-240x60.gif" />
683 EXPECTED;
684 $this->assertSame($expected, strip_pluginfile_content($source));
688 * @covers \purify_html
690 public function test_purify_html_ruby() {
692 $this->resetAfterTest();
694 $ruby =
695 "<p><ruby><rb>京都</rb><rp>(</rp><rt>きょうと</rt><rp>)</rp></ruby>は" .
696 "<ruby><rb>日本</rb><rp>(</rp><rt>にほん</rt><rp>)</rp></ruby>の" .
697 "<ruby><rb>都</rb><rp>(</rp><rt>みやこ</rt><rp>)</rp></ruby>です。</p>";
698 $illegal = '<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>';
700 $cleaned = purify_html($ruby . $illegal);
701 $this->assertEquals($ruby, $cleaned);
706 * Tests for content_to_text.
708 * @param string $content The content
709 * @param int|false $format The content format
710 * @param string $expected Expected value
711 * @dataProvider provider_content_to_text
712 * @covers ::content_to_text
714 public function test_content_to_text($content, $format, $expected) {
715 $content = content_to_text($content, $format);
716 $this->assertEquals($expected, $content);
720 * Data provider for test_content_to_text.
722 public static function provider_content_to_text() {
723 return array(
724 array('asd', false, 'asd'),
725 // Trim '\r\n '.
726 array("Note that:\n\n3 > 1 ", FORMAT_PLAIN, "Note that:\n\n3 > 1"),
727 array("Note that:\n\n3 > 1\r\n", FORMAT_PLAIN, "Note that:\n\n3 > 1"),
728 // Multiple spaces to one.
729 array('<span class="eheh">京都</span> -> hehe', FORMAT_HTML, '京都 -> hehe'),
730 array('<span class="eheh">京都</span> -> hehe', false, '京都 -> hehe'),
731 array('asd asd', false, 'asd asd'),
732 // From markdown to html and html to text.
733 array('asd __lera__ con la', FORMAT_MARKDOWN, 'asd LERA con la'),
734 // HTML to text.
735 array('<p class="frogs">This is a <strong class=\'fishes\'>test</strong></p>', FORMAT_HTML, 'This is a TEST'),
736 array("<span lang='en' class='multilang'>english</span>
737 <span lang='ca' class='multilang'>català</span>
738 <span lang='es' class='multilang'>español</span>
739 <span lang='fr' class='multilang'>français</span>", FORMAT_HTML, "english català español français")
744 * Data provider for validate_email() function.
746 * @return array Returns aray of test data for the test_validate_email function
748 public function data_validate_email() {
749 return [
750 // Test addresses that should pass.
752 'email' => 'moodle@example.com',
753 'result' => true
756 'email' => 'moodle@localhost.local',
757 'result' => true
760 'email' => 'verp_email+is=mighty@moodle.org',
761 'result' => true
764 'email' => "but_potentially'dangerous'too@example.org",
765 'result' => true
768 'email' => 'posts+AAAAAAAAAAIAAAAAAAAGQQAAAAABFSXz1eM/P/lR2bYyljM+@posts.moodle.org',
769 'result' => true
772 // Test addresses that should NOT pass.
774 'email' => 'moodle@localhost',
775 'result' => false
778 'email' => '"attacker\\" -oQ/tmp/ -X/var/www/vhost/moodle/backdoor.php some"@email.com',
779 'result' => false
782 'email' => "moodle@example.com>\r\nRCPT TO:<victim@example.com",
783 'result' => false
786 'email' => 'greater>than@example.com',
787 'result' => false
790 'email' => 'less<than@example.com',
791 'result' => false
794 'email' => '"this<is>validbutwerejectit"@example.com',
795 'result' => false
798 // Empty e-mail addresess are not valid.
800 'email' => '',
801 'result' => false,
804 'email' => null,
805 'result' => false,
808 'email' => false,
809 'result' => false,
812 // Extra email addresses from Wikipedia page on Email Addresses.
813 // Valid.
815 'email' => 'simple@example.com',
816 'result' => true
819 'email' => 'very.common@example.com',
820 'result' => true
823 'email' => 'disposable.style.email.with+symbol@example.com',
824 'result' => true
827 'email' => 'other.email-with-hyphen@example.com',
828 'result' => true
831 'email' => 'fully-qualified-domain@example.com',
832 'result' => true
835 'email' => 'user.name+tag+sorting@example.com',
836 'result' => true
838 // One-letter local-part.
840 'email' => 'x@example.com',
841 'result' => true
844 'email' => 'example-indeed@strange-example.com',
845 'result' => true
847 // See the List of Internet top-level domains.
849 'email' => 'example@s.example',
850 'result' => true
852 // Quoted double dot.
854 'email' => '"john..doe"@example.org',
855 'result' => true
858 // Invalid.
859 // No @ character.
861 'email' => 'Abc.example.com',
862 'result' => false
864 // Only one @ is allowed outside quotation marks.
866 'email' => 'A@b@c@example.com',
867 'result' => false
869 // None of the special characters in this local-part are allowed outside quotation marks.
871 'email' => 'a"b(c)d,e:f;g<h>i[j\k]l@example.com',
872 'result' => false
874 // Quoted strings must be dot separated or the only element making up the local-part.
876 'email' => 'just"not"right@example.com',
877 'result' => false
879 // Spaces, quotes, and backslashes may only exist when within quoted strings and preceded by a backslash.
881 'email' => 'this is"not\allowed@example.com',
882 'result' => false
884 // Even if escaped (preceded by a backslash), spaces, quotes, and backslashes must still be contained by quotes.
886 'email' => 'this\ still\"not\\allowed@example.com',
887 'result' => false
889 // Local part is longer than 64 characters.
891 'email' => '1234567890123456789012345678901234567890123456789012345678901234+x@example.com',
892 'result' => false
898 * Tests valid and invalid email address using the validate_email() function.
900 * @param string $email the email address to test
901 * @param boolean $result Expected result (true or false)
902 * @dataProvider data_validate_email
903 * @covers ::validate_email
905 public function test_validate_email($email, $result) {
906 if ($result) {
907 $this->assertTrue(validate_email($email));
908 } else {
909 $this->assertFalse(validate_email($email));
914 * Data provider for test_get_file_argument.
916 public static function provider_get_file_argument() {
917 return array(
918 // Serving SCORM content w/o HTTP GET params.
919 array(array(
920 'SERVER_SOFTWARE' => 'Apache',
921 'SERVER_PORT' => '80',
922 'REQUEST_METHOD' => 'GET',
923 'REQUEST_URI' => '/pluginfile.php/3854/mod_scorm/content/1/swf.html',
924 'SCRIPT_NAME' => '/pluginfile.php',
925 'PATH_INFO' => '/3854/mod_scorm/content/1/swf.html',
926 ), 0, '/3854/mod_scorm/content/1/swf.html'),
927 array(array(
928 'SERVER_SOFTWARE' => 'Apache',
929 'SERVER_PORT' => '80',
930 'REQUEST_METHOD' => 'GET',
931 'REQUEST_URI' => '/pluginfile.php/3854/mod_scorm/content/1/swf.html',
932 'SCRIPT_NAME' => '/pluginfile.php',
933 'PATH_INFO' => '/3854/mod_scorm/content/1/swf.html',
934 ), 1, '/3854/mod_scorm/content/1/swf.html'),
935 // Serving SCORM content w/ HTTP GET 'file' as first param.
936 array(array(
937 'SERVER_SOFTWARE' => 'Apache',
938 'SERVER_PORT' => '80',
939 'REQUEST_METHOD' => 'GET',
940 'REQUEST_URI' => '/pluginfile.php/3854/mod_scorm/content/1/swf.html?file=video_.swf',
941 'SCRIPT_NAME' => '/pluginfile.php',
942 'PATH_INFO' => '/3854/mod_scorm/content/1/swf.html',
943 ), 0, '/3854/mod_scorm/content/1/swf.html'),
944 array(array(
945 'SERVER_SOFTWARE' => 'Apache',
946 'SERVER_PORT' => '80',
947 'REQUEST_METHOD' => 'GET',
948 'REQUEST_URI' => '/pluginfile.php/3854/mod_scorm/content/1/swf.html?file=video_.swf',
949 'SCRIPT_NAME' => '/pluginfile.php',
950 'PATH_INFO' => '/3854/mod_scorm/content/1/swf.html',
951 ), 1, '/3854/mod_scorm/content/1/swf.html'),
952 // Serving SCORM content w/ HTTP GET 'file' not as first param.
953 array(array(
954 'SERVER_SOFTWARE' => 'Apache',
955 'SERVER_PORT' => '80',
956 'REQUEST_METHOD' => 'GET',
957 'REQUEST_URI' => '/pluginfile.php/3854/mod_scorm/content/1/swf.html?foo=bar&file=video_.swf',
958 'SCRIPT_NAME' => '/pluginfile.php',
959 'PATH_INFO' => '/3854/mod_scorm/content/1/swf.html',
960 ), 0, '/3854/mod_scorm/content/1/swf.html'),
961 array(array(
962 'SERVER_SOFTWARE' => 'Apache',
963 'SERVER_PORT' => '80',
964 'REQUEST_METHOD' => 'GET',
965 'REQUEST_URI' => '/pluginfile.php/3854/mod_scorm/content/1/swf.html?foo=bar&file=video_.swf',
966 'SCRIPT_NAME' => '/pluginfile.php',
967 'PATH_INFO' => '/3854/mod_scorm/content/1/swf.html',
968 ), 1, '/3854/mod_scorm/content/1/swf.html'),
969 // Serving content from a generic activity w/ HTTP GET 'file', still forcing slash arguments.
970 array(array(
971 'SERVER_SOFTWARE' => 'Apache',
972 'SERVER_PORT' => '80',
973 'REQUEST_METHOD' => 'GET',
974 'REQUEST_URI' => '/pluginfile.php/3854/whatever/content/1/swf.html?file=video_.swf',
975 'SCRIPT_NAME' => '/pluginfile.php',
976 'PATH_INFO' => '/3854/whatever/content/1/swf.html',
977 ), 0, '/3854/whatever/content/1/swf.html'),
978 array(array(
979 'SERVER_SOFTWARE' => 'Apache',
980 'SERVER_PORT' => '80',
981 'REQUEST_METHOD' => 'GET',
982 'REQUEST_URI' => '/pluginfile.php/3854/whatever/content/1/swf.html?file=video_.swf',
983 'SCRIPT_NAME' => '/pluginfile.php',
984 'PATH_INFO' => '/3854/whatever/content/1/swf.html',
985 ), 1, '/3854/whatever/content/1/swf.html'),
986 // Serving content from a generic activity w/ HTTP GET 'file', still forcing slash arguments (edge case).
987 array(array(
988 'SERVER_SOFTWARE' => 'Apache',
989 'SERVER_PORT' => '80',
990 'REQUEST_METHOD' => 'GET',
991 'REQUEST_URI' => '/pluginfile.php/?file=video_.swf',
992 'SCRIPT_NAME' => '/pluginfile.php',
993 'PATH_INFO' => '/',
994 ), 0, 'video_.swf'),
995 array(array(
996 'SERVER_SOFTWARE' => 'Apache',
997 'SERVER_PORT' => '80',
998 'REQUEST_METHOD' => 'GET',
999 'REQUEST_URI' => '/pluginfile.php/?file=video_.swf',
1000 'SCRIPT_NAME' => '/pluginfile.php',
1001 'PATH_INFO' => '/',
1002 ), 1, 'video_.swf'),
1003 // Serving content from a generic activity w/ HTTP GET 'file', w/o forcing slash arguments.
1004 array(array(
1005 'SERVER_SOFTWARE' => 'Apache',
1006 'SERVER_PORT' => '80',
1007 'REQUEST_METHOD' => 'GET',
1008 'REQUEST_URI' => '/pluginfile.php?file=%2F3854%2Fwhatever%2Fcontent%2F1%2Fswf.html%3Ffile%3Dvideo_.swf',
1009 'SCRIPT_NAME' => '/pluginfile.php',
1010 ), 0, '/3854/whatever/content/1/swf.html?file=video_.swf'),
1011 array(array(
1012 'SERVER_SOFTWARE' => 'Apache',
1013 'SERVER_PORT' => '80',
1014 'REQUEST_METHOD' => 'GET',
1015 'REQUEST_URI' => '/pluginfile.php?file=%2F3854%2Fwhatever%2Fcontent%2F1%2Fswf.html%3Ffile%3Dvideo_.swf',
1016 'SCRIPT_NAME' => '/pluginfile.php',
1017 ), 1, '/3854/whatever/content/1/swf.html?file=video_.swf'),
1022 * Tests for get_file_argument() function.
1024 * @param array $server mockup for $_SERVER.
1025 * @param string $cfgslasharguments slasharguments setting.
1026 * @param string|false $expected Expected value.
1027 * @dataProvider provider_get_file_argument
1028 * @covers ::get_file_argument
1030 public function test_get_file_argument($server, $cfgslasharguments, $expected) {
1031 global $CFG;
1033 // Overwrite the related settings.
1034 $currentsetting = $CFG->slasharguments;
1035 $CFG->slasharguments = $cfgslasharguments;
1036 // Mock global $_SERVER.
1037 $currentserver = isset($_SERVER) ? $_SERVER : null;
1038 $_SERVER = $server;
1039 initialise_fullme();
1040 if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
1041 $this->fail('Only HTTP GET mocked request allowed.');
1043 if (empty($_SERVER['REQUEST_URI'])) {
1044 $this->fail('Invalid HTTP GET mocked request.');
1046 // Mock global $_GET.
1047 $currentget = isset($_GET) ? $_GET : null;
1048 $_GET = array();
1049 $querystring = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
1050 if (!empty($querystring)) {
1051 $_SERVER['QUERY_STRING'] = $querystring;
1052 parse_str($querystring, $_GET);
1055 $this->assertEquals($expected, get_file_argument());
1057 // Restore the current settings and global values.
1058 $CFG->slasharguments = $currentsetting;
1059 if (is_null($currentserver)) {
1060 unset($_SERVER);
1061 } else {
1062 $_SERVER = $currentserver;
1064 if (is_null($currentget)) {
1065 unset($_GET);
1066 } else {
1067 $_GET = $currentget;
1072 * Tests for extract_draft_file_urls_from_text() function.
1074 * @covers ::extract_draft_file_urls_from_text
1076 public function test_extract_draft_file_urls_from_text() {
1077 global $CFG;
1079 $url1 = "{$CFG->wwwroot}/draftfile.php/5/user/draft/99999999/test1.jpg";
1080 $url2 = "{$CFG->wwwroot}/draftfile.php/5/user/draft/99999998/test2.jpg";
1082 $html = "<p>This is a test.</p><p><img src=\"{$url1}\" alt=\"\" role=\"presentation\"></p>
1083 <br>Test content.<p></p><p><img src=\"{$url2}\" alt=\"\" width=\"2048\" height=\"1536\"
1084 role=\"presentation\" class=\"img-fluid atto_image_button_text-bottom\"><br></p>";
1085 $draftareas = array(
1086 array(
1087 'urlbase' => 'draftfile.php',
1088 'contextid' => '5',
1089 'component' => 'user',
1090 'filearea' => 'draft',
1091 'itemid' => '99999999',
1092 'filename' => 'test1.jpg',
1093 0 => "{$CFG->wwwroot}/draftfile.php/5/user/draft/99999999/test1.jpg",
1094 1 => 'draftfile.php',
1095 2 => '5',
1096 3 => 'user',
1097 4 => 'draft',
1098 5 => '99999999',
1099 6 => 'test1.jpg'
1101 array(
1102 'urlbase' => 'draftfile.php',
1103 'contextid' => '5',
1104 'component' => 'user',
1105 'filearea' => 'draft',
1106 'itemid' => '99999998',
1107 'filename' => 'test2.jpg',
1108 0 => "{$CFG->wwwroot}/draftfile.php/5/user/draft/99999998/test2.jpg",
1109 1 => 'draftfile.php',
1110 2 => '5',
1111 3 => 'user',
1112 4 => 'draft',
1113 5 => '99999998',
1114 6 => 'test2.jpg'
1117 $extracteddraftareas = extract_draft_file_urls_from_text($html, false, 5, 'user', 'draft');
1118 $this->assertEquals($draftareas, $extracteddraftareas);
1122 * @covers ::print_password_policy
1124 public function test_print_password_policy() {
1125 $this->resetAfterTest(true);
1126 global $CFG;
1128 $policydisabled = '';
1130 // Set password policy to disabled.
1131 $CFG->passwordpolicy = false;
1133 // Check for empty response.
1134 $this->assertEquals($policydisabled, print_password_policy());
1136 // Now set the policy to enabled with every control disabled.
1137 $CFG->passwordpolicy = true;
1138 $CFG->minpasswordlength = 0;
1139 $CFG->minpassworddigits = 0;
1140 $CFG->minpasswordlower = 0;
1141 $CFG->minpasswordupper = 0;
1142 $CFG->minpasswordnonalphanum = 0;
1143 $CFG->maxconsecutiveidentchars = 0;
1145 // Check for empty response.
1146 $this->assertEquals($policydisabled, print_password_policy());
1148 // Now enable some controls, and check that the policy responds with policy text.
1149 $CFG->minpasswordlength = 8;
1150 $CFG->minpassworddigits = 1;
1151 $CFG->minpasswordlower = 1;
1152 $CFG->minpasswordupper = 1;
1153 $CFG->minpasswordnonalphanum = 1;
1154 $CFG->maxconsecutiveidentchars = 1;
1156 $this->assertNotEquals($policydisabled, print_password_policy());
1160 * Data provider for the testing get_html_lang_attribute_value().
1162 * @return string[][]
1164 public function get_html_lang_attribute_value_provider() {
1165 return [
1166 'Empty lang code' => [' ', 'unknown'],
1167 'English' => ['en', 'en'],
1168 'English, US' => ['en_us', 'en-us'],
1173 * Test for get_html_lang_attribute_value().
1175 * @covers ::get_html_lang_attribute_value()
1176 * @dataProvider get_html_lang_attribute_value_provider
1177 * @param string $langcode The language code to convert.
1178 * @param string $expected The expected converted value.
1179 * @return void
1181 public function test_get_html_lang_attribute_value(string $langcode, string $expected): void {
1182 $this->assertEquals($expected, get_html_lang_attribute_value($langcode));
1186 * Test the coding exceptions when returning URL as relative path from $CFG->wwwroot.
1188 * @param moodle_url $url The URL pointing to a web resource.
1189 * @param string $exmessage The expected output URL.
1190 * @throws coding_exception If called on a non-local URL.
1191 * @see \moodle_url::out_as_local_url()
1192 * @covers \moodle_url::out_as_local_url
1193 * @dataProvider out_as_local_url_coding_exception_provider
1195 public function test_out_as_local_url_coding_exception(\moodle_url $url, string $exmessage) {
1196 $this->expectException(\coding_exception::class);
1197 $this->expectExceptionMessage($exmessage);
1198 $localurl = $url->out_as_local_url();
1202 * Data provider for throwing coding exceptions in <u>\moodle_url::out_as_local_url()</u>.
1204 * @return array
1205 * @throws moodle_exception On seriously malformed URLs (<u>parse_url</u>).
1206 * @see \moodle_url::out_as_local_url()
1207 * @see parse_url()
1209 public function out_as_local_url_coding_exception_provider() {
1210 return [
1211 'Google Maps CDN (HTTPS)' => [
1212 new \moodle_url('https://maps.googleapis.com/maps/api/js', ['key' => 'googlemapkey3', 'sensor' => 'false']),
1213 'Coding error detected, it must be fixed by a programmer: out_as_local_url called on a non-local URL'
1215 'Google Maps CDN (HTTP)' => [
1216 new \moodle_url('http://maps.googleapis.com/maps/api/js', ['key' => 'googlemapkey3', 'sensor' => 'false']),
1217 'Coding error detected, it must be fixed by a programmer: out_as_local_url called on a non-local URL'
1223 * Test URL as relative path from $CFG->wwwroot.
1225 * @param moodle_url $url The URL pointing to a web resource.
1226 * @param string $expected The expected local URL.
1227 * @throws coding_exception If called on a non-local URL.
1228 * @see \moodle_url::out_as_local_url()
1229 * @covers \moodle_url::out_as_local_url
1230 * @dataProvider out_as_local_url_provider
1232 public function test_out_as_local_url(\moodle_url $url, string $expected) {
1233 $this->assertEquals($expected, $url->out_as_local_url(false));
1237 * Data provider for returning local paths via <u>\moodle_url::out_as_local_url()</u>.
1239 * @return array
1240 * @throws moodle_exception On seriously malformed URLs (<u>parse_url</u>).
1241 * @see \moodle_url::out_as_local_url()
1242 * @see parse_url()
1244 public function out_as_local_url_provider() {
1245 global $CFG;
1246 $wwwroot = rtrim($CFG->wwwroot, '/');
1248 return [
1249 'Environment XML file' => [
1250 new \moodle_url('/admin/environment.xml'),
1251 '/admin/environment.xml'
1253 'H5P JS internal resource' => [
1254 new \moodle_url('/h5p/js/embed.js'),
1255 '/h5p/js/embed.js'
1257 'A Moodle JS resource using the full path including the proper JS Handler' => [
1258 new \moodle_url($wwwroot . '/lib/javascript.php/1/lib/editor/tiny/js/tinymce/tinymce.js'),
1259 '/lib/javascript.php/1/lib/editor/tiny/js/tinymce/tinymce.js'
1265 * Test URL as relative path from $CFG->wwwroot.
1267 * @param moodle_url $url The URL pointing to a web resource.
1268 * @param bool $expected The expected result.
1269 * @see \moodle_url::is_local_url()
1270 * @covers \moodle_url::is_local_url
1271 * @dataProvider is_local_url_provider
1273 public function test_is_local_url(\moodle_url $url, bool $expected) {
1274 $this->assertEquals($expected, $url->is_local_url(), "'{$url}' is not a local URL!");
1278 * Data provider for testing <u>\moodle_url::is_local_url()</u>.
1280 * @return array
1281 * @see \moodle_url::is_local_url()
1283 public function is_local_url_provider() {
1284 global $CFG;
1285 $wwwroot = rtrim($CFG->wwwroot, '/');
1287 return [
1288 'Google Maps CDN (HTTPS)' => [
1289 new \moodle_url('https://maps.googleapis.com/maps/api/js', ['key' => 'googlemapkey3', 'sensor' => 'false']),
1290 false
1292 'Google Maps CDN (HTTP)' => [
1293 new \moodle_url('http://maps.googleapis.com/maps/api/js', ['key' => 'googlemapkey3', 'sensor' => 'false']),
1294 false
1296 'wwwroot' => [
1297 new \moodle_url($wwwroot),
1298 true
1300 'wwwroot/' => [
1301 new \moodle_url($wwwroot . '/'),
1302 true
1304 'Environment XML file' => [
1305 new \moodle_url('/admin/environment.xml'),
1306 true
1308 'H5P JS internal resource' => [
1309 new \moodle_url('/h5p/js/embed.js'),
1310 true
1316 * Data provider for strip_querystring tests.
1318 * @return array
1320 public function strip_querystring_provider(): array {
1321 return [
1322 'Null' => [null, ''],
1323 'Empty string' => ['', ''],
1324 'No querystring' => ['https://example.com', 'https://example.com'],
1325 'Querystring' => ['https://example.com?foo=bar', 'https://example.com'],
1326 'Querystring with fragment' => ['https://example.com?foo=bar#baz', 'https://example.com'],
1327 'Querystring with fragment and path' => ['https://example.com/foo/bar?foo=bar#baz', 'https://example.com/foo/bar'],
1332 * Test the strip_querystring function with various exampels.
1334 * @dataProvider strip_querystring_provider
1335 * @param mixed $value
1336 * @param mixed $expected
1337 * @covers ::strip_querystring
1339 public function test_strip_querystring($value, $expected): void {
1340 $this->assertEquals($expected, strip_querystring($value));