weekly back-to-dev release 2.7dev
[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));
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);
a3d5830a 378 }
67233725
DC
379
380 /**
381 * Testing prepare draft area
382 *
383 * @copyright 2012 Dongsheng Cai {@link http://dongsheng.org}
384 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
385 */
386 public function test_prepare_draft_area() {
387 global $USER, $DB;
388
389 $this->resetAfterTest(true);
390
391 $generator = $this->getDataGenerator();
392 $user = $generator->create_user();
393 $usercontext = context_user::instance($user->id);
394 $USER = $DB->get_record('user', array('id'=>$user->id));
395
7051415c
DC
396 $repositorypluginname = 'user';
397
398 $args = array();
399 $args['type'] = $repositorypluginname;
400 $repos = repository::get_instances($args);
401 $userrepository = reset($repos);
402 $this->assertInstanceOf('repository', $userrepository);
67233725
DC
403
404 $fs = get_file_storage();
7051415c 405
67233725
DC
406 $syscontext = context_system::instance();
407 $component = 'core';
408 $filearea = 'unittest';
409 $itemid = 0;
410 $filepath = '/';
411 $filename = 'test.txt';
412 $sourcefield = 'Copyright stuff';
413
414 $filerecord = array(
415 'contextid' => $syscontext->id,
416 'component' => $component,
417 'filearea' => $filearea,
418 'itemid' => $itemid,
419 'filepath' => $filepath,
420 'filename' => $filename,
421 'source' => $sourcefield,
422 );
423 $ref = $fs->pack_reference($filerecord);
67233725 424 $originalfile = $fs->create_file_from_string($filerecord, 'Test content');
67233725
DC
425 $fileid = $originalfile->get_id();
426 $this->assertInstanceOf('stored_file', $originalfile);
427
1d987cb7 428 // Create a user private file.
7051415c
DC
429 $userfilerecord = new stdClass;
430 $userfilerecord->contextid = $usercontext->id;
431 $userfilerecord->component = 'user';
432 $userfilerecord->filearea = 'private';
433 $userfilerecord->itemid = 0;
434 $userfilerecord->filepath = '/';
435 $userfilerecord->filename = 'userfile.txt';
436 $userfilerecord->source = 'test';
437 $userfile = $fs->create_file_from_string($userfilerecord, 'User file content');
438 $userfileref = $fs->pack_reference($userfilerecord);
439
440 $filerefrecord = clone((object)$filerecord);
441 $filerefrecord->filename = 'testref.txt';
1d987cb7
PS
442
443 // Create a file reference.
7051415c
DC
444 $fileref = $fs->create_file_from_reference($filerefrecord, $userrepository->id, $userfileref);
445 $this->assertInstanceOf('stored_file', $fileref);
291a3d1f 446 $this->assertEquals($userrepository->id, $fileref->get_repository_id());
1d987cb7 447 $this->assertSame($userfile->get_contenthash(), $fileref->get_contenthash());
7051415c
DC
448 $this->assertEquals($userfile->get_filesize(), $fileref->get_filesize());
449 $this->assertRegExp('#' . $userfile->get_filename(). '$#', $fileref->get_reference_details());
450
67233725
DC
451 $draftitemid = 0;
452 file_prepare_draft_area($draftitemid, $syscontext->id, $component, $filearea, $itemid);
453
454 $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid);
1d987cb7 455 $this->assertCount(3, $draftfiles);
67233725
DC
456
457 $draftfile = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $filepath, $filename);
458 $source = unserialize($draftfile->get_source());
1d987cb7
PS
459 $this->assertSame($ref, $source->original);
460 $this->assertSame($sourcefield, $source->source);
67233725 461
7051415c
DC
462 $draftfileref = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $filepath, $filerefrecord->filename);
463 $this->assertInstanceOf('stored_file', $draftfileref);
1d987cb7 464 $this->assertTrue($draftfileref->is_external_file());
67233725 465
1d987cb7 466 // Change some information.
67233725
DC
467 $author = 'Dongsheng Cai';
468 $draftfile->set_author($author);
7051415c 469 $newsourcefield = 'Get from Flickr';
67233725
DC
470 $license = 'GPLv3';
471 $draftfile->set_license($license);
1d987cb7 472 // If you want to really just change source field, do this.
67233725
DC
473 $source = unserialize($draftfile->get_source());
474 $newsourcefield = 'From flickr';
475 $source->source = $newsourcefield;
476 $draftfile->set_source(serialize($source));
477
1d987cb7 478 // Save changed file.
67233725
DC
479 file_save_draft_area_files($draftitemid, $syscontext->id, $component, $filearea, $itemid);
480
481 $file = $fs->get_file($syscontext->id, $component, $filearea, $itemid, $filepath, $filename);
482
1d987cb7 483 // Make sure it's the original file id.
67233725
DC
484 $this->assertEquals($fileid, $file->get_id());
485 $this->assertInstanceOf('stored_file', $file);
1d987cb7
PS
486 $this->assertSame($author, $file->get_author());
487 $this->assertSame($license, $file->get_license());
67233725
DC
488 $this->assertEquals($newsourcefield, $file->get_source());
489 }
61506a0a
DC
490
491 /**
1d987cb7 492 * Testing deleting original files.
61506a0a
DC
493 *
494 * @copyright 2012 Dongsheng Cai {@link http://dongsheng.org}
495 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
496 */
497 public function test_delete_original_file_from_draft() {
498 global $USER, $DB;
499
500 $this->resetAfterTest(true);
501
502 $generator = $this->getDataGenerator();
503 $user = $generator->create_user();
504 $usercontext = context_user::instance($user->id);
505 $USER = $DB->get_record('user', array('id'=>$user->id));
506
507 $repositorypluginname = 'user';
508
509 $args = array();
510 $args['type'] = $repositorypluginname;
511 $repos = repository::get_instances($args);
512 $userrepository = reset($repos);
513 $this->assertInstanceOf('repository', $userrepository);
514
515 $fs = get_file_storage();
516 $syscontext = context_system::instance();
517
518 $filecontent = 'User file content';
519
1d987cb7 520 // Create a user private file.
61506a0a
DC
521 $userfilerecord = new stdClass;
522 $userfilerecord->contextid = $usercontext->id;
523 $userfilerecord->component = 'user';
524 $userfilerecord->filearea = 'private';
525 $userfilerecord->itemid = 0;
526 $userfilerecord->filepath = '/';
527 $userfilerecord->filename = 'userfile.txt';
528 $userfilerecord->source = 'test';
529 $userfile = $fs->create_file_from_string($userfilerecord, $filecontent);
530 $userfileref = $fs->pack_reference($userfilerecord);
531 $contenthash = $userfile->get_contenthash();
532
533 $filerecord = array(
534 'contextid' => $syscontext->id,
535 'component' => 'core',
536 'filearea' => 'phpunit',
537 'itemid' => 0,
538 'filepath' => '/',
539 'filename' => 'test.txt',
540 );
1d987cb7 541 // Create a file reference.
61506a0a
DC
542 $fileref = $fs->create_file_from_reference($filerecord, $userrepository->id, $userfileref);
543 $this->assertInstanceOf('stored_file', $fileref);
291a3d1f 544 $this->assertEquals($userrepository->id, $fileref->get_repository_id());
1d987cb7 545 $this->assertSame($userfile->get_contenthash(), $fileref->get_contenthash());
61506a0a
DC
546 $this->assertEquals($userfile->get_filesize(), $fileref->get_filesize());
547 $this->assertRegExp('#' . $userfile->get_filename(). '$#', $fileref->get_reference_details());
548
549 $draftitemid = 0;
550 file_prepare_draft_area($draftitemid, $usercontext->id, 'user', 'private', 0);
551 $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid);
1d987cb7 552 $this->assertCount(2, $draftfiles);
61506a0a
DC
553 $draftfile = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $userfilerecord->filepath, $userfilerecord->filename);
554 $draftfile->delete();
1d987cb7 555 // Save changed file.
61506a0a
DC
556 file_save_draft_area_files($draftitemid, $usercontext->id, 'user', 'private', 0);
557
1d987cb7 558 // The file reference should be a regular moodle file now.
61506a0a 559 $fileref = $fs->get_file($syscontext->id, 'core', 'phpunit', 0, '/', 'test.txt');
1d987cb7
PS
560 $this->assertFalse($fileref->is_external_file());
561 $this->assertSame($contenthash, $fileref->get_contenthash());
61506a0a
DC
562 $this->assertEquals($filecontent, $fileref->get_content());
563 }
85cb4b65 564
565 /**
566 * Tests the strip_double_headers function in the curl class.
567 */
568 public function test_curl_strip_double_headers() {
569 // Example from issue tracker.
570 $mdl30648example = <<<EOF
571HTTP/1.0 407 Proxy Authentication Required
572Server: squid/2.7.STABLE9
573Date: Thu, 08 Dec 2011 14:44:33 GMT
574Content-Type: text/html
575Content-Length: 1275
576X-Squid-Error: ERR_CACHE_ACCESS_DENIED 0
577Proxy-Authenticate: Basic realm="Squid proxy-caching web server"
578X-Cache: MISS from homer.lancs.ac.uk
579X-Cache-Lookup: NONE from homer.lancs.ac.uk:3128
580Via: 1.0 homer.lancs.ac.uk:3128 (squid/2.7.STABLE9)
581Connection: close
582
583HTTP/1.0 200 OK
584Server: Apache
585X-Lb-Nocache: true
586Cache-Control: private, max-age=15
587ETag: "4d69af5d8ba873ea9192c489e151bd7b"
588Content-Type: text/html
589Date: Thu, 08 Dec 2011 14:44:53 GMT
590Set-Cookie: BBC-UID=c4de2e109c8df6a51de627cee11b214bd4fb6054a030222488317afb31b343360MoodleBot/1.0; expires=Mon, 07-Dec-15 14:44:53 GMT; path=/; domain=bbc.co.uk
591X-Cache-Action: MISS
592X-Cache-Age: 0
593Vary: 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
594X-Cache: MISS from ww
595
596<html>...
597EOF;
598 $mdl30648expected = <<<EOF
599HTTP/1.0 200 OK
600Server: Apache
601X-Lb-Nocache: true
602Cache-Control: private, max-age=15
603ETag: "4d69af5d8ba873ea9192c489e151bd7b"
604Content-Type: text/html
605Date: Thu, 08 Dec 2011 14:44:53 GMT
606Set-Cookie: BBC-UID=c4de2e109c8df6a51de627cee11b214bd4fb6054a030222488317afb31b343360MoodleBot/1.0; expires=Mon, 07-Dec-15 14:44:53 GMT; path=/; domain=bbc.co.uk
607X-Cache-Action: MISS
608X-Cache-Age: 0
609Vary: 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
610X-Cache: MISS from ww
611
612<html>...
613EOF;
614 // For HTTP, replace the \n with \r\n.
615 $mdl30648example = preg_replace("~(?!<\r)\n~", "\r\n", $mdl30648example);
616 $mdl30648expected = preg_replace("~(?!<\r)\n~", "\r\n", $mdl30648expected);
617
618 // Test stripping works OK.
1d987cb7 619 $this->assertSame($mdl30648expected, curl::strip_double_headers($mdl30648example));
85cb4b65 620 // Test it does nothing to the 'plain' data.
1d987cb7 621 $this->assertSame($mdl30648expected, curl::strip_double_headers($mdl30648expected));
85cb4b65 622
623 // Example from OU proxy.
624 $httpsexample = <<<EOF
625HTTP/1.0 200 Connection established
626
627HTTP/1.1 200 OK
628Date: Fri, 22 Feb 2013 17:14:23 GMT
629Server: Apache/2
630X-Powered-By: PHP/5.3.3-7+squeeze14
631Content-Type: text/xml
632Connection: close
633Content-Encoding: gzip
634Transfer-Encoding: chunked
635
636<?xml version="1.0" encoding="ISO-8859-1" ?>
637<rss version="2.0">...
638EOF;
639 $httpsexpected = <<<EOF
640HTTP/1.1 200 OK
641Date: Fri, 22 Feb 2013 17:14:23 GMT
642Server: Apache/2
643X-Powered-By: PHP/5.3.3-7+squeeze14
644Content-Type: text/xml
645Connection: close
646Content-Encoding: gzip
647Transfer-Encoding: chunked
648
649<?xml version="1.0" encoding="ISO-8859-1" ?>
650<rss version="2.0">...
651EOF;
652 // For HTTP, replace the \n with \r\n.
653 $httpsexample = preg_replace("~(?!<\r)\n~", "\r\n", $httpsexample);
654 $httpsexpected = preg_replace("~(?!<\r)\n~", "\r\n", $httpsexpected);
655
656 // Test stripping works OK.
1d987cb7 657 $this->assertSame($httpsexpected, curl::strip_double_headers($httpsexample));
85cb4b65 658 // Test it does nothing to the 'plain' data.
1d987cb7 659 $this->assertSame($httpsexpected, curl::strip_double_headers($httpsexpected));
85cb4b65 660 }
a3d5830a 661}