Commit | Line | Data |
---|---|---|
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 © 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 | ||
33 | defined('MOODLE_INTERNAL') || die(); | |
34 | ||
35 | ||
f0202ae9 | 36 | class web_testcase extends advanced_testcase { |
a3d5830a PS |
37 | |
38 | function test_format_string() { | |
39 | global $CFG; | |
40 | ||
41 | // Ampersands | |
42 | $this->assertEquals(format_string("& &&&&& &&"), "& &&&&& &&"); | |
43 | $this->assertEquals(format_string("ANother & &&&&& Category"), "ANother & &&&&& Category"); | |
44 | $this->assertEquals(format_string("ANother & &&&&& Category", true), "ANother & &&&&& Category"); | |
45 | $this->assertEquals(format_string("Nick's Test Site & Other things", true), "Nick's Test Site & Other things"); | |
46 | ||
47 | // String entities | |
48 | $this->assertEquals(format_string("""), """); | |
49 | ||
50 | // Digital entities | |
51 | $this->assertEquals(format_string("&11234;"), "&11234;"); | |
52 | ||
53 | // Unicode entities | |
54 | $this->assertEquals(format_string("ᅻ"), "ᅻ"); | |
55 | ||
56 | // < and > signs | |
57 | $originalformatstringstriptags = $CFG->formatstringstriptags; | |
58 | ||
59 | $CFG->formatstringstriptags = false; | |
60 | $this->assertEquals(format_string('x < 1'), 'x < 1'); | |
61 | $this->assertEquals(format_string('x > 1'), 'x > 1'); | |
62 | $this->assertEquals(format_string('x < 1 and x > 0'), 'x < 1 and x > 0'); | |
63 | ||
64 | $CFG->formatstringstriptags = true; | |
65 | $this->assertEquals(format_string('x < 1'), 'x < 1'); | |
66 | $this->assertEquals(format_string('x > 1'), 'x > 1'); | |
67 | $this->assertEquals(format_string('x < 1 and x > 0'), 'x < 1 and x > 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 " Strict', s('This Breaks " Strict')); | |
81 | $this->assertEquals('This Breaks <a>" Strict</a>', 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: ৿.', s('An entity: ৿.')); |
328ac306 TH |
91 | $this->assertEquals('An entity: б.', s('An entity: б.')); |
92 | $this->assertEquals('An entity: &amp;.', s('An entity: &.')); | |
93 | $this->assertEquals('Not an entity: &amp;#x09ff;.', s('Not an entity: &#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('& so is this',FORMAT_HTML)); | |
103 | $this->assertEquals('Two bullets: '.textlib::code2utf8(8226).' *', | |
104 | format_text_email('Two bullets: • •',FORMAT_HTML)); | |
105 | $this->assertEquals(textlib::code2utf8(0x7fd2).textlib::code2utf8(0x7fd2), | |
106 | format_text_email('習習',FORMAT_HTML)); | |
107 | } | |
108 | ||
109 | function test_highlight() { | |
110 | $this->assertEquals(highlight('good', 'This is good'), 'This is <span class="highlight">good</span>'); | |
111 | $this->assertEquals(highlight('SpaN', 'span'), '<span class="highlight">span</span>'); | |
112 | $this->assertEquals(highlight('span', 'SpaN'), '<span class="highlight">SpaN</span>'); | |
113 | $this->assertEquals(highlight('span', '<span>span</span>'), '<span><span class="highlight">span</span></span>'); | |
114 | $this->assertEquals(highlight('good is', 'He is good'), 'He <span class="highlight">is</span> <span class="highlight">good</span>'); | |
115 | $this->assertEquals(highlight('+good', 'This is good'), 'This is <span class="highlight">good</span>'); | |
116 | $this->assertEquals(highlight('-good', 'This is good'), 'This is good'); | |
117 | $this->assertEquals(highlight('+good', 'This is goodness'), 'This is goodness'); | |
118 | $this->assertEquals(highlight('good', 'This is goodness'), 'This is <span class="highlight">good</span>ness'); | |
119 | } | |
120 | ||
121 | function test_replace_ampersands() { | |
122 | $this->assertEquals(replace_ampersands_not_followed_by_entity("This & that "), "This & that "); | |
123 | $this->assertEquals(replace_ampersands_not_followed_by_entity("This   that "), "This &nbsp that "); | |
124 | } | |
125 | ||
126 | function test_strip_links() { | |
127 | $this->assertEquals(strip_links('this is a <a href="http://someaddress.com/query">link</a>'), 'this is a link'); | |
128 | } | |
129 | ||
130 | function test_wikify_links() { | |
131 | $this->assertEquals(wikify_links('this is a <a href="http://someaddress.com/query">link</a>'), 'this is a link [ http://someaddress.com/query ]'); | |
132 | } | |
133 | ||
ffe4de97 | 134 | /** |
135 | * Tests moodle_url::get_path(). | |
136 | */ | |
137 | public function test_moodle_url_get_path() { | |
138 | $url = new moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1'); | |
139 | $this->assertEquals('/my/file/is/here.txt', $url->get_path()); | |
140 | ||
141 | $url = new moodle_url('http://www.example.org/'); | |
142 | $this->assertEquals('/', $url->get_path()); | |
143 | ||
144 | $url = new moodle_url('http://www.example.org/pluginfile.php/slash/arguments'); | |
145 | $this->assertEquals('/pluginfile.php/slash/arguments', $url->get_path()); | |
146 | $this->assertEquals('/pluginfile.php', $url->get_path(false)); | |
147 | } | |
148 | ||
a3d5830a PS |
149 | function test_moodle_url_round_trip() { |
150 | $strurl = 'http://moodle.org/course/view.php?id=5'; | |
151 | $url = new moodle_url($strurl); | |
152 | $this->assertEquals($strurl, $url->out(false)); | |
153 | ||
154 | $strurl = 'http://moodle.org/user/index.php?contextid=53&sifirst=M&silast=D'; | |
155 | $url = new moodle_url($strurl); | |
156 | $this->assertEquals($strurl, $url->out(false)); | |
157 | } | |
158 | ||
616396a6 SH |
159 | /** |
160 | * Test Moodle URL objects created with a param with empty value. | |
161 | */ | |
162 | function test_moodle_url_empty_param_values() { | |
163 | $strurl = 'http://moodle.org/course/view.php?id=0'; | |
164 | $url = new moodle_url($strurl, array('id' => 0)); | |
165 | $this->assertEquals($strurl, $url->out(false)); | |
166 | ||
45d97522 | 167 | $strurl = 'http://moodle.org/course/view.php?id'; |
616396a6 SH |
168 | $url = new moodle_url($strurl, array('id' => false)); |
169 | $this->assertEquals($strurl, $url->out(false)); | |
170 | ||
45d97522 | 171 | $strurl = 'http://moodle.org/course/view.php?id'; |
616396a6 SH |
172 | $url = new moodle_url($strurl, array('id' => null)); |
173 | $this->assertEquals($strurl, $url->out(false)); | |
174 | ||
a697000b SH |
175 | $strurl = 'http://moodle.org/course/view.php?id'; |
176 | $url = new moodle_url($strurl, array('id' => '')); | |
177 | $this->assertEquals($strurl, $url->out(false)); | |
178 | ||
45d97522 | 179 | $strurl = 'http://moodle.org/course/view.php?id'; |
616396a6 SH |
180 | $url = new moodle_url($strurl); |
181 | $this->assertEquals($strurl, $url->out(false)); | |
182 | } | |
183 | ||
a3d5830a PS |
184 | function test_moodle_url_round_trip_array_params() { |
185 | $strurl = 'http://example.com/?a%5B1%5D=1&a%5B2%5D=2'; | |
186 | $url = new moodle_url($strurl); | |
187 | $this->assertEquals($strurl, $url->out(false)); | |
188 | ||
189 | $url = new moodle_url('http://example.com/?a[1]=1&a[2]=2'); | |
190 | $this->assertEquals($strurl, $url->out(false)); | |
191 | ||
192 | // For un-keyed array params, we expect 0..n keys to be returned | |
193 | $strurl = 'http://example.com/?a%5B0%5D=0&a%5B1%5D=1'; | |
194 | $url = new moodle_url('http://example.com/?a[]=0&a[]=1'); | |
195 | $this->assertEquals($strurl, $url->out(false)); | |
196 | } | |
197 | ||
198 | function test_compare_url() { | |
199 | $url1 = new moodle_url('index.php', array('var1' => 1, 'var2' => 2)); | |
200 | $url2 = new moodle_url('index2.php', array('var1' => 1, 'var2' => 2, 'var3' => 3)); | |
201 | ||
202 | $this->assertFalse($url1->compare($url2, URL_MATCH_BASE)); | |
203 | $this->assertFalse($url1->compare($url2, URL_MATCH_PARAMS)); | |
204 | $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT)); | |
205 | ||
206 | $url2 = new moodle_url('index.php', array('var1' => 1, 'var3' => 3)); | |
207 | ||
208 | $this->assertTrue($url1->compare($url2, URL_MATCH_BASE)); | |
209 | $this->assertFalse($url1->compare($url2, URL_MATCH_PARAMS)); | |
210 | $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT)); | |
211 | ||
212 | $url2 = new moodle_url('index.php', array('var1' => 1, 'var2' => 2, 'var3' => 3)); | |
213 | ||
214 | $this->assertTrue($url1->compare($url2, URL_MATCH_BASE)); | |
215 | $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS)); | |
216 | $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT)); | |
217 | ||
218 | $url2 = new moodle_url('index.php', array('var2' => 2, 'var1' => 1)); | |
219 | ||
220 | $this->assertTrue($url1->compare($url2, URL_MATCH_BASE)); | |
221 | $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS)); | |
222 | $this->assertTrue($url1->compare($url2, URL_MATCH_EXACT)); | |
223 | } | |
224 | ||
225 | function test_out_as_local_url() { | |
72b181ba RT |
226 | global $CFG; |
227 | // Test http url. | |
a3d5830a PS |
228 | $url1 = new moodle_url('/lib/tests/weblib_test.php'); |
229 | $this->assertEquals('/lib/tests/weblib_test.php', $url1->out_as_local_url()); | |
72b181ba RT |
230 | |
231 | // Test https url. | |
232 | $httpswwwroot = str_replace("http://", "https://", $CFG->wwwroot); | |
233 | $url2 = new moodle_url($httpswwwroot.'/login/profile.php'); | |
234 | $this->assertEquals('/login/profile.php', $url2->out_as_local_url()); | |
235 | ||
236 | // Test http url matching wwwroot. | |
237 | $url3 = new moodle_url($CFG->wwwroot); | |
238 | $this->assertEquals('', $url3->out_as_local_url()); | |
239 | ||
240 | // Test http url matching wwwroot ending with slash (/). | |
241 | $url3 = new moodle_url($CFG->wwwroot.'/'); | |
242 | $this->assertEquals('/', $url3->out_as_local_url()); | |
a3d5830a PS |
243 | } |
244 | ||
245 | /** | |
246 | * @expectedException coding_exception | |
247 | * @return void | |
248 | */ | |
249 | function test_out_as_local_url_error() { | |
4ca04fb5 | 250 | $url2 = new moodle_url('http://www.google.com/lib/tests/weblib_test.php'); |
a3d5830a PS |
251 | $url2->out_as_local_url(); |
252 | } | |
253 | ||
72b181ba RT |
254 | /** |
255 | * You should get error with modified url | |
256 | * | |
257 | * @expectedException coding_exception | |
258 | * @return void | |
259 | */ | |
260 | public function test_modified_url_out_as_local_url_error() { | |
261 | global $CFG; | |
262 | ||
263 | $modifiedurl = $CFG->wwwroot.'1'; | |
264 | $url3 = new moodle_url($modifiedurl.'/login/profile.php'); | |
265 | $url3->out_as_local_url(); | |
266 | } | |
267 | ||
268 | /** | |
269 | * Try get local url from external https url and you should get error | |
270 | * | |
271 | * @expectedException coding_exception | |
272 | * @return void | |
273 | */ | |
274 | public function test_https_out_as_local_url_error() { | |
275 | $url4 = new moodle_url('https://www.google.com/lib/tests/weblib_test.php'); | |
276 | $url4->out_as_local_url(); | |
277 | } | |
278 | ||
a3d5830a PS |
279 | public function test_clean_text() { |
280 | $text = "lala <applet>xx</applet>"; | |
281 | $this->assertEquals($text, clean_text($text, FORMAT_PLAIN)); | |
282 | $this->assertEquals('lala xx', clean_text($text, FORMAT_MARKDOWN)); | |
283 | $this->assertEquals('lala xx', clean_text($text, FORMAT_MOODLE)); | |
284 | $this->assertEquals('lala xx', clean_text($text, FORMAT_HTML)); | |
285 | } | |
f0202ae9 | 286 | |
bd319d12 | 287 | public function test_qualified_me() { |
f0202ae9 PS |
288 | global $PAGE, $FULLME, $CFG; |
289 | $this->resetAfterTest(); | |
290 | ||
291 | $PAGE = new moodle_page(); | |
292 | ||
293 | $FULLME = $CFG->wwwroot.'/course/view.php?id=1&xx=yy'; | |
294 | $this->assertEquals($FULLME, qualified_me()); | |
295 | ||
296 | $PAGE->set_url('/course/view.php', array('id'=>1)); | |
297 | $this->assertEquals($CFG->wwwroot.'/course/view.php?id=1', qualified_me()); | |
298 | } | |
837e3042 PS |
299 | |
300 | public function test_null_progres_trace() { | |
301 | $this->resetAfterTest(false); | |
302 | ||
303 | $trace = new null_progress_trace(); | |
837e3042 PS |
304 | $trace->output('do'); |
305 | $trace->output('re', 1); | |
306 | $trace->output('mi', 2); | |
307 | $trace->finished(); | |
308 | $output = ob_get_contents(); | |
837e3042 | 309 | $this->assertSame('', $output); |
4fa7fdea | 310 | $this->expectOutputString(''); |
837e3042 PS |
311 | } |
312 | ||
313 | public function test_text_progres_trace() { | |
314 | $this->resetAfterTest(false); | |
315 | ||
316 | $trace = new text_progress_trace(); | |
837e3042 PS |
317 | $trace->output('do'); |
318 | $trace->output('re', 1); | |
319 | $trace->output('mi', 2); | |
320 | $trace->finished(); | |
4fa7fdea | 321 | $this->expectOutputString("do\n re\n mi\n"); |
837e3042 PS |
322 | } |
323 | ||
324 | public function test_html_progres_trace() { | |
325 | $this->resetAfterTest(false); | |
326 | ||
327 | $trace = new html_progress_trace(); | |
837e3042 PS |
328 | $trace->output('do'); |
329 | $trace->output('re', 1); | |
330 | $trace->output('mi', 2); | |
331 | $trace->finished(); | |
4fa7fdea | 332 | $this->expectOutputString("<p>do</p>\n<p>  re</p>\n<p>    mi</p>\n"); |
837e3042 PS |
333 | } |
334 | ||
335 | public function test_html_list_progress_trace() { | |
336 | $this->resetAfterTest(false); | |
337 | ||
338 | $trace = new html_list_progress_trace(); | |
837e3042 PS |
339 | $trace->output('do'); |
340 | $trace->output('re', 1); | |
341 | $trace->output('mi', 2); | |
342 | $trace->finished(); | |
4fa7fdea | 343 | $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 |
344 | } |
345 | ||
346 | public function test_progres_trace_buffer() { | |
347 | $this->resetAfterTest(false); | |
348 | ||
349 | $trace = new progress_trace_buffer(new html_progress_trace()); | |
350 | ob_start(); | |
351 | $trace->output('do'); | |
352 | $trace->output('re', 1); | |
353 | $trace->output('mi', 2); | |
354 | $trace->finished(); | |
355 | $output = ob_get_contents(); | |
356 | ob_end_clean(); | |
357 | $this->assertSame("<p>do</p>\n<p>  re</p>\n<p>    mi</p>\n", $output); | |
358 | $this->assertSame($output, $trace->get_buffer()); | |
359 | ||
360 | $trace = new progress_trace_buffer(new html_progress_trace(), false); | |
837e3042 PS |
361 | $trace->output('do'); |
362 | $trace->output('re', 1); | |
363 | $trace->output('mi', 2); | |
364 | $trace->finished(); | |
837e3042 PS |
365 | $this->assertSame("<p>do</p>\n<p>  re</p>\n<p>    mi</p>\n", $trace->get_buffer()); |
366 | $this->assertSame("<p>do</p>\n<p>  re</p>\n<p>    mi</p>\n", $trace->get_buffer()); | |
367 | $trace->reset_buffer(); | |
368 | $this->assertSame('', $trace->get_buffer()); | |
4fa7fdea | 369 | $this->expectOutputString(''); |
837e3042 | 370 | } |
e7259ec9 PS |
371 | |
372 | public function test_combined_progres_trace() { | |
373 | $this->resetAfterTest(false); | |
374 | ||
375 | $trace1 = new progress_trace_buffer(new html_progress_trace(), false); | |
376 | $trace2 = new progress_trace_buffer(new text_progress_trace(), false); | |
377 | ||
378 | $trace = new combined_progress_trace(array($trace1, $trace2)); | |
e7259ec9 PS |
379 | $trace->output('do'); |
380 | $trace->output('re', 1); | |
381 | $trace->output('mi', 2); | |
382 | $trace->finished(); | |
e7259ec9 PS |
383 | $this->assertSame("<p>do</p>\n<p>  re</p>\n<p>    mi</p>\n", $trace1->get_buffer()); |
384 | $this->assertSame("do\n re\n mi\n", $trace2->get_buffer()); | |
4fa7fdea | 385 | $this->expectOutputString(''); |
e7259ec9 | 386 | } |
a3d5830a | 387 | } |