MDL-55091 phpunit: Following has been deprecated.
[moodle.git] / lib / tests / weblib_test.php
bloba8c10f62ad99eb1d6baba9740cce5e1a4fa6a302
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
27 defined('MOODLE_INTERNAL') || die();
30 class core_weblib_testcase extends advanced_testcase {
32 public function test_format_string() {
33 global $CFG;
35 // Ampersands.
36 $this->assertSame("&amp; &amp;&amp;&amp;&amp;&amp; &amp;&amp;", format_string("& &&&&& &&"));
37 $this->assertSame("ANother &amp; &amp;&amp;&amp;&amp;&amp; Category", format_string("ANother & &&&&& Category"));
38 $this->assertSame("ANother &amp; &amp;&amp;&amp;&amp;&amp; Category", format_string("ANother & &&&&& Category", true));
39 $this->assertSame("Nick's Test Site &amp; Other things", format_string("Nick's Test Site & Other things", true));
40 $this->assertSame("& < > \" '", format_string("& < > \" '", true, ['escape' => false]));
42 // String entities.
43 $this->assertSame("&quot;", format_string("&quot;"));
45 // Digital entities.
46 $this->assertSame("&11234;", format_string("&11234;"));
48 // Unicode entities.
49 $this->assertSame("&#4475;", format_string("&#4475;"));
51 // < and > signs.
52 $originalformatstringstriptags = $CFG->formatstringstriptags;
54 $CFG->formatstringstriptags = false;
55 $this->assertSame('x &lt; 1', format_string('x < 1'));
56 $this->assertSame('x &gt; 1', format_string('x > 1'));
57 $this->assertSame('x &lt; 1 and x &gt; 0', format_string('x < 1 and x > 0'));
59 $CFG->formatstringstriptags = true;
60 $this->assertSame('x &lt; 1', format_string('x < 1'));
61 $this->assertSame('x &gt; 1', format_string('x > 1'));
62 $this->assertSame('x &lt; 1 and x &gt; 0', format_string('x < 1 and x > 0'));
64 $CFG->formatstringstriptags = $originalformatstringstriptags;
67 public function test_s() {
68 // Special cases.
69 $this->assertSame('0', s(0));
70 $this->assertSame('0', s('0'));
71 $this->assertSame('0', s(false));
72 $this->assertSame('', s(null));
74 // Normal cases.
75 $this->assertSame('This Breaks &quot; Strict', s('This Breaks " Strict'));
76 $this->assertSame('This Breaks &lt;a&gt;&quot; Strict&lt;/a&gt;', s('This Breaks <a>" Strict</a>'));
78 // Unicode characters.
79 $this->assertSame('Café', s('Café'));
80 $this->assertSame('一, 二, 三', s('一, 二, 三'));
82 // Don't escape already-escaped numeric entities. (Note, this behaviour
83 // may not be desirable. Perhaps we should remove these tests and that
84 // functionality, but we can only do that if we understand why it was added.)
85 $this->assertSame('An entity: &#x09ff;.', s('An entity: &#x09ff;.'));
86 $this->assertSame('An entity: &#1073;.', s('An entity: &#1073;.'));
87 $this->assertSame('An entity: &amp;amp;.', s('An entity: &amp;.'));
88 $this->assertSame('Not an entity: &amp;amp;#x09ff;.', s('Not an entity: &amp;#x09ff;.'));
91 public function test_format_text_email() {
92 $this->assertSame("This is a TEST\n",
93 format_text_email('<p>This is a <strong>test</strong></p>', FORMAT_HTML));
94 $this->assertSame("This is a TEST\n",
95 format_text_email('<p class="frogs">This is a <strong class=\'fishes\'>test</strong></p>', FORMAT_HTML));
96 $this->assertSame('& so is this',
97 format_text_email('&amp; so is this', FORMAT_HTML));
98 $this->assertSame('Two bullets: ' . core_text::code2utf8(8226) . ' ' . core_text::code2utf8(8226),
99 format_text_email('Two bullets: &#x2022; &#8226;', FORMAT_HTML));
100 $this->assertSame(core_text::code2utf8(0x7fd2).core_text::code2utf8(0x7fd2),
101 format_text_email('&#x7fd2;&#x7FD2;', FORMAT_HTML));
104 public function test_obfuscate_email() {
105 $email = 'some.user@example.com';
106 $obfuscated = obfuscate_email($email);
107 $this->assertNotSame($email, $obfuscated);
108 $back = core_text::entities_to_utf8(urldecode($email), true);
109 $this->assertSame($email, $back);
112 public function test_obfuscate_text() {
113 $text = 'Žluťoučký koníček 32131';
114 $obfuscated = obfuscate_text($text);
115 $this->assertNotSame($text, $obfuscated);
116 $back = core_text::entities_to_utf8($obfuscated, true);
117 $this->assertSame($text, $back);
120 public function test_highlight() {
121 $this->assertSame('This is <span class="highlight">good</span>',
122 highlight('good', 'This is good'));
124 $this->assertSame('<span class="highlight">span</span>',
125 highlight('SpaN', 'span'));
127 $this->assertSame('<span class="highlight">SpaN</span>',
128 highlight('span', 'SpaN'));
130 $this->assertSame('<span><span class="highlight">span</span></span>',
131 highlight('span', '<span>span</span>'));
133 $this->assertSame('He <span class="highlight">is</span> <span class="highlight">good</span>',
134 highlight('good is', 'He is good'));
136 $this->assertSame('This is <span class="highlight">good</span>',
137 highlight('+good', 'This is good'));
139 $this->assertSame('This is good',
140 highlight('-good', 'This is good'));
142 $this->assertSame('This is goodness',
143 highlight('+good', 'This is goodness'));
145 $this->assertSame('This is <span class="highlight">good</span>ness',
146 highlight('good', 'This is goodness'));
148 $this->assertSame('<p><b>test</b> <b>1</b></p><p><b>1</b></p>',
149 highlight('test 1', '<p>test 1</p><p>1</p>', false, '<b>', '</b>'));
151 $this->assertSame('<p><b>test</b> <b>1</b></p><p><b>1</b></p>',
152 highlight('test +1', '<p>test 1</p><p>1</p>', false, '<b>', '</b>'));
154 $this->assertSame('<p><b>test</b> 1</p><p>1</p>',
155 highlight('test -1', '<p>test 1</p><p>1</p>', false, '<b>', '</b>'));
158 public function test_replace_ampersands() {
159 $this->assertSame("This &amp; that &nbsp;", replace_ampersands_not_followed_by_entity("This & that &nbsp;"));
160 $this->assertSame("This &amp;nbsp that &nbsp;", replace_ampersands_not_followed_by_entity("This &nbsp that &nbsp;"));
163 public function test_strip_links() {
164 $this->assertSame('this is a link', strip_links('this is a <a href="http://someaddress.com/query">link</a>'));
167 public function test_wikify_links() {
168 $this->assertSame('this is a link [ http://someaddress.com/query ]', wikify_links('this is a <a href="http://someaddress.com/query">link</a>'));
172 * Test basic moodle_url construction.
174 public function test_moodle_url_constructor() {
175 global $CFG;
177 $url = new moodle_url('/index.php');
178 $this->assertSame($CFG->wwwroot.'/index.php', $url->out());
180 $url = new moodle_url('/index.php', array());
181 $this->assertSame($CFG->wwwroot.'/index.php', $url->out());
183 $url = new moodle_url('/index.php', array('id' => 2));
184 $this->assertSame($CFG->wwwroot.'/index.php?id=2', $url->out());
186 $url = new moodle_url('/index.php', array('id' => 'two'));
187 $this->assertSame($CFG->wwwroot.'/index.php?id=two', $url->out());
189 $url = new moodle_url('/index.php', array('id' => 1, 'cid' => '2'));
190 $this->assertSame($CFG->wwwroot.'/index.php?id=1&amp;cid=2', $url->out());
191 $this->assertSame($CFG->wwwroot.'/index.php?id=1&cid=2', $url->out(false));
193 $url = new moodle_url('/index.php', null, 'test');
194 $this->assertSame($CFG->wwwroot.'/index.php#test', $url->out());
196 $url = new moodle_url('/index.php', array('id' => 2), 'test');
197 $this->assertSame($CFG->wwwroot.'/index.php?id=2#test', $url->out());
201 * Tests moodle_url::get_path().
203 public function test_moodle_url_get_path() {
204 $url = new moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1');
205 $this->assertSame('/my/file/is/here.txt', $url->get_path());
207 $url = new moodle_url('http://www.example.org/');
208 $this->assertSame('/', $url->get_path());
210 $url = new moodle_url('http://www.example.org/pluginfile.php/slash/arguments');
211 $this->assertSame('/pluginfile.php/slash/arguments', $url->get_path());
212 $this->assertSame('/pluginfile.php', $url->get_path(false));
215 public function test_moodle_url_round_trip() {
216 $strurl = 'http://moodle.org/course/view.php?id=5';
217 $url = new moodle_url($strurl);
218 $this->assertSame($strurl, $url->out(false));
220 $strurl = 'http://moodle.org/user/index.php?contextid=53&sifirst=M&silast=D';
221 $url = new moodle_url($strurl);
222 $this->assertSame($strurl, $url->out(false));
226 * Test Moodle URL objects created with a param with empty value.
228 public function test_moodle_url_empty_param_values() {
229 $strurl = 'http://moodle.org/course/view.php?id=0';
230 $url = new moodle_url($strurl, array('id' => 0));
231 $this->assertSame($strurl, $url->out(false));
233 $strurl = 'http://moodle.org/course/view.php?id';
234 $url = new moodle_url($strurl, array('id' => false));
235 $this->assertSame($strurl, $url->out(false));
237 $strurl = 'http://moodle.org/course/view.php?id';
238 $url = new moodle_url($strurl, array('id' => null));
239 $this->assertSame($strurl, $url->out(false));
241 $strurl = 'http://moodle.org/course/view.php?id';
242 $url = new moodle_url($strurl, array('id' => ''));
243 $this->assertSame($strurl, $url->out(false));
245 $strurl = 'http://moodle.org/course/view.php?id';
246 $url = new moodle_url($strurl);
247 $this->assertSame($strurl, $url->out(false));
251 * Test set good scheme on Moodle URL objects.
253 public function test_moodle_url_set_good_scheme() {
254 $url = new moodle_url('http://moodle.org/foo/bar');
255 $url->set_scheme('myscheme');
256 $this->assertSame('myscheme://moodle.org/foo/bar', $url->out());
260 * Test set bad scheme on Moodle URL objects.
262 * @expectedException coding_exception
264 public function test_moodle_url_set_bad_scheme() {
265 $url = new moodle_url('http://moodle.org/foo/bar');
266 $url->set_scheme('not a valid $ scheme');
269 public function test_moodle_url_round_trip_array_params() {
270 $strurl = 'http://example.com/?a%5B1%5D=1&a%5B2%5D=2';
271 $url = new moodle_url($strurl);
272 $this->assertSame($strurl, $url->out(false));
274 $url = new moodle_url('http://example.com/?a[1]=1&a[2]=2');
275 $this->assertSame($strurl, $url->out(false));
277 // For un-keyed array params, we expect 0..n keys to be returned.
278 $strurl = 'http://example.com/?a%5B0%5D=0&a%5B1%5D=1';
279 $url = new moodle_url('http://example.com/?a[]=0&a[]=1');
280 $this->assertSame($strurl, $url->out(false));
283 public function test_compare_url() {
284 $url1 = new moodle_url('index.php', array('var1' => 1, 'var2' => 2));
285 $url2 = new moodle_url('index2.php', array('var1' => 1, 'var2' => 2, 'var3' => 3));
287 $this->assertFalse($url1->compare($url2, URL_MATCH_BASE));
288 $this->assertFalse($url1->compare($url2, URL_MATCH_PARAMS));
289 $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT));
291 $url2 = new moodle_url('index.php', array('var1' => 1, 'var3' => 3));
293 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE));
294 $this->assertFalse($url1->compare($url2, URL_MATCH_PARAMS));
295 $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT));
297 $url2 = new moodle_url('index.php', array('var1' => 1, 'var2' => 2, 'var3' => 3));
299 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE));
300 $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS));
301 $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT));
303 $url2 = new moodle_url('index.php', array('var2' => 2, 'var1' => 1));
305 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE));
306 $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS));
307 $this->assertTrue($url1->compare($url2, URL_MATCH_EXACT));
309 $url1->set_anchor('test');
310 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE));
311 $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS));
312 $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT));
314 $url2->set_anchor('test');
315 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE));
316 $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS));
317 $this->assertTrue($url1->compare($url2, URL_MATCH_EXACT));
320 public function test_out_as_local_url() {
321 global $CFG;
322 // Test http url.
323 $url1 = new moodle_url('/lib/tests/weblib_test.php');
324 $this->assertSame('/lib/tests/weblib_test.php', $url1->out_as_local_url());
326 // Test https url.
327 $httpswwwroot = str_replace("http://", "https://", $CFG->wwwroot);
328 $url2 = new moodle_url($httpswwwroot.'/login/profile.php');
329 $this->assertSame('/login/profile.php', $url2->out_as_local_url());
331 // Test http url matching wwwroot.
332 $url3 = new moodle_url($CFG->wwwroot);
333 $this->assertSame('', $url3->out_as_local_url());
335 // Test http url matching wwwroot ending with slash (/).
336 $url3 = new moodle_url($CFG->wwwroot.'/');
337 $this->assertSame('/', $url3->out_as_local_url());
341 * @expectedException coding_exception
342 * @return void
344 public function test_out_as_local_url_error() {
345 $url2 = new moodle_url('http://www.google.com/lib/tests/weblib_test.php');
346 $url2->out_as_local_url();
350 * You should get error with modified url
352 * @expectedException coding_exception
353 * @return void
355 public function test_modified_url_out_as_local_url_error() {
356 global $CFG;
358 $modifiedurl = $CFG->wwwroot.'1';
359 $url3 = new moodle_url($modifiedurl.'/login/profile.php');
360 $url3->out_as_local_url();
364 * Try get local url from external https url and you should get error
366 * @expectedException coding_exception
368 public function test_https_out_as_local_url_error() {
369 $url4 = new moodle_url('https://www.google.com/lib/tests/weblib_test.php');
370 $url4->out_as_local_url();
373 public function test_moodle_url_get_scheme() {
374 // Should return the scheme only.
375 $url = new moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1');
376 $this->assertSame('http', $url->get_scheme());
378 // Should work for secure URLs.
379 $url = new moodle_url('https://www.example.org:447/my/file/is/here.txt?really=1');
380 $this->assertSame('https', $url->get_scheme());
382 // Should return an empty string if no scheme is specified.
383 $url = new moodle_url('www.example.org:447/my/file/is/here.txt?really=1');
384 $this->assertSame('', $url->get_scheme());
387 public function test_moodle_url_get_host() {
388 // Should return the host part only.
389 $url = new moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1');
390 $this->assertSame('www.example.org', $url->get_host());
393 public function test_moodle_url_get_port() {
394 // Should return the port if one provided.
395 $url = new moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1');
396 $this->assertSame(447, $url->get_port());
398 // Should return an empty string if port not specified.
399 $url = new moodle_url('http://www.example.org/some/path/here.php');
400 $this->assertSame('', $url->get_port());
403 public function test_clean_text() {
404 $text = "lala <applet>xx</applet>";
405 $this->assertSame($text, clean_text($text, FORMAT_PLAIN));
406 $this->assertSame('lala xx', clean_text($text, FORMAT_MARKDOWN));
407 $this->assertSame('lala xx', clean_text($text, FORMAT_MOODLE));
408 $this->assertSame('lala xx', clean_text($text, FORMAT_HTML));
411 public function test_qualified_me() {
412 global $PAGE, $FULLME, $CFG;
413 $this->resetAfterTest();
415 $PAGE = new moodle_page();
417 $FULLME = $CFG->wwwroot.'/course/view.php?id=1&xx=yy';
418 $this->assertSame($FULLME, qualified_me());
420 $PAGE->set_url('/course/view.php', array('id'=>1));
421 $this->assertSame($CFG->wwwroot.'/course/view.php?id=1', qualified_me());
424 public function test_null_progres_trace() {
425 $this->resetAfterTest(false);
427 $trace = new null_progress_trace();
428 $trace->output('do');
429 $trace->output('re', 1);
430 $trace->output('mi', 2);
431 $trace->finished();
432 $output = ob_get_contents();
433 $this->assertSame('', $output);
434 $this->expectOutputString('');
437 public function test_text_progres_trace() {
438 $this->resetAfterTest(false);
440 $trace = new text_progress_trace();
441 $trace->output('do');
442 $trace->output('re', 1);
443 $trace->output('mi', 2);
444 $trace->finished();
445 $this->expectOutputString("do\n re\n mi\n");
448 public function test_html_progres_trace() {
449 $this->resetAfterTest(false);
451 $trace = new html_progress_trace();
452 $trace->output('do');
453 $trace->output('re', 1);
454 $trace->output('mi', 2);
455 $trace->finished();
456 $this->expectOutputString("<p>do</p>\n<p>&#160;&#160;re</p>\n<p>&#160;&#160;&#160;&#160;mi</p>\n");
459 public function test_html_list_progress_trace() {
460 $this->resetAfterTest(false);
462 $trace = new html_list_progress_trace();
463 $trace->output('do');
464 $trace->output('re', 1);
465 $trace->output('mi', 2);
466 $trace->finished();
467 $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");
470 public function test_progres_trace_buffer() {
471 $this->resetAfterTest(false);
473 $trace = new progress_trace_buffer(new html_progress_trace());
474 ob_start();
475 $trace->output('do');
476 $trace->output('re', 1);
477 $trace->output('mi', 2);
478 $trace->finished();
479 $output = ob_get_contents();
480 ob_end_clean();
481 $this->assertSame("<p>do</p>\n<p>&#160;&#160;re</p>\n<p>&#160;&#160;&#160;&#160;mi</p>\n", $output);
482 $this->assertSame($output, $trace->get_buffer());
484 $trace = new progress_trace_buffer(new html_progress_trace(), false);
485 $trace->output('do');
486 $trace->output('re', 1);
487 $trace->output('mi', 2);
488 $trace->finished();
489 $this->assertSame("<p>do</p>\n<p>&#160;&#160;re</p>\n<p>&#160;&#160;&#160;&#160;mi</p>\n", $trace->get_buffer());
490 $this->assertSame("<p>do</p>\n<p>&#160;&#160;re</p>\n<p>&#160;&#160;&#160;&#160;mi</p>\n", $trace->get_buffer());
491 $trace->reset_buffer();
492 $this->assertSame('', $trace->get_buffer());
493 $this->expectOutputString('');
496 public function test_combined_progres_trace() {
497 $this->resetAfterTest(false);
499 $trace1 = new progress_trace_buffer(new html_progress_trace(), false);
500 $trace2 = new progress_trace_buffer(new text_progress_trace(), false);
502 $trace = new combined_progress_trace(array($trace1, $trace2));
503 $trace->output('do');
504 $trace->output('re', 1);
505 $trace->output('mi', 2);
506 $trace->finished();
507 $this->assertSame("<p>do</p>\n<p>&#160;&#160;re</p>\n<p>&#160;&#160;&#160;&#160;mi</p>\n", $trace1->get_buffer());
508 $this->assertSame("do\n re\n mi\n", $trace2->get_buffer());
509 $this->expectOutputString('');
512 public function test_set_debugging() {
513 global $CFG;
515 $this->resetAfterTest();
517 $this->assertEquals(DEBUG_DEVELOPER, $CFG->debug);
518 $this->assertTrue($CFG->debugdeveloper);
519 $this->assertNotEmpty($CFG->debugdisplay);
521 set_debugging(DEBUG_DEVELOPER, true);
522 $this->assertEquals(DEBUG_DEVELOPER, $CFG->debug);
523 $this->assertTrue($CFG->debugdeveloper);
524 $this->assertNotEmpty($CFG->debugdisplay);
526 set_debugging(DEBUG_DEVELOPER, false);
527 $this->assertEquals(DEBUG_DEVELOPER, $CFG->debug);
528 $this->assertTrue($CFG->debugdeveloper);
529 $this->assertEmpty($CFG->debugdisplay);
531 set_debugging(-1);
532 $this->assertEquals(-1, $CFG->debug);
533 $this->assertTrue($CFG->debugdeveloper);
535 set_debugging(DEBUG_ALL);
536 $this->assertEquals(DEBUG_ALL, $CFG->debug);
537 $this->assertFalse($CFG->debugdeveloper);
539 set_debugging(DEBUG_NORMAL);
540 $this->assertEquals(DEBUG_NORMAL, $CFG->debug);
541 $this->assertFalse($CFG->debugdeveloper);
543 set_debugging(DEBUG_MINIMAL);
544 $this->assertEquals(DEBUG_MINIMAL, $CFG->debug);
545 $this->assertFalse($CFG->debugdeveloper);
547 set_debugging(DEBUG_NONE);
548 $this->assertEquals(DEBUG_NONE, $CFG->debug);
549 $this->assertFalse($CFG->debugdeveloper);
552 public function test_strip_pluginfile_content() {
553 $source = <<<SOURCE
554 Hello!
556 I'm writing to you from the Moodle Majlis in Muscat, Oman, where we just had several days of Moodle community goodness.
558 URL outside a tag: https://moodle.org/logo/logo-240x60.gif
559 Plugin url outside a tag: @@PLUGINFILE@@/logo-240x60.gif
561 External link 1:<img src='https://moodle.org/logo/logo-240x60.gif' alt='Moodle'/>
562 External link 2:<img alt="Moodle" src="https://moodle.org/logo/logo-240x60.gif"/>
563 Internal link 1:<img src='@@PLUGINFILE@@/logo-240x60.gif' alt='Moodle'/>
564 Internal link 2:<img alt="Moodle" src="@@PLUGINFILE@@logo-240x60.gif"/>
565 Anchor link 1:<a href="@@PLUGINFILE@@logo-240x60.gif" alt="bananas">Link text</a>
566 Anchor link 2:<a title="bananas" href="../logo-240x60.gif">Link text</a>
567 Anchor + ext. img:<a title="bananas" href="../logo-240x60.gif"><img alt="Moodle" src="@@PLUGINFILE@@logo-240x60.gif"/></a>
568 Ext. anchor + img:<a href="@@PLUGINFILE@@logo-240x60.gif"><img alt="Moodle" src="https://moodle.org/logo/logo-240x60.gif"/></a>
569 SOURCE;
570 $expected = <<<EXPECTED
571 Hello!
573 I'm writing to you from the Moodle Majlis in Muscat, Oman, where we just had several days of Moodle community goodness.
575 URL outside a tag: https://moodle.org/logo/logo-240x60.gif
576 Plugin url outside a tag: @@PLUGINFILE@@/logo-240x60.gif
578 External link 1:<img src="https://moodle.org/logo/logo-240x60.gif" alt="Moodle" />
579 External link 2:<img alt="Moodle" src="https://moodle.org/logo/logo-240x60.gif" />
580 Internal link 1:
581 Internal link 2:
582 Anchor link 1:Link text
583 Anchor link 2:<a title="bananas" href="../logo-240x60.gif">Link text</a>
584 Anchor + ext. img:<a title="bananas" href="../logo-240x60.gif"></a>
585 Ext. anchor + img:<img alt="Moodle" src="https://moodle.org/logo/logo-240x60.gif" />
586 EXPECTED;
587 $this->assertSame($expected, strip_pluginfile_content($source));
590 public function test_purify_html_ruby() {
592 $this->resetAfterTest();
594 $ruby =
595 "<p><ruby><rb>京都</rb><rp>(</rp><rt>きょうと</rt><rp>)</rp></ruby>は" .
596 "<ruby><rb>日本</rb><rp>(</rp><rt>にほん</rt><rp>)</rp></ruby>の" .
597 "<ruby><rb>都</rb><rp>(</rp><rt>みやこ</rt><rp>)</rp></ruby>です。</p>";
598 $illegal = '<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>';
600 $cleaned = purify_html($ruby . $illegal);
601 $this->assertEquals($ruby, $cleaned);
606 * Tests for content_to_text.
608 * @param string $content The content
609 * @param int|false $format The content format
610 * @param string $expected Expected value
611 * @dataProvider provider_content_to_text
613 public function test_content_to_text($content, $format, $expected) {
614 $content = content_to_text($content, $format);
615 $this->assertEquals($expected, $content);
619 * Data provider for test_content_to_text.
621 public static function provider_content_to_text() {
622 return array(
623 array('asd', false, 'asd'),
624 // Trim '\r\n '.
625 array("Note that:\n\n3 > 1 ", FORMAT_PLAIN, "Note that:\n\n3 > 1"),
626 array("Note that:\n\n3 > 1\r\n", FORMAT_PLAIN, "Note that:\n\n3 > 1"),
627 // Multiple spaces to one.
628 array('<span class="eheh">京都</span> -> hehe', FORMAT_HTML, '京都 -> hehe'),
629 array('<span class="eheh">京都</span> -> hehe', false, '京都 -> hehe'),
630 array('asd asd', false, 'asd asd'),
631 // From markdown to html and html to text.
632 array('asd __lera__ con la', FORMAT_MARKDOWN, 'asd LERA con la'),
633 // HTML to text.
634 array('<p class="frogs">This is a <strong class=\'fishes\'>test</strong></p>', FORMAT_HTML, 'This is a TEST'),
635 array("<span lang='en' class='multilang'>english</span>
636 <span lang='ca' class='multilang'>català</span>
637 <span lang='es' class='multilang'>español</span>
638 <span lang='fr' class='multilang'>français</span>", FORMAT_HTML, "english català español français")