Merge branch 'MDL-44349-master' of https://github.com/lucisgit/moodle
[moodle.git] / lib / tests / filelib_test.php
CommitLineData
a3d5830a
PS
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
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
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29require_once($CFG->libdir . '/filelib.php');
ac29403e 30require_once($CFG->dirroot . '/repository/lib.php');
a3d5830a 31
1d987cb7 32class 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));
b1652d6b 132 // Do not test the response starts with DOCTYPE here because some servers may return different headers.
35ea5db0
PS
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 /**
74c4405b 180 * Test curl basics.
35ea5db0 181 */
74c4405b 182 public function test_curl_basics() {
35ea5db0
PS
183 global $CFG;
184
74c4405b 185 // Test HTTP 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
74c4405b
FM
213 // Test 404 request.
214 $curl = new curl();
215 $contents = $curl->get($this->getExternalTestFileUrl('/i.do.not.exist'));
216 $response = $curl->getResponse();
217 $this->assertSame('404 Not Found', reset($response));
218 $this->assertSame(0, $curl->get_errno());
219 }
220
221 public function test_curl_redirects() {
222 global $CFG;
223
35ea5db0 224 // Test full URL redirects.
a9d2f1b4 225 $testurl = $this->getExternalTestFileUrl('/test_redir.php');
35ea5db0
PS
226
227 $curl = new curl();
228 $contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>2));
74c4405b
FM
229 $response = $curl->getResponse();
230 $this->assertSame('200 OK', reset($response));
35ea5db0
PS
231 $this->assertSame(0, $curl->get_errno());
232 $this->assertSame(2, $curl->info['redirect_count']);
233 $this->assertSame('done', $contents);
234
235 $curl = new curl();
236 $curl->emulateredirects = true;
237 $contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>2));
74c4405b
FM
238 $response = $curl->getResponse();
239 $this->assertSame('200 OK', reset($response));
35ea5db0
PS
240 $this->assertSame(0, $curl->get_errno());
241 $this->assertSame(2, $curl->info['redirect_count']);
242 $this->assertSame('done', $contents);
243
244 $curl = new curl();
245 $contents = $curl->get("$testurl?redir=3", array(), array('CURLOPT_FOLLOWLOCATION'=>0));
74c4405b
FM
246 $response = $curl->getResponse();
247 $this->assertSame('302 Found', reset($response));
35ea5db0
PS
248 $this->assertSame(0, $curl->get_errno());
249 $this->assertSame(302, $curl->info['http_code']);
250 $this->assertSame('', $contents);
251
252 $curl = new curl();
253 $curl->emulateredirects = true;
254 $contents = $curl->get("$testurl?redir=3", array(), array('CURLOPT_FOLLOWLOCATION'=>0));
74c4405b
FM
255 $response = $curl->getResponse();
256 $this->assertSame('302 Found', reset($response));
35ea5db0
PS
257 $this->assertSame(0, $curl->get_errno());
258 $this->assertSame(302, $curl->info['http_code']);
259 $this->assertSame('', $contents);
260
261 $curl = new curl();
262 $contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>1));
263 $this->assertSame(CURLE_TOO_MANY_REDIRECTS, $curl->get_errno());
264 $this->assertNotEmpty($contents);
265
266 $curl = new curl();
267 $curl->emulateredirects = true;
268 $contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>1));
269 $this->assertSame(CURLE_TOO_MANY_REDIRECTS, $curl->get_errno());
270 $this->assertNotEmpty($contents);
271
272 $curl = new curl();
273 $tofile = "$CFG->tempdir/test.html";
274 @unlink($tofile);
275 $fp = fopen($tofile, 'w');
276 $result = $curl->get("$testurl?redir=1", array(), array('CURLOPT_FILE'=>$fp));
277 $this->assertTrue($result);
278 fclose($fp);
279 $this->assertFileExists($tofile);
280 $this->assertSame('done', file_get_contents($tofile));
281 @unlink($tofile);
282
283 $curl = new curl();
284 $curl->emulateredirects = true;
285 $tofile = "$CFG->tempdir/test.html";
286 @unlink($tofile);
287 $fp = fopen($tofile, 'w');
288 $result = $curl->get("$testurl?redir=1", array(), array('CURLOPT_FILE'=>$fp));
289 $this->assertTrue($result);
290 fclose($fp);
291 $this->assertFileExists($tofile);
292 $this->assertSame('done', file_get_contents($tofile));
293 @unlink($tofile);
294
295 $curl = new curl();
296 $tofile = "$CFG->tempdir/test.html";
297 @unlink($tofile);
298 $result = $curl->download_one("$testurl?redir=1", array(), array('filepath'=>$tofile));
299 $this->assertTrue($result);
300 $this->assertFileExists($tofile);
301 $this->assertSame('done', file_get_contents($tofile));
302 @unlink($tofile);
303
304 $curl = new curl();
305 $curl->emulateredirects = true;
306 $tofile = "$CFG->tempdir/test.html";
307 @unlink($tofile);
308 $result = $curl->download_one("$testurl?redir=1", array(), array('filepath'=>$tofile));
309 $this->assertTrue($result);
310 $this->assertFileExists($tofile);
311 $this->assertSame('done', file_get_contents($tofile));
312 @unlink($tofile);
74c4405b 313 }
35ea5db0 314
74c4405b 315 public function test_curl_relative_redirects() {
35ea5db0 316 // Test relative location redirects.
a9d2f1b4 317 $testurl = $this->getExternalTestFileUrl('/test_relative_redir.php');
35ea5db0
PS
318
319 $curl = new curl();
320 $contents = $curl->get($testurl);
74c4405b
FM
321 $response = $curl->getResponse();
322 $this->assertSame('200 OK', reset($response));
35ea5db0
PS
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 $curl->emulateredirects = true;
329 $contents = $curl->get($testurl);
74c4405b
FM
330 $response = $curl->getResponse();
331 $this->assertSame('200 OK', reset($response));
35ea5db0
PS
332 $this->assertSame(0, $curl->get_errno());
333 $this->assertSame(1, $curl->info['redirect_count']);
334 $this->assertSame('done', $contents);
335
35ea5db0 336 // Test different redirect types.
a9d2f1b4 337 $testurl = $this->getExternalTestFileUrl('/test_relative_redir.php');
35ea5db0
PS
338
339 $curl = new curl();
340 $contents = $curl->get("$testurl?type=301");
74c4405b
FM
341 $response = $curl->getResponse();
342 $this->assertSame('200 OK', reset($response));
35ea5db0
PS
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=301");
74c4405b
FM
350 $response = $curl->getResponse();
351 $this->assertSame('200 OK', reset($response));
35ea5db0
PS
352 $this->assertSame(0, $curl->get_errno());
353 $this->assertSame(1, $curl->info['redirect_count']);
354 $this->assertSame('done', $contents);
355
356 $curl = new curl();
357 $contents = $curl->get("$testurl?type=302");
74c4405b
FM
358 $response = $curl->getResponse();
359 $this->assertSame('200 OK', reset($response));
35ea5db0
PS
360 $this->assertSame(0, $curl->get_errno());
361 $this->assertSame(1, $curl->info['redirect_count']);
362 $this->assertSame('done', $contents);
363
364 $curl = new curl();
365 $curl->emulateredirects = true;
366 $contents = $curl->get("$testurl?type=302");
74c4405b
FM
367 $response = $curl->getResponse();
368 $this->assertSame('200 OK', reset($response));
35ea5db0
PS
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 $contents = $curl->get("$testurl?type=303");
74c4405b
FM
375 $response = $curl->getResponse();
376 $this->assertSame('200 OK', reset($response));
35ea5db0
PS
377 $this->assertSame(0, $curl->get_errno());
378 $this->assertSame(1, $curl->info['redirect_count']);
379 $this->assertSame('done', $contents);
380
381 $curl = new curl();
382 $curl->emulateredirects = true;
383 $contents = $curl->get("$testurl?type=303");
74c4405b
FM
384 $response = $curl->getResponse();
385 $this->assertSame('200 OK', reset($response));
35ea5db0
PS
386 $this->assertSame(0, $curl->get_errno());
387 $this->assertSame(1, $curl->info['redirect_count']);
388 $this->assertSame('done', $contents);
389
390 $curl = new curl();
391 $contents = $curl->get("$testurl?type=307");
74c4405b
FM
392 $response = $curl->getResponse();
393 $this->assertSame('200 OK', reset($response));
35ea5db0
PS
394 $this->assertSame(0, $curl->get_errno());
395 $this->assertSame(1, $curl->info['redirect_count']);
396 $this->assertSame('done', $contents);
397
398 $curl = new curl();
399 $curl->emulateredirects = true;
400 $contents = $curl->get("$testurl?type=307");
74c4405b
FM
401 $response = $curl->getResponse();
402 $this->assertSame('200 OK', reset($response));
35ea5db0
PS
403 $this->assertSame(0, $curl->get_errno());
404 $this->assertSame(1, $curl->info['redirect_count']);
405 $this->assertSame('done', $contents);
406
407 $curl = new curl();
408 $contents = $curl->get("$testurl?type=308");
74c4405b
FM
409 $response = $curl->getResponse();
410 $this->assertSame('200 OK', reset($response));
35ea5db0
PS
411 $this->assertSame(0, $curl->get_errno());
412 $this->assertSame(1, $curl->info['redirect_count']);
413 $this->assertSame('done', $contents);
414
415 $curl = new curl();
416 $curl->emulateredirects = true;
417 $contents = $curl->get("$testurl?type=308");
74c4405b
FM
418 $response = $curl->getResponse();
419 $this->assertSame('200 OK', reset($response));
35ea5db0
PS
420 $this->assertSame(0, $curl->get_errno());
421 $this->assertSame(1, $curl->info['redirect_count']);
422 $this->assertSame('done', $contents);
ba72e295 423
74c4405b
FM
424 }
425
426 public function test_curl_proxybypass() {
427 global $CFG;
428 $testurl = $this->getExternalTestFileUrl('/test.html');
429
ba72e295
FM
430 $oldproxy = $CFG->proxyhost;
431 $oldproxybypass = $CFG->proxybypass;
432
433 // Test without proxy bypass and inaccessible proxy.
434 $CFG->proxyhost = 'i.do.not.exist';
435 $CFG->proxybypass = '';
436 $curl = new curl();
74c4405b 437 $contents = $curl->get($testurl);
ba72e295
FM
438 $this->assertNotEquals(0, $curl->get_errno());
439 $this->assertNotEquals('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
440
441 // Test with proxy bypass.
74c4405b
FM
442 $testurlhost = parse_url($testurl, PHP_URL_HOST);
443 $CFG->proxybypass = $testurlhost;
ba72e295 444 $curl = new curl();
74c4405b 445 $contents = $curl->get($testurl);
ba72e295
FM
446 $this->assertSame(0, $curl->get_errno());
447 $this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
448
449 $CFG->proxyhost = $oldproxy;
450 $CFG->proxybypass = $oldproxybypass;
a3d5830a 451 }
67233725 452
74c4405b
FM
453 public function test_curl_post() {
454 $testurl = $this->getExternalTestFileUrl('/test_post.php');
102230b2
FM
455
456 // Test post request.
457 $curl = new curl();
74c4405b 458 $contents = $curl->post($testurl, 'data=moodletest');
102230b2
FM
459 $response = $curl->getResponse();
460 $this->assertSame('200 OK', reset($response));
461 $this->assertSame(0, $curl->get_errno());
462 $this->assertSame('OK', $contents);
463
464 // Test 100 requests.
465 $curl = new curl();
466 $curl->setHeader('Expect: 100-continue');
74c4405b 467 $contents = $curl->post($testurl, 'data=moodletest');
102230b2
FM
468 $response = $curl->getResponse();
469 $this->assertSame('200 OK', reset($response));
470 $this->assertSame(0, $curl->get_errno());
471 $this->assertSame('OK', $contents);
472 }
473
67233725
DC
474 /**
475 * Testing prepare draft area
476 *
477 * @copyright 2012 Dongsheng Cai {@link http://dongsheng.org}
478 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
479 */
480 public function test_prepare_draft_area() {
481 global $USER, $DB;
482
483 $this->resetAfterTest(true);
484
485 $generator = $this->getDataGenerator();
486 $user = $generator->create_user();
487 $usercontext = context_user::instance($user->id);
488 $USER = $DB->get_record('user', array('id'=>$user->id));
489
7051415c
DC
490 $repositorypluginname = 'user';
491
492 $args = array();
493 $args['type'] = $repositorypluginname;
494 $repos = repository::get_instances($args);
495 $userrepository = reset($repos);
496 $this->assertInstanceOf('repository', $userrepository);
67233725
DC
497
498 $fs = get_file_storage();
7051415c 499
67233725
DC
500 $syscontext = context_system::instance();
501 $component = 'core';
502 $filearea = 'unittest';
503 $itemid = 0;
504 $filepath = '/';
505 $filename = 'test.txt';
506 $sourcefield = 'Copyright stuff';
507
508 $filerecord = array(
509 'contextid' => $syscontext->id,
510 'component' => $component,
511 'filearea' => $filearea,
512 'itemid' => $itemid,
513 'filepath' => $filepath,
514 'filename' => $filename,
515 'source' => $sourcefield,
516 );
517 $ref = $fs->pack_reference($filerecord);
67233725 518 $originalfile = $fs->create_file_from_string($filerecord, 'Test content');
67233725
DC
519 $fileid = $originalfile->get_id();
520 $this->assertInstanceOf('stored_file', $originalfile);
521
1d987cb7 522 // Create a user private file.
7051415c
DC
523 $userfilerecord = new stdClass;
524 $userfilerecord->contextid = $usercontext->id;
525 $userfilerecord->component = 'user';
526 $userfilerecord->filearea = 'private';
527 $userfilerecord->itemid = 0;
528 $userfilerecord->filepath = '/';
529 $userfilerecord->filename = 'userfile.txt';
530 $userfilerecord->source = 'test';
531 $userfile = $fs->create_file_from_string($userfilerecord, 'User file content');
532 $userfileref = $fs->pack_reference($userfilerecord);
533
534 $filerefrecord = clone((object)$filerecord);
535 $filerefrecord->filename = 'testref.txt';
1d987cb7
PS
536
537 // Create a file reference.
7051415c
DC
538 $fileref = $fs->create_file_from_reference($filerefrecord, $userrepository->id, $userfileref);
539 $this->assertInstanceOf('stored_file', $fileref);
291a3d1f 540 $this->assertEquals($userrepository->id, $fileref->get_repository_id());
1d987cb7 541 $this->assertSame($userfile->get_contenthash(), $fileref->get_contenthash());
7051415c
DC
542 $this->assertEquals($userfile->get_filesize(), $fileref->get_filesize());
543 $this->assertRegExp('#' . $userfile->get_filename(). '$#', $fileref->get_reference_details());
544
67233725
DC
545 $draftitemid = 0;
546 file_prepare_draft_area($draftitemid, $syscontext->id, $component, $filearea, $itemid);
547
548 $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid);
1d987cb7 549 $this->assertCount(3, $draftfiles);
67233725
DC
550
551 $draftfile = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $filepath, $filename);
552 $source = unserialize($draftfile->get_source());
1d987cb7
PS
553 $this->assertSame($ref, $source->original);
554 $this->assertSame($sourcefield, $source->source);
67233725 555
7051415c
DC
556 $draftfileref = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $filepath, $filerefrecord->filename);
557 $this->assertInstanceOf('stored_file', $draftfileref);
1d987cb7 558 $this->assertTrue($draftfileref->is_external_file());
67233725 559
1d987cb7 560 // Change some information.
67233725
DC
561 $author = 'Dongsheng Cai';
562 $draftfile->set_author($author);
7051415c 563 $newsourcefield = 'Get from Flickr';
67233725
DC
564 $license = 'GPLv3';
565 $draftfile->set_license($license);
1d987cb7 566 // If you want to really just change source field, do this.
67233725
DC
567 $source = unserialize($draftfile->get_source());
568 $newsourcefield = 'From flickr';
569 $source->source = $newsourcefield;
570 $draftfile->set_source(serialize($source));
571
1d987cb7 572 // Save changed file.
67233725
DC
573 file_save_draft_area_files($draftitemid, $syscontext->id, $component, $filearea, $itemid);
574
575 $file = $fs->get_file($syscontext->id, $component, $filearea, $itemid, $filepath, $filename);
576
1d987cb7 577 // Make sure it's the original file id.
67233725
DC
578 $this->assertEquals($fileid, $file->get_id());
579 $this->assertInstanceOf('stored_file', $file);
1d987cb7
PS
580 $this->assertSame($author, $file->get_author());
581 $this->assertSame($license, $file->get_license());
67233725
DC
582 $this->assertEquals($newsourcefield, $file->get_source());
583 }
61506a0a
DC
584
585 /**
1d987cb7 586 * Testing deleting original files.
61506a0a
DC
587 *
588 * @copyright 2012 Dongsheng Cai {@link http://dongsheng.org}
589 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
590 */
591 public function test_delete_original_file_from_draft() {
592 global $USER, $DB;
593
594 $this->resetAfterTest(true);
595
596 $generator = $this->getDataGenerator();
597 $user = $generator->create_user();
598 $usercontext = context_user::instance($user->id);
599 $USER = $DB->get_record('user', array('id'=>$user->id));
600
601 $repositorypluginname = 'user';
602
603 $args = array();
604 $args['type'] = $repositorypluginname;
605 $repos = repository::get_instances($args);
606 $userrepository = reset($repos);
607 $this->assertInstanceOf('repository', $userrepository);
608
609 $fs = get_file_storage();
610 $syscontext = context_system::instance();
611
612 $filecontent = 'User file content';
613
1d987cb7 614 // Create a user private file.
61506a0a
DC
615 $userfilerecord = new stdClass;
616 $userfilerecord->contextid = $usercontext->id;
617 $userfilerecord->component = 'user';
618 $userfilerecord->filearea = 'private';
619 $userfilerecord->itemid = 0;
620 $userfilerecord->filepath = '/';
621 $userfilerecord->filename = 'userfile.txt';
622 $userfilerecord->source = 'test';
623 $userfile = $fs->create_file_from_string($userfilerecord, $filecontent);
624 $userfileref = $fs->pack_reference($userfilerecord);
625 $contenthash = $userfile->get_contenthash();
626
627 $filerecord = array(
628 'contextid' => $syscontext->id,
629 'component' => 'core',
630 'filearea' => 'phpunit',
631 'itemid' => 0,
632 'filepath' => '/',
633 'filename' => 'test.txt',
634 );
1d987cb7 635 // Create a file reference.
61506a0a
DC
636 $fileref = $fs->create_file_from_reference($filerecord, $userrepository->id, $userfileref);
637 $this->assertInstanceOf('stored_file', $fileref);
291a3d1f 638 $this->assertEquals($userrepository->id, $fileref->get_repository_id());
1d987cb7 639 $this->assertSame($userfile->get_contenthash(), $fileref->get_contenthash());
61506a0a
DC
640 $this->assertEquals($userfile->get_filesize(), $fileref->get_filesize());
641 $this->assertRegExp('#' . $userfile->get_filename(). '$#', $fileref->get_reference_details());
642
643 $draftitemid = 0;
644 file_prepare_draft_area($draftitemid, $usercontext->id, 'user', 'private', 0);
645 $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid);
1d987cb7 646 $this->assertCount(2, $draftfiles);
61506a0a
DC
647 $draftfile = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $userfilerecord->filepath, $userfilerecord->filename);
648 $draftfile->delete();
1d987cb7 649 // Save changed file.
61506a0a
DC
650 file_save_draft_area_files($draftitemid, $usercontext->id, 'user', 'private', 0);
651
1d987cb7 652 // The file reference should be a regular moodle file now.
61506a0a 653 $fileref = $fs->get_file($syscontext->id, 'core', 'phpunit', 0, '/', 'test.txt');
1d987cb7
PS
654 $this->assertFalse($fileref->is_external_file());
655 $this->assertSame($contenthash, $fileref->get_contenthash());
61506a0a
DC
656 $this->assertEquals($filecontent, $fileref->get_content());
657 }
85cb4b65 658
659 /**
660 * Tests the strip_double_headers function in the curl class.
661 */
662 public function test_curl_strip_double_headers() {
663 // Example from issue tracker.
664 $mdl30648example = <<<EOF
665HTTP/1.0 407 Proxy Authentication Required
666Server: squid/2.7.STABLE9
667Date: Thu, 08 Dec 2011 14:44:33 GMT
668Content-Type: text/html
669Content-Length: 1275
670X-Squid-Error: ERR_CACHE_ACCESS_DENIED 0
671Proxy-Authenticate: Basic realm="Squid proxy-caching web server"
672X-Cache: MISS from homer.lancs.ac.uk
673X-Cache-Lookup: NONE from homer.lancs.ac.uk:3128
674Via: 1.0 homer.lancs.ac.uk:3128 (squid/2.7.STABLE9)
675Connection: close
676
677HTTP/1.0 200 OK
678Server: Apache
679X-Lb-Nocache: true
c3a3f540 680Cache-Control: private, max-age=15, no-transform
85cb4b65 681ETag: "4d69af5d8ba873ea9192c489e151bd7b"
682Content-Type: text/html
683Date: Thu, 08 Dec 2011 14:44:53 GMT
684Set-Cookie: BBC-UID=c4de2e109c8df6a51de627cee11b214bd4fb6054a030222488317afb31b343360MoodleBot/1.0; expires=Mon, 07-Dec-15 14:44:53 GMT; path=/; domain=bbc.co.uk
685X-Cache-Action: MISS
686X-Cache-Age: 0
687Vary: 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
688X-Cache: MISS from ww
689
690<html>...
691EOF;
692 $mdl30648expected = <<<EOF
693HTTP/1.0 200 OK
694Server: Apache
695X-Lb-Nocache: true
c3a3f540 696Cache-Control: private, max-age=15, no-transform
85cb4b65 697ETag: "4d69af5d8ba873ea9192c489e151bd7b"
698Content-Type: text/html
699Date: Thu, 08 Dec 2011 14:44:53 GMT
700Set-Cookie: BBC-UID=c4de2e109c8df6a51de627cee11b214bd4fb6054a030222488317afb31b343360MoodleBot/1.0; expires=Mon, 07-Dec-15 14:44:53 GMT; path=/; domain=bbc.co.uk
701X-Cache-Action: MISS
702X-Cache-Age: 0
703Vary: 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
704X-Cache: MISS from ww
705
706<html>...
707EOF;
708 // For HTTP, replace the \n with \r\n.
709 $mdl30648example = preg_replace("~(?!<\r)\n~", "\r\n", $mdl30648example);
710 $mdl30648expected = preg_replace("~(?!<\r)\n~", "\r\n", $mdl30648expected);
711
712 // Test stripping works OK.
1d987cb7 713 $this->assertSame($mdl30648expected, curl::strip_double_headers($mdl30648example));
85cb4b65 714 // Test it does nothing to the 'plain' data.
1d987cb7 715 $this->assertSame($mdl30648expected, curl::strip_double_headers($mdl30648expected));
85cb4b65 716
717 // Example from OU proxy.
718 $httpsexample = <<<EOF
719HTTP/1.0 200 Connection established
720
721HTTP/1.1 200 OK
722Date: Fri, 22 Feb 2013 17:14:23 GMT
723Server: Apache/2
724X-Powered-By: PHP/5.3.3-7+squeeze14
725Content-Type: text/xml
726Connection: close
727Content-Encoding: gzip
728Transfer-Encoding: chunked
729
730<?xml version="1.0" encoding="ISO-8859-1" ?>
731<rss version="2.0">...
732EOF;
733 $httpsexpected = <<<EOF
734HTTP/1.1 200 OK
735Date: Fri, 22 Feb 2013 17:14:23 GMT
736Server: Apache/2
737X-Powered-By: PHP/5.3.3-7+squeeze14
738Content-Type: text/xml
739Connection: close
740Content-Encoding: gzip
741Transfer-Encoding: chunked
742
743<?xml version="1.0" encoding="ISO-8859-1" ?>
744<rss version="2.0">...
745EOF;
746 // For HTTP, replace the \n with \r\n.
747 $httpsexample = preg_replace("~(?!<\r)\n~", "\r\n", $httpsexample);
748 $httpsexpected = preg_replace("~(?!<\r)\n~", "\r\n", $httpsexpected);
749
750 // Test stripping works OK.
1d987cb7 751 $this->assertSame($httpsexpected, curl::strip_double_headers($httpsexample));
85cb4b65 752 // Test it does nothing to the 'plain' data.
1d987cb7 753 $this->assertSame($httpsexpected, curl::strip_double_headers($httpsexpected));
85cb4b65 754 }
a3d5830a 755}