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() { | |
73 | $this->assertEquals(s("This Breaks \" Strict"), "This Breaks " Strict"); | |
74 | $this->assertEquals(s("This Breaks <a>\" Strict</a>"), "This Breaks <a>" Strict</a>"); | |
75 | } | |
76 | ||
77 | function test_format_text_email() { | |
78 | $this->assertEquals("This is a TEST", | |
79 | format_text_email('<p>This is a <strong>test</strong></p>',FORMAT_HTML)); | |
80 | $this->assertEquals("This is a TEST", | |
81 | format_text_email('<p class="frogs">This is a <strong class=\'fishes\'>test</strong></p>',FORMAT_HTML)); | |
82 | $this->assertEquals('& so is this', | |
83 | format_text_email('& so is this',FORMAT_HTML)); | |
84 | $this->assertEquals('Two bullets: '.textlib::code2utf8(8226).' *', | |
85 | format_text_email('Two bullets: • •',FORMAT_HTML)); | |
86 | $this->assertEquals(textlib::code2utf8(0x7fd2).textlib::code2utf8(0x7fd2), | |
87 | format_text_email('習習',FORMAT_HTML)); | |
88 | } | |
89 | ||
90 | function test_highlight() { | |
91 | $this->assertEquals(highlight('good', 'This is good'), 'This is <span class="highlight">good</span>'); | |
92 | $this->assertEquals(highlight('SpaN', 'span'), '<span class="highlight">span</span>'); | |
93 | $this->assertEquals(highlight('span', 'SpaN'), '<span class="highlight">SpaN</span>'); | |
94 | $this->assertEquals(highlight('span', '<span>span</span>'), '<span><span class="highlight">span</span></span>'); | |
95 | $this->assertEquals(highlight('good is', 'He is good'), 'He <span class="highlight">is</span> <span class="highlight">good</span>'); | |
96 | $this->assertEquals(highlight('+good', 'This is good'), 'This is <span class="highlight">good</span>'); | |
97 | $this->assertEquals(highlight('-good', 'This is good'), 'This is good'); | |
98 | $this->assertEquals(highlight('+good', 'This is goodness'), 'This is goodness'); | |
99 | $this->assertEquals(highlight('good', 'This is goodness'), 'This is <span class="highlight">good</span>ness'); | |
100 | } | |
101 | ||
102 | function test_replace_ampersands() { | |
103 | $this->assertEquals(replace_ampersands_not_followed_by_entity("This & that "), "This & that "); | |
104 | $this->assertEquals(replace_ampersands_not_followed_by_entity("This   that "), "This &nbsp that "); | |
105 | } | |
106 | ||
107 | function test_strip_links() { | |
108 | $this->assertEquals(strip_links('this is a <a href="http://someaddress.com/query">link</a>'), 'this is a link'); | |
109 | } | |
110 | ||
111 | function test_wikify_links() { | |
112 | $this->assertEquals(wikify_links('this is a <a href="http://someaddress.com/query">link</a>'), 'this is a link [ http://someaddress.com/query ]'); | |
113 | } | |
114 | ||
ffe4de97 | 115 | /** |
116 | * Tests moodle_url::get_path(). | |
117 | */ | |
118 | public function test_moodle_url_get_path() { | |
119 | $url = new moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1'); | |
120 | $this->assertEquals('/my/file/is/here.txt', $url->get_path()); | |
121 | ||
122 | $url = new moodle_url('http://www.example.org/'); | |
123 | $this->assertEquals('/', $url->get_path()); | |
124 | ||
125 | $url = new moodle_url('http://www.example.org/pluginfile.php/slash/arguments'); | |
126 | $this->assertEquals('/pluginfile.php/slash/arguments', $url->get_path()); | |
127 | $this->assertEquals('/pluginfile.php', $url->get_path(false)); | |
128 | } | |
129 | ||
a3d5830a PS |
130 | function test_moodle_url_round_trip() { |
131 | $strurl = 'http://moodle.org/course/view.php?id=5'; | |
132 | $url = new moodle_url($strurl); | |
133 | $this->assertEquals($strurl, $url->out(false)); | |
134 | ||
135 | $strurl = 'http://moodle.org/user/index.php?contextid=53&sifirst=M&silast=D'; | |
136 | $url = new moodle_url($strurl); | |
137 | $this->assertEquals($strurl, $url->out(false)); | |
138 | } | |
139 | ||
140 | function test_moodle_url_round_trip_array_params() { | |
141 | $strurl = 'http://example.com/?a%5B1%5D=1&a%5B2%5D=2'; | |
142 | $url = new moodle_url($strurl); | |
143 | $this->assertEquals($strurl, $url->out(false)); | |
144 | ||
145 | $url = new moodle_url('http://example.com/?a[1]=1&a[2]=2'); | |
146 | $this->assertEquals($strurl, $url->out(false)); | |
147 | ||
148 | // For un-keyed array params, we expect 0..n keys to be returned | |
149 | $strurl = 'http://example.com/?a%5B0%5D=0&a%5B1%5D=1'; | |
150 | $url = new moodle_url('http://example.com/?a[]=0&a[]=1'); | |
151 | $this->assertEquals($strurl, $url->out(false)); | |
152 | } | |
153 | ||
154 | function test_compare_url() { | |
155 | $url1 = new moodle_url('index.php', array('var1' => 1, 'var2' => 2)); | |
156 | $url2 = new moodle_url('index2.php', array('var1' => 1, 'var2' => 2, 'var3' => 3)); | |
157 | ||
158 | $this->assertFalse($url1->compare($url2, URL_MATCH_BASE)); | |
159 | $this->assertFalse($url1->compare($url2, URL_MATCH_PARAMS)); | |
160 | $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT)); | |
161 | ||
162 | $url2 = new moodle_url('index.php', array('var1' => 1, 'var3' => 3)); | |
163 | ||
164 | $this->assertTrue($url1->compare($url2, URL_MATCH_BASE)); | |
165 | $this->assertFalse($url1->compare($url2, URL_MATCH_PARAMS)); | |
166 | $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT)); | |
167 | ||
168 | $url2 = new moodle_url('index.php', array('var1' => 1, 'var2' => 2, 'var3' => 3)); | |
169 | ||
170 | $this->assertTrue($url1->compare($url2, URL_MATCH_BASE)); | |
171 | $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS)); | |
172 | $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT)); | |
173 | ||
174 | $url2 = new moodle_url('index.php', array('var2' => 2, 'var1' => 1)); | |
175 | ||
176 | $this->assertTrue($url1->compare($url2, URL_MATCH_BASE)); | |
177 | $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS)); | |
178 | $this->assertTrue($url1->compare($url2, URL_MATCH_EXACT)); | |
179 | } | |
180 | ||
181 | function test_out_as_local_url() { | |
72b181ba RT |
182 | global $CFG; |
183 | // Test http url. | |
a3d5830a PS |
184 | $url1 = new moodle_url('/lib/tests/weblib_test.php'); |
185 | $this->assertEquals('/lib/tests/weblib_test.php', $url1->out_as_local_url()); | |
72b181ba RT |
186 | |
187 | // Test https url. | |
188 | $httpswwwroot = str_replace("http://", "https://", $CFG->wwwroot); | |
189 | $url2 = new moodle_url($httpswwwroot.'/login/profile.php'); | |
190 | $this->assertEquals('/login/profile.php', $url2->out_as_local_url()); | |
191 | ||
192 | // Test http url matching wwwroot. | |
193 | $url3 = new moodle_url($CFG->wwwroot); | |
194 | $this->assertEquals('', $url3->out_as_local_url()); | |
195 | ||
196 | // Test http url matching wwwroot ending with slash (/). | |
197 | $url3 = new moodle_url($CFG->wwwroot.'/'); | |
198 | $this->assertEquals('/', $url3->out_as_local_url()); | |
a3d5830a PS |
199 | } |
200 | ||
201 | /** | |
202 | * @expectedException coding_exception | |
203 | * @return void | |
204 | */ | |
205 | function test_out_as_local_url_error() { | |
4ca04fb5 | 206 | $url2 = new moodle_url('http://www.google.com/lib/tests/weblib_test.php'); |
a3d5830a PS |
207 | $url2->out_as_local_url(); |
208 | } | |
209 | ||
72b181ba RT |
210 | /** |
211 | * You should get error with modified url | |
212 | * | |
213 | * @expectedException coding_exception | |
214 | * @return void | |
215 | */ | |
216 | public function test_modified_url_out_as_local_url_error() { | |
217 | global $CFG; | |
218 | ||
219 | $modifiedurl = $CFG->wwwroot.'1'; | |
220 | $url3 = new moodle_url($modifiedurl.'/login/profile.php'); | |
221 | $url3->out_as_local_url(); | |
222 | } | |
223 | ||
224 | /** | |
225 | * Try get local url from external https url and you should get error | |
226 | * | |
227 | * @expectedException coding_exception | |
228 | * @return void | |
229 | */ | |
230 | public function test_https_out_as_local_url_error() { | |
231 | $url4 = new moodle_url('https://www.google.com/lib/tests/weblib_test.php'); | |
232 | $url4->out_as_local_url(); | |
233 | } | |
234 | ||
a3d5830a PS |
235 | public function test_clean_text() { |
236 | $text = "lala <applet>xx</applet>"; | |
237 | $this->assertEquals($text, clean_text($text, FORMAT_PLAIN)); | |
238 | $this->assertEquals('lala xx', clean_text($text, FORMAT_MARKDOWN)); | |
239 | $this->assertEquals('lala xx', clean_text($text, FORMAT_MOODLE)); | |
240 | $this->assertEquals('lala xx', clean_text($text, FORMAT_HTML)); | |
241 | } | |
f0202ae9 | 242 | |
bd319d12 | 243 | public function test_qualified_me() { |
f0202ae9 PS |
244 | global $PAGE, $FULLME, $CFG; |
245 | $this->resetAfterTest(); | |
246 | ||
247 | $PAGE = new moodle_page(); | |
248 | ||
249 | $FULLME = $CFG->wwwroot.'/course/view.php?id=1&xx=yy'; | |
250 | $this->assertEquals($FULLME, qualified_me()); | |
251 | ||
252 | $PAGE->set_url('/course/view.php', array('id'=>1)); | |
253 | $this->assertEquals($CFG->wwwroot.'/course/view.php?id=1', qualified_me()); | |
254 | } | |
837e3042 PS |
255 | |
256 | public function test_null_progres_trace() { | |
257 | $this->resetAfterTest(false); | |
258 | ||
259 | $trace = new null_progress_trace(); | |
837e3042 PS |
260 | $trace->output('do'); |
261 | $trace->output('re', 1); | |
262 | $trace->output('mi', 2); | |
263 | $trace->finished(); | |
264 | $output = ob_get_contents(); | |
837e3042 | 265 | $this->assertSame('', $output); |
4fa7fdea | 266 | $this->expectOutputString(''); |
837e3042 PS |
267 | } |
268 | ||
269 | public function test_text_progres_trace() { | |
270 | $this->resetAfterTest(false); | |
271 | ||
272 | $trace = new text_progress_trace(); | |
837e3042 PS |
273 | $trace->output('do'); |
274 | $trace->output('re', 1); | |
275 | $trace->output('mi', 2); | |
276 | $trace->finished(); | |
4fa7fdea | 277 | $this->expectOutputString("do\n re\n mi\n"); |
837e3042 PS |
278 | } |
279 | ||
280 | public function test_html_progres_trace() { | |
281 | $this->resetAfterTest(false); | |
282 | ||
283 | $trace = new html_progress_trace(); | |
837e3042 PS |
284 | $trace->output('do'); |
285 | $trace->output('re', 1); | |
286 | $trace->output('mi', 2); | |
287 | $trace->finished(); | |
4fa7fdea | 288 | $this->expectOutputString("<p>do</p>\n<p>  re</p>\n<p>    mi</p>\n"); |
837e3042 PS |
289 | } |
290 | ||
291 | public function test_html_list_progress_trace() { | |
292 | $this->resetAfterTest(false); | |
293 | ||
294 | $trace = new html_list_progress_trace(); | |
837e3042 PS |
295 | $trace->output('do'); |
296 | $trace->output('re', 1); | |
297 | $trace->output('mi', 2); | |
298 | $trace->finished(); | |
4fa7fdea | 299 | $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 |
300 | } |
301 | ||
302 | public function test_progres_trace_buffer() { | |
303 | $this->resetAfterTest(false); | |
304 | ||
305 | $trace = new progress_trace_buffer(new html_progress_trace()); | |
306 | ob_start(); | |
307 | $trace->output('do'); | |
308 | $trace->output('re', 1); | |
309 | $trace->output('mi', 2); | |
310 | $trace->finished(); | |
311 | $output = ob_get_contents(); | |
312 | ob_end_clean(); | |
313 | $this->assertSame("<p>do</p>\n<p>  re</p>\n<p>    mi</p>\n", $output); | |
314 | $this->assertSame($output, $trace->get_buffer()); | |
315 | ||
316 | $trace = new progress_trace_buffer(new html_progress_trace(), false); | |
837e3042 PS |
317 | $trace->output('do'); |
318 | $trace->output('re', 1); | |
319 | $trace->output('mi', 2); | |
320 | $trace->finished(); | |
837e3042 PS |
321 | $this->assertSame("<p>do</p>\n<p>  re</p>\n<p>    mi</p>\n", $trace->get_buffer()); |
322 | $this->assertSame("<p>do</p>\n<p>  re</p>\n<p>    mi</p>\n", $trace->get_buffer()); | |
323 | $trace->reset_buffer(); | |
324 | $this->assertSame('', $trace->get_buffer()); | |
4fa7fdea | 325 | $this->expectOutputString(''); |
837e3042 | 326 | } |
e7259ec9 PS |
327 | |
328 | public function test_combined_progres_trace() { | |
329 | $this->resetAfterTest(false); | |
330 | ||
331 | $trace1 = new progress_trace_buffer(new html_progress_trace(), false); | |
332 | $trace2 = new progress_trace_buffer(new text_progress_trace(), false); | |
333 | ||
334 | $trace = new combined_progress_trace(array($trace1, $trace2)); | |
e7259ec9 PS |
335 | $trace->output('do'); |
336 | $trace->output('re', 1); | |
337 | $trace->output('mi', 2); | |
338 | $trace->finished(); | |
e7259ec9 PS |
339 | $this->assertSame("<p>do</p>\n<p>  re</p>\n<p>    mi</p>\n", $trace1->get_buffer()); |
340 | $this->assertSame("do\n re\n mi\n", $trace2->get_buffer()); | |
4fa7fdea | 341 | $this->expectOutputString(''); |
e7259ec9 | 342 | } |
a3d5830a | 343 | } |