weekly release 2.6dev
[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/**
18 * These tests rely on the rsstest.xml file on download.moodle.org,
19 * from eloys listing:
20 * rsstest.xml: One valid rss feed.
21 * md5: 8fd047914863bf9b3a4b1514ec51c32c
22 * size: 32188
23 *
24 * If networking/proxy configuration is wrong these tests will fail..
25 *
26 * @package core
27 * @category phpunit
28 * @copyright &copy; 2006 The Open University
29 * @author T.J.Hunt@open.ac.uk
30 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
31 */
32
33defined('MOODLE_INTERNAL') || die();
34
35
f0202ae9 36class web_testcase extends advanced_testcase {
a3d5830a
PS
37
38 function test_format_string() {
39 global $CFG;
40
41 // Ampersands
42 $this->assertEquals(format_string("& &&&&& &&"), "&amp; &amp;&amp;&amp;&amp;&amp; &amp;&amp;");
43 $this->assertEquals(format_string("ANother & &&&&& Category"), "ANother &amp; &amp;&amp;&amp;&amp;&amp; Category");
44 $this->assertEquals(format_string("ANother & &&&&& Category", true), "ANother &amp; &amp;&amp;&amp;&amp;&amp; Category");
45 $this->assertEquals(format_string("Nick's Test Site & Other things", true), "Nick's Test Site &amp; Other things");
46
47 // String entities
48 $this->assertEquals(format_string("&quot;"), "&quot;");
49
50 // Digital entities
51 $this->assertEquals(format_string("&11234;"), "&11234;");
52
53 // Unicode entities
54 $this->assertEquals(format_string("&#4475;"), "&#4475;");
55
56 // < and > signs
57 $originalformatstringstriptags = $CFG->formatstringstriptags;
58
59 $CFG->formatstringstriptags = false;
60 $this->assertEquals(format_string('x < 1'), 'x &lt; 1');
61 $this->assertEquals(format_string('x > 1'), 'x &gt; 1');
62 $this->assertEquals(format_string('x < 1 and x > 0'), 'x &lt; 1 and x &gt; 0');
63
64 $CFG->formatstringstriptags = true;
65 $this->assertEquals(format_string('x < 1'), 'x &lt; 1');
66 $this->assertEquals(format_string('x > 1'), 'x &gt; 1');
67 $this->assertEquals(format_string('x < 1 and x > 0'), 'x &lt; 1 and x &gt; 0');
68
69 $CFG->formatstringstriptags = $originalformatstringstriptags;
70 }
71
72 function test_s() {
328ac306
TH
73 // Special cases.
74 $this->assertSame('0', s(0));
75 $this->assertSame('0', s('0'));
76 $this->assertSame('0', s(false));
77 $this->assertSame('', s(null));
78
79 // Normal cases.
80 $this->assertEquals('This Breaks &quot; Strict', s('This Breaks " Strict'));
81 $this->assertEquals('This Breaks &lt;a&gt;&quot; Strict&lt;/a&gt;', s('This Breaks <a>" Strict</a>'));
82
83 // Unicode characters.
84 $this->assertEquals('Café', s('Café'));
85 $this->assertEquals('一, 二, 三', s('一, 二, 三'));
86
87 // Don't escape already-escaped numeric entities. (Note, this behaviour
88 // may not be desirable. Perhaps we should remove these tests and that
89 // functionality, but we can only do that if we understand why it was added.)
0c6f9e75 90 $this->assertEquals('An entity: &#x09ff;.', s('An entity: &#x09ff;.'));
328ac306
TH
91 $this->assertEquals('An entity: &#1073;.', s('An entity: &#1073;.'));
92 $this->assertEquals('An entity: &amp;amp;.', s('An entity: &amp;.'));
93 $this->assertEquals('Not an entity: &amp;amp;#x09ff;.', s('Not an entity: &amp;#x09ff;.'));
a3d5830a
PS
94 }
95
96 function test_format_text_email() {
97 $this->assertEquals("This is a TEST",
98 format_text_email('<p>This is a <strong>test</strong></p>',FORMAT_HTML));
99 $this->assertEquals("This is a TEST",
100 format_text_email('<p class="frogs">This is a <strong class=\'fishes\'>test</strong></p>',FORMAT_HTML));
101 $this->assertEquals('& so is this',
102 format_text_email('&amp; so is this',FORMAT_HTML));
103 $this->assertEquals('Two bullets: '.textlib::code2utf8(8226).' *',
104 format_text_email('Two bullets: &#x2022; &#8226;',FORMAT_HTML));
105 $this->assertEquals(textlib::code2utf8(0x7fd2).textlib::code2utf8(0x7fd2),
106 format_text_email('&#x7fd2;&#x7FD2;',FORMAT_HTML));
107 }
108
66056f99
PS
109 function test_obfuscate_email() {
110 $email = 'some.user@example.com';
111 $obfuscated = obfuscate_email($email);
112 $this->assertNotSame($email, $obfuscated);
113 $back = textlib::entities_to_utf8(urldecode($email), true);
114 $this->assertSame($email, $back);
115 }
116
117 function test_obfuscate_text() {
118 $text = 'Žluťoučký koníček 32131';
119 $obfuscated = obfuscate_text($text);
120 $this->assertNotSame($text, $obfuscated);
121 $back = textlib::entities_to_utf8($obfuscated, true);
122 $this->assertSame($text, $back);
123 }
124
a3d5830a
PS
125 function test_highlight() {
126 $this->assertEquals(highlight('good', 'This is good'), 'This is <span class="highlight">good</span>');
127 $this->assertEquals(highlight('SpaN', 'span'), '<span class="highlight">span</span>');
128 $this->assertEquals(highlight('span', 'SpaN'), '<span class="highlight">SpaN</span>');
129 $this->assertEquals(highlight('span', '<span>span</span>'), '<span><span class="highlight">span</span></span>');
130 $this->assertEquals(highlight('good is', 'He is good'), 'He <span class="highlight">is</span> <span class="highlight">good</span>');
131 $this->assertEquals(highlight('+good', 'This is good'), 'This is <span class="highlight">good</span>');
132 $this->assertEquals(highlight('-good', 'This is good'), 'This is good');
133 $this->assertEquals(highlight('+good', 'This is goodness'), 'This is goodness');
134 $this->assertEquals(highlight('good', 'This is goodness'), 'This is <span class="highlight">good</span>ness');
135 }
136
137 function test_replace_ampersands() {
138 $this->assertEquals(replace_ampersands_not_followed_by_entity("This & that &nbsp;"), "This &amp; that &nbsp;");
139 $this->assertEquals(replace_ampersands_not_followed_by_entity("This &nbsp that &nbsp;"), "This &amp;nbsp that &nbsp;");
140 }
141
142 function test_strip_links() {
143 $this->assertEquals(strip_links('this is a <a href="http://someaddress.com/query">link</a>'), 'this is a link');
144 }
145
146 function test_wikify_links() {
147 $this->assertEquals(wikify_links('this is a <a href="http://someaddress.com/query">link</a>'), 'this is a link [ http://someaddress.com/query ]');
148 }
149
ffe4de97 150 /**
151 * Tests moodle_url::get_path().
152 */
153 public function test_moodle_url_get_path() {
154 $url = new moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1');
155 $this->assertEquals('/my/file/is/here.txt', $url->get_path());
156
157 $url = new moodle_url('http://www.example.org/');
158 $this->assertEquals('/', $url->get_path());
159
160 $url = new moodle_url('http://www.example.org/pluginfile.php/slash/arguments');
161 $this->assertEquals('/pluginfile.php/slash/arguments', $url->get_path());
162 $this->assertEquals('/pluginfile.php', $url->get_path(false));
163 }
164
a3d5830a
PS
165 function test_moodle_url_round_trip() {
166 $strurl = 'http://moodle.org/course/view.php?id=5';
167 $url = new moodle_url($strurl);
168 $this->assertEquals($strurl, $url->out(false));
169
170 $strurl = 'http://moodle.org/user/index.php?contextid=53&sifirst=M&silast=D';
171 $url = new moodle_url($strurl);
172 $this->assertEquals($strurl, $url->out(false));
173 }
174
616396a6
SH
175 /**
176 * Test Moodle URL objects created with a param with empty value.
177 */
178 function test_moodle_url_empty_param_values() {
179 $strurl = 'http://moodle.org/course/view.php?id=0';
180 $url = new moodle_url($strurl, array('id' => 0));
181 $this->assertEquals($strurl, $url->out(false));
182
45d97522 183 $strurl = 'http://moodle.org/course/view.php?id';
616396a6
SH
184 $url = new moodle_url($strurl, array('id' => false));
185 $this->assertEquals($strurl, $url->out(false));
186
45d97522 187 $strurl = 'http://moodle.org/course/view.php?id';
616396a6
SH
188 $url = new moodle_url($strurl, array('id' => null));
189 $this->assertEquals($strurl, $url->out(false));
190
a697000b
SH
191 $strurl = 'http://moodle.org/course/view.php?id';
192 $url = new moodle_url($strurl, array('id' => ''));
193 $this->assertEquals($strurl, $url->out(false));
194
45d97522 195 $strurl = 'http://moodle.org/course/view.php?id';
616396a6
SH
196 $url = new moodle_url($strurl);
197 $this->assertEquals($strurl, $url->out(false));
198 }
199
a3d5830a
PS
200 function test_moodle_url_round_trip_array_params() {
201 $strurl = 'http://example.com/?a%5B1%5D=1&a%5B2%5D=2';
202 $url = new moodle_url($strurl);
203 $this->assertEquals($strurl, $url->out(false));
204
205 $url = new moodle_url('http://example.com/?a[1]=1&a[2]=2');
206 $this->assertEquals($strurl, $url->out(false));
207
208 // For un-keyed array params, we expect 0..n keys to be returned
209 $strurl = 'http://example.com/?a%5B0%5D=0&a%5B1%5D=1';
210 $url = new moodle_url('http://example.com/?a[]=0&a[]=1');
211 $this->assertEquals($strurl, $url->out(false));
212 }
213
214 function test_compare_url() {
215 $url1 = new moodle_url('index.php', array('var1' => 1, 'var2' => 2));
216 $url2 = new moodle_url('index2.php', array('var1' => 1, 'var2' => 2, 'var3' => 3));
217
218 $this->assertFalse($url1->compare($url2, URL_MATCH_BASE));
219 $this->assertFalse($url1->compare($url2, URL_MATCH_PARAMS));
220 $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT));
221
222 $url2 = new moodle_url('index.php', array('var1' => 1, 'var3' => 3));
223
224 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE));
225 $this->assertFalse($url1->compare($url2, URL_MATCH_PARAMS));
226 $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT));
227
228 $url2 = new moodle_url('index.php', array('var1' => 1, 'var2' => 2, 'var3' => 3));
229
230 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE));
231 $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS));
232 $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT));
233
234 $url2 = new moodle_url('index.php', array('var2' => 2, 'var1' => 1));
235
236 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE));
237 $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS));
238 $this->assertTrue($url1->compare($url2, URL_MATCH_EXACT));
239 }
240
241 function test_out_as_local_url() {
72b181ba
RT
242 global $CFG;
243 // Test http url.
a3d5830a
PS
244 $url1 = new moodle_url('/lib/tests/weblib_test.php');
245 $this->assertEquals('/lib/tests/weblib_test.php', $url1->out_as_local_url());
72b181ba
RT
246
247 // Test https url.
248 $httpswwwroot = str_replace("http://", "https://", $CFG->wwwroot);
249 $url2 = new moodle_url($httpswwwroot.'/login/profile.php');
250 $this->assertEquals('/login/profile.php', $url2->out_as_local_url());
251
252 // Test http url matching wwwroot.
253 $url3 = new moodle_url($CFG->wwwroot);
254 $this->assertEquals('', $url3->out_as_local_url());
255
256 // Test http url matching wwwroot ending with slash (/).
257 $url3 = new moodle_url($CFG->wwwroot.'/');
258 $this->assertEquals('/', $url3->out_as_local_url());
a3d5830a
PS
259 }
260
261 /**
262 * @expectedException coding_exception
263 * @return void
264 */
265 function test_out_as_local_url_error() {
4ca04fb5 266 $url2 = new moodle_url('http://www.google.com/lib/tests/weblib_test.php');
a3d5830a
PS
267 $url2->out_as_local_url();
268 }
269
72b181ba
RT
270 /**
271 * You should get error with modified url
272 *
273 * @expectedException coding_exception
274 * @return void
275 */
276 public function test_modified_url_out_as_local_url_error() {
277 global $CFG;
278
279 $modifiedurl = $CFG->wwwroot.'1';
280 $url3 = new moodle_url($modifiedurl.'/login/profile.php');
281 $url3->out_as_local_url();
282 }
283
284 /**
285 * Try get local url from external https url and you should get error
286 *
287 * @expectedException coding_exception
288 * @return void
289 */
290 public function test_https_out_as_local_url_error() {
291 $url4 = new moodle_url('https://www.google.com/lib/tests/weblib_test.php');
292 $url4->out_as_local_url();
293 }
294
a3d5830a
PS
295 public function test_clean_text() {
296 $text = "lala <applet>xx</applet>";
297 $this->assertEquals($text, clean_text($text, FORMAT_PLAIN));
298 $this->assertEquals('lala xx', clean_text($text, FORMAT_MARKDOWN));
299 $this->assertEquals('lala xx', clean_text($text, FORMAT_MOODLE));
300 $this->assertEquals('lala xx', clean_text($text, FORMAT_HTML));
301 }
f0202ae9 302
bd319d12 303 public function test_qualified_me() {
f0202ae9
PS
304 global $PAGE, $FULLME, $CFG;
305 $this->resetAfterTest();
306
307 $PAGE = new moodle_page();
308
309 $FULLME = $CFG->wwwroot.'/course/view.php?id=1&xx=yy';
310 $this->assertEquals($FULLME, qualified_me());
311
312 $PAGE->set_url('/course/view.php', array('id'=>1));
313 $this->assertEquals($CFG->wwwroot.'/course/view.php?id=1', qualified_me());
314 }
837e3042
PS
315
316 public function test_null_progres_trace() {
317 $this->resetAfterTest(false);
318
319 $trace = new null_progress_trace();
837e3042
PS
320 $trace->output('do');
321 $trace->output('re', 1);
322 $trace->output('mi', 2);
323 $trace->finished();
324 $output = ob_get_contents();
837e3042 325 $this->assertSame('', $output);
4fa7fdea 326 $this->expectOutputString('');
837e3042
PS
327 }
328
329 public function test_text_progres_trace() {
330 $this->resetAfterTest(false);
331
332 $trace = new text_progress_trace();
837e3042
PS
333 $trace->output('do');
334 $trace->output('re', 1);
335 $trace->output('mi', 2);
336 $trace->finished();
4fa7fdea 337 $this->expectOutputString("do\n re\n mi\n");
837e3042
PS
338 }
339
340 public function test_html_progres_trace() {
341 $this->resetAfterTest(false);
342
343 $trace = new html_progress_trace();
837e3042
PS
344 $trace->output('do');
345 $trace->output('re', 1);
346 $trace->output('mi', 2);
347 $trace->finished();
4fa7fdea 348 $this->expectOutputString("<p>do</p>\n<p>&#160;&#160;re</p>\n<p>&#160;&#160;&#160;&#160;mi</p>\n");
837e3042
PS
349 }
350
351 public function test_html_list_progress_trace() {
352 $this->resetAfterTest(false);
353
354 $trace = new html_list_progress_trace();
837e3042
PS
355 $trace->output('do');
356 $trace->output('re', 1);
357 $trace->output('mi', 2);
358 $trace->finished();
4fa7fdea 359 $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
360 }
361
362 public function test_progres_trace_buffer() {
363 $this->resetAfterTest(false);
364
365 $trace = new progress_trace_buffer(new html_progress_trace());
366 ob_start();
367 $trace->output('do');
368 $trace->output('re', 1);
369 $trace->output('mi', 2);
370 $trace->finished();
371 $output = ob_get_contents();
372 ob_end_clean();
373 $this->assertSame("<p>do</p>\n<p>&#160;&#160;re</p>\n<p>&#160;&#160;&#160;&#160;mi</p>\n", $output);
374 $this->assertSame($output, $trace->get_buffer());
375
376 $trace = new progress_trace_buffer(new html_progress_trace(), false);
837e3042
PS
377 $trace->output('do');
378 $trace->output('re', 1);
379 $trace->output('mi', 2);
380 $trace->finished();
837e3042
PS
381 $this->assertSame("<p>do</p>\n<p>&#160;&#160;re</p>\n<p>&#160;&#160;&#160;&#160;mi</p>\n", $trace->get_buffer());
382 $this->assertSame("<p>do</p>\n<p>&#160;&#160;re</p>\n<p>&#160;&#160;&#160;&#160;mi</p>\n", $trace->get_buffer());
383 $trace->reset_buffer();
384 $this->assertSame('', $trace->get_buffer());
4fa7fdea 385 $this->expectOutputString('');
837e3042 386 }
e7259ec9
PS
387
388 public function test_combined_progres_trace() {
389 $this->resetAfterTest(false);
390
391 $trace1 = new progress_trace_buffer(new html_progress_trace(), false);
392 $trace2 = new progress_trace_buffer(new text_progress_trace(), false);
393
394 $trace = new combined_progress_trace(array($trace1, $trace2));
e7259ec9
PS
395 $trace->output('do');
396 $trace->output('re', 1);
397 $trace->output('mi', 2);
398 $trace->finished();
e7259ec9
PS
399 $this->assertSame("<p>do</p>\n<p>&#160;&#160;re</p>\n<p>&#160;&#160;&#160;&#160;mi</p>\n", $trace1->get_buffer());
400 $this->assertSame("do\n re\n mi\n", $trace2->get_buffer());
4fa7fdea 401 $this->expectOutputString('');
e7259ec9 402 }
a3d5830a 403}