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. | |
a3d5830a | 87 | $testhtml = "http://download.moodle.org/unittest/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 PS |
111 | // Test https success. |
112 | $testhtml = "https://download.moodle.org/unittest/test.html"; | |
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 PS |
120 | // Now 404. |
121 | $testhtml = "http://download.moodle.org/unittest/test.html_nonexistent"; | |
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 PS |
135 | // Invalid url. |
136 | $testhtml = "ftp://download.moodle.org/unittest/test.html"; | |
137 | ||
a3d5830a | 138 | $contents = download_file_content($testhtml); |
35ea5db0 PS |
139 | $this->assertFalse($contents); |
140 | ||
35ea5db0 PS |
141 | // Test standard redirects. |
142 | $testurl = 'http://download.moodle.org/unittest/test_redir.php'; | |
143 | ||
144 | $contents = download_file_content("$testurl?redir=2"); | |
145 | $this->assertSame('done', $contents); | |
146 | ||
147 | $response = download_file_content("$testurl?redir=2", null, null, true); | |
148 | $this->assertInstanceOf('stdClass', $response); | |
149 | $this->assertSame('200', $response->status); | |
150 | $this->assertTrue(is_array($response->headers)); | |
846f6fbe | 151 | $this->assertRegExp('|^HTTP/1\.[01] 200 OK$|', rtrim($response->response_code)); |
35ea5db0 PS |
152 | $this->assertSame('done', $response->results); |
153 | $this->assertSame('', $response->error); | |
154 | ||
1d987cb7 | 155 | /* |
35ea5db0 PS |
156 | // Commented out for performance reasons. |
157 | ||
158 | $contents = download_file_content("$testurl?redir=6"); | |
159 | $this->assertFalse(false, $contents); | |
160 | $this->assertDebuggingCalled(); | |
161 | ||
162 | $response = download_file_content("$testurl?redir=6", null, null, true); | |
163 | $this->assertInstanceOf('stdClass', $response); | |
164 | $this->assertSame('0', $response->status); | |
165 | $this->assertTrue(is_array($response->headers)); | |
166 | $this->assertFalse($response->results); | |
167 | $this->assertNotEmpty($response->error); | |
1d987cb7 | 168 | */ |
35ea5db0 PS |
169 | |
170 | // Test relative redirects. | |
171 | $testurl = 'http://download.moodle.org/unittest/test_relative_redir.php'; | |
172 | ||
173 | $contents = download_file_content("$testurl"); | |
174 | $this->assertSame('done', $contents); | |
175 | ||
176 | $contents = download_file_content("$testurl?unused=xxx"); | |
177 | $this->assertSame('done', $contents); | |
178 | } | |
179 | ||
180 | /** | |
181 | * Test curl class. | |
182 | */ | |
183 | public function test_curl_class() { | |
184 | global $CFG; | |
185 | ||
186 | // Test https success. | |
187 | $testhtml = "https://download.moodle.org/unittest/test.html"; | |
188 | ||
189 | $curl = new curl(); | |
190 | $contents = $curl->get($testhtml); | |
191 | $this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents)); | |
192 | $this->assertSame(0, $curl->get_errno()); | |
193 | ||
194 | $curl = new curl(); | |
195 | $tofile = "$CFG->tempdir/test.html"; | |
196 | @unlink($tofile); | |
197 | $fp = fopen($tofile, 'w'); | |
198 | $result = $curl->get($testhtml, array(), array('CURLOPT_FILE'=>$fp)); | |
199 | $this->assertTrue($result); | |
200 | fclose($fp); | |
201 | $this->assertFileExists($tofile); | |
202 | $this->assertSame($contents, file_get_contents($tofile)); | |
203 | @unlink($tofile); | |
204 | ||
205 | $curl = new curl(); | |
206 | $tofile = "$CFG->tempdir/test.html"; | |
207 | @unlink($tofile); | |
208 | $result = $curl->download_one($testhtml, array(), array('filepath'=>$tofile)); | |
209 | $this->assertTrue($result); | |
210 | $this->assertFileExists($tofile); | |
211 | $this->assertSame($contents, file_get_contents($tofile)); | |
212 | @unlink($tofile); | |
213 | ||
214 | // Test full URL redirects. | |
215 | $testurl = 'http://download.moodle.org/unittest/test_redir.php'; | |
216 | ||
217 | $curl = new curl(); | |
218 | $contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>2)); | |
219 | $this->assertSame(0, $curl->get_errno()); | |
220 | $this->assertSame(2, $curl->info['redirect_count']); | |
221 | $this->assertSame('done', $contents); | |
222 | ||
223 | $curl = new curl(); | |
224 | $curl->emulateredirects = true; | |
225 | $contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>2)); | |
226 | $this->assertSame(0, $curl->get_errno()); | |
227 | $this->assertSame(2, $curl->info['redirect_count']); | |
228 | $this->assertSame('done', $contents); | |
229 | ||
230 | $curl = new curl(); | |
231 | $contents = $curl->get("$testurl?redir=3", array(), array('CURLOPT_FOLLOWLOCATION'=>0)); | |
232 | $this->assertSame(0, $curl->get_errno()); | |
233 | $this->assertSame(302, $curl->info['http_code']); | |
234 | $this->assertSame('', $contents); | |
235 | ||
236 | $curl = new curl(); | |
237 | $curl->emulateredirects = true; | |
238 | $contents = $curl->get("$testurl?redir=3", array(), array('CURLOPT_FOLLOWLOCATION'=>0)); | |
239 | $this->assertSame(0, $curl->get_errno()); | |
240 | $this->assertSame(302, $curl->info['http_code']); | |
241 | $this->assertSame('', $contents); | |
242 | ||
243 | $curl = new curl(); | |
244 | $contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>1)); | |
245 | $this->assertSame(CURLE_TOO_MANY_REDIRECTS, $curl->get_errno()); | |
246 | $this->assertNotEmpty($contents); | |
247 | ||
248 | $curl = new curl(); | |
249 | $curl->emulateredirects = true; | |
250 | $contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>1)); | |
251 | $this->assertSame(CURLE_TOO_MANY_REDIRECTS, $curl->get_errno()); | |
252 | $this->assertNotEmpty($contents); | |
253 | ||
254 | $curl = new curl(); | |
255 | $tofile = "$CFG->tempdir/test.html"; | |
256 | @unlink($tofile); | |
257 | $fp = fopen($tofile, 'w'); | |
258 | $result = $curl->get("$testurl?redir=1", array(), array('CURLOPT_FILE'=>$fp)); | |
259 | $this->assertTrue($result); | |
260 | fclose($fp); | |
261 | $this->assertFileExists($tofile); | |
262 | $this->assertSame('done', file_get_contents($tofile)); | |
263 | @unlink($tofile); | |
264 | ||
265 | $curl = new curl(); | |
266 | $curl->emulateredirects = true; | |
267 | $tofile = "$CFG->tempdir/test.html"; | |
268 | @unlink($tofile); | |
269 | $fp = fopen($tofile, 'w'); | |
270 | $result = $curl->get("$testurl?redir=1", array(), array('CURLOPT_FILE'=>$fp)); | |
271 | $this->assertTrue($result); | |
272 | fclose($fp); | |
273 | $this->assertFileExists($tofile); | |
274 | $this->assertSame('done', file_get_contents($tofile)); | |
275 | @unlink($tofile); | |
276 | ||
277 | $curl = new curl(); | |
278 | $tofile = "$CFG->tempdir/test.html"; | |
279 | @unlink($tofile); | |
280 | $result = $curl->download_one("$testurl?redir=1", array(), array('filepath'=>$tofile)); | |
281 | $this->assertTrue($result); | |
282 | $this->assertFileExists($tofile); | |
283 | $this->assertSame('done', file_get_contents($tofile)); | |
284 | @unlink($tofile); | |
285 | ||
286 | $curl = new curl(); | |
287 | $curl->emulateredirects = true; | |
288 | $tofile = "$CFG->tempdir/test.html"; | |
289 | @unlink($tofile); | |
290 | $result = $curl->download_one("$testurl?redir=1", array(), array('filepath'=>$tofile)); | |
291 | $this->assertTrue($result); | |
292 | $this->assertFileExists($tofile); | |
293 | $this->assertSame('done', file_get_contents($tofile)); | |
294 | @unlink($tofile); | |
295 | ||
35ea5db0 PS |
296 | // Test relative location redirects. |
297 | $testurl = 'http://download.moodle.org/unittest/test_relative_redir.php'; | |
298 | ||
299 | $curl = new curl(); | |
300 | $contents = $curl->get($testurl); | |
301 | $this->assertSame(0, $curl->get_errno()); | |
302 | $this->assertSame(1, $curl->info['redirect_count']); | |
303 | $this->assertSame('done', $contents); | |
304 | ||
305 | $curl = new curl(); | |
306 | $curl->emulateredirects = true; | |
307 | $contents = $curl->get($testurl); | |
308 | $this->assertSame(0, $curl->get_errno()); | |
309 | $this->assertSame(1, $curl->info['redirect_count']); | |
310 | $this->assertSame('done', $contents); | |
311 | ||
35ea5db0 PS |
312 | // Test different redirect types. |
313 | $testurl = 'http://download.moodle.org/unittest/test_relative_redir.php'; | |
314 | ||
315 | $curl = new curl(); | |
316 | $contents = $curl->get("$testurl?type=301"); | |
317 | $this->assertSame(0, $curl->get_errno()); | |
318 | $this->assertSame(1, $curl->info['redirect_count']); | |
319 | $this->assertSame('done', $contents); | |
320 | ||
321 | $curl = new curl(); | |
322 | $curl->emulateredirects = true; | |
323 | $contents = $curl->get("$testurl?type=301"); | |
324 | $this->assertSame(0, $curl->get_errno()); | |
325 | $this->assertSame(1, $curl->info['redirect_count']); | |
326 | $this->assertSame('done', $contents); | |
327 | ||
328 | $curl = new curl(); | |
329 | $contents = $curl->get("$testurl?type=302"); | |
330 | $this->assertSame(0, $curl->get_errno()); | |
331 | $this->assertSame(1, $curl->info['redirect_count']); | |
332 | $this->assertSame('done', $contents); | |
333 | ||
334 | $curl = new curl(); | |
335 | $curl->emulateredirects = true; | |
336 | $contents = $curl->get("$testurl?type=302"); | |
337 | $this->assertSame(0, $curl->get_errno()); | |
338 | $this->assertSame(1, $curl->info['redirect_count']); | |
339 | $this->assertSame('done', $contents); | |
340 | ||
341 | $curl = new curl(); | |
342 | $contents = $curl->get("$testurl?type=303"); | |
343 | $this->assertSame(0, $curl->get_errno()); | |
344 | $this->assertSame(1, $curl->info['redirect_count']); | |
345 | $this->assertSame('done', $contents); | |
346 | ||
347 | $curl = new curl(); | |
348 | $curl->emulateredirects = true; | |
349 | $contents = $curl->get("$testurl?type=303"); | |
350 | $this->assertSame(0, $curl->get_errno()); | |
351 | $this->assertSame(1, $curl->info['redirect_count']); | |
352 | $this->assertSame('done', $contents); | |
353 | ||
354 | $curl = new curl(); | |
355 | $contents = $curl->get("$testurl?type=307"); | |
356 | $this->assertSame(0, $curl->get_errno()); | |
357 | $this->assertSame(1, $curl->info['redirect_count']); | |
358 | $this->assertSame('done', $contents); | |
359 | ||
360 | $curl = new curl(); | |
361 | $curl->emulateredirects = true; | |
362 | $contents = $curl->get("$testurl?type=307"); | |
363 | $this->assertSame(0, $curl->get_errno()); | |
364 | $this->assertSame(1, $curl->info['redirect_count']); | |
365 | $this->assertSame('done', $contents); | |
366 | ||
367 | $curl = new curl(); | |
368 | $contents = $curl->get("$testurl?type=308"); | |
369 | $this->assertSame(0, $curl->get_errno()); | |
370 | $this->assertSame(1, $curl->info['redirect_count']); | |
371 | $this->assertSame('done', $contents); | |
372 | ||
373 | $curl = new curl(); | |
374 | $curl->emulateredirects = true; | |
375 | $contents = $curl->get("$testurl?type=308"); | |
376 | $this->assertSame(0, $curl->get_errno()); | |
377 | $this->assertSame(1, $curl->info['redirect_count']); | |
378 | $this->assertSame('done', $contents); | |
a3d5830a | 379 | } |
67233725 DC |
380 | |
381 | /** | |
382 | * Testing prepare draft area | |
383 | * | |
384 | * @copyright 2012 Dongsheng Cai {@link http://dongsheng.org} | |
385 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
386 | */ | |
387 | public function test_prepare_draft_area() { | |
388 | global $USER, $DB; | |
389 | ||
390 | $this->resetAfterTest(true); | |
391 | ||
392 | $generator = $this->getDataGenerator(); | |
393 | $user = $generator->create_user(); | |
394 | $usercontext = context_user::instance($user->id); | |
395 | $USER = $DB->get_record('user', array('id'=>$user->id)); | |
396 | ||
7051415c DC |
397 | $repositorypluginname = 'user'; |
398 | ||
399 | $args = array(); | |
400 | $args['type'] = $repositorypluginname; | |
401 | $repos = repository::get_instances($args); | |
402 | $userrepository = reset($repos); | |
403 | $this->assertInstanceOf('repository', $userrepository); | |
67233725 DC |
404 | |
405 | $fs = get_file_storage(); | |
7051415c | 406 | |
67233725 DC |
407 | $syscontext = context_system::instance(); |
408 | $component = 'core'; | |
409 | $filearea = 'unittest'; | |
410 | $itemid = 0; | |
411 | $filepath = '/'; | |
412 | $filename = 'test.txt'; | |
413 | $sourcefield = 'Copyright stuff'; | |
414 | ||
415 | $filerecord = array( | |
416 | 'contextid' => $syscontext->id, | |
417 | 'component' => $component, | |
418 | 'filearea' => $filearea, | |
419 | 'itemid' => $itemid, | |
420 | 'filepath' => $filepath, | |
421 | 'filename' => $filename, | |
422 | 'source' => $sourcefield, | |
423 | ); | |
424 | $ref = $fs->pack_reference($filerecord); | |
67233725 | 425 | $originalfile = $fs->create_file_from_string($filerecord, 'Test content'); |
67233725 DC |
426 | $fileid = $originalfile->get_id(); |
427 | $this->assertInstanceOf('stored_file', $originalfile); | |
428 | ||
1d987cb7 | 429 | // Create a user private file. |
7051415c DC |
430 | $userfilerecord = new stdClass; |
431 | $userfilerecord->contextid = $usercontext->id; | |
432 | $userfilerecord->component = 'user'; | |
433 | $userfilerecord->filearea = 'private'; | |
434 | $userfilerecord->itemid = 0; | |
435 | $userfilerecord->filepath = '/'; | |
436 | $userfilerecord->filename = 'userfile.txt'; | |
437 | $userfilerecord->source = 'test'; | |
438 | $userfile = $fs->create_file_from_string($userfilerecord, 'User file content'); | |
439 | $userfileref = $fs->pack_reference($userfilerecord); | |
440 | ||
441 | $filerefrecord = clone((object)$filerecord); | |
442 | $filerefrecord->filename = 'testref.txt'; | |
1d987cb7 PS |
443 | |
444 | // Create a file reference. | |
7051415c DC |
445 | $fileref = $fs->create_file_from_reference($filerefrecord, $userrepository->id, $userfileref); |
446 | $this->assertInstanceOf('stored_file', $fileref); | |
291a3d1f | 447 | $this->assertEquals($userrepository->id, $fileref->get_repository_id()); |
1d987cb7 | 448 | $this->assertSame($userfile->get_contenthash(), $fileref->get_contenthash()); |
7051415c DC |
449 | $this->assertEquals($userfile->get_filesize(), $fileref->get_filesize()); |
450 | $this->assertRegExp('#' . $userfile->get_filename(). '$#', $fileref->get_reference_details()); | |
451 | ||
67233725 DC |
452 | $draftitemid = 0; |
453 | file_prepare_draft_area($draftitemid, $syscontext->id, $component, $filearea, $itemid); | |
454 | ||
455 | $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid); | |
1d987cb7 | 456 | $this->assertCount(3, $draftfiles); |
67233725 DC |
457 | |
458 | $draftfile = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $filepath, $filename); | |
459 | $source = unserialize($draftfile->get_source()); | |
1d987cb7 PS |
460 | $this->assertSame($ref, $source->original); |
461 | $this->assertSame($sourcefield, $source->source); | |
67233725 | 462 | |
7051415c DC |
463 | $draftfileref = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $filepath, $filerefrecord->filename); |
464 | $this->assertInstanceOf('stored_file', $draftfileref); | |
1d987cb7 | 465 | $this->assertTrue($draftfileref->is_external_file()); |
67233725 | 466 | |
1d987cb7 | 467 | // Change some information. |
67233725 DC |
468 | $author = 'Dongsheng Cai'; |
469 | $draftfile->set_author($author); | |
7051415c | 470 | $newsourcefield = 'Get from Flickr'; |
67233725 DC |
471 | $license = 'GPLv3'; |
472 | $draftfile->set_license($license); | |
1d987cb7 | 473 | // If you want to really just change source field, do this. |
67233725 DC |
474 | $source = unserialize($draftfile->get_source()); |
475 | $newsourcefield = 'From flickr'; | |
476 | $source->source = $newsourcefield; | |
477 | $draftfile->set_source(serialize($source)); | |
478 | ||
1d987cb7 | 479 | // Save changed file. |
67233725 DC |
480 | file_save_draft_area_files($draftitemid, $syscontext->id, $component, $filearea, $itemid); |
481 | ||
482 | $file = $fs->get_file($syscontext->id, $component, $filearea, $itemid, $filepath, $filename); | |
483 | ||
1d987cb7 | 484 | // Make sure it's the original file id. |
67233725 DC |
485 | $this->assertEquals($fileid, $file->get_id()); |
486 | $this->assertInstanceOf('stored_file', $file); | |
1d987cb7 PS |
487 | $this->assertSame($author, $file->get_author()); |
488 | $this->assertSame($license, $file->get_license()); | |
67233725 DC |
489 | $this->assertEquals($newsourcefield, $file->get_source()); |
490 | } | |
61506a0a DC |
491 | |
492 | /** | |
1d987cb7 | 493 | * Testing deleting original files. |
61506a0a DC |
494 | * |
495 | * @copyright 2012 Dongsheng Cai {@link http://dongsheng.org} | |
496 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
497 | */ | |
498 | public function test_delete_original_file_from_draft() { | |
499 | global $USER, $DB; | |
500 | ||
501 | $this->resetAfterTest(true); | |
502 | ||
503 | $generator = $this->getDataGenerator(); | |
504 | $user = $generator->create_user(); | |
505 | $usercontext = context_user::instance($user->id); | |
506 | $USER = $DB->get_record('user', array('id'=>$user->id)); | |
507 | ||
508 | $repositorypluginname = 'user'; | |
509 | ||
510 | $args = array(); | |
511 | $args['type'] = $repositorypluginname; | |
512 | $repos = repository::get_instances($args); | |
513 | $userrepository = reset($repos); | |
514 | $this->assertInstanceOf('repository', $userrepository); | |
515 | ||
516 | $fs = get_file_storage(); | |
517 | $syscontext = context_system::instance(); | |
518 | ||
519 | $filecontent = 'User file content'; | |
520 | ||
1d987cb7 | 521 | // Create a user private file. |
61506a0a DC |
522 | $userfilerecord = new stdClass; |
523 | $userfilerecord->contextid = $usercontext->id; | |
524 | $userfilerecord->component = 'user'; | |
525 | $userfilerecord->filearea = 'private'; | |
526 | $userfilerecord->itemid = 0; | |
527 | $userfilerecord->filepath = '/'; | |
528 | $userfilerecord->filename = 'userfile.txt'; | |
529 | $userfilerecord->source = 'test'; | |
530 | $userfile = $fs->create_file_from_string($userfilerecord, $filecontent); | |
531 | $userfileref = $fs->pack_reference($userfilerecord); | |
532 | $contenthash = $userfile->get_contenthash(); | |
533 | ||
534 | $filerecord = array( | |
535 | 'contextid' => $syscontext->id, | |
536 | 'component' => 'core', | |
537 | 'filearea' => 'phpunit', | |
538 | 'itemid' => 0, | |
539 | 'filepath' => '/', | |
540 | 'filename' => 'test.txt', | |
541 | ); | |
1d987cb7 | 542 | // Create a file reference. |
61506a0a DC |
543 | $fileref = $fs->create_file_from_reference($filerecord, $userrepository->id, $userfileref); |
544 | $this->assertInstanceOf('stored_file', $fileref); | |
291a3d1f | 545 | $this->assertEquals($userrepository->id, $fileref->get_repository_id()); |
1d987cb7 | 546 | $this->assertSame($userfile->get_contenthash(), $fileref->get_contenthash()); |
61506a0a DC |
547 | $this->assertEquals($userfile->get_filesize(), $fileref->get_filesize()); |
548 | $this->assertRegExp('#' . $userfile->get_filename(). '$#', $fileref->get_reference_details()); | |
549 | ||
550 | $draftitemid = 0; | |
551 | file_prepare_draft_area($draftitemid, $usercontext->id, 'user', 'private', 0); | |
552 | $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid); | |
1d987cb7 | 553 | $this->assertCount(2, $draftfiles); |
61506a0a DC |
554 | $draftfile = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $userfilerecord->filepath, $userfilerecord->filename); |
555 | $draftfile->delete(); | |
1d987cb7 | 556 | // Save changed file. |
61506a0a DC |
557 | file_save_draft_area_files($draftitemid, $usercontext->id, 'user', 'private', 0); |
558 | ||
1d987cb7 | 559 | // The file reference should be a regular moodle file now. |
61506a0a | 560 | $fileref = $fs->get_file($syscontext->id, 'core', 'phpunit', 0, '/', 'test.txt'); |
1d987cb7 PS |
561 | $this->assertFalse($fileref->is_external_file()); |
562 | $this->assertSame($contenthash, $fileref->get_contenthash()); | |
61506a0a DC |
563 | $this->assertEquals($filecontent, $fileref->get_content()); |
564 | } | |
85cb4b65 | 565 | |
566 | /** | |
567 | * Tests the strip_double_headers function in the curl class. | |
568 | */ | |
569 | public function test_curl_strip_double_headers() { | |
570 | // Example from issue tracker. | |
571 | $mdl30648example = <<<EOF | |
572 | HTTP/1.0 407 Proxy Authentication Required | |
573 | Server: squid/2.7.STABLE9 | |
574 | Date: Thu, 08 Dec 2011 14:44:33 GMT | |
575 | Content-Type: text/html | |
576 | Content-Length: 1275 | |
577 | X-Squid-Error: ERR_CACHE_ACCESS_DENIED 0 | |
578 | Proxy-Authenticate: Basic realm="Squid proxy-caching web server" | |
579 | X-Cache: MISS from homer.lancs.ac.uk | |
580 | X-Cache-Lookup: NONE from homer.lancs.ac.uk:3128 | |
581 | Via: 1.0 homer.lancs.ac.uk:3128 (squid/2.7.STABLE9) | |
582 | Connection: close | |
583 | ||
584 | HTTP/1.0 200 OK | |
585 | Server: Apache | |
586 | X-Lb-Nocache: true | |
587 | Cache-Control: private, max-age=15 | |
588 | ETag: "4d69af5d8ba873ea9192c489e151bd7b" | |
589 | Content-Type: text/html | |
590 | Date: Thu, 08 Dec 2011 14:44:53 GMT | |
591 | Set-Cookie: BBC-UID=c4de2e109c8df6a51de627cee11b214bd4fb6054a030222488317afb31b343360MoodleBot/1.0; expires=Mon, 07-Dec-15 14:44:53 GMT; path=/; domain=bbc.co.uk | |
592 | X-Cache-Action: MISS | |
593 | X-Cache-Age: 0 | |
594 | 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 | |
595 | X-Cache: MISS from ww | |
596 | ||
597 | <html>... | |
598 | EOF; | |
599 | $mdl30648expected = <<<EOF | |
600 | HTTP/1.0 200 OK | |
601 | Server: Apache | |
602 | X-Lb-Nocache: true | |
603 | Cache-Control: private, max-age=15 | |
604 | ETag: "4d69af5d8ba873ea9192c489e151bd7b" | |
605 | Content-Type: text/html | |
606 | Date: Thu, 08 Dec 2011 14:44:53 GMT | |
607 | Set-Cookie: BBC-UID=c4de2e109c8df6a51de627cee11b214bd4fb6054a030222488317afb31b343360MoodleBot/1.0; expires=Mon, 07-Dec-15 14:44:53 GMT; path=/; domain=bbc.co.uk | |
608 | X-Cache-Action: MISS | |
609 | X-Cache-Age: 0 | |
610 | 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 | |
611 | X-Cache: MISS from ww | |
612 | ||
613 | <html>... | |
614 | EOF; | |
615 | // For HTTP, replace the \n with \r\n. | |
616 | $mdl30648example = preg_replace("~(?!<\r)\n~", "\r\n", $mdl30648example); | |
617 | $mdl30648expected = preg_replace("~(?!<\r)\n~", "\r\n", $mdl30648expected); | |
618 | ||
619 | // Test stripping works OK. | |
1d987cb7 | 620 | $this->assertSame($mdl30648expected, curl::strip_double_headers($mdl30648example)); |
85cb4b65 | 621 | // Test it does nothing to the 'plain' data. |
1d987cb7 | 622 | $this->assertSame($mdl30648expected, curl::strip_double_headers($mdl30648expected)); |
85cb4b65 | 623 | |
624 | // Example from OU proxy. | |
625 | $httpsexample = <<<EOF | |
626 | HTTP/1.0 200 Connection established | |
627 | ||
628 | HTTP/1.1 200 OK | |
629 | Date: Fri, 22 Feb 2013 17:14:23 GMT | |
630 | Server: Apache/2 | |
631 | X-Powered-By: PHP/5.3.3-7+squeeze14 | |
632 | Content-Type: text/xml | |
633 | Connection: close | |
634 | Content-Encoding: gzip | |
635 | Transfer-Encoding: chunked | |
636 | ||
637 | <?xml version="1.0" encoding="ISO-8859-1" ?> | |
638 | <rss version="2.0">... | |
639 | EOF; | |
640 | $httpsexpected = <<<EOF | |
641 | HTTP/1.1 200 OK | |
642 | Date: Fri, 22 Feb 2013 17:14:23 GMT | |
643 | Server: Apache/2 | |
644 | X-Powered-By: PHP/5.3.3-7+squeeze14 | |
645 | Content-Type: text/xml | |
646 | Connection: close | |
647 | Content-Encoding: gzip | |
648 | Transfer-Encoding: chunked | |
649 | ||
650 | <?xml version="1.0" encoding="ISO-8859-1" ?> | |
651 | <rss version="2.0">... | |
652 | EOF; | |
653 | // For HTTP, replace the \n with \r\n. | |
654 | $httpsexample = preg_replace("~(?!<\r)\n~", "\r\n", $httpsexample); | |
655 | $httpsexpected = preg_replace("~(?!<\r)\n~", "\r\n", $httpsexpected); | |
656 | ||
657 | // Test stripping works OK. | |
1d987cb7 | 658 | $this->assertSame($httpsexpected, curl::strip_double_headers($httpsexample)); |
85cb4b65 | 659 | // Test it does nothing to the 'plain' data. |
1d987cb7 | 660 | $this->assertSame($httpsexpected, curl::strip_double_headers($httpsexpected)); |
85cb4b65 | 661 | } |
a3d5830a | 662 | } |