2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
22 * @copyright © 2006 The Open University
23 * @author T.J.Hunt@open.ac.uk
24 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
27 defined('MOODLE_INTERNAL') ||
die();
30 class core_weblib_testcase
extends advanced_testcase
{
32 public function test_format_string() {
36 $this->assertSame("& &&&&& &&", format_string("& &&&&& &&"));
37 $this->assertSame("ANother & &&&&& Category", format_string("ANother & &&&&& Category"));
38 $this->assertSame("ANother & &&&&& Category", format_string("ANother & &&&&& Category", true));
39 $this->assertSame("Nick's Test Site & Other things", format_string("Nick's Test Site & Other things", true));
40 $this->assertSame("& < > \" '", format_string("& < > \" '", true, ['escape' => false]));
43 $this->assertSame(""", format_string("""));
46 $this->assertSame("&11234;", format_string("&11234;"));
49 $this->assertSame("ᅻ", format_string("ᅻ"));
52 $originalformatstringstriptags = $CFG->formatstringstriptags
;
54 $CFG->formatstringstriptags
= false;
55 $this->assertSame('x < 1', format_string('x < 1'));
56 $this->assertSame('x > 1', format_string('x > 1'));
57 $this->assertSame('x < 1 and x > 0', format_string('x < 1 and x > 0'));
59 $CFG->formatstringstriptags
= true;
60 $this->assertSame('x < 1', format_string('x < 1'));
61 $this->assertSame('x > 1', format_string('x > 1'));
62 $this->assertSame('x < 1 and x > 0', format_string('x < 1 and x > 0'));
64 $CFG->formatstringstriptags
= $originalformatstringstriptags;
68 * The format string static caching should include the filters option to make
69 * sure filters are correctly applied when requested.
71 public function test_format_string_static_caching_with_filters() {
74 $this->resetAfterTest(true);
75 $this->setAdminUser();
76 $generator = $this->getDataGenerator();
77 $course = $generator->create_course();
78 $user = $generator->create_user();
79 $rawstring = 'Shortname <a href="#">link</a> curseword';
80 $expectednofilter = strip_links($rawstring);
81 $expectedfilter = 'Shortname link \*\**';
83 $context = context_course
::instance($course->id
);
85 'context' => $context,
90 $this->setUser($user);
92 // Format the string without filters. It should just strip the
94 $nofilterresult = format_string($rawstring, $striplinks, $options);
95 $this->assertEquals($expectednofilter, $nofilterresult);
97 // Add the censor filter. Make sure it's enabled globally.
98 $CFG->filterall
= true;
99 $CFG->stringfilters
= 'censor';
100 $CFG->filter_censor_badwords
= 'curseword';
101 filter_set_global_state('censor', TEXTFILTER_ON
);
102 filter_set_local_state('censor', $context->id
, TEXTFILTER_ON
);
103 // This time we want to apply the filters.
104 $options['filter'] = true;
105 $filterresult = format_string($rawstring, $striplinks, $options);
106 $this->assertRegExp("/$expectedfilter/", $filterresult);
108 filter_set_local_state('censor', $context->id
, TEXTFILTER_OFF
);
110 // Confirm that we get back the cached string. The result should be
111 // the same as the filtered text above even though we've disabled the
112 // censor filter in between.
113 $cachedresult = format_string($rawstring, $striplinks, $options);
114 $this->assertRegExp("/$expectedfilter/", $cachedresult);
117 public function test_s() {
119 $this->assertSame('0', s(0));
120 $this->assertSame('0', s('0'));
121 $this->assertSame('0', s(false));
122 $this->assertSame('', s(null));
125 $this->assertSame('This Breaks " Strict', s('This Breaks " Strict'));
126 $this->assertSame('This Breaks <a>" Strict</a>', s('This Breaks <a>" Strict</a>'));
128 // Unicode characters.
129 $this->assertSame('Café', s('Café'));
130 $this->assertSame('一, 二, 三', s('一, 二, 三'));
132 // Don't escape already-escaped numeric entities. (Note, this behaviour
133 // may not be desirable. Perhaps we should remove these tests and that
134 // functionality, but we can only do that if we understand why it was added.)
135 $this->assertSame('An entity: ৿.', s('An entity: ৿.'));
136 $this->assertSame('An entity: б.', s('An entity: б.'));
137 $this->assertSame('An entity: &amp;.', s('An entity: &.'));
138 $this->assertSame('Not an entity: &amp;#x09ff;.', s('Not an entity: &#x09ff;.'));
140 // Test all ASCII characters (0-127).
141 for ($i = 0; $i <= 127; $i++
) {
142 $character = chr($i);
143 $result = s($character);
144 switch ($character) {
146 $this->assertSame('"', $result);
149 $this->assertSame('&', $result);
152 $this->assertSame(''', $result);
155 $this->assertSame('<', $result);
158 $this->assertSame('>', $result);
161 $this->assertSame($character, $result);
167 public function test_format_text_email() {
168 $this->assertSame("This is a TEST\n",
169 format_text_email('<p>This is a <strong>test</strong></p>', FORMAT_HTML
));
170 $this->assertSame("This is a TEST\n",
171 format_text_email('<p class="frogs">This is a <strong class=\'fishes\'>test</strong></p>', FORMAT_HTML
));
172 $this->assertSame('& so is this',
173 format_text_email('& so is this', FORMAT_HTML
));
174 $this->assertSame('Two bullets: ' . core_text
::code2utf8(8226) . ' ' . core_text
::code2utf8(8226),
175 format_text_email('Two bullets: • •', FORMAT_HTML
));
176 $this->assertSame(core_text
::code2utf8(0x7fd2).core_text
::code2utf8(0x7fd2),
177 format_text_email('習習', FORMAT_HTML
));
180 public function test_obfuscate_email() {
181 $email = 'some.user@example.com';
182 $obfuscated = obfuscate_email($email);
183 $this->assertNotSame($email, $obfuscated);
184 $back = core_text
::entities_to_utf8(urldecode($email), true);
185 $this->assertSame($email, $back);
188 public function test_obfuscate_text() {
189 $text = 'Žluťoučký koníček 32131';
190 $obfuscated = obfuscate_text($text);
191 $this->assertNotSame($text, $obfuscated);
192 $back = core_text
::entities_to_utf8($obfuscated, true);
193 $this->assertSame($text, $back);
196 public function test_highlight() {
197 $this->assertSame('This is <span class="highlight">good</span>',
198 highlight('good', 'This is good'));
200 $this->assertSame('<span class="highlight">span</span>',
201 highlight('SpaN', 'span'));
203 $this->assertSame('<span class="highlight">SpaN</span>',
204 highlight('span', 'SpaN'));
206 $this->assertSame('<span><span class="highlight">span</span></span>',
207 highlight('span', '<span>span</span>'));
209 $this->assertSame('He <span class="highlight">is</span> <span class="highlight">good</span>',
210 highlight('good is', 'He is good'));
212 $this->assertSame('This is <span class="highlight">good</span>',
213 highlight('+good', 'This is good'));
215 $this->assertSame('This is good',
216 highlight('-good', 'This is good'));
218 $this->assertSame('This is goodness',
219 highlight('+good', 'This is goodness'));
221 $this->assertSame('This is <span class="highlight">good</span>ness',
222 highlight('good', 'This is goodness'));
224 $this->assertSame('<p><b>test</b> <b>1</b></p><p><b>1</b></p>',
225 highlight('test 1', '<p>test 1</p><p>1</p>', false, '<b>', '</b>'));
227 $this->assertSame('<p><b>test</b> <b>1</b></p><p><b>1</b></p>',
228 highlight('test +1', '<p>test 1</p><p>1</p>', false, '<b>', '</b>'));
230 $this->assertSame('<p><b>test</b> 1</p><p>1</p>',
231 highlight('test -1', '<p>test 1</p><p>1</p>', false, '<b>', '</b>'));
234 public function test_replace_ampersands() {
235 $this->assertSame("This & that ", replace_ampersands_not_followed_by_entity("This & that "));
236 $this->assertSame("This &nbsp that ", replace_ampersands_not_followed_by_entity("This   that "));
239 public function test_strip_links() {
240 $this->assertSame('this is a link', strip_links('this is a <a href="http://someaddress.com/query">link</a>'));
243 public function test_wikify_links() {
244 $this->assertSame('this is a link [ http://someaddress.com/query ]', wikify_links('this is a <a href="http://someaddress.com/query">link</a>'));
247 public function test_clean_text() {
248 $text = "lala <applet>xx</applet>";
249 $this->assertSame($text, clean_text($text, FORMAT_PLAIN
));
250 $this->assertSame('lala xx', clean_text($text, FORMAT_MARKDOWN
));
251 $this->assertSame('lala xx', clean_text($text, FORMAT_MOODLE
));
252 $this->assertSame('lala xx', clean_text($text, FORMAT_HTML
));
255 public function test_qualified_me() {
256 global $PAGE, $FULLME, $CFG;
257 $this->resetAfterTest();
259 $PAGE = new moodle_page();
261 $FULLME = $CFG->wwwroot
.'/course/view.php?id=1&xx=yy';
262 $this->assertSame($FULLME, qualified_me());
264 $PAGE->set_url('/course/view.php', array('id'=>1));
265 $this->assertSame($CFG->wwwroot
.'/course/view.php?id=1', qualified_me());
268 public function test_null_progres_trace() {
269 $this->resetAfterTest(false);
271 $trace = new null_progress_trace();
272 $trace->output('do');
273 $trace->output('re', 1);
274 $trace->output('mi', 2);
276 $output = ob_get_contents();
277 $this->assertSame('', $output);
278 $this->expectOutputString('');
281 public function test_text_progres_trace() {
282 $this->resetAfterTest(false);
284 $trace = new text_progress_trace();
285 $trace->output('do');
286 $trace->output('re', 1);
287 $trace->output('mi', 2);
289 $this->expectOutputString("do\n re\n mi\n");
292 public function test_html_progres_trace() {
293 $this->resetAfterTest(false);
295 $trace = new html_progress_trace();
296 $trace->output('do');
297 $trace->output('re', 1);
298 $trace->output('mi', 2);
300 $this->expectOutputString("<p>do</p>\n<p>  re</p>\n<p>    mi</p>\n");
303 public function test_html_list_progress_trace() {
304 $this->resetAfterTest(false);
306 $trace = new html_list_progress_trace();
307 $trace->output('do');
308 $trace->output('re', 1);
309 $trace->output('mi', 2);
311 $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");
314 public function test_progres_trace_buffer() {
315 $this->resetAfterTest(false);
317 $trace = new progress_trace_buffer(new html_progress_trace());
319 $trace->output('do');
320 $trace->output('re', 1);
321 $trace->output('mi', 2);
323 $output = ob_get_contents();
325 $this->assertSame("<p>do</p>\n<p>  re</p>\n<p>    mi</p>\n", $output);
326 $this->assertSame($output, $trace->get_buffer());
328 $trace = new progress_trace_buffer(new html_progress_trace(), false);
329 $trace->output('do');
330 $trace->output('re', 1);
331 $trace->output('mi', 2);
333 $this->assertSame("<p>do</p>\n<p>  re</p>\n<p>    mi</p>\n", $trace->get_buffer());
334 $this->assertSame("<p>do</p>\n<p>  re</p>\n<p>    mi</p>\n", $trace->get_buffer());
335 $trace->reset_buffer();
336 $this->assertSame('', $trace->get_buffer());
337 $this->expectOutputString('');
340 public function test_combined_progres_trace() {
341 $this->resetAfterTest(false);
343 $trace1 = new progress_trace_buffer(new html_progress_trace(), false);
344 $trace2 = new progress_trace_buffer(new text_progress_trace(), false);
346 $trace = new combined_progress_trace(array($trace1, $trace2));
347 $trace->output('do');
348 $trace->output('re', 1);
349 $trace->output('mi', 2);
351 $this->assertSame("<p>do</p>\n<p>  re</p>\n<p>    mi</p>\n", $trace1->get_buffer());
352 $this->assertSame("do\n re\n mi\n", $trace2->get_buffer());
353 $this->expectOutputString('');
356 public function test_set_debugging() {
359 $this->resetAfterTest();
361 $this->assertEquals(DEBUG_DEVELOPER
, $CFG->debug
);
362 $this->assertTrue($CFG->debugdeveloper
);
363 $this->assertNotEmpty($CFG->debugdisplay
);
365 set_debugging(DEBUG_DEVELOPER
, true);
366 $this->assertEquals(DEBUG_DEVELOPER
, $CFG->debug
);
367 $this->assertTrue($CFG->debugdeveloper
);
368 $this->assertNotEmpty($CFG->debugdisplay
);
370 set_debugging(DEBUG_DEVELOPER
, false);
371 $this->assertEquals(DEBUG_DEVELOPER
, $CFG->debug
);
372 $this->assertTrue($CFG->debugdeveloper
);
373 $this->assertEmpty($CFG->debugdisplay
);
376 $this->assertEquals(-1, $CFG->debug
);
377 $this->assertTrue($CFG->debugdeveloper
);
379 set_debugging(DEBUG_ALL
);
380 $this->assertEquals(DEBUG_ALL
, $CFG->debug
);
381 $this->assertFalse($CFG->debugdeveloper
);
383 set_debugging(DEBUG_NORMAL
);
384 $this->assertEquals(DEBUG_NORMAL
, $CFG->debug
);
385 $this->assertFalse($CFG->debugdeveloper
);
387 set_debugging(DEBUG_MINIMAL
);
388 $this->assertEquals(DEBUG_MINIMAL
, $CFG->debug
);
389 $this->assertFalse($CFG->debugdeveloper
);
391 set_debugging(DEBUG_NONE
);
392 $this->assertEquals(DEBUG_NONE
, $CFG->debug
);
393 $this->assertFalse($CFG->debugdeveloper
);
396 public function test_strip_pluginfile_content() {
400 I'm writing to you from the Moodle Majlis in Muscat, Oman, where we just had several days of Moodle community goodness.
402 URL outside a tag: https://moodle.org/logo/logo-240x60.gif
403 Plugin url outside a tag: @@PLUGINFILE@@/logo-240x60.gif
405 External link 1:<img src='https://moodle.org/logo/logo-240x60.gif' alt='Moodle'/>
406 External link 2:<img alt="Moodle" src="https://moodle.org/logo/logo-240x60.gif"/>
407 Internal link 1:<img src='@@PLUGINFILE@@/logo-240x60.gif' alt='Moodle'/>
408 Internal link 2:<img alt="Moodle" src="@@PLUGINFILE@@logo-240x60.gif"/>
409 Anchor link 1:<a href="@@PLUGINFILE@@logo-240x60.gif" alt="bananas">Link text</a>
410 Anchor link 2:<a title="bananas" href="../logo-240x60.gif">Link text</a>
411 Anchor + ext. img:<a title="bananas" href="../logo-240x60.gif"><img alt="Moodle" src="@@PLUGINFILE@@logo-240x60.gif"/></a>
412 Ext. anchor + img:<a href="@@PLUGINFILE@@logo-240x60.gif"><img alt="Moodle" src="https://moodle.org/logo/logo-240x60.gif"/></a>
414 $expected = <<<EXPECTED
417 I'm writing to you from the Moodle Majlis in Muscat, Oman, where we just had several days of Moodle community goodness.
419 URL outside a tag: https://moodle.org/logo/logo-240x60.gif
420 Plugin url outside a tag: @@PLUGINFILE@@/logo-240x60.gif
422 External link 1:<img src="https://moodle.org/logo/logo-240x60.gif" alt="Moodle" />
423 External link 2:<img alt="Moodle" src="https://moodle.org/logo/logo-240x60.gif" />
426 Anchor link 1:Link text
427 Anchor link 2:<a title="bananas" href="../logo-240x60.gif">Link text</a>
428 Anchor + ext. img:<a title="bananas" href="../logo-240x60.gif"></a>
429 Ext. anchor + img:<img alt="Moodle" src="https://moodle.org/logo/logo-240x60.gif" />
431 $this->assertSame($expected, strip_pluginfile_content($source));
434 public function test_purify_html_ruby() {
436 $this->resetAfterTest();
439 "<p><ruby><rb>京都</rb><rp>(</rp><rt>きょうと</rt><rp>)</rp></ruby>は" .
440 "<ruby><rb>日本</rb><rp>(</rp><rt>にほん</rt><rp>)</rp></ruby>の" .
441 "<ruby><rb>都</rb><rp>(</rp><rt>みやこ</rt><rp>)</rp></ruby>です。</p>";
442 $illegal = '<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>';
444 $cleaned = purify_html($ruby . $illegal);
445 $this->assertEquals($ruby, $cleaned);
450 * Tests for content_to_text.
452 * @param string $content The content
453 * @param int|false $format The content format
454 * @param string $expected Expected value
455 * @dataProvider provider_content_to_text
457 public function test_content_to_text($content, $format, $expected) {
458 $content = content_to_text($content, $format);
459 $this->assertEquals($expected, $content);
463 * Data provider for test_content_to_text.
465 public static function provider_content_to_text() {
467 array('asd', false, 'asd'),
469 array("Note that:\n\n3 > 1 ", FORMAT_PLAIN
, "Note that:\n\n3 > 1"),
470 array("Note that:\n\n3 > 1\r\n", FORMAT_PLAIN
, "Note that:\n\n3 > 1"),
471 // Multiple spaces to one.
472 array('<span class="eheh">京都</span> -> hehe', FORMAT_HTML
, '京都 -> hehe'),
473 array('<span class="eheh">京都</span> -> hehe', false, '京都 -> hehe'),
474 array('asd asd', false, 'asd asd'),
475 // From markdown to html and html to text.
476 array('asd __lera__ con la', FORMAT_MARKDOWN
, 'asd LERA con la'),
478 array('<p class="frogs">This is a <strong class=\'fishes\'>test</strong></p>', FORMAT_HTML
, 'This is a TEST'),
479 array("<span lang='en' class='multilang'>english</span>
480 <span lang='ca' class='multilang'>català</span>
481 <span lang='es' class='multilang'>español</span>
482 <span lang='fr' class='multilang'>français</span>", FORMAT_HTML
, "english català español français")
487 * Tests for validate_email() function.
489 public function test_validate_email() {
491 $this->assertTrue(validate_email('moodle@example.com'));
492 $this->assertTrue(validate_email('moodle@localhost.local'));
493 $this->assertTrue(validate_email('verp_email+is=mighty@moodle.org'));
494 $this->assertTrue(validate_email("but_potentially'dangerous'too@example.org"));
495 $this->assertTrue(validate_email('posts+AAAAAAAAAAIAAAAAAAAGQQAAAAABFSXz1eM/P/lR2bYyljM+@posts.moodle.org'));
497 $this->assertFalse(validate_email('moodle@localhost'));
498 $this->assertFalse(validate_email('"attacker\\" -oQ/tmp/ -X/var/www/vhost/moodle/backdoor.php some"@email.com'));
499 $this->assertFalse(validate_email("moodle@example.com>\r\nRCPT TO:<victim@example.com"));
503 * Data provider for test_get_file_argument.
505 public static function provider_get_file_argument() {
507 // Serving SCORM content w/o HTTP GET params.
509 'SERVER_SOFTWARE' => 'Apache',
510 'SERVER_PORT' => '80',
511 'REQUEST_METHOD' => 'GET',
512 'REQUEST_URI' => '/pluginfile.php/3854/mod_scorm/content/1/swf.html',
513 'SCRIPT_NAME' => '/pluginfile.php',
514 'PATH_INFO' => '/3854/mod_scorm/content/1/swf.html',
515 ), 0, '/3854/mod_scorm/content/1/swf.html'),
517 'SERVER_SOFTWARE' => 'Apache',
518 'SERVER_PORT' => '80',
519 'REQUEST_METHOD' => 'GET',
520 'REQUEST_URI' => '/pluginfile.php/3854/mod_scorm/content/1/swf.html',
521 'SCRIPT_NAME' => '/pluginfile.php',
522 'PATH_INFO' => '/3854/mod_scorm/content/1/swf.html',
523 ), 1, '/3854/mod_scorm/content/1/swf.html'),
524 // Serving SCORM content w/ HTTP GET 'file' as first param.
526 'SERVER_SOFTWARE' => 'Apache',
527 'SERVER_PORT' => '80',
528 'REQUEST_METHOD' => 'GET',
529 'REQUEST_URI' => '/pluginfile.php/3854/mod_scorm/content/1/swf.html?file=video_.swf',
530 'SCRIPT_NAME' => '/pluginfile.php',
531 'PATH_INFO' => '/3854/mod_scorm/content/1/swf.html',
532 ), 0, '/3854/mod_scorm/content/1/swf.html'),
534 'SERVER_SOFTWARE' => 'Apache',
535 'SERVER_PORT' => '80',
536 'REQUEST_METHOD' => 'GET',
537 'REQUEST_URI' => '/pluginfile.php/3854/mod_scorm/content/1/swf.html?file=video_.swf',
538 'SCRIPT_NAME' => '/pluginfile.php',
539 'PATH_INFO' => '/3854/mod_scorm/content/1/swf.html',
540 ), 1, '/3854/mod_scorm/content/1/swf.html'),
541 // Serving SCORM content w/ HTTP GET 'file' not as first param.
543 'SERVER_SOFTWARE' => 'Apache',
544 'SERVER_PORT' => '80',
545 'REQUEST_METHOD' => 'GET',
546 'REQUEST_URI' => '/pluginfile.php/3854/mod_scorm/content/1/swf.html?foo=bar&file=video_.swf',
547 'SCRIPT_NAME' => '/pluginfile.php',
548 'PATH_INFO' => '/3854/mod_scorm/content/1/swf.html',
549 ), 0, '/3854/mod_scorm/content/1/swf.html'),
551 'SERVER_SOFTWARE' => 'Apache',
552 'SERVER_PORT' => '80',
553 'REQUEST_METHOD' => 'GET',
554 'REQUEST_URI' => '/pluginfile.php/3854/mod_scorm/content/1/swf.html?foo=bar&file=video_.swf',
555 'SCRIPT_NAME' => '/pluginfile.php',
556 'PATH_INFO' => '/3854/mod_scorm/content/1/swf.html',
557 ), 1, '/3854/mod_scorm/content/1/swf.html'),
558 // Serving content from a generic activity w/ HTTP GET 'file', still forcing slash arguments.
560 'SERVER_SOFTWARE' => 'Apache',
561 'SERVER_PORT' => '80',
562 'REQUEST_METHOD' => 'GET',
563 'REQUEST_URI' => '/pluginfile.php/3854/whatever/content/1/swf.html?file=video_.swf',
564 'SCRIPT_NAME' => '/pluginfile.php',
565 'PATH_INFO' => '/3854/whatever/content/1/swf.html',
566 ), 0, '/3854/whatever/content/1/swf.html'),
568 'SERVER_SOFTWARE' => 'Apache',
569 'SERVER_PORT' => '80',
570 'REQUEST_METHOD' => 'GET',
571 'REQUEST_URI' => '/pluginfile.php/3854/whatever/content/1/swf.html?file=video_.swf',
572 'SCRIPT_NAME' => '/pluginfile.php',
573 'PATH_INFO' => '/3854/whatever/content/1/swf.html',
574 ), 1, '/3854/whatever/content/1/swf.html'),
575 // Serving content from a generic activity w/ HTTP GET 'file', still forcing slash arguments (edge case).
577 'SERVER_SOFTWARE' => 'Apache',
578 'SERVER_PORT' => '80',
579 'REQUEST_METHOD' => 'GET',
580 'REQUEST_URI' => '/pluginfile.php/?file=video_.swf',
581 'SCRIPT_NAME' => '/pluginfile.php',
585 'SERVER_SOFTWARE' => 'Apache',
586 'SERVER_PORT' => '80',
587 'REQUEST_METHOD' => 'GET',
588 'REQUEST_URI' => '/pluginfile.php/?file=video_.swf',
589 'SCRIPT_NAME' => '/pluginfile.php',
592 // Serving content from a generic activity w/ HTTP GET 'file', w/o forcing slash arguments.
594 'SERVER_SOFTWARE' => 'Apache',
595 'SERVER_PORT' => '80',
596 'REQUEST_METHOD' => 'GET',
597 'REQUEST_URI' => '/pluginfile.php?file=%2F3854%2Fwhatever%2Fcontent%2F1%2Fswf.html%3Ffile%3Dvideo_.swf',
598 'SCRIPT_NAME' => '/pluginfile.php',
599 ), 0, '/3854/whatever/content/1/swf.html?file=video_.swf'),
601 'SERVER_SOFTWARE' => 'Apache',
602 'SERVER_PORT' => '80',
603 'REQUEST_METHOD' => 'GET',
604 'REQUEST_URI' => '/pluginfile.php?file=%2F3854%2Fwhatever%2Fcontent%2F1%2Fswf.html%3Ffile%3Dvideo_.swf',
605 'SCRIPT_NAME' => '/pluginfile.php',
606 ), 1, '/3854/whatever/content/1/swf.html?file=video_.swf'),
611 * Tests for get_file_argument() function.
613 * @param array $server mockup for $_SERVER.
614 * @param string $cfgslasharguments slasharguments setting.
615 * @param string|false $expected Expected value.
616 * @dataProvider provider_get_file_argument
618 public function test_get_file_argument($server, $cfgslasharguments, $expected) {
621 // Overwrite the related settings.
622 $currentsetting = $CFG->slasharguments
;
623 $CFG->slasharguments
= $cfgslasharguments;
624 // Mock global $_SERVER.
625 $currentserver = isset($_SERVER) ?
$_SERVER : null;
628 if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
629 $this->fail('Only HTTP GET mocked request allowed.');
631 if (empty($_SERVER['REQUEST_URI'])) {
632 $this->fail('Invalid HTTP GET mocked request.');
634 // Mock global $_GET.
635 $currentget = isset($_GET) ?
$_GET : null;
637 $querystring = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY
);
638 if (!empty($querystring)) {
639 $_SERVER['QUERY_STRING'] = $querystring;
640 parse_str($querystring, $_GET);
643 $this->assertEquals($expected, get_file_argument());
645 // Restore the current settings and global values.
646 $CFG->slasharguments
= $currentsetting;
647 if (is_null($currentserver)) {
650 $_SERVER = $currentserver;
652 if (is_null($currentget)) {
660 * Tests for extract_draft_file_urls_from_text() function.
662 public function test_extract_draft_file_urls_from_text() {
665 $url1 = "{$CFG->wwwroot}/draftfile.php/5/user/draft/99999999/test1.jpg";
666 $url2 = "{$CFG->wwwroot}/draftfile.php/5/user/draft/99999998/test2.jpg";
668 $html = "<p>This is a test.</p><p><img src=\"${url1}\" alt=\"\" role=\"presentation\"></p>
669 <br>Test content.<p></p><p><img src=\"{$url2}\" alt=\"\" width=\"2048\" height=\"1536\"
670 role=\"presentation\" class=\"img-responsive atto_image_button_text-bottom\"><br></p>";
673 'urlbase' => 'draftfile.php',
675 'component' => 'user',
676 'filearea' => 'draft',
677 'itemid' => '99999999',
678 'filename' => 'test1.jpg',
679 0 => "{$CFG->wwwroot}/draftfile.php/5/user/draft/99999999/test1.jpg",
680 1 => 'draftfile.php',
688 'urlbase' => 'draftfile.php',
690 'component' => 'user',
691 'filearea' => 'draft',
692 'itemid' => '99999998',
693 'filename' => 'test2.jpg',
694 0 => "{$CFG->wwwroot}/draftfile.php/5/user/draft/99999998/test2.jpg",
695 1 => 'draftfile.php',
703 $extracteddraftareas = extract_draft_file_urls_from_text($html, false, 5, 'user', 'draft');
704 $this->assertEquals($draftareas, $extracteddraftareas);