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 | ||
a3d5830a PS |
17 | /** |
18 | * Unit tests for /lib/filelib.php. | |
19 | * | |
20 | * @package core_files | |
21 | * @category phpunit | |
22 | * @copyright 2009 Jerome Mouneyrac | |
23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
24 | */ | |
25 | ||
26 | defined('MOODLE_INTERNAL') || die(); | |
27 | ||
28 | global $CFG; | |
29 | require_once($CFG->libdir . '/filelib.php'); | |
ac29403e | 30 | require_once($CFG->dirroot . '/repository/lib.php'); |
a3d5830a | 31 | |
1d987cb7 | 32 | class core_filelib_testcase extends advanced_testcase { |
a3d5830a PS |
33 | public function test_format_postdata_for_curlcall() { |
34 | ||
1d987cb7 PS |
35 | // POST params with just simple types. |
36 | $postdatatoconvert = array( 'userid' => 1, 'roleid' => 22, 'name' => 'john'); | |
a3d5830a PS |
37 | $expectedresult = "userid=1&roleid=22&name=john"; |
38 | $postdata = format_postdata_for_curlcall($postdatatoconvert); | |
1d987cb7 | 39 | $this->assertEquals($expectedresult, $postdata); |
a3d5830a | 40 | |
1d987cb7 PS |
41 | // POST params with a string containing & character. |
42 | $postdatatoconvert = array( 'name' => 'john&emilie', 'roleid' => 22); | |
43 | $expectedresult = "name=john%26emilie&roleid=22"; // Urlencode: '%26' => '&'. | |
a3d5830a | 44 | $postdata = format_postdata_for_curlcall($postdatatoconvert); |
1d987cb7 | 45 | $this->assertEquals($expectedresult, $postdata); |
a3d5830a | 46 | |
1d987cb7 PS |
47 | // POST params with an empty value. |
48 | $postdatatoconvert = array( 'name' => null, 'roleid' => 22); | |
a3d5830a PS |
49 | $expectedresult = "name=&roleid=22"; |
50 | $postdata = format_postdata_for_curlcall($postdatatoconvert); | |
1d987cb7 | 51 | $this->assertEquals($expectedresult, $postdata); |
a3d5830a | 52 | |
1d987cb7 PS |
53 | // POST params with complex types. |
54 | $postdatatoconvert = array( 'users' => array( | |
a3d5830a PS |
55 | array( |
56 | 'id' => 2, | |
57 | 'customfields' => array( | |
58 | array | |
59 | ( | |
60 | 'type' => 'Color', | |
61 | 'value' => 'violet' | |
62 | ) | |
63 | ) | |
64 | ) | |
65 | ) | |
66 | ); | |
67 | $expectedresult = "users[0][id]=2&users[0][customfields][0][type]=Color&users[0][customfields][0][value]=violet"; | |
68 | $postdata = format_postdata_for_curlcall($postdatatoconvert); | |
1d987cb7 | 69 | $this->assertEquals($expectedresult, $postdata); |
a3d5830a | 70 | |
1d987cb7 | 71 | // POST params with other complex types. |
a3d5830a PS |
72 | $postdatatoconvert = array ('members' => |
73 | array( | |
74 | array('groupid' => 1, 'userid' => 1) | |
75 | , array('groupid' => 1, 'userid' => 2) | |
76 | ) | |
77 | ); | |
78 | $expectedresult = "members[0][groupid]=1&members[0][userid]=1&members[1][groupid]=1&members[1][userid]=2"; | |
79 | $postdata = format_postdata_for_curlcall($postdatatoconvert); | |
1d987cb7 | 80 | $this->assertEquals($expectedresult, $postdata); |
a3d5830a PS |
81 | } |
82 | ||
83 | public function test_download_file_content() { | |
35ea5db0 PS |
84 | global $CFG; |
85 | ||
86 | // Test http success first. | |
a9d2f1b4 | 87 | $testhtml = $this->getExternalTestFileUrl('/test.html'); |
35ea5db0 PS |
88 | |
89 | $contents = download_file_content($testhtml); | |
90 | $this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents)); | |
91 | ||
92 | $tofile = "$CFG->tempdir/test.html"; | |
93 | @unlink($tofile); | |
94 | $result = download_file_content($testhtml, null, null, false, 300, 20, false, $tofile); | |
95 | $this->assertTrue($result); | |
96 | $this->assertFileExists($tofile); | |
1d987cb7 | 97 | $this->assertSame(file_get_contents($tofile), $contents); |
35ea5db0 PS |
98 | @unlink($tofile); |
99 | ||
100 | $result = download_file_content($testhtml, null, null, false, 300, 20, false, null, true); | |
101 | $this->assertSame($contents, $result); | |
102 | ||
103 | $response = download_file_content($testhtml, null, null, true); | |
104 | $this->assertInstanceOf('stdClass', $response); | |
105 | $this->assertSame('200', $response->status); | |
106 | $this->assertTrue(is_array($response->headers)); | |
846f6fbe | 107 | $this->assertRegExp('|^HTTP/1\.[01] 200 OK$|', rtrim($response->response_code)); |
35ea5db0 PS |
108 | $this->assertSame($contents, $response->results); |
109 | $this->assertSame('', $response->error); | |
110 | ||
35ea5db0 | 111 | // Test https success. |
a9d2f1b4 | 112 | $testhtml = $this->getExternalTestFileUrl('/test.html', true); |
35ea5db0 PS |
113 | |
114 | $contents = download_file_content($testhtml, null, null, false, 300, 20, true); | |
115 | $this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents)); | |
116 | ||
117 | $contents = download_file_content($testhtml); | |
118 | $this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents)); | |
119 | ||
35ea5db0 | 120 | // Now 404. |
a9d2f1b4 | 121 | $testhtml = $this->getExternalTestFileUrl('/test.html_nonexistent'); |
35ea5db0 PS |
122 | |
123 | $contents = download_file_content($testhtml); | |
124 | $this->assertFalse($contents); | |
125 | $this->assertDebuggingCalled(); | |
126 | ||
127 | $response = download_file_content($testhtml, null, null, true); | |
128 | $this->assertInstanceOf('stdClass', $response); | |
129 | $this->assertSame('404', $response->status); | |
130 | $this->assertTrue(is_array($response->headers)); | |
846f6fbe | 131 | $this->assertRegExp('|^HTTP/1\.[01] 404 Not Found$|', rtrim($response->response_code)); |
35ea5db0 PS |
132 | $this->assertStringStartsWith('<!DOCTYPE', $response->results); |
133 | $this->assertSame('', $response->error); | |
134 | ||
35ea5db0 | 135 | // Invalid url. |
a9d2f1b4 PS |
136 | $testhtml = $this->getExternalTestFileUrl('/test.html'); |
137 | $testhtml = str_replace('http://', 'ftp://', $testhtml); | |
35ea5db0 | 138 | |
a3d5830a | 139 | $contents = download_file_content($testhtml); |
35ea5db0 PS |
140 | $this->assertFalse($contents); |
141 | ||
35ea5db0 | 142 | // Test standard redirects. |
a9d2f1b4 | 143 | $testurl = $this->getExternalTestFileUrl('/test_redir.php'); |
35ea5db0 PS |
144 | |
145 | $contents = download_file_content("$testurl?redir=2"); | |
146 | $this->assertSame('done', $contents); | |
147 | ||
148 | $response = download_file_content("$testurl?redir=2", null, null, true); | |
149 | $this->assertInstanceOf('stdClass', $response); | |
150 | $this->assertSame('200', $response->status); | |
151 | $this->assertTrue(is_array($response->headers)); | |
846f6fbe | 152 | $this->assertRegExp('|^HTTP/1\.[01] 200 OK$|', rtrim($response->response_code)); |
35ea5db0 PS |
153 | $this->assertSame('done', $response->results); |
154 | $this->assertSame('', $response->error); | |
155 | ||
a9d2f1b4 | 156 | // Commented out this block if there are performance problems. |
1d987cb7 | 157 | /* |
35ea5db0 PS |
158 | $contents = download_file_content("$testurl?redir=6"); |
159 | $this->assertFalse(false, $contents); | |
160 | $this->assertDebuggingCalled(); | |
35ea5db0 PS |
161 | $response = download_file_content("$testurl?redir=6", null, null, true); |
162 | $this->assertInstanceOf('stdClass', $response); | |
163 | $this->assertSame('0', $response->status); | |
164 | $this->assertTrue(is_array($response->headers)); | |
165 | $this->assertFalse($response->results); | |
166 | $this->assertNotEmpty($response->error); | |
1d987cb7 | 167 | */ |
35ea5db0 PS |
168 | |
169 | // Test relative redirects. | |
a9d2f1b4 | 170 | $testurl = $this->getExternalTestFileUrl('/test_relative_redir.php'); |
35ea5db0 PS |
171 | |
172 | $contents = download_file_content("$testurl"); | |
173 | $this->assertSame('done', $contents); | |
174 | ||
175 | $contents = download_file_content("$testurl?unused=xxx"); | |
176 | $this->assertSame('done', $contents); | |
177 | } | |
178 | ||
179 | /** | |
180 | * Test curl class. | |
181 | */ | |
182 | public function test_curl_class() { | |
183 | global $CFG; | |
184 | ||
185 | // Test https success. | |
a9d2f1b4 | 186 | $testhtml = $this->getExternalTestFileUrl('/test.html'); |
35ea5db0 PS |
187 | |
188 | $curl = new curl(); | |
189 | $contents = $curl->get($testhtml); | |
190 | $this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents)); | |
191 | $this->assertSame(0, $curl->get_errno()); | |
192 | ||
193 | $curl = new curl(); | |
194 | $tofile = "$CFG->tempdir/test.html"; | |
195 | @unlink($tofile); | |
196 | $fp = fopen($tofile, 'w'); | |
197 | $result = $curl->get($testhtml, array(), array('CURLOPT_FILE'=>$fp)); | |
198 | $this->assertTrue($result); | |
199 | fclose($fp); | |
200 | $this->assertFileExists($tofile); | |
201 | $this->assertSame($contents, file_get_contents($tofile)); | |
202 | @unlink($tofile); | |
203 | ||
204 | $curl = new curl(); | |
205 | $tofile = "$CFG->tempdir/test.html"; | |
206 | @unlink($tofile); | |
207 | $result = $curl->download_one($testhtml, array(), array('filepath'=>$tofile)); | |
208 | $this->assertTrue($result); | |
209 | $this->assertFileExists($tofile); | |
210 | $this->assertSame($contents, file_get_contents($tofile)); | |
211 | @unlink($tofile); | |
212 | ||
213 | // Test full URL redirects. | |
a9d2f1b4 | 214 | $testurl = $this->getExternalTestFileUrl('/test_redir.php'); |
35ea5db0 PS |
215 | |
216 | $curl = new curl(); | |
217 | $contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>2)); | |
218 | $this->assertSame(0, $curl->get_errno()); | |
219 | $this->assertSame(2, $curl->info['redirect_count']); | |
220 | $this->assertSame('done', $contents); | |
221 | ||
222 | $curl = new curl(); | |
223 | $curl->emulateredirects = true; | |
224 | $contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>2)); | |
225 | $this->assertSame(0, $curl->get_errno()); | |
226 | $this->assertSame(2, $curl->info['redirect_count']); | |
227 | $this->assertSame('done', $contents); | |
228 | ||
229 | $curl = new curl(); | |
230 | $contents = $curl->get("$testurl?redir=3", array(), array('CURLOPT_FOLLOWLOCATION'=>0)); | |
231 | $this->assertSame(0, $curl->get_errno()); | |
232 | $this->assertSame(302, $curl->info['http_code']); | |
233 | $this->assertSame('', $contents); | |
234 | ||
235 | $curl = new curl(); | |
236 | $curl->emulateredirects = true; | |
237 | $contents = $curl->get("$testurl?redir=3", array(), array('CURLOPT_FOLLOWLOCATION'=>0)); | |
238 | $this->assertSame(0, $curl->get_errno()); | |
239 | $this->assertSame(302, $curl->info['http_code']); | |
240 | $this->assertSame('', $contents); | |
241 | ||
242 | $curl = new curl(); | |
243 | $contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>1)); | |
244 | $this->assertSame(CURLE_TOO_MANY_REDIRECTS, $curl->get_errno()); | |
245 | $this->assertNotEmpty($contents); | |
246 | ||
247 | $curl = new curl(); | |
248 | $curl->emulateredirects = true; | |
249 | $contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>1)); | |
250 | $this->assertSame(CURLE_TOO_MANY_REDIRECTS, $curl->get_errno()); | |
251 | $this->assertNotEmpty($contents); | |
252 | ||
253 | $curl = new curl(); | |
254 | $tofile = "$CFG->tempdir/test.html"; | |
255 | @unlink($tofile); | |
256 | $fp = fopen($tofile, 'w'); | |
257 | $result = $curl->get("$testurl?redir=1", array(), array('CURLOPT_FILE'=>$fp)); | |
258 | $this->assertTrue($result); | |
259 | fclose($fp); | |
260 | $this->assertFileExists($tofile); | |
261 | $this->assertSame('done', file_get_contents($tofile)); | |
262 | @unlink($tofile); | |
263 | ||
264 | $curl = new curl(); | |
265 | $curl->emulateredirects = true; | |
266 | $tofile = "$CFG->tempdir/test.html"; | |
267 | @unlink($tofile); | |
268 | $fp = fopen($tofile, 'w'); | |
269 | $result = $curl->get("$testurl?redir=1", array(), array('CURLOPT_FILE'=>$fp)); | |
270 | $this->assertTrue($result); | |
271 | fclose($fp); | |
272 | $this->assertFileExists($tofile); | |
273 | $this->assertSame('done', file_get_contents($tofile)); | |
274 | @unlink($tofile); | |
275 | ||
276 | $curl = new curl(); | |
277 | $tofile = "$CFG->tempdir/test.html"; | |
278 | @unlink($tofile); | |
279 | $result = $curl->download_one("$testurl?redir=1", array(), array('filepath'=>$tofile)); | |
280 | $this->assertTrue($result); | |
281 | $this->assertFileExists($tofile); | |
282 | $this->assertSame('done', file_get_contents($tofile)); | |
283 | @unlink($tofile); | |
284 | ||
285 | $curl = new curl(); | |
286 | $curl->emulateredirects = true; | |
287 | $tofile = "$CFG->tempdir/test.html"; | |
288 | @unlink($tofile); | |
289 | $result = $curl->download_one("$testurl?redir=1", array(), array('filepath'=>$tofile)); | |
290 | $this->assertTrue($result); | |
291 | $this->assertFileExists($tofile); | |
292 | $this->assertSame('done', file_get_contents($tofile)); | |
293 | @unlink($tofile); | |
294 | ||
35ea5db0 | 295 | // Test relative location redirects. |
a9d2f1b4 | 296 | $testurl = $this->getExternalTestFileUrl('/test_relative_redir.php'); |
35ea5db0 PS |
297 | |
298 | $curl = new curl(); | |
299 | $contents = $curl->get($testurl); | |
300 | $this->assertSame(0, $curl->get_errno()); | |
301 | $this->assertSame(1, $curl->info['redirect_count']); | |
302 | $this->assertSame('done', $contents); | |
303 | ||
304 | $curl = new curl(); | |
305 | $curl->emulateredirects = true; | |
306 | $contents = $curl->get($testurl); | |
307 | $this->assertSame(0, $curl->get_errno()); | |
308 | $this->assertSame(1, $curl->info['redirect_count']); | |
309 | $this->assertSame('done', $contents); | |
310 | ||
35ea5db0 | 311 | // Test different redirect types. |
a9d2f1b4 | 312 | $testurl = $this->getExternalTestFileUrl('/test_relative_redir.php'); |
35ea5db0 PS |
313 | |
314 | $curl = new curl(); | |
315 | $contents = $curl->get("$testurl?type=301"); | |
316 | $this->assertSame(0, $curl->get_errno()); | |
317 | $this->assertSame(1, $curl->info['redirect_count']); | |
318 | $this->assertSame('done', $contents); | |
319 | ||
320 | $curl = new curl(); | |
321 | $curl->emulateredirects = true; | |
322 | $contents = $curl->get("$testurl?type=301"); | |
323 | $this->assertSame(0, $curl->get_errno()); | |
324 | $this->assertSame(1, $curl->info['redirect_count']); | |
325 | $this->assertSame('done', $contents); | |
326 | ||
327 | $curl = new curl(); | |
328 | $contents = $curl->get("$testurl?type=302"); | |
329 | $this->assertSame(0, $curl->get_errno()); | |
330 | $this->assertSame(1, $curl->info['redirect_count']); | |
331 | $this->assertSame('done', $contents); | |
332 | ||
333 | $curl = new curl(); | |
334 | $curl->emulateredirects = true; | |
335 | $contents = $curl->get("$testurl?type=302"); | |
336 | $this->assertSame(0, $curl->get_errno()); | |
337 | $this->assertSame(1, $curl->info['redirect_count']); | |
338 | $this->assertSame('done', $contents); | |
339 | ||
340 | $curl = new curl(); | |
341 | $contents = $curl->get("$testurl?type=303"); | |
342 | $this->assertSame(0, $curl->get_errno()); | |
343 | $this->assertSame(1, $curl->info['redirect_count']); | |
344 | $this->assertSame('done', $contents); | |
345 | ||
346 | $curl = new curl(); | |
347 | $curl->emulateredirects = true; | |
348 | $contents = $curl->get("$testurl?type=303"); | |
349 | $this->assertSame(0, $curl->get_errno()); | |
350 | $this->assertSame(1, $curl->info['redirect_count']); | |
351 | $this->assertSame('done', $contents); | |
352 | ||
353 | $curl = new curl(); | |
354 | $contents = $curl->get("$testurl?type=307"); | |
355 | $this->assertSame(0, $curl->get_errno()); | |
356 | $this->assertSame(1, $curl->info['redirect_count']); | |
357 | $this->assertSame('done', $contents); | |
358 | ||
359 | $curl = new curl(); | |
360 | $curl->emulateredirects = true; | |
361 | $contents = $curl->get("$testurl?type=307"); | |
362 | $this->assertSame(0, $curl->get_errno()); | |
363 | $this->assertSame(1, $curl->info['redirect_count']); | |
364 | $this->assertSame('done', $contents); | |
365 | ||
366 | $curl = new curl(); | |
367 | $contents = $curl->get("$testurl?type=308"); | |
368 | $this->assertSame(0, $curl->get_errno()); | |
369 | $this->assertSame(1, $curl->info['redirect_count']); | |
370 | $this->assertSame('done', $contents); | |
371 | ||
372 | $curl = new curl(); | |
373 | $curl->emulateredirects = true; | |
374 | $contents = $curl->get("$testurl?type=308"); | |
375 | $this->assertSame(0, $curl->get_errno()); | |
376 | $this->assertSame(1, $curl->info['redirect_count']); | |
377 | $this->assertSame('done', $contents); | |
ba72e295 FM |
378 | |
379 | $oldproxy = $CFG->proxyhost; | |
380 | $oldproxybypass = $CFG->proxybypass; | |
381 | ||
382 | // Test without proxy bypass and inaccessible proxy. | |
383 | $CFG->proxyhost = 'i.do.not.exist'; | |
384 | $CFG->proxybypass = ''; | |
385 | $curl = new curl(); | |
386 | $contents = $curl->get($testhtml); | |
387 | $this->assertNotEquals(0, $curl->get_errno()); | |
388 | $this->assertNotEquals('47250a973d1b88d9445f94db4ef2c97a', md5($contents)); | |
389 | ||
390 | // Test with proxy bypass. | |
391 | $testhtmlhost = parse_url($testhtml, PHP_URL_HOST); | |
392 | $CFG->proxybypass = $testhtmlhost; | |
393 | $curl = new curl(); | |
394 | $contents = $curl->get($testhtml); | |
395 | $this->assertSame(0, $curl->get_errno()); | |
396 | $this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents)); | |
397 | ||
398 | $CFG->proxyhost = $oldproxy; | |
399 | $CFG->proxybypass = $oldproxybypass; | |
a3d5830a | 400 | } |
67233725 | 401 | |
102230b2 FM |
402 | public function test_curl_response_headers() { |
403 | ||
404 | // Test 404 request. | |
405 | $curl = new curl(); | |
406 | $contents = $curl->get($this->getExternalTestFileUrl('/i.do.not.exist')); | |
407 | $response = $curl->getResponse(); | |
408 | $this->assertSame('404 Not Found', reset($response)); | |
409 | $this->assertSame(0, $curl->get_errno()); | |
410 | ||
411 | $testhtml = $this->getExternalTestFileUrl('/test.html'); | |
412 | ||
413 | // Test standard request. | |
414 | $curl = new curl(); | |
415 | $contents = $curl->get($testhtml); | |
416 | $response = $curl->getResponse(); | |
417 | $this->assertSame('200 OK', reset($response)); | |
418 | $this->assertSame(0, $curl->get_errno()); | |
419 | ||
420 | $testurl = $this->getExternalTestFileUrl('/test_relative_redir.php'); | |
421 | ||
422 | // Test a redirect without follow location. | |
423 | $curl = new curl(); | |
424 | $contents = $curl->get("$testurl?redir=3", array(), array('CURLOPT_FOLLOWLOCATION'=>0)); | |
425 | $response = $curl->getResponse(); | |
426 | $this->assertSame('302 Found', reset($response)); | |
427 | $this->assertSame(0, $curl->get_errno()); | |
428 | ||
429 | // Test a redirect without follow location and emulated redirect. | |
430 | $curl = new curl(); | |
431 | $curl->emulateredirects = true; | |
432 | $contents = $curl->get("$testurl?redir=3", array(), array('CURLOPT_FOLLOWLOCATION'=>0)); | |
433 | $response = $curl->getResponse(); | |
434 | $this->assertSame('302 Found', reset($response)); | |
435 | $this->assertSame(0, $curl->get_errno()); | |
436 | ||
437 | // Test a redirect. | |
438 | $curl = new curl(); | |
439 | $contents = $curl->get("$testurl?type=302"); | |
440 | $response = $curl->getResponse(); | |
441 | $this->assertSame('200 OK', reset($response)); | |
442 | $this->assertSame(0, $curl->get_errno()); | |
443 | $this->assertSame(1, $curl->info['redirect_count']); | |
444 | $this->assertSame('done', $contents); | |
445 | ||
446 | // Test a redirect with emulated redirect. | |
447 | $curl = new curl(); | |
448 | $curl->emulateredirects = true; | |
449 | $contents = $curl->get("$testurl?type=302"); | |
450 | $response = $curl->getResponse(); | |
451 | $this->assertSame('200 OK', reset($response)); | |
452 | $this->assertSame(0, $curl->get_errno()); | |
453 | $this->assertSame('done', $contents); | |
454 | ||
455 | $testpost = $this->getExternalTestFileUrl('/test_post.php'); | |
456 | ||
457 | // Test post request. | |
458 | $curl = new curl(); | |
459 | $contents = $curl->post($testpost, 'data=moodletest'); | |
460 | $response = $curl->getResponse(); | |
461 | $this->assertSame('200 OK', reset($response)); | |
462 | $this->assertSame(0, $curl->get_errno()); | |
463 | $this->assertSame('OK', $contents); | |
464 | ||
465 | // Test 100 requests. | |
466 | $curl = new curl(); | |
467 | $curl->setHeader('Expect: 100-continue'); | |
468 | $contents = $curl->post($testpost, 'data=moodletest'); | |
469 | $response = $curl->getResponse(); | |
470 | $this->assertSame('200 OK', reset($response)); | |
471 | $this->assertSame(0, $curl->get_errno()); | |
472 | $this->assertSame('OK', $contents); | |
473 | } | |
474 | ||
67233725 DC |
475 | /** |
476 | * Testing prepare draft area | |
477 | * | |
478 | * @copyright 2012 Dongsheng Cai {@link http://dongsheng.org} | |
479 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
480 | */ | |
481 | public function test_prepare_draft_area() { | |
482 | global $USER, $DB; | |
483 | ||
484 | $this->resetAfterTest(true); | |
485 | ||
486 | $generator = $this->getDataGenerator(); | |
487 | $user = $generator->create_user(); | |
488 | $usercontext = context_user::instance($user->id); | |
489 | $USER = $DB->get_record('user', array('id'=>$user->id)); | |
490 | ||
7051415c DC |
491 | $repositorypluginname = 'user'; |
492 | ||
493 | $args = array(); | |
494 | $args['type'] = $repositorypluginname; | |
495 | $repos = repository::get_instances($args); | |
496 | $userrepository = reset($repos); | |
497 | $this->assertInstanceOf('repository', $userrepository); | |
67233725 DC |
498 | |
499 | $fs = get_file_storage(); | |
7051415c | 500 | |
67233725 DC |
501 | $syscontext = context_system::instance(); |
502 | $component = 'core'; | |
503 | $filearea = 'unittest'; | |
504 | $itemid = 0; | |
505 | $filepath = '/'; | |
506 | $filename = 'test.txt'; | |
507 | $sourcefield = 'Copyright stuff'; | |
508 | ||
509 | $filerecord = array( | |
510 | 'contextid' => $syscontext->id, | |
511 | 'component' => $component, | |
512 | 'filearea' => $filearea, | |
513 | 'itemid' => $itemid, | |
514 | 'filepath' => $filepath, | |
515 | 'filename' => $filename, | |
516 | 'source' => $sourcefield, | |
517 | ); | |
518 | $ref = $fs->pack_reference($filerecord); | |
67233725 | 519 | $originalfile = $fs->create_file_from_string($filerecord, 'Test content'); |
67233725 DC |
520 | $fileid = $originalfile->get_id(); |
521 | $this->assertInstanceOf('stored_file', $originalfile); | |
522 | ||
1d987cb7 | 523 | // Create a user private file. |
7051415c DC |
524 | $userfilerecord = new stdClass; |
525 | $userfilerecord->contextid = $usercontext->id; | |
526 | $userfilerecord->component = 'user'; | |
527 | $userfilerecord->filearea = 'private'; | |
528 | $userfilerecord->itemid = 0; | |
529 | $userfilerecord->filepath = '/'; | |
530 | $userfilerecord->filename = 'userfile.txt'; | |
531 | $userfilerecord->source = 'test'; | |
532 | $userfile = $fs->create_file_from_string($userfilerecord, 'User file content'); | |
533 | $userfileref = $fs->pack_reference($userfilerecord); | |
534 | ||
535 | $filerefrecord = clone((object)$filerecord); | |
536 | $filerefrecord->filename = 'testref.txt'; | |
1d987cb7 PS |
537 | |
538 | // Create a file reference. | |
7051415c DC |
539 | $fileref = $fs->create_file_from_reference($filerefrecord, $userrepository->id, $userfileref); |
540 | $this->assertInstanceOf('stored_file', $fileref); | |
291a3d1f | 541 | $this->assertEquals($userrepository->id, $fileref->get_repository_id()); |
1d987cb7 | 542 | $this->assertSame($userfile->get_contenthash(), $fileref->get_contenthash()); |
7051415c DC |
543 | $this->assertEquals($userfile->get_filesize(), $fileref->get_filesize()); |
544 | $this->assertRegExp('#' . $userfile->get_filename(). '$#', $fileref->get_reference_details()); | |
545 | ||
67233725 DC |
546 | $draftitemid = 0; |
547 | file_prepare_draft_area($draftitemid, $syscontext->id, $component, $filearea, $itemid); | |
548 | ||
549 | $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid); | |
1d987cb7 | 550 | $this->assertCount(3, $draftfiles); |
67233725 DC |
551 | |
552 | $draftfile = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $filepath, $filename); | |
553 | $source = unserialize($draftfile->get_source()); | |
1d987cb7 PS |
554 | $this->assertSame($ref, $source->original); |
555 | $this->assertSame($sourcefield, $source->source); | |
67233725 | 556 | |
7051415c DC |
557 | $draftfileref = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $filepath, $filerefrecord->filename); |
558 | $this->assertInstanceOf('stored_file', $draftfileref); | |
1d987cb7 | 559 | $this->assertTrue($draftfileref->is_external_file()); |
67233725 | 560 | |
1d987cb7 | 561 | // Change some information. |
67233725 DC |
562 | $author = 'Dongsheng Cai'; |
563 | $draftfile->set_author($author); | |
7051415c | 564 | $newsourcefield = 'Get from Flickr'; |
67233725 DC |
565 | $license = 'GPLv3'; |
566 | $draftfile->set_license($license); | |
1d987cb7 | 567 | // If you want to really just change source field, do this. |
67233725 DC |
568 | $source = unserialize($draftfile->get_source()); |
569 | $newsourcefield = 'From flickr'; | |
570 | $source->source = $newsourcefield; | |
571 | $draftfile->set_source(serialize($source)); | |
572 | ||
1d987cb7 | 573 | // Save changed file. |
67233725 DC |
574 | file_save_draft_area_files($draftitemid, $syscontext->id, $component, $filearea, $itemid); |
575 | ||
576 | $file = $fs->get_file($syscontext->id, $component, $filearea, $itemid, $filepath, $filename); | |
577 | ||
1d987cb7 | 578 | // Make sure it's the original file id. |
67233725 DC |
579 | $this->assertEquals($fileid, $file->get_id()); |
580 | $this->assertInstanceOf('stored_file', $file); | |
1d987cb7 PS |
581 | $this->assertSame($author, $file->get_author()); |
582 | $this->assertSame($license, $file->get_license()); | |
67233725 DC |
583 | $this->assertEquals($newsourcefield, $file->get_source()); |
584 | } | |
61506a0a DC |
585 | |
586 | /** | |
1d987cb7 | 587 | * Testing deleting original files. |
61506a0a DC |
588 | * |
589 | * @copyright 2012 Dongsheng Cai {@link http://dongsheng.org} | |
590 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
591 | */ | |
592 | public function test_delete_original_file_from_draft() { | |
593 | global $USER, $DB; | |
594 | ||
595 | $this->resetAfterTest(true); | |
596 | ||
597 | $generator = $this->getDataGenerator(); | |
598 | $user = $generator->create_user(); | |
599 | $usercontext = context_user::instance($user->id); | |
600 | $USER = $DB->get_record('user', array('id'=>$user->id)); | |
601 | ||
602 | $repositorypluginname = 'user'; | |
603 | ||
604 | $args = array(); | |
605 | $args['type'] = $repositorypluginname; | |
606 | $repos = repository::get_instances($args); | |
607 | $userrepository = reset($repos); | |
608 | $this->assertInstanceOf('repository', $userrepository); | |
609 | ||
610 | $fs = get_file_storage(); | |
611 | $syscontext = context_system::instance(); | |
612 | ||
613 | $filecontent = 'User file content'; | |
614 | ||
1d987cb7 | 615 | // Create a user private file. |
61506a0a DC |
616 | $userfilerecord = new stdClass; |
617 | $userfilerecord->contextid = $usercontext->id; | |
618 | $userfilerecord->component = 'user'; | |
619 | $userfilerecord->filearea = 'private'; | |
620 | $userfilerecord->itemid = 0; | |
621 | $userfilerecord->filepath = '/'; | |
622 | $userfilerecord->filename = 'userfile.txt'; | |
623 | $userfilerecord->source = 'test'; | |
624 | $userfile = $fs->create_file_from_string($userfilerecord, $filecontent); | |
625 | $userfileref = $fs->pack_reference($userfilerecord); | |
626 | $contenthash = $userfile->get_contenthash(); | |
627 | ||
628 | $filerecord = array( | |
629 | 'contextid' => $syscontext->id, | |
630 | 'component' => 'core', | |
631 | 'filearea' => 'phpunit', | |
632 | 'itemid' => 0, | |
633 | 'filepath' => '/', | |
634 | 'filename' => 'test.txt', | |
635 | ); | |
1d987cb7 | 636 | // Create a file reference. |
61506a0a DC |
637 | $fileref = $fs->create_file_from_reference($filerecord, $userrepository->id, $userfileref); |
638 | $this->assertInstanceOf('stored_file', $fileref); | |
291a3d1f | 639 | $this->assertEquals($userrepository->id, $fileref->get_repository_id()); |
1d987cb7 | 640 | $this->assertSame($userfile->get_contenthash(), $fileref->get_contenthash()); |
61506a0a DC |
641 | $this->assertEquals($userfile->get_filesize(), $fileref->get_filesize()); |
642 | $this->assertRegExp('#' . $userfile->get_filename(). '$#', $fileref->get_reference_details()); | |
643 | ||
644 | $draftitemid = 0; | |
645 | file_prepare_draft_area($draftitemid, $usercontext->id, 'user', 'private', 0); | |
646 | $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid); | |
1d987cb7 | 647 | $this->assertCount(2, $draftfiles); |
61506a0a DC |
648 | $draftfile = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $userfilerecord->filepath, $userfilerecord->filename); |
649 | $draftfile->delete(); | |
1d987cb7 | 650 | // Save changed file. |
61506a0a DC |
651 | file_save_draft_area_files($draftitemid, $usercontext->id, 'user', 'private', 0); |
652 | ||
1d987cb7 | 653 | // The file reference should be a regular moodle file now. |
61506a0a | 654 | $fileref = $fs->get_file($syscontext->id, 'core', 'phpunit', 0, '/', 'test.txt'); |
1d987cb7 PS |
655 | $this->assertFalse($fileref->is_external_file()); |
656 | $this->assertSame($contenthash, $fileref->get_contenthash()); | |
61506a0a DC |
657 | $this->assertEquals($filecontent, $fileref->get_content()); |
658 | } | |
85cb4b65 | 659 | |
660 | /** | |
661 | * Tests the strip_double_headers function in the curl class. | |
662 | */ | |
663 | public function test_curl_strip_double_headers() { | |
664 | // Example from issue tracker. | |
665 | $mdl30648example = <<<EOF | |
666 | HTTP/1.0 407 Proxy Authentication Required | |
667 | Server: squid/2.7.STABLE9 | |
668 | Date: Thu, 08 Dec 2011 14:44:33 GMT | |
669 | Content-Type: text/html | |
670 | Content-Length: 1275 | |
671 | X-Squid-Error: ERR_CACHE_ACCESS_DENIED 0 | |
672 | Proxy-Authenticate: Basic realm="Squid proxy-caching web server" | |
673 | X-Cache: MISS from homer.lancs.ac.uk | |
674 | X-Cache-Lookup: NONE from homer.lancs.ac.uk:3128 | |
675 | Via: 1.0 homer.lancs.ac.uk:3128 (squid/2.7.STABLE9) | |
676 | Connection: close | |
677 | ||
678 | HTTP/1.0 200 OK | |
679 | Server: Apache | |
680 | X-Lb-Nocache: true | |
681 | Cache-Control: private, max-age=15 | |
682 | ETag: "4d69af5d8ba873ea9192c489e151bd7b" | |
683 | Content-Type: text/html | |
684 | Date: Thu, 08 Dec 2011 14:44:53 GMT | |
685 | Set-Cookie: BBC-UID=c4de2e109c8df6a51de627cee11b214bd4fb6054a030222488317afb31b343360MoodleBot/1.0; expires=Mon, 07-Dec-15 14:44:53 GMT; path=/; domain=bbc.co.uk | |
686 | X-Cache-Action: MISS | |
687 | X-Cache-Age: 0 | |
688 | Vary: Cookie,X-Country,X-Ip-is-uk-combined,X-Ip-is-advertise-combined,X-Ip_is_uk_combined,X-Ip_is_advertise_combined, X-GeoIP | |
689 | X-Cache: MISS from ww | |
690 | ||
691 | <html>... | |
692 | EOF; | |
693 | $mdl30648expected = <<<EOF | |
694 | HTTP/1.0 200 OK | |
695 | Server: Apache | |
696 | X-Lb-Nocache: true | |
697 | Cache-Control: private, max-age=15 | |
698 | ETag: "4d69af5d8ba873ea9192c489e151bd7b" | |
699 | Content-Type: text/html | |
700 | Date: Thu, 08 Dec 2011 14:44:53 GMT | |
701 | Set-Cookie: BBC-UID=c4de2e109c8df6a51de627cee11b214bd4fb6054a030222488317afb31b343360MoodleBot/1.0; expires=Mon, 07-Dec-15 14:44:53 GMT; path=/; domain=bbc.co.uk | |
702 | X-Cache-Action: MISS | |
703 | X-Cache-Age: 0 | |
704 | Vary: Cookie,X-Country,X-Ip-is-uk-combined,X-Ip-is-advertise-combined,X-Ip_is_uk_combined,X-Ip_is_advertise_combined, X-GeoIP | |
705 | X-Cache: MISS from ww | |
706 | ||
707 | <html>... | |
708 | EOF; | |
709 | // For HTTP, replace the \n with \r\n. | |
710 | $mdl30648example = preg_replace("~(?!<\r)\n~", "\r\n", $mdl30648example); | |
711 | $mdl30648expected = preg_replace("~(?!<\r)\n~", "\r\n", $mdl30648expected); | |
712 | ||
713 | // Test stripping works OK. | |
1d987cb7 | 714 | $this->assertSame($mdl30648expected, curl::strip_double_headers($mdl30648example)); |
85cb4b65 | 715 | // Test it does nothing to the 'plain' data. |
1d987cb7 | 716 | $this->assertSame($mdl30648expected, curl::strip_double_headers($mdl30648expected)); |
85cb4b65 | 717 | |
718 | // Example from OU proxy. | |
719 | $httpsexample = <<<EOF | |
720 | HTTP/1.0 200 Connection established | |
721 | ||
722 | HTTP/1.1 200 OK | |
723 | Date: Fri, 22 Feb 2013 17:14:23 GMT | |
724 | Server: Apache/2 | |
725 | X-Powered-By: PHP/5.3.3-7+squeeze14 | |
726 | Content-Type: text/xml | |
727 | Connection: close | |
728 | Content-Encoding: gzip | |
729 | Transfer-Encoding: chunked | |
730 | ||
731 | <?xml version="1.0" encoding="ISO-8859-1" ?> | |
732 | <rss version="2.0">... | |
733 | EOF; | |
734 | $httpsexpected = <<<EOF | |
735 | HTTP/1.1 200 OK | |
736 | Date: Fri, 22 Feb 2013 17:14:23 GMT | |
737 | Server: Apache/2 | |
738 | X-Powered-By: PHP/5.3.3-7+squeeze14 | |
739 | Content-Type: text/xml | |
740 | Connection: close | |
741 | Content-Encoding: gzip | |
742 | Transfer-Encoding: chunked | |
743 | ||
744 | <?xml version="1.0" encoding="ISO-8859-1" ?> | |
745 | <rss version="2.0">... | |
746 | EOF; | |
747 | // For HTTP, replace the \n with \r\n. | |
748 | $httpsexample = preg_replace("~(?!<\r)\n~", "\r\n", $httpsexample); | |
749 | $httpsexpected = preg_replace("~(?!<\r)\n~", "\r\n", $httpsexpected); | |
750 | ||
751 | // Test stripping works OK. | |
1d987cb7 | 752 | $this->assertSame($httpsexpected, curl::strip_double_headers($httpsexample)); |
85cb4b65 | 753 | // Test it does nothing to the 'plain' data. |
1d987cb7 | 754 | $this->assertSame($httpsexpected, curl::strip_double_headers($httpsexpected)); |
85cb4b65 | 755 | } |
a3d5830a | 756 | } |