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));
42 $this->assertSame(""", format_string("""));
45 $this->assertSame("&11234;", format_string("&11234;"));
48 $this->assertSame("ᅻ", format_string("ᅻ"));
51 $originalformatstringstriptags = $CFG->formatstringstriptags
;
53 $CFG->formatstringstriptags
= false;
54 $this->assertSame('x < 1', format_string('x < 1'));
55 $this->assertSame('x > 1', format_string('x > 1'));
56 $this->assertSame('x < 1 and x > 0', format_string('x < 1 and x > 0'));
58 $CFG->formatstringstriptags
= true;
59 $this->assertSame('x < 1', format_string('x < 1'));
60 $this->assertSame('x > 1', format_string('x > 1'));
61 $this->assertSame('x < 1 and x > 0', format_string('x < 1 and x > 0'));
63 $CFG->formatstringstriptags
= $originalformatstringstriptags;
66 public function test_s() {
68 $this->assertSame('0', s(0));
69 $this->assertSame('0', s('0'));
70 $this->assertSame('0', s(false));
71 $this->assertSame('', s(null));
74 $this->assertSame('This Breaks " Strict', s('This Breaks " Strict'));
75 $this->assertSame('This Breaks <a>" Strict</a>', s('This Breaks <a>" Strict</a>'));
77 // Unicode characters.
78 $this->assertSame('Café', s('Café'));
79 $this->assertSame('一, 二, 三', s('一, 二, 三'));
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.)
84 $this->assertSame('An entity: ৿.', s('An entity: ৿.'));
85 $this->assertSame('An entity: б.', s('An entity: б.'));
86 $this->assertSame('An entity: &amp;.', s('An entity: &.'));
87 $this->assertSame('Not an entity: &amp;#x09ff;.', s('Not an entity: &#x09ff;.'));
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('& so is this', FORMAT_HTML
));
97 $this->assertSame('Two bullets: '.core_text
::code2utf8(8226).' *',
98 format_text_email('Two bullets: • •', FORMAT_HTML
));
99 $this->assertSame(core_text
::code2utf8(0x7fd2).core_text
::code2utf8(0x7fd2),
100 format_text_email('習習', FORMAT_HTML
));
103 public function test_obfuscate_email() {
104 $email = 'some.user@example.com';
105 $obfuscated = obfuscate_email($email);
106 $this->assertNotSame($email, $obfuscated);
107 $back = core_text
::entities_to_utf8(urldecode($email), true);
108 $this->assertSame($email, $back);
111 public function test_obfuscate_text() {
112 $text = 'Žluťoučký koníček 32131';
113 $obfuscated = obfuscate_text($text);
114 $this->assertNotSame($text, $obfuscated);
115 $back = core_text
::entities_to_utf8($obfuscated, true);
116 $this->assertSame($text, $back);
119 public function test_highlight() {
120 $this->assertSame('This is <span class="highlight">good</span>', highlight('good', 'This is good'));
121 $this->assertSame('<span class="highlight">span</span>', highlight('SpaN', 'span'));
122 $this->assertSame('<span class="highlight">SpaN</span>', highlight('span', 'SpaN'));
123 $this->assertSame('<span><span class="highlight">span</span></span>', highlight('span', '<span>span</span>'));
124 $this->assertSame('He <span class="highlight">is</span> <span class="highlight">good</span>', highlight('good is', 'He is good'));
125 $this->assertSame('This is <span class="highlight">good</span>', highlight('+good', 'This is good'));
126 $this->assertSame('This is good', highlight('-good', 'This is good'));
127 $this->assertSame('This is goodness', highlight('+good', 'This is goodness'));
128 $this->assertSame('This is <span class="highlight">good</span>ness', highlight('good', 'This is goodness'));
131 public function test_replace_ampersands() {
132 $this->assertSame("This & that ", replace_ampersands_not_followed_by_entity("This & that "));
133 $this->assertSame("This &nbsp that ", replace_ampersands_not_followed_by_entity("This   that "));
136 public function test_strip_links() {
137 $this->assertSame('this is a link', strip_links('this is a <a href="http://someaddress.com/query">link</a>'));
140 public function test_wikify_links() {
141 $this->assertSame('this is a link [ http://someaddress.com/query ]', wikify_links('this is a <a href="http://someaddress.com/query">link</a>'));
145 * Test basic moodle_url construction.
147 public function test_moodle_url_constructor() {
150 $url = new moodle_url('/index.php');
151 $this->assertSame($CFG->wwwroot
.'/index.php', $url->out());
153 $url = new moodle_url('/index.php', array());
154 $this->assertSame($CFG->wwwroot
.'/index.php', $url->out());
156 $url = new moodle_url('/index.php', array('id' => 2));
157 $this->assertSame($CFG->wwwroot
.'/index.php?id=2', $url->out());
159 $url = new moodle_url('/index.php', array('id' => 'two'));
160 $this->assertSame($CFG->wwwroot
.'/index.php?id=two', $url->out());
162 $url = new moodle_url('/index.php', array('id' => 1, 'cid' => '2'));
163 $this->assertSame($CFG->wwwroot
.'/index.php?id=1&cid=2', $url->out());
164 $this->assertSame($CFG->wwwroot
.'/index.php?id=1&cid=2', $url->out(false));
166 $url = new moodle_url('/index.php', null, 'test');
167 $this->assertSame($CFG->wwwroot
.'/index.php#test', $url->out());
169 $url = new moodle_url('/index.php', array('id' => 2), 'test');
170 $this->assertSame($CFG->wwwroot
.'/index.php?id=2#test', $url->out());
174 * Tests moodle_url::get_path().
176 public function test_moodle_url_get_path() {
177 $url = new moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1');
178 $this->assertSame('/my/file/is/here.txt', $url->get_path());
180 $url = new moodle_url('http://www.example.org/');
181 $this->assertSame('/', $url->get_path());
183 $url = new moodle_url('http://www.example.org/pluginfile.php/slash/arguments');
184 $this->assertSame('/pluginfile.php/slash/arguments', $url->get_path());
185 $this->assertSame('/pluginfile.php', $url->get_path(false));
188 public function test_moodle_url_round_trip() {
189 $strurl = 'http://moodle.org/course/view.php?id=5';
190 $url = new moodle_url($strurl);
191 $this->assertSame($strurl, $url->out(false));
193 $strurl = 'http://moodle.org/user/index.php?contextid=53&sifirst=M&silast=D';
194 $url = new moodle_url($strurl);
195 $this->assertSame($strurl, $url->out(false));
199 * Test Moodle URL objects created with a param with empty value.
201 public function test_moodle_url_empty_param_values() {
202 $strurl = 'http://moodle.org/course/view.php?id=0';
203 $url = new moodle_url($strurl, array('id' => 0));
204 $this->assertSame($strurl, $url->out(false));
206 $strurl = 'http://moodle.org/course/view.php?id';
207 $url = new moodle_url($strurl, array('id' => false));
208 $this->assertSame($strurl, $url->out(false));
210 $strurl = 'http://moodle.org/course/view.php?id';
211 $url = new moodle_url($strurl, array('id' => null));
212 $this->assertSame($strurl, $url->out(false));
214 $strurl = 'http://moodle.org/course/view.php?id';
215 $url = new moodle_url($strurl, array('id' => ''));
216 $this->assertSame($strurl, $url->out(false));
218 $strurl = 'http://moodle.org/course/view.php?id';
219 $url = new moodle_url($strurl);
220 $this->assertSame($strurl, $url->out(false));
223 public function test_moodle_url_round_trip_array_params() {
224 $strurl = 'http://example.com/?a%5B1%5D=1&a%5B2%5D=2';
225 $url = new moodle_url($strurl);
226 $this->assertSame($strurl, $url->out(false));
228 $url = new moodle_url('http://example.com/?a[1]=1&a[2]=2');
229 $this->assertSame($strurl, $url->out(false));
231 // For un-keyed array params, we expect 0..n keys to be returned.
232 $strurl = 'http://example.com/?a%5B0%5D=0&a%5B1%5D=1';
233 $url = new moodle_url('http://example.com/?a[]=0&a[]=1');
234 $this->assertSame($strurl, $url->out(false));
237 public function test_compare_url() {
238 $url1 = new moodle_url('index.php', array('var1' => 1, 'var2' => 2));
239 $url2 = new moodle_url('index2.php', array('var1' => 1, 'var2' => 2, 'var3' => 3));
241 $this->assertFalse($url1->compare($url2, URL_MATCH_BASE
));
242 $this->assertFalse($url1->compare($url2, URL_MATCH_PARAMS
));
243 $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT
));
245 $url2 = new moodle_url('index.php', array('var1' => 1, 'var3' => 3));
247 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE
));
248 $this->assertFalse($url1->compare($url2, URL_MATCH_PARAMS
));
249 $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT
));
251 $url2 = new moodle_url('index.php', array('var1' => 1, 'var2' => 2, 'var3' => 3));
253 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE
));
254 $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS
));
255 $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT
));
257 $url2 = new moodle_url('index.php', array('var2' => 2, 'var1' => 1));
259 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE
));
260 $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS
));
261 $this->assertTrue($url1->compare($url2, URL_MATCH_EXACT
));
264 public function test_out_as_local_url() {
267 $url1 = new moodle_url('/lib/tests/weblib_test.php');
268 $this->assertSame('/lib/tests/weblib_test.php', $url1->out_as_local_url());
271 $httpswwwroot = str_replace("http://", "https://", $CFG->wwwroot
);
272 $url2 = new moodle_url($httpswwwroot.'/login/profile.php');
273 $this->assertSame('/login/profile.php', $url2->out_as_local_url());
275 // Test http url matching wwwroot.
276 $url3 = new moodle_url($CFG->wwwroot
);
277 $this->assertSame('', $url3->out_as_local_url());
279 // Test http url matching wwwroot ending with slash (/).
280 $url3 = new moodle_url($CFG->wwwroot
.'/');
281 $this->assertSame('/', $url3->out_as_local_url());
285 * @expectedException coding_exception
288 public function test_out_as_local_url_error() {
289 $url2 = new moodle_url('http://www.google.com/lib/tests/weblib_test.php');
290 $url2->out_as_local_url();
294 * You should get error with modified url
296 * @expectedException coding_exception
299 public function test_modified_url_out_as_local_url_error() {
302 $modifiedurl = $CFG->wwwroot
.'1';
303 $url3 = new moodle_url($modifiedurl.'/login/profile.php');
304 $url3->out_as_local_url();
308 * Try get local url from external https url and you should get error
310 * @expectedException coding_exception
312 public function test_https_out_as_local_url_error() {
313 $url4 = new moodle_url('https://www.google.com/lib/tests/weblib_test.php');
314 $url4->out_as_local_url();
317 public function test_moodle_url_get_scheme() {
318 // Should return the scheme only.
319 $url = new moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1');
320 $this->assertSame('http', $url->get_scheme());
322 // Should work for secure URLs.
323 $url = new moodle_url('https://www.example.org:447/my/file/is/here.txt?really=1');
324 $this->assertSame('https', $url->get_scheme());
326 // Should return an empty string if no scheme is specified.
327 $url = new moodle_url('www.example.org:447/my/file/is/here.txt?really=1');
328 $this->assertSame('', $url->get_scheme());
331 public function test_moodle_url_get_host() {
332 // Should return the host part only.
333 $url = new moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1');
334 $this->assertSame('www.example.org', $url->get_host());
337 public function test_moodle_url_get_port() {
338 // Should return the port if one provided.
339 $url = new moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1');
340 $this->assertSame(447, $url->get_port());
342 // Should return an empty string if port not specified.
343 $url = new moodle_url('http://www.example.org/some/path/here.php');
344 $this->assertSame('', $url->get_port());
347 public function test_clean_text() {
348 $text = "lala <applet>xx</applet>";
349 $this->assertSame($text, clean_text($text, FORMAT_PLAIN
));
350 $this->assertSame('lala xx', clean_text($text, FORMAT_MARKDOWN
));
351 $this->assertSame('lala xx', clean_text($text, FORMAT_MOODLE
));
352 $this->assertSame('lala xx', clean_text($text, FORMAT_HTML
));
355 public function test_qualified_me() {
356 global $PAGE, $FULLME, $CFG;
357 $this->resetAfterTest();
359 $PAGE = new moodle_page();
361 $FULLME = $CFG->wwwroot
.'/course/view.php?id=1&xx=yy';
362 $this->assertSame($FULLME, qualified_me());
364 $PAGE->set_url('/course/view.php', array('id'=>1));
365 $this->assertSame($CFG->wwwroot
.'/course/view.php?id=1', qualified_me());
368 public function test_null_progres_trace() {
369 $this->resetAfterTest(false);
371 $trace = new null_progress_trace();
372 $trace->output('do');
373 $trace->output('re', 1);
374 $trace->output('mi', 2);
376 $output = ob_get_contents();
377 $this->assertSame('', $output);
378 $this->expectOutputString('');
381 public function test_text_progres_trace() {
382 $this->resetAfterTest(false);
384 $trace = new text_progress_trace();
385 $trace->output('do');
386 $trace->output('re', 1);
387 $trace->output('mi', 2);
389 $this->expectOutputString("do\n re\n mi\n");
392 public function test_html_progres_trace() {
393 $this->resetAfterTest(false);
395 $trace = new html_progress_trace();
396 $trace->output('do');
397 $trace->output('re', 1);
398 $trace->output('mi', 2);
400 $this->expectOutputString("<p>do</p>\n<p>  re</p>\n<p>    mi</p>\n");
403 public function test_html_list_progress_trace() {
404 $this->resetAfterTest(false);
406 $trace = new html_list_progress_trace();
407 $trace->output('do');
408 $trace->output('re', 1);
409 $trace->output('mi', 2);
411 $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");
414 public function test_progres_trace_buffer() {
415 $this->resetAfterTest(false);
417 $trace = new progress_trace_buffer(new html_progress_trace());
419 $trace->output('do');
420 $trace->output('re', 1);
421 $trace->output('mi', 2);
423 $output = ob_get_contents();
425 $this->assertSame("<p>do</p>\n<p>  re</p>\n<p>    mi</p>\n", $output);
426 $this->assertSame($output, $trace->get_buffer());
428 $trace = new progress_trace_buffer(new html_progress_trace(), false);
429 $trace->output('do');
430 $trace->output('re', 1);
431 $trace->output('mi', 2);
433 $this->assertSame("<p>do</p>\n<p>  re</p>\n<p>    mi</p>\n", $trace->get_buffer());
434 $this->assertSame("<p>do</p>\n<p>  re</p>\n<p>    mi</p>\n", $trace->get_buffer());
435 $trace->reset_buffer();
436 $this->assertSame('', $trace->get_buffer());
437 $this->expectOutputString('');
440 public function test_combined_progres_trace() {
441 $this->resetAfterTest(false);
443 $trace1 = new progress_trace_buffer(new html_progress_trace(), false);
444 $trace2 = new progress_trace_buffer(new text_progress_trace(), false);
446 $trace = new combined_progress_trace(array($trace1, $trace2));
447 $trace->output('do');
448 $trace->output('re', 1);
449 $trace->output('mi', 2);
451 $this->assertSame("<p>do</p>\n<p>  re</p>\n<p>    mi</p>\n", $trace1->get_buffer());
452 $this->assertSame("do\n re\n mi\n", $trace2->get_buffer());
453 $this->expectOutputString('');
456 public function test_set_debugging() {
459 $this->resetAfterTest();
461 $this->assertEquals(DEBUG_DEVELOPER
, $CFG->debug
);
462 $this->assertTrue($CFG->debugdeveloper
);
463 $this->assertNotEmpty($CFG->debugdisplay
);
465 set_debugging(DEBUG_DEVELOPER
, true);
466 $this->assertEquals(DEBUG_DEVELOPER
, $CFG->debug
);
467 $this->assertTrue($CFG->debugdeveloper
);
468 $this->assertNotEmpty($CFG->debugdisplay
);
470 set_debugging(DEBUG_DEVELOPER
, false);
471 $this->assertEquals(DEBUG_DEVELOPER
, $CFG->debug
);
472 $this->assertTrue($CFG->debugdeveloper
);
473 $this->assertEmpty($CFG->debugdisplay
);
476 $this->assertEquals(-1, $CFG->debug
);
477 $this->assertTrue($CFG->debugdeveloper
);
479 set_debugging(DEBUG_ALL
);
480 $this->assertEquals(DEBUG_ALL
, $CFG->debug
);
481 $this->assertFalse($CFG->debugdeveloper
);
483 set_debugging(DEBUG_NORMAL
);
484 $this->assertEquals(DEBUG_NORMAL
, $CFG->debug
);
485 $this->assertFalse($CFG->debugdeveloper
);
487 set_debugging(DEBUG_MINIMAL
);
488 $this->assertEquals(DEBUG_MINIMAL
, $CFG->debug
);
489 $this->assertFalse($CFG->debugdeveloper
);
491 set_debugging(DEBUG_NONE
);
492 $this->assertEquals(DEBUG_NONE
, $CFG->debug
);
493 $this->assertFalse($CFG->debugdeveloper
);