MDL-51718 weblib: Allow moodle_url scheme to be set
[moodle.git] / lib / tests / weblib_test.php
CommitLineData
a3d5830a
PS
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
a9d2f1b4 18 * Weblib tests.
a3d5830a
PS
19 *
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
25 */
26
27defined('MOODLE_INTERNAL') || die();
28
29
b33bf039 30class core_weblib_testcase extends advanced_testcase {
a3d5830a 31
b33bf039 32 public function test_format_string() {
a3d5830a
PS
33 global $CFG;
34
b33bf039
PS
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));
a3d5830a 40
b33bf039
PS
41 // String entities.
42 $this->assertSame("&quot;", format_string("&quot;"));
a3d5830a 43
b33bf039
PS
44 // Digital entities.
45 $this->assertSame("&11234;", format_string("&11234;"));
a3d5830a 46
b33bf039
PS
47 // Unicode entities.
48 $this->assertSame("&#4475;", format_string("&#4475;"));
a3d5830a 49
b33bf039 50 // < and > signs.
a3d5830a
PS
51 $originalformatstringstriptags = $CFG->formatstringstriptags;
52
53 $CFG->formatstringstriptags = false;
b33bf039
PS
54 $this->assertSame('x &lt; 1', format_string('x < 1'));
55 $this->assertSame('x &gt; 1', format_string('x > 1'));
56 $this->assertSame('x &lt; 1 and x &gt; 0', format_string('x < 1 and x > 0'));
a3d5830a
PS
57
58 $CFG->formatstringstriptags = true;
b33bf039
PS
59 $this->assertSame('x &lt; 1', format_string('x < 1'));
60 $this->assertSame('x &gt; 1', format_string('x > 1'));
61 $this->assertSame('x &lt; 1 and x &gt; 0', format_string('x < 1 and x > 0'));
a3d5830a
PS
62
63 $CFG->formatstringstriptags = $originalformatstringstriptags;
64 }
65
b33bf039 66 public function test_s() {
328ac306
TH
67 // Special cases.
68 $this->assertSame('0', s(0));
69 $this->assertSame('0', s('0'));
70 $this->assertSame('0', s(false));
71 $this->assertSame('', s(null));
72
73 // Normal cases.
b33bf039
PS
74 $this->assertSame('This Breaks &quot; Strict', s('This Breaks " Strict'));
75 $this->assertSame('This Breaks &lt;a&gt;&quot; Strict&lt;/a&gt;', s('This Breaks <a>" Strict</a>'));
328ac306
TH
76
77 // Unicode characters.
b33bf039
PS
78 $this->assertSame('Café', s('Café'));
79 $this->assertSame('一, 二, 三', s('一, 二, 三'));
328ac306
TH
80
81 // Don't escape already-escaped numeric entities. (Note, this behaviour
82 // may not be desirable. Perhaps we should remove these tests and that
83 // functionality, but we can only do that if we understand why it was added.)
b33bf039
PS
84 $this->assertSame('An entity: &#x09ff;.', s('An entity: &#x09ff;.'));
85 $this->assertSame('An entity: &#1073;.', s('An entity: &#1073;.'));
86 $this->assertSame('An entity: &amp;amp;.', s('An entity: &amp;.'));
87 $this->assertSame('Not an entity: &amp;amp;#x09ff;.', s('Not an entity: &amp;#x09ff;.'));
a3d5830a
PS
88 }
89
b33bf039
PS
90 public function test_format_text_email() {
91 $this->assertSame("This is a TEST",
92 format_text_email('<p>This is a <strong>test</strong></p>', FORMAT_HTML));
93 $this->assertSame("This is a TEST",
94 format_text_email('<p class="frogs">This is a <strong class=\'fishes\'>test</strong></p>', FORMAT_HTML));
95 $this->assertSame('& so is this',
96 format_text_email('&amp; so is this', FORMAT_HTML));
44250050 97 $this->assertSame('Two bullets: ' . core_text::code2utf8(8226) . ' ' . core_text::code2utf8(8226),
b33bf039
PS
98 format_text_email('Two bullets: &#x2022; &#8226;', FORMAT_HTML));
99 $this->assertSame(core_text::code2utf8(0x7fd2).core_text::code2utf8(0x7fd2),
100 format_text_email('&#x7fd2;&#x7FD2;', FORMAT_HTML));
a3d5830a
PS
101 }
102
b33bf039 103 public function test_obfuscate_email() {
66056f99
PS
104 $email = 'some.user@example.com';
105 $obfuscated = obfuscate_email($email);
106 $this->assertNotSame($email, $obfuscated);
b33bf039 107 $back = core_text::entities_to_utf8(urldecode($email), true);
66056f99
PS
108 $this->assertSame($email, $back);
109 }
110
b33bf039 111 public function test_obfuscate_text() {
66056f99
PS
112 $text = 'Žluťoučký koníček 32131';
113 $obfuscated = obfuscate_text($text);
114 $this->assertNotSame($text, $obfuscated);
b33bf039 115 $back = core_text::entities_to_utf8($obfuscated, true);
66056f99
PS
116 $this->assertSame($text, $back);
117 }
118
b33bf039 119 public function test_highlight() {
1fa2823d
TH
120 $this->assertSame('This is <span class="highlight">good</span>',
121 highlight('good', 'This is good'));
122
123 $this->assertSame('<span class="highlight">span</span>',
124 highlight('SpaN', 'span'));
125
126 $this->assertSame('<span class="highlight">SpaN</span>',
127 highlight('span', 'SpaN'));
128
129 $this->assertSame('<span><span class="highlight">span</span></span>',
130 highlight('span', '<span>span</span>'));
131
132 $this->assertSame('He <span class="highlight">is</span> <span class="highlight">good</span>',
133 highlight('good is', 'He is good'));
134
135 $this->assertSame('This is <span class="highlight">good</span>',
136 highlight('+good', 'This is good'));
137
138 $this->assertSame('This is good',
139 highlight('-good', 'This is good'));
140
141 $this->assertSame('This is goodness',
142 highlight('+good', 'This is goodness'));
143
144 $this->assertSame('This is <span class="highlight">good</span>ness',
145 highlight('good', 'This is goodness'));
146
147 $this->assertSame('<p><b>test</b> <b>1</b></p><p><b>1</b></p>',
148 highlight('test 1', '<p>test 1</p><p>1</p>', false, '<b>', '</b>'));
149
150 $this->assertSame('<p><b>test</b> <b>1</b></p><p><b>1</b></p>',
151 highlight('test +1', '<p>test 1</p><p>1</p>', false, '<b>', '</b>'));
152
153 $this->assertSame('<p><b>test</b> 1</p><p>1</p>',
154 highlight('test -1', '<p>test 1</p><p>1</p>', false, '<b>', '</b>'));
a3d5830a
PS
155 }
156
b33bf039
PS
157 public function test_replace_ampersands() {
158 $this->assertSame("This &amp; that &nbsp;", replace_ampersands_not_followed_by_entity("This & that &nbsp;"));
159 $this->assertSame("This &amp;nbsp that &nbsp;", replace_ampersands_not_followed_by_entity("This &nbsp that &nbsp;"));
a3d5830a
PS
160 }
161
b33bf039
PS
162 public function test_strip_links() {
163 $this->assertSame('this is a link', strip_links('this is a <a href="http://someaddress.com/query">link</a>'));
a3d5830a
PS
164 }
165
b33bf039
PS
166 public function test_wikify_links() {
167 $this->assertSame('this is a link [ http://someaddress.com/query ]', wikify_links('this is a <a href="http://someaddress.com/query">link</a>'));
a3d5830a
PS
168 }
169
61b223a6
SH
170 /**
171 * Test basic moodle_url construction.
172 */
b33bf039 173 public function test_moodle_url_constructor() {
61b223a6
SH
174 global $CFG;
175
176 $url = new moodle_url('/index.php');
b33bf039 177 $this->assertSame($CFG->wwwroot.'/index.php', $url->out());
61b223a6
SH
178
179 $url = new moodle_url('/index.php', array());
b33bf039 180 $this->assertSame($CFG->wwwroot.'/index.php', $url->out());
61b223a6
SH
181
182 $url = new moodle_url('/index.php', array('id' => 2));
b33bf039 183 $this->assertSame($CFG->wwwroot.'/index.php?id=2', $url->out());
61b223a6
SH
184
185 $url = new moodle_url('/index.php', array('id' => 'two'));
b33bf039 186 $this->assertSame($CFG->wwwroot.'/index.php?id=two', $url->out());
61b223a6
SH
187
188 $url = new moodle_url('/index.php', array('id' => 1, 'cid' => '2'));
b33bf039
PS
189 $this->assertSame($CFG->wwwroot.'/index.php?id=1&amp;cid=2', $url->out());
190 $this->assertSame($CFG->wwwroot.'/index.php?id=1&cid=2', $url->out(false));
61b223a6
SH
191
192 $url = new moodle_url('/index.php', null, 'test');
b33bf039 193 $this->assertSame($CFG->wwwroot.'/index.php#test', $url->out());
61b223a6
SH
194
195 $url = new moodle_url('/index.php', array('id' => 2), 'test');
b33bf039 196 $this->assertSame($CFG->wwwroot.'/index.php?id=2#test', $url->out());
61b223a6
SH
197 }
198
ffe4de97 199 /**
200 * Tests moodle_url::get_path().
201 */
202 public function test_moodle_url_get_path() {
203 $url = new moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1');
b33bf039 204 $this->assertSame('/my/file/is/here.txt', $url->get_path());
ffe4de97 205
206 $url = new moodle_url('http://www.example.org/');
b33bf039 207 $this->assertSame('/', $url->get_path());
ffe4de97 208
209 $url = new moodle_url('http://www.example.org/pluginfile.php/slash/arguments');
b33bf039
PS
210 $this->assertSame('/pluginfile.php/slash/arguments', $url->get_path());
211 $this->assertSame('/pluginfile.php', $url->get_path(false));
ffe4de97 212 }
213
b33bf039 214 public function test_moodle_url_round_trip() {
a3d5830a
PS
215 $strurl = 'http://moodle.org/course/view.php?id=5';
216 $url = new moodle_url($strurl);
b33bf039 217 $this->assertSame($strurl, $url->out(false));
a3d5830a
PS
218
219 $strurl = 'http://moodle.org/user/index.php?contextid=53&sifirst=M&silast=D';
220 $url = new moodle_url($strurl);
b33bf039 221 $this->assertSame($strurl, $url->out(false));
a3d5830a
PS
222 }
223
616396a6
SH
224 /**
225 * Test Moodle URL objects created with a param with empty value.
226 */
b33bf039 227 public function test_moodle_url_empty_param_values() {
616396a6
SH
228 $strurl = 'http://moodle.org/course/view.php?id=0';
229 $url = new moodle_url($strurl, array('id' => 0));
b33bf039 230 $this->assertSame($strurl, $url->out(false));
616396a6 231
45d97522 232 $strurl = 'http://moodle.org/course/view.php?id';
616396a6 233 $url = new moodle_url($strurl, array('id' => false));
b33bf039 234 $this->assertSame($strurl, $url->out(false));
616396a6 235
45d97522 236 $strurl = 'http://moodle.org/course/view.php?id';
616396a6 237 $url = new moodle_url($strurl, array('id' => null));
b33bf039 238 $this->assertSame($strurl, $url->out(false));
616396a6 239
a697000b
SH
240 $strurl = 'http://moodle.org/course/view.php?id';
241 $url = new moodle_url($strurl, array('id' => ''));
b33bf039 242 $this->assertSame($strurl, $url->out(false));
a697000b 243
45d97522 244 $strurl = 'http://moodle.org/course/view.php?id';
616396a6 245 $url = new moodle_url($strurl);
b33bf039 246 $this->assertSame($strurl, $url->out(false));
616396a6
SH
247 }
248
462065e8
BH
249 /**
250 * Test set good scheme on Moodle URL objects.
251 */
252 public function test_moodle_url_set_good_scheme() {
253 $url = new moodle_url('http://moodle.org/foo/bar');
254 $url->set_scheme('myscheme');
255 $this->assertSame('myscheme://moodle.org/foo/bar', $url->out());
256 }
257
258 /**
259 * Test set bad scheme on Moodle URL objects.
260 */
261 public function test_moodle_url_set_bad_scheme() {
262 $url = new moodle_url('http://moodle.org/foo/bar');
263 $this->setExpectedException('coding_exception');
264 $url->set_scheme('not a valid $ scheme');
265 }
266
b33bf039 267 public function test_moodle_url_round_trip_array_params() {
a3d5830a
PS
268 $strurl = 'http://example.com/?a%5B1%5D=1&a%5B2%5D=2';
269 $url = new moodle_url($strurl);
b33bf039 270 $this->assertSame($strurl, $url->out(false));
a3d5830a
PS
271
272 $url = new moodle_url('http://example.com/?a[1]=1&a[2]=2');
b33bf039 273 $this->assertSame($strurl, $url->out(false));
a3d5830a 274
b33bf039 275 // For un-keyed array params, we expect 0..n keys to be returned.
a3d5830a
PS
276 $strurl = 'http://example.com/?a%5B0%5D=0&a%5B1%5D=1';
277 $url = new moodle_url('http://example.com/?a[]=0&a[]=1');
b33bf039 278 $this->assertSame($strurl, $url->out(false));
a3d5830a
PS
279 }
280
b33bf039 281 public function test_compare_url() {
a3d5830a
PS
282 $url1 = new moodle_url('index.php', array('var1' => 1, 'var2' => 2));
283 $url2 = new moodle_url('index2.php', array('var1' => 1, 'var2' => 2, 'var3' => 3));
284
285 $this->assertFalse($url1->compare($url2, URL_MATCH_BASE));
286 $this->assertFalse($url1->compare($url2, URL_MATCH_PARAMS));
287 $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT));
288
289 $url2 = new moodle_url('index.php', array('var1' => 1, 'var3' => 3));
290
291 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE));
292 $this->assertFalse($url1->compare($url2, URL_MATCH_PARAMS));
293 $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT));
294
295 $url2 = new moodle_url('index.php', array('var1' => 1, 'var2' => 2, 'var3' => 3));
296
297 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE));
298 $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS));
299 $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT));
300
301 $url2 = new moodle_url('index.php', array('var2' => 2, 'var1' => 1));
302
303 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE));
304 $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS));
305 $this->assertTrue($url1->compare($url2, URL_MATCH_EXACT));
99151092
SH
306
307 $url1->set_anchor('test');
308 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE));
309 $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS));
310 $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT));
311
312 $url2->set_anchor('test');
313 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE));
314 $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS));
315 $this->assertTrue($url1->compare($url2, URL_MATCH_EXACT));
a3d5830a
PS
316 }
317
b33bf039 318 public function test_out_as_local_url() {
72b181ba
RT
319 global $CFG;
320 // Test http url.
a3d5830a 321 $url1 = new moodle_url('/lib/tests/weblib_test.php');
b33bf039 322 $this->assertSame('/lib/tests/weblib_test.php', $url1->out_as_local_url());
72b181ba
RT
323
324 // Test https url.
325 $httpswwwroot = str_replace("http://", "https://", $CFG->wwwroot);
326 $url2 = new moodle_url($httpswwwroot.'/login/profile.php');
b33bf039 327 $this->assertSame('/login/profile.php', $url2->out_as_local_url());
72b181ba
RT
328
329 // Test http url matching wwwroot.
330 $url3 = new moodle_url($CFG->wwwroot);
b33bf039 331 $this->assertSame('', $url3->out_as_local_url());
72b181ba
RT
332
333 // Test http url matching wwwroot ending with slash (/).
334 $url3 = new moodle_url($CFG->wwwroot.'/');
b33bf039 335 $this->assertSame('/', $url3->out_as_local_url());
a3d5830a
PS
336 }
337
338 /**
339 * @expectedException coding_exception
340 * @return void
341 */
b33bf039 342 public function test_out_as_local_url_error() {
4ca04fb5 343 $url2 = new moodle_url('http://www.google.com/lib/tests/weblib_test.php');
a3d5830a
PS
344 $url2->out_as_local_url();
345 }
346
72b181ba
RT
347 /**
348 * You should get error with modified url
349 *
350 * @expectedException coding_exception
351 * @return void
352 */
353 public function test_modified_url_out_as_local_url_error() {
354 global $CFG;
355
356 $modifiedurl = $CFG->wwwroot.'1';
357 $url3 = new moodle_url($modifiedurl.'/login/profile.php');
358 $url3->out_as_local_url();
359 }
360
361 /**
362 * Try get local url from external https url and you should get error
363 *
364 * @expectedException coding_exception
72b181ba
RT
365 */
366 public function test_https_out_as_local_url_error() {
367 $url4 = new moodle_url('https://www.google.com/lib/tests/weblib_test.php');
368 $url4->out_as_local_url();
369 }
370
d06ffbdf
SC
371 public function test_moodle_url_get_scheme() {
372 // Should return the scheme only.
373 $url = new moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1');
374 $this->assertSame('http', $url->get_scheme());
375
376 // Should work for secure URLs.
377 $url = new moodle_url('https://www.example.org:447/my/file/is/here.txt?really=1');
378 $this->assertSame('https', $url->get_scheme());
379
380 // Should return an empty string if no scheme is specified.
381 $url = new moodle_url('www.example.org:447/my/file/is/here.txt?really=1');
382 $this->assertSame('', $url->get_scheme());
383 }
384
385 public function test_moodle_url_get_host() {
386 // Should return the host part only.
387 $url = new moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1');
388 $this->assertSame('www.example.org', $url->get_host());
389 }
390
391 public function test_moodle_url_get_port() {
392 // Should return the port if one provided.
393 $url = new moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1');
394 $this->assertSame(447, $url->get_port());
395
396 // Should return an empty string if port not specified.
397 $url = new moodle_url('http://www.example.org/some/path/here.php');
398 $this->assertSame('', $url->get_port());
399 }
400
a3d5830a
PS
401 public function test_clean_text() {
402 $text = "lala <applet>xx</applet>";
b33bf039
PS
403 $this->assertSame($text, clean_text($text, FORMAT_PLAIN));
404 $this->assertSame('lala xx', clean_text($text, FORMAT_MARKDOWN));
405 $this->assertSame('lala xx', clean_text($text, FORMAT_MOODLE));
406 $this->assertSame('lala xx', clean_text($text, FORMAT_HTML));
a3d5830a 407 }
f0202ae9 408
bd319d12 409 public function test_qualified_me() {
f0202ae9
PS
410 global $PAGE, $FULLME, $CFG;
411 $this->resetAfterTest();
412
413 $PAGE = new moodle_page();
414
415 $FULLME = $CFG->wwwroot.'/course/view.php?id=1&xx=yy';
b33bf039 416 $this->assertSame($FULLME, qualified_me());
f0202ae9
PS
417
418 $PAGE->set_url('/course/view.php', array('id'=>1));
b33bf039 419 $this->assertSame($CFG->wwwroot.'/course/view.php?id=1', qualified_me());
f0202ae9 420 }
837e3042
PS
421
422 public function test_null_progres_trace() {
423 $this->resetAfterTest(false);
424
425 $trace = new null_progress_trace();
837e3042
PS
426 $trace->output('do');
427 $trace->output('re', 1);
428 $trace->output('mi', 2);
429 $trace->finished();
430 $output = ob_get_contents();
837e3042 431 $this->assertSame('', $output);
4fa7fdea 432 $this->expectOutputString('');
837e3042
PS
433 }
434
435 public function test_text_progres_trace() {
436 $this->resetAfterTest(false);
437
438 $trace = new text_progress_trace();
837e3042
PS
439 $trace->output('do');
440 $trace->output('re', 1);
441 $trace->output('mi', 2);
442 $trace->finished();
4fa7fdea 443 $this->expectOutputString("do\n re\n mi\n");
837e3042
PS
444 }
445
446 public function test_html_progres_trace() {
447 $this->resetAfterTest(false);
448
449 $trace = new html_progress_trace();
837e3042
PS
450 $trace->output('do');
451 $trace->output('re', 1);
452 $trace->output('mi', 2);
453 $trace->finished();
4fa7fdea 454 $this->expectOutputString("<p>do</p>\n<p>&#160;&#160;re</p>\n<p>&#160;&#160;&#160;&#160;mi</p>\n");
837e3042
PS
455 }
456
457 public function test_html_list_progress_trace() {
458 $this->resetAfterTest(false);
459
460 $trace = new html_list_progress_trace();
837e3042
PS
461 $trace->output('do');
462 $trace->output('re', 1);
463 $trace->output('mi', 2);
464 $trace->finished();
4fa7fdea 465 $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");
837e3042
PS
466 }
467
468 public function test_progres_trace_buffer() {
469 $this->resetAfterTest(false);
470
471 $trace = new progress_trace_buffer(new html_progress_trace());
472 ob_start();
473 $trace->output('do');
474 $trace->output('re', 1);
475 $trace->output('mi', 2);
476 $trace->finished();
477 $output = ob_get_contents();
478 ob_end_clean();
479 $this->assertSame("<p>do</p>\n<p>&#160;&#160;re</p>\n<p>&#160;&#160;&#160;&#160;mi</p>\n", $output);
480 $this->assertSame($output, $trace->get_buffer());
481
482 $trace = new progress_trace_buffer(new html_progress_trace(), false);
837e3042
PS
483 $trace->output('do');
484 $trace->output('re', 1);
485 $trace->output('mi', 2);
486 $trace->finished();
837e3042
PS
487 $this->assertSame("<p>do</p>\n<p>&#160;&#160;re</p>\n<p>&#160;&#160;&#160;&#160;mi</p>\n", $trace->get_buffer());
488 $this->assertSame("<p>do</p>\n<p>&#160;&#160;re</p>\n<p>&#160;&#160;&#160;&#160;mi</p>\n", $trace->get_buffer());
489 $trace->reset_buffer();
490 $this->assertSame('', $trace->get_buffer());
4fa7fdea 491 $this->expectOutputString('');
837e3042 492 }
e7259ec9
PS
493
494 public function test_combined_progres_trace() {
495 $this->resetAfterTest(false);
496
497 $trace1 = new progress_trace_buffer(new html_progress_trace(), false);
498 $trace2 = new progress_trace_buffer(new text_progress_trace(), false);
499
500 $trace = new combined_progress_trace(array($trace1, $trace2));
e7259ec9
PS
501 $trace->output('do');
502 $trace->output('re', 1);
503 $trace->output('mi', 2);
504 $trace->finished();
e7259ec9
PS
505 $this->assertSame("<p>do</p>\n<p>&#160;&#160;re</p>\n<p>&#160;&#160;&#160;&#160;mi</p>\n", $trace1->get_buffer());
506 $this->assertSame("do\n re\n mi\n", $trace2->get_buffer());
4fa7fdea 507 $this->expectOutputString('');
e7259ec9 508 }
037d673e
PS
509
510 public function test_set_debugging() {
511 global $CFG;
512
513 $this->resetAfterTest();
514
515 $this->assertEquals(DEBUG_DEVELOPER, $CFG->debug);
516 $this->assertTrue($CFG->debugdeveloper);
517 $this->assertNotEmpty($CFG->debugdisplay);
518
519 set_debugging(DEBUG_DEVELOPER, true);
520 $this->assertEquals(DEBUG_DEVELOPER, $CFG->debug);
521 $this->assertTrue($CFG->debugdeveloper);
522 $this->assertNotEmpty($CFG->debugdisplay);
523
524 set_debugging(DEBUG_DEVELOPER, false);
525 $this->assertEquals(DEBUG_DEVELOPER, $CFG->debug);
526 $this->assertTrue($CFG->debugdeveloper);
527 $this->assertEmpty($CFG->debugdisplay);
528
529 set_debugging(-1);
530 $this->assertEquals(-1, $CFG->debug);
531 $this->assertTrue($CFG->debugdeveloper);
532
533 set_debugging(DEBUG_ALL);
534 $this->assertEquals(DEBUG_ALL, $CFG->debug);
535 $this->assertFalse($CFG->debugdeveloper);
536
537 set_debugging(DEBUG_NORMAL);
538 $this->assertEquals(DEBUG_NORMAL, $CFG->debug);
539 $this->assertFalse($CFG->debugdeveloper);
540
541 set_debugging(DEBUG_MINIMAL);
542 $this->assertEquals(DEBUG_MINIMAL, $CFG->debug);
543 $this->assertFalse($CFG->debugdeveloper);
544
545 set_debugging(DEBUG_NONE);
546 $this->assertEquals(DEBUG_NONE, $CFG->debug);
547 $this->assertFalse($CFG->debugdeveloper);
548 }
0a8b3583
FM
549
550 public function test_strip_pluginfile_content() {
551 $source = <<<SOURCE
552Hello!
553
554I'm writing to you from the Moodle Majlis in Muscat, Oman, where we just had several days of Moodle community goodness.
555
556URL outside a tag: https://moodle.org/logo/logo-240x60.gif
557Plugin url outside a tag: @@PLUGINFILE@@/logo-240x60.gif
558
559External link 1:<img src='https://moodle.org/logo/logo-240x60.gif' alt='Moodle'/>
560External link 2:<img alt="Moodle" src="https://moodle.org/logo/logo-240x60.gif"/>
561Internal link 1:<img src='@@PLUGINFILE@@/logo-240x60.gif' alt='Moodle'/>
562Internal link 2:<img alt="Moodle" src="@@PLUGINFILE@@logo-240x60.gif"/>
563Anchor link 1:<a href="@@PLUGINFILE@@logo-240x60.gif" alt="bananas">Link text</a>
564Anchor link 2:<a title="bananas" href="../logo-240x60.gif">Link text</a>
565Anchor + ext. img:<a title="bananas" href="../logo-240x60.gif"><img alt="Moodle" src="@@PLUGINFILE@@logo-240x60.gif"/></a>
566Ext. anchor + img:<a href="@@PLUGINFILE@@logo-240x60.gif"><img alt="Moodle" src="https://moodle.org/logo/logo-240x60.gif"/></a>
567SOURCE;
568 $expected = <<<EXPECTED
569Hello!
570
571I'm writing to you from the Moodle Majlis in Muscat, Oman, where we just had several days of Moodle community goodness.
572
573URL outside a tag: https://moodle.org/logo/logo-240x60.gif
574Plugin url outside a tag: @@PLUGINFILE@@/logo-240x60.gif
575
576External link 1:<img src="https://moodle.org/logo/logo-240x60.gif" alt="Moodle" />
577External link 2:<img alt="Moodle" src="https://moodle.org/logo/logo-240x60.gif" />
578Internal link 1:
579Internal link 2:
580Anchor link 1:Link text
581Anchor link 2:<a title="bananas" href="../logo-240x60.gif">Link text</a>
582Anchor + ext. img:<a title="bananas" href="../logo-240x60.gif"></a>
583Ext. anchor + img:<img alt="Moodle" src="https://moodle.org/logo/logo-240x60.gif" />
584EXPECTED;
585 $this->assertSame($expected, strip_pluginfile_content($source));
586 }
587
b42cb679
JC
588 public function test_purify_html_ruby() {
589
590 $this->resetAfterTest();
591
592 $ruby =
593 "<p><ruby><rb>京都</rb><rp>(</rp><rt>きょうと</rt><rp>)</rp></ruby>は" .
594 "<ruby><rb>日本</rb><rp>(</rp><rt>にほん</rt><rp>)</rp></ruby>の" .
595 "<ruby><rb>都</rb><rp>(</rp><rt>みやこ</rt><rp>)</rp></ruby>です。</p>";
596 $illegal = '<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>';
597
598 $cleaned = purify_html($ruby . $illegal);
599 $this->assertEquals($ruby, $cleaned);
600
601 }
602
a3d5830a 603}