weekly release 3.4dev
[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
605bab84
JD
221 /**
222 * Test a curl basic request with security enabled.
223 */
224 public function test_curl_basics_with_security_helper() {
225 $this->resetAfterTest();
226
227 // Test a request with a basic hostname filter applied.
228 $testhtml = $this->getExternalTestFileUrl('/test.html');
229 $url = new moodle_url($testhtml);
230 $host = $url->get_host();
231 set_config('curlsecurityblockedhosts', $host); // Blocks $host.
232
233 // Create curl with the default security enabled. We expect this to be blocked.
234 $curl = new curl();
235 $contents = $curl->get($testhtml);
236 $expected = $curl->get_security()->get_blocked_url_string();
237 $this->assertSame($expected, $contents);
238 $this->assertSame(0, $curl->get_errno());
239
240 // Now, create a curl using the 'ignoresecurity' override.
241 // We expect this request to pass, despite the admin setting having been set earlier.
242 $curl = new curl(['ignoresecurity' => true]);
243 $contents = $curl->get($testhtml);
244 $this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
245 $this->assertSame(0, $curl->get_errno());
246
247 // Now, try injecting a mock security helper into curl. This will override the default helper.
248 $mockhelper = $this->getMockBuilder('\core\files\curl_security_helper')->getMock();
249
250 // Make the mock return a different string.
251 $mockhelper->expects($this->any())->method('get_blocked_url_string')->will($this->returnValue('You shall not pass'));
252
253 // And make the mock security helper block all URLs. This helper instance doesn't care about config.
254 $mockhelper->expects($this->any())->method('url_is_blocked')->will($this->returnValue(true));
255
256 $curl = new curl(['securityhelper' => $mockhelper]);
257 $contents = $curl->get($testhtml);
258 $this->assertSame('You shall not pass', $curl->get_security()->get_blocked_url_string());
259 $this->assertSame($curl->get_security()->get_blocked_url_string(), $contents);
260 }
261
74c4405b
FM
262 public function test_curl_redirects() {
263 global $CFG;
264
35ea5db0 265 // Test full URL redirects.
a9d2f1b4 266 $testurl = $this->getExternalTestFileUrl('/test_redir.php');
35ea5db0
PS
267
268 $curl = new curl();
269 $contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>2));
74c4405b
FM
270 $response = $curl->getResponse();
271 $this->assertSame('200 OK', reset($response));
35ea5db0
PS
272 $this->assertSame(0, $curl->get_errno());
273 $this->assertSame(2, $curl->info['redirect_count']);
274 $this->assertSame('done', $contents);
275
276 $curl = new curl();
277 $curl->emulateredirects = true;
278 $contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>2));
74c4405b
FM
279 $response = $curl->getResponse();
280 $this->assertSame('200 OK', reset($response));
35ea5db0
PS
281 $this->assertSame(0, $curl->get_errno());
282 $this->assertSame(2, $curl->info['redirect_count']);
283 $this->assertSame('done', $contents);
284
90048c7c
TH
285 // This test was failing for people behind Squid proxies. Squid does not
286 // fully support HTTP 1.1, so converts things to HTTP 1.0, where the name
287 // of the status code is different.
288 reset($response);
289 if (key($response) === 'HTTP/1.0') {
290 $responsecode302 = '302 Moved Temporarily';
291 } else {
292 $responsecode302 = '302 Found';
293 }
294
35ea5db0
PS
295 $curl = new curl();
296 $contents = $curl->get("$testurl?redir=3", array(), array('CURLOPT_FOLLOWLOCATION'=>0));
74c4405b 297 $response = $curl->getResponse();
90048c7c 298 $this->assertSame($responsecode302, reset($response));
35ea5db0
PS
299 $this->assertSame(0, $curl->get_errno());
300 $this->assertSame(302, $curl->info['http_code']);
301 $this->assertSame('', $contents);
302
303 $curl = new curl();
304 $curl->emulateredirects = true;
305 $contents = $curl->get("$testurl?redir=3", array(), array('CURLOPT_FOLLOWLOCATION'=>0));
74c4405b 306 $response = $curl->getResponse();
90048c7c 307 $this->assertSame($responsecode302, reset($response));
35ea5db0
PS
308 $this->assertSame(0, $curl->get_errno());
309 $this->assertSame(302, $curl->info['http_code']);
310 $this->assertSame('', $contents);
311
312 $curl = new curl();
313 $contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>1));
314 $this->assertSame(CURLE_TOO_MANY_REDIRECTS, $curl->get_errno());
315 $this->assertNotEmpty($contents);
316
317 $curl = new curl();
318 $curl->emulateredirects = true;
319 $contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>1));
320 $this->assertSame(CURLE_TOO_MANY_REDIRECTS, $curl->get_errno());
321 $this->assertNotEmpty($contents);
322
323 $curl = new curl();
324 $tofile = "$CFG->tempdir/test.html";
325 @unlink($tofile);
326 $fp = fopen($tofile, 'w');
327 $result = $curl->get("$testurl?redir=1", array(), array('CURLOPT_FILE'=>$fp));
328 $this->assertTrue($result);
329 fclose($fp);
330 $this->assertFileExists($tofile);
331 $this->assertSame('done', file_get_contents($tofile));
332 @unlink($tofile);
333
334 $curl = new curl();
335 $curl->emulateredirects = true;
336 $tofile = "$CFG->tempdir/test.html";
337 @unlink($tofile);
338 $fp = fopen($tofile, 'w');
339 $result = $curl->get("$testurl?redir=1", array(), array('CURLOPT_FILE'=>$fp));
340 $this->assertTrue($result);
341 fclose($fp);
342 $this->assertFileExists($tofile);
343 $this->assertSame('done', file_get_contents($tofile));
344 @unlink($tofile);
345
346 $curl = new curl();
347 $tofile = "$CFG->tempdir/test.html";
348 @unlink($tofile);
349 $result = $curl->download_one("$testurl?redir=1", array(), array('filepath'=>$tofile));
350 $this->assertTrue($result);
351 $this->assertFileExists($tofile);
352 $this->assertSame('done', file_get_contents($tofile));
353 @unlink($tofile);
354
355 $curl = new curl();
356 $curl->emulateredirects = true;
357 $tofile = "$CFG->tempdir/test.html";
358 @unlink($tofile);
359 $result = $curl->download_one("$testurl?redir=1", array(), array('filepath'=>$tofile));
360 $this->assertTrue($result);
361 $this->assertFileExists($tofile);
362 $this->assertSame('done', file_get_contents($tofile));
363 @unlink($tofile);
74c4405b 364 }
35ea5db0 365
74c4405b 366 public function test_curl_relative_redirects() {
35ea5db0 367 // Test relative location redirects.
a9d2f1b4 368 $testurl = $this->getExternalTestFileUrl('/test_relative_redir.php');
35ea5db0
PS
369
370 $curl = new curl();
371 $contents = $curl->get($testurl);
74c4405b
FM
372 $response = $curl->getResponse();
373 $this->assertSame('200 OK', reset($response));
35ea5db0
PS
374 $this->assertSame(0, $curl->get_errno());
375 $this->assertSame(1, $curl->info['redirect_count']);
376 $this->assertSame('done', $contents);
377
378 $curl = new curl();
379 $curl->emulateredirects = true;
380 $contents = $curl->get($testurl);
74c4405b
FM
381 $response = $curl->getResponse();
382 $this->assertSame('200 OK', reset($response));
35ea5db0
PS
383 $this->assertSame(0, $curl->get_errno());
384 $this->assertSame(1, $curl->info['redirect_count']);
385 $this->assertSame('done', $contents);
386
35ea5db0 387 // Test different redirect types.
a9d2f1b4 388 $testurl = $this->getExternalTestFileUrl('/test_relative_redir.php');
35ea5db0
PS
389
390 $curl = new curl();
391 $contents = $curl->get("$testurl?type=301");
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=301");
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=302");
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=302");
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);
423
424 $curl = new curl();
425 $contents = $curl->get("$testurl?type=303");
74c4405b
FM
426 $response = $curl->getResponse();
427 $this->assertSame('200 OK', reset($response));
35ea5db0
PS
428 $this->assertSame(0, $curl->get_errno());
429 $this->assertSame(1, $curl->info['redirect_count']);
430 $this->assertSame('done', $contents);
431
432 $curl = new curl();
433 $curl->emulateredirects = true;
434 $contents = $curl->get("$testurl?type=303");
74c4405b
FM
435 $response = $curl->getResponse();
436 $this->assertSame('200 OK', reset($response));
35ea5db0
PS
437 $this->assertSame(0, $curl->get_errno());
438 $this->assertSame(1, $curl->info['redirect_count']);
439 $this->assertSame('done', $contents);
440
441 $curl = new curl();
442 $contents = $curl->get("$testurl?type=307");
74c4405b
FM
443 $response = $curl->getResponse();
444 $this->assertSame('200 OK', reset($response));
35ea5db0
PS
445 $this->assertSame(0, $curl->get_errno());
446 $this->assertSame(1, $curl->info['redirect_count']);
447 $this->assertSame('done', $contents);
448
449 $curl = new curl();
450 $curl->emulateredirects = true;
451 $contents = $curl->get("$testurl?type=307");
74c4405b
FM
452 $response = $curl->getResponse();
453 $this->assertSame('200 OK', reset($response));
35ea5db0
PS
454 $this->assertSame(0, $curl->get_errno());
455 $this->assertSame(1, $curl->info['redirect_count']);
456 $this->assertSame('done', $contents);
457
458 $curl = new curl();
459 $contents = $curl->get("$testurl?type=308");
74c4405b
FM
460 $response = $curl->getResponse();
461 $this->assertSame('200 OK', reset($response));
35ea5db0
PS
462 $this->assertSame(0, $curl->get_errno());
463 $this->assertSame(1, $curl->info['redirect_count']);
464 $this->assertSame('done', $contents);
465
466 $curl = new curl();
467 $curl->emulateredirects = true;
468 $contents = $curl->get("$testurl?type=308");
74c4405b
FM
469 $response = $curl->getResponse();
470 $this->assertSame('200 OK', reset($response));
35ea5db0
PS
471 $this->assertSame(0, $curl->get_errno());
472 $this->assertSame(1, $curl->info['redirect_count']);
473 $this->assertSame('done', $contents);
ba72e295 474
74c4405b
FM
475 }
476
477 public function test_curl_proxybypass() {
478 global $CFG;
479 $testurl = $this->getExternalTestFileUrl('/test.html');
480
ba72e295
FM
481 $oldproxy = $CFG->proxyhost;
482 $oldproxybypass = $CFG->proxybypass;
483
484 // Test without proxy bypass and inaccessible proxy.
485 $CFG->proxyhost = 'i.do.not.exist';
486 $CFG->proxybypass = '';
487 $curl = new curl();
74c4405b 488 $contents = $curl->get($testurl);
ba72e295
FM
489 $this->assertNotEquals(0, $curl->get_errno());
490 $this->assertNotEquals('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
491
492 // Test with proxy bypass.
74c4405b
FM
493 $testurlhost = parse_url($testurl, PHP_URL_HOST);
494 $CFG->proxybypass = $testurlhost;
ba72e295 495 $curl = new curl();
74c4405b 496 $contents = $curl->get($testurl);
ba72e295
FM
497 $this->assertSame(0, $curl->get_errno());
498 $this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
499
500 $CFG->proxyhost = $oldproxy;
501 $CFG->proxybypass = $oldproxybypass;
a3d5830a 502 }
67233725 503
74c4405b
FM
504 public function test_curl_post() {
505 $testurl = $this->getExternalTestFileUrl('/test_post.php');
102230b2
FM
506
507 // Test post request.
508 $curl = new curl();
74c4405b 509 $contents = $curl->post($testurl, 'data=moodletest');
102230b2
FM
510 $response = $curl->getResponse();
511 $this->assertSame('200 OK', reset($response));
512 $this->assertSame(0, $curl->get_errno());
513 $this->assertSame('OK', $contents);
514
515 // Test 100 requests.
516 $curl = new curl();
517 $curl->setHeader('Expect: 100-continue');
74c4405b 518 $contents = $curl->post($testurl, 'data=moodletest');
102230b2
FM
519 $response = $curl->getResponse();
520 $this->assertSame('200 OK', reset($response));
521 $this->assertSame(0, $curl->get_errno());
522 $this->assertSame('OK', $contents);
523 }
524
218d751d
FM
525 public function test_curl_file() {
526 $this->resetAfterTest();
527 $testurl = $this->getExternalTestFileUrl('/test_file.php');
528
529 $fs = get_file_storage();
530 $filerecord = array(
531 'contextid' => context_system::instance()->id,
532 'component' => 'test',
533 'filearea' => 'curl_post',
534 'itemid' => 0,
535 'filepath' => '/',
536 'filename' => 'test.txt'
537 );
538 $teststring = 'moodletest';
539 $testfile = $fs->create_file_from_string($filerecord, $teststring);
540
541 // Test post with file.
542 $data = array('testfile' => $testfile);
543 $curl = new curl();
544 $contents = $curl->post($testurl, $data);
545 $this->assertSame('OK', $contents);
546 }
547
38a73bf4
FM
548 public function test_curl_protocols() {
549
550 // HTTP and HTTPS requests were verified in previous requests. Now check
551 // that we can selectively disable some protocols.
552 $curl = new curl();
553
554 // Other protocols than HTTP(S) are disabled by default.
555 $testurl = 'file:///';
556 $curl->get($testurl);
557 $this->assertNotEmpty($curl->error);
558 $this->assertEquals(CURLE_UNSUPPORTED_PROTOCOL, $curl->errno);
559
560 $testurl = 'ftp://nowhere';
561 $curl->get($testurl);
562 $this->assertNotEmpty($curl->error);
563 $this->assertEquals(CURLE_UNSUPPORTED_PROTOCOL, $curl->errno);
564
565 $testurl = 'telnet://somewhere';
566 $curl->get($testurl);
567 $this->assertNotEmpty($curl->error);
568 $this->assertEquals(CURLE_UNSUPPORTED_PROTOCOL, $curl->errno);
569
570 // Protocols are also disabled during redirections.
571 $testurl = $this->getExternalTestFileUrl('/test_redir_proto.php');
572 $curl->get($testurl, array('proto' => 'file'));
573 $this->assertNotEmpty($curl->error);
574 $this->assertEquals(CURLE_UNSUPPORTED_PROTOCOL, $curl->errno);
575
576 $testurl = $this->getExternalTestFileUrl('/test_redir_proto.php');
577 $curl->get($testurl, array('proto' => 'ftp'));
578 $this->assertNotEmpty($curl->error);
579 $this->assertEquals(CURLE_UNSUPPORTED_PROTOCOL, $curl->errno);
580
581 $testurl = $this->getExternalTestFileUrl('/test_redir_proto.php');
582 $curl->get($testurl, array('proto' => 'telnet'));
583 $this->assertNotEmpty($curl->error);
584 $this->assertEquals(CURLE_UNSUPPORTED_PROTOCOL, $curl->errno);
585 }
586
67233725
DC
587 /**
588 * Testing prepare draft area
589 *
590 * @copyright 2012 Dongsheng Cai {@link http://dongsheng.org}
591 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
592 */
593 public function test_prepare_draft_area() {
594 global $USER, $DB;
595
596 $this->resetAfterTest(true);
597
598 $generator = $this->getDataGenerator();
599 $user = $generator->create_user();
600 $usercontext = context_user::instance($user->id);
601 $USER = $DB->get_record('user', array('id'=>$user->id));
602
7051415c
DC
603 $repositorypluginname = 'user';
604
605 $args = array();
606 $args['type'] = $repositorypluginname;
607 $repos = repository::get_instances($args);
608 $userrepository = reset($repos);
609 $this->assertInstanceOf('repository', $userrepository);
67233725
DC
610
611 $fs = get_file_storage();
7051415c 612
67233725
DC
613 $syscontext = context_system::instance();
614 $component = 'core';
615 $filearea = 'unittest';
616 $itemid = 0;
617 $filepath = '/';
618 $filename = 'test.txt';
619 $sourcefield = 'Copyright stuff';
620
621 $filerecord = array(
622 'contextid' => $syscontext->id,
623 'component' => $component,
624 'filearea' => $filearea,
625 'itemid' => $itemid,
626 'filepath' => $filepath,
627 'filename' => $filename,
628 'source' => $sourcefield,
629 );
630 $ref = $fs->pack_reference($filerecord);
67233725 631 $originalfile = $fs->create_file_from_string($filerecord, 'Test content');
67233725
DC
632 $fileid = $originalfile->get_id();
633 $this->assertInstanceOf('stored_file', $originalfile);
634
1d987cb7 635 // Create a user private file.
7051415c
DC
636 $userfilerecord = new stdClass;
637 $userfilerecord->contextid = $usercontext->id;
638 $userfilerecord->component = 'user';
639 $userfilerecord->filearea = 'private';
640 $userfilerecord->itemid = 0;
641 $userfilerecord->filepath = '/';
642 $userfilerecord->filename = 'userfile.txt';
643 $userfilerecord->source = 'test';
644 $userfile = $fs->create_file_from_string($userfilerecord, 'User file content');
645 $userfileref = $fs->pack_reference($userfilerecord);
646
647 $filerefrecord = clone((object)$filerecord);
648 $filerefrecord->filename = 'testref.txt';
1d987cb7
PS
649
650 // Create a file reference.
7051415c
DC
651 $fileref = $fs->create_file_from_reference($filerefrecord, $userrepository->id, $userfileref);
652 $this->assertInstanceOf('stored_file', $fileref);
291a3d1f 653 $this->assertEquals($userrepository->id, $fileref->get_repository_id());
1d987cb7 654 $this->assertSame($userfile->get_contenthash(), $fileref->get_contenthash());
7051415c
DC
655 $this->assertEquals($userfile->get_filesize(), $fileref->get_filesize());
656 $this->assertRegExp('#' . $userfile->get_filename(). '$#', $fileref->get_reference_details());
657
67233725
DC
658 $draftitemid = 0;
659 file_prepare_draft_area($draftitemid, $syscontext->id, $component, $filearea, $itemid);
660
661 $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid);
1d987cb7 662 $this->assertCount(3, $draftfiles);
67233725
DC
663
664 $draftfile = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $filepath, $filename);
665 $source = unserialize($draftfile->get_source());
1d987cb7
PS
666 $this->assertSame($ref, $source->original);
667 $this->assertSame($sourcefield, $source->source);
67233725 668
7051415c
DC
669 $draftfileref = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $filepath, $filerefrecord->filename);
670 $this->assertInstanceOf('stored_file', $draftfileref);
1d987cb7 671 $this->assertTrue($draftfileref->is_external_file());
67233725 672
1d987cb7 673 // Change some information.
67233725
DC
674 $author = 'Dongsheng Cai';
675 $draftfile->set_author($author);
7051415c 676 $newsourcefield = 'Get from Flickr';
67233725
DC
677 $license = 'GPLv3';
678 $draftfile->set_license($license);
1d987cb7 679 // If you want to really just change source field, do this.
67233725
DC
680 $source = unserialize($draftfile->get_source());
681 $newsourcefield = 'From flickr';
682 $source->source = $newsourcefield;
683 $draftfile->set_source(serialize($source));
684
1d987cb7 685 // Save changed file.
67233725
DC
686 file_save_draft_area_files($draftitemid, $syscontext->id, $component, $filearea, $itemid);
687
688 $file = $fs->get_file($syscontext->id, $component, $filearea, $itemid, $filepath, $filename);
689
1d987cb7 690 // Make sure it's the original file id.
67233725
DC
691 $this->assertEquals($fileid, $file->get_id());
692 $this->assertInstanceOf('stored_file', $file);
1d987cb7
PS
693 $this->assertSame($author, $file->get_author());
694 $this->assertSame($license, $file->get_license());
67233725
DC
695 $this->assertEquals($newsourcefield, $file->get_source());
696 }
61506a0a
DC
697
698 /**
1d987cb7 699 * Testing deleting original files.
61506a0a
DC
700 *
701 * @copyright 2012 Dongsheng Cai {@link http://dongsheng.org}
702 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
703 */
704 public function test_delete_original_file_from_draft() {
705 global $USER, $DB;
706
707 $this->resetAfterTest(true);
708
709 $generator = $this->getDataGenerator();
710 $user = $generator->create_user();
711 $usercontext = context_user::instance($user->id);
712 $USER = $DB->get_record('user', array('id'=>$user->id));
713
714 $repositorypluginname = 'user';
715
716 $args = array();
717 $args['type'] = $repositorypluginname;
718 $repos = repository::get_instances($args);
719 $userrepository = reset($repos);
720 $this->assertInstanceOf('repository', $userrepository);
721
722 $fs = get_file_storage();
723 $syscontext = context_system::instance();
724
725 $filecontent = 'User file content';
726
1d987cb7 727 // Create a user private file.
61506a0a
DC
728 $userfilerecord = new stdClass;
729 $userfilerecord->contextid = $usercontext->id;
730 $userfilerecord->component = 'user';
731 $userfilerecord->filearea = 'private';
732 $userfilerecord->itemid = 0;
733 $userfilerecord->filepath = '/';
734 $userfilerecord->filename = 'userfile.txt';
735 $userfilerecord->source = 'test';
736 $userfile = $fs->create_file_from_string($userfilerecord, $filecontent);
737 $userfileref = $fs->pack_reference($userfilerecord);
738 $contenthash = $userfile->get_contenthash();
739
740 $filerecord = array(
741 'contextid' => $syscontext->id,
742 'component' => 'core',
743 'filearea' => 'phpunit',
744 'itemid' => 0,
745 'filepath' => '/',
746 'filename' => 'test.txt',
747 );
1d987cb7 748 // Create a file reference.
61506a0a
DC
749 $fileref = $fs->create_file_from_reference($filerecord, $userrepository->id, $userfileref);
750 $this->assertInstanceOf('stored_file', $fileref);
291a3d1f 751 $this->assertEquals($userrepository->id, $fileref->get_repository_id());
1d987cb7 752 $this->assertSame($userfile->get_contenthash(), $fileref->get_contenthash());
61506a0a
DC
753 $this->assertEquals($userfile->get_filesize(), $fileref->get_filesize());
754 $this->assertRegExp('#' . $userfile->get_filename(). '$#', $fileref->get_reference_details());
755
756 $draftitemid = 0;
757 file_prepare_draft_area($draftitemid, $usercontext->id, 'user', 'private', 0);
758 $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid);
1d987cb7 759 $this->assertCount(2, $draftfiles);
61506a0a
DC
760 $draftfile = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $userfilerecord->filepath, $userfilerecord->filename);
761 $draftfile->delete();
1d987cb7 762 // Save changed file.
61506a0a
DC
763 file_save_draft_area_files($draftitemid, $usercontext->id, 'user', 'private', 0);
764
1d987cb7 765 // The file reference should be a regular moodle file now.
61506a0a 766 $fileref = $fs->get_file($syscontext->id, 'core', 'phpunit', 0, '/', 'test.txt');
1d987cb7
PS
767 $this->assertFalse($fileref->is_external_file());
768 $this->assertSame($contenthash, $fileref->get_contenthash());
61506a0a
DC
769 $this->assertEquals($filecontent, $fileref->get_content());
770 }
85cb4b65 771
772 /**
773 * Tests the strip_double_headers function in the curl class.
774 */
775 public function test_curl_strip_double_headers() {
776 // Example from issue tracker.
777 $mdl30648example = <<<EOF
778HTTP/1.0 407 Proxy Authentication Required
779Server: squid/2.7.STABLE9
780Date: Thu, 08 Dec 2011 14:44:33 GMT
781Content-Type: text/html
782Content-Length: 1275
783X-Squid-Error: ERR_CACHE_ACCESS_DENIED 0
784Proxy-Authenticate: Basic realm="Squid proxy-caching web server"
785X-Cache: MISS from homer.lancs.ac.uk
786X-Cache-Lookup: NONE from homer.lancs.ac.uk:3128
787Via: 1.0 homer.lancs.ac.uk:3128 (squid/2.7.STABLE9)
788Connection: close
789
790HTTP/1.0 200 OK
791Server: Apache
792X-Lb-Nocache: true
c3a3f540 793Cache-Control: private, max-age=15, no-transform
85cb4b65 794ETag: "4d69af5d8ba873ea9192c489e151bd7b"
795Content-Type: text/html
796Date: Thu, 08 Dec 2011 14:44:53 GMT
797Set-Cookie: BBC-UID=c4de2e109c8df6a51de627cee11b214bd4fb6054a030222488317afb31b343360MoodleBot/1.0; expires=Mon, 07-Dec-15 14:44:53 GMT; path=/; domain=bbc.co.uk
798X-Cache-Action: MISS
799X-Cache-Age: 0
800Vary: 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
801X-Cache: MISS from ww
802
803<html>...
804EOF;
805 $mdl30648expected = <<<EOF
806HTTP/1.0 200 OK
807Server: Apache
808X-Lb-Nocache: true
c3a3f540 809Cache-Control: private, max-age=15, no-transform
85cb4b65 810ETag: "4d69af5d8ba873ea9192c489e151bd7b"
811Content-Type: text/html
812Date: Thu, 08 Dec 2011 14:44:53 GMT
813Set-Cookie: BBC-UID=c4de2e109c8df6a51de627cee11b214bd4fb6054a030222488317afb31b343360MoodleBot/1.0; expires=Mon, 07-Dec-15 14:44:53 GMT; path=/; domain=bbc.co.uk
814X-Cache-Action: MISS
815X-Cache-Age: 0
816Vary: 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
817X-Cache: MISS from ww
818
819<html>...
820EOF;
821 // For HTTP, replace the \n with \r\n.
822 $mdl30648example = preg_replace("~(?!<\r)\n~", "\r\n", $mdl30648example);
823 $mdl30648expected = preg_replace("~(?!<\r)\n~", "\r\n", $mdl30648expected);
824
825 // Test stripping works OK.
1d987cb7 826 $this->assertSame($mdl30648expected, curl::strip_double_headers($mdl30648example));
85cb4b65 827 // Test it does nothing to the 'plain' data.
1d987cb7 828 $this->assertSame($mdl30648expected, curl::strip_double_headers($mdl30648expected));
85cb4b65 829
830 // Example from OU proxy.
831 $httpsexample = <<<EOF
832HTTP/1.0 200 Connection established
833
834HTTP/1.1 200 OK
835Date: Fri, 22 Feb 2013 17:14:23 GMT
836Server: Apache/2
837X-Powered-By: PHP/5.3.3-7+squeeze14
838Content-Type: text/xml
839Connection: close
840Content-Encoding: gzip
841Transfer-Encoding: chunked
842
843<?xml version="1.0" encoding="ISO-8859-1" ?>
844<rss version="2.0">...
845EOF;
846 $httpsexpected = <<<EOF
847HTTP/1.1 200 OK
848Date: Fri, 22 Feb 2013 17:14:23 GMT
849Server: Apache/2
850X-Powered-By: PHP/5.3.3-7+squeeze14
851Content-Type: text/xml
852Connection: close
853Content-Encoding: gzip
854Transfer-Encoding: chunked
855
856<?xml version="1.0" encoding="ISO-8859-1" ?>
857<rss version="2.0">...
858EOF;
859 // For HTTP, replace the \n with \r\n.
860 $httpsexample = preg_replace("~(?!<\r)\n~", "\r\n", $httpsexample);
861 $httpsexpected = preg_replace("~(?!<\r)\n~", "\r\n", $httpsexpected);
862
863 // Test stripping works OK.
1d987cb7 864 $this->assertSame($httpsexpected, curl::strip_double_headers($httpsexample));
85cb4b65 865 // Test it does nothing to the 'plain' data.
1d987cb7 866 $this->assertSame($httpsexpected, curl::strip_double_headers($httpsexpected));
85cb4b65 867 }
91fed57a 868
869 /**
870 * Tests the get_mimetype_description function.
871 */
872 public function test_get_mimetype_description() {
873 $this->resetAfterTest();
874
875 // Test example type (.doc).
876 $this->assertEquals(get_string('application/msword', 'mimetypes'),
877 get_mimetype_description(array('filename' => 'test.doc')));
878
879 // Test an unknown file type.
880 $this->assertEquals(get_string('document/unknown', 'mimetypes'),
881 get_mimetype_description(array('filename' => 'test.frog')));
882
883 // Test a custom filetype with no lang string specified.
884 core_filetypes::add_type('frog', 'application/x-frog', 'document');
885 $this->assertEquals('application/x-frog',
886 get_mimetype_description(array('filename' => 'test.frog')));
887
888 // Test custom description.
889 core_filetypes::update_type('frog', 'frog', 'application/x-frog', 'document',
890 array(), '', 'Froggy file');
891 $this->assertEquals('Froggy file',
892 get_mimetype_description(array('filename' => 'test.frog')));
893
894 // Test custom description using multilang filter.
e37c1286 895 filter_manager::reset_caches();
91fed57a 896 filter_set_global_state('multilang', TEXTFILTER_ON);
897 filter_set_applies_to_strings('multilang', true);
898 core_filetypes::update_type('frog', 'frog', 'application/x-frog', 'document',
899 array(), '', '<span lang="en" class="multilang">Green amphibian</span>' .
900 '<span lang="fr" class="multilang">Amphibian vert</span>');
901 $this->assertEquals('Green amphibian',
902 get_mimetype_description(array('filename' => 'test.frog')));
903 }
904
905 /**
906 * Tests the get_mimetypes_array function.
907 */
908 public function test_get_mimetypes_array() {
909 $mimeinfo = get_mimetypes_array();
910
911 // Test example MIME type (doc).
912 $this->assertEquals('application/msword', $mimeinfo['doc']['type']);
913 $this->assertEquals('document', $mimeinfo['doc']['icon']);
914 $this->assertEquals(array('document'), $mimeinfo['doc']['groups']);
915 $this->assertFalse(isset($mimeinfo['doc']['string']));
916 $this->assertFalse(isset($mimeinfo['doc']['defaulticon']));
917 $this->assertFalse(isset($mimeinfo['doc']['customdescription']));
918
919 // Check the less common fields using other examples.
920 $this->assertEquals('image', $mimeinfo['png']['string']);
921 $this->assertEquals(true, $mimeinfo['txt']['defaulticon']);
922 }
cbb7c6a9 923
0c4c3b9a
JP
924 /**
925 * Tests for get_mimetype_for_sending function.
926 */
927 public function test_get_mimetype_for_sending() {
928 // Without argument.
929 $this->assertEquals('application/octet-stream', get_mimetype_for_sending());
930
931 // Argument is null.
932 $this->assertEquals('application/octet-stream', get_mimetype_for_sending(null));
933
934 // Filename having no extension.
935 $this->assertEquals('application/octet-stream', get_mimetype_for_sending('filenamewithoutextension'));
936
937 // Test using the extensions listed from the get_mimetypes_array function.
938 $mimetypes = get_mimetypes_array();
939 foreach ($mimetypes as $ext => $info) {
940 if ($ext === 'xxx') {
941 $this->assertEquals('application/octet-stream', get_mimetype_for_sending('SampleFile.' . $ext));
942 } else {
943 $this->assertEquals($info['type'], get_mimetype_for_sending('SampleFile.' . $ext));
944 }
945 }
946 }
947
cbb7c6a9
DC
948 /**
949 * Test curl agent settings.
950 */
951 public function test_curl_useragent() {
9a1adda5 952 $curl = new testable_curl();
cbb7c6a9
DC
953 $options = $curl->get_options();
954 $this->assertNotEmpty($options);
955
956 $curl->call_apply_opt($options);
957 $this->assertTrue(in_array('User-Agent: MoodleBot/1.0', $curl->header));
958 $this->assertFalse(in_array('User-Agent: Test/1.0', $curl->header));
959
960 $options['CURLOPT_USERAGENT'] = 'Test/1.0';
961 $curl->call_apply_opt($options);
962 $this->assertTrue(in_array('User-Agent: Test/1.0', $curl->header));
963 $this->assertFalse(in_array('User-Agent: MoodleBot/1.0', $curl->header));
964
965 $curl->set_option('CURLOPT_USERAGENT', 'AnotherUserAgent/1.0');
966 $curl->call_apply_opt();
967 $this->assertTrue(in_array('User-Agent: AnotherUserAgent/1.0', $curl->header));
968 $this->assertFalse(in_array('User-Agent: Test/1.0', $curl->header));
969
970 $curl->set_option('CURLOPT_USERAGENT', 'AnotherUserAgent/1.1');
971 $options = $curl->get_options();
972 $curl->call_apply_opt($options);
973 $this->assertTrue(in_array('User-Agent: AnotherUserAgent/1.1', $curl->header));
974 $this->assertFalse(in_array('User-Agent: AnotherUserAgent/1.0', $curl->header));
975
976 $curl->unset_option('CURLOPT_USERAGENT');
977 $curl->call_apply_opt();
978 $this->assertTrue(in_array('User-Agent: MoodleBot/1.0', $curl->header));
9a1adda5
EL
979
980 // Finally, test it via exttests, to ensure the agent is sent properly.
981 // Matching.
982 $testurl = $this->getExternalTestFileUrl('/test_agent.php');
983 $extcurl = new curl();
984 $contents = $extcurl->get($testurl, array(), array('CURLOPT_USERAGENT' => 'AnotherUserAgent/1.2'));
985 $response = $extcurl->getResponse();
986 $this->assertSame('200 OK', reset($response));
987 $this->assertSame(0, $extcurl->get_errno());
988 $this->assertSame('OK', $contents);
989 // Not matching.
990 $contents = $extcurl->get($testurl, array(), array('CURLOPT_USERAGENT' => 'NonMatchingUserAgent/1.2'));
991 $response = $extcurl->getResponse();
992 $this->assertSame('200 OK', reset($response));
993 $this->assertSame(0, $extcurl->get_errno());
994 $this->assertSame('', $contents);
cbb7c6a9 995 }
296ab1e2
JL
996
997 /**
998 * Test file_rewrite_pluginfile_urls.
999 */
1000 public function test_file_rewrite_pluginfile_urls() {
1001
1002 $syscontext = context_system::instance();
1003 $originaltext = 'Fake test with an image <img src="@@PLUGINFILE@@/image.png">';
1004
1005 // Do the rewrite.
1006 $finaltext = file_rewrite_pluginfile_urls($originaltext, 'pluginfile.php', $syscontext->id, 'user', 'private', 0);
1007 $this->assertContains("pluginfile.php", $finaltext);
1008
1009 // Now undo.
1010 $options = array('reverse' => true);
1011 $finaltext = file_rewrite_pluginfile_urls($finaltext, 'pluginfile.php', $syscontext->id, 'user', 'private', 0, $options);
1012
1013 // Compare the final text is the same that the original.
1014 $this->assertEquals($originaltext, $finaltext);
1015 }
46089705
JL
1016
1017 /**
1018 * Helpter function to create draft files
1019 *
1020 * @param array $filedata data for the file record (to not use defaults)
1021 * @return stored_file the stored file instance
1022 */
1023 public static function create_draft_file($filedata = array()) {
1024 global $USER;
1025
1026 self::setAdminUser();
1027 $fs = get_file_storage();
1028
1029 $filerecord = array(
1030 'component' => 'user',
1031 'filearea' => 'draft',
1032 'itemid' => isset($filedata['itemid']) ? $filedata['itemid'] : file_get_unused_draft_itemid(),
1033 'author' => isset($filedata['author']) ? $filedata['author'] : fullname($USER),
1034 'filepath' => isset($filedata['filepath']) ? $filedata['filepath'] : '/',
1035 'filename' => isset($filedata['filename']) ? $filedata['filename'] : 'file.txt',
1036 );
1037
1038 if (isset($filedata['contextid'])) {
1039 $filerecord['contextid'] = $filedata['contextid'];
1040 } else {
1041 $usercontext = context_user::instance($USER->id);
1042 $filerecord['contextid'] = $usercontext->id;
1043 }
1044 $source = isset($filedata['source']) ? $filedata['source'] : serialize((object)array('source' => 'From string'));
1045 $content = isset($filedata['content']) ? $filedata['content'] : 'some content here';
1046
1047 $file = $fs->create_file_from_string($filerecord, $content);
1048 $file->set_source($source);
1049
1050 return $file;
1051 }
1052
1053 /**
1054 * Test file_merge_files_from_draft_area_into_filearea
1055 */
1056 public function test_file_merge_files_from_draft_area_into_filearea() {
1057 global $USER, $CFG;
1058
1059 $this->resetAfterTest(true);
1060 $this->setAdminUser();
1061 $fs = get_file_storage();
1062 $usercontext = context_user::instance($USER->id);
1063
1064 // Create a draft file.
1065 $filename = 'data.txt';
1066 $filerecord = array(
1067 'filename' => $filename,
1068 );
1069 $file = self::create_draft_file($filerecord);
ce78e33a 1070 $draftitemid = $file->get_itemid();
46089705
JL
1071
1072 $maxbytes = $CFG->userquota;
1073 $maxareabytes = $CFG->userquota;
1074 $options = array('subdirs' => 1,
1075 'maxbytes' => $maxbytes,
1076 'maxfiles' => -1,
1077 'areamaxbytes' => $maxareabytes);
1078
1079 // Add new file.
ce78e33a 1080 file_merge_files_from_draft_area_into_filearea($draftitemid, $usercontext->id, 'user', 'private', 0, $options);
46089705
JL
1081
1082 $files = $fs->get_area_files($usercontext->id, 'user', 'private', 0);
1083 // Directory and file.
1084 $this->assertCount(2, $files);
1085 $found = false;
1086 foreach ($files as $file) {
1087 if (!$file->is_directory()) {
1088 $found = true;
1089 $this->assertEquals($filename, $file->get_filename());
1090 $this->assertEquals('some content here', $file->get_content());
1091 }
1092 }
1093 $this->assertTrue($found);
1094
1095 // Add two more files.
1096 $filerecord = array(
ce78e33a 1097 'itemid' => $draftitemid,
46089705
JL
1098 'filename' => 'second.txt',
1099 );
1100 self::create_draft_file($filerecord);
1101 $filerecord = array(
ce78e33a 1102 'itemid' => $draftitemid,
46089705
JL
1103 'filename' => 'third.txt',
1104 );
1105 $file = self::create_draft_file($filerecord);
1106
1107 file_merge_files_from_draft_area_into_filearea($file->get_itemid(), $usercontext->id, 'user', 'private', 0, $options);
1108
1109 $files = $fs->get_area_files($usercontext->id, 'user', 'private', 0);
1110 $this->assertCount(4, $files);
1111
1112 // Update contents of one file.
1113 $filerecord = array(
1114 'filename' => 'second.txt',
1115 'content' => 'new content',
1116 );
1117 $file = self::create_draft_file($filerecord);
1118 file_merge_files_from_draft_area_into_filearea($file->get_itemid(), $usercontext->id, 'user', 'private', 0, $options);
1119
1120 $files = $fs->get_area_files($usercontext->id, 'user', 'private', 0);
1121 $this->assertCount(4, $files);
1122 $found = false;
1123 foreach ($files as $file) {
1124 if ($file->get_filename() == 'second.txt') {
1125 $found = true;
1126 $this->assertEquals('new content', $file->get_content());
1127 }
1128 }
1129 $this->assertTrue($found);
1130
1131 // Update author.
1132 // Set different author in the current file.
1133 foreach ($files as $file) {
1134 if ($file->get_filename() == 'second.txt') {
1135 $file->set_author('Nobody');
1136 }
1137 }
1138 $filerecord = array(
1139 'filename' => 'second.txt',
1140 );
1141 $file = self::create_draft_file($filerecord);
1142
1143 file_merge_files_from_draft_area_into_filearea($file->get_itemid(), $usercontext->id, 'user', 'private', 0, $options);
1144
1145 $files = $fs->get_area_files($usercontext->id, 'user', 'private', 0);
1146 $this->assertCount(4, $files);
1147 $found = false;
1148 foreach ($files as $file) {
1149 if ($file->get_filename() == 'second.txt') {
1150 $found = true;
1151 $this->assertEquals(fullname($USER), $file->get_author());
1152 }
1153 }
1154 $this->assertTrue($found);
1155
1156 }
1157
1158 /**
1159 * Test max area bytes for file_merge_files_from_draft_area_into_filearea
1160 */
1161 public function test_file_merge_files_from_draft_area_into_filearea_max_area_bytes() {
1162 global $USER;
1163
1164 $this->resetAfterTest(true);
1165 $this->setAdminUser();
1166 $fs = get_file_storage();
1167
1168 $file = self::create_draft_file();
1169 $options = array('subdirs' => 1,
1170 'maxbytes' => 5,
1171 'maxfiles' => -1,
1172 'areamaxbytes' => 10);
1173
1174 // Add new file.
1175 file_merge_files_from_draft_area_into_filearea($file->get_itemid(), $file->get_contextid(), 'user', 'private', 0, $options);
1176 $usercontext = context_user::instance($USER->id);
1177 $files = $fs->get_area_files($usercontext->id, 'user', 'private', 0);
1178 $this->assertCount(0, $files);
1179 }
1180
1181 /**
1182 * Test max file bytes for file_merge_files_from_draft_area_into_filearea
1183 */
1184 public function test_file_merge_files_from_draft_area_into_filearea_max_file_bytes() {
1185 global $USER;
1186
1187 $this->resetAfterTest(true);
1188 $this->setAdminUser();
1189 $fs = get_file_storage();
1190
1191 $file = self::create_draft_file();
1192 $options = array('subdirs' => 1,
1193 'maxbytes' => 1,
1194 'maxfiles' => -1,
1195 'areamaxbytes' => 100);
1196
1197 // Add new file.
1198 file_merge_files_from_draft_area_into_filearea($file->get_itemid(), $file->get_contextid(), 'user', 'private', 0, $options);
1199 $usercontext = context_user::instance($USER->id);
1200 // Check we only get the base directory, not a new file.
1201 $files = $fs->get_area_files($usercontext->id, 'user', 'private', 0);
1202 $this->assertCount(1, $files);
1203 $file = array_shift($files);
1204 $this->assertTrue($file->is_directory());
1205 }
1206
1207 /**
1208 * Test max file number for file_merge_files_from_draft_area_into_filearea
1209 */
1210 public function test_file_merge_files_from_draft_area_into_filearea_max_files() {
1211 global $USER;
1212
1213 $this->resetAfterTest(true);
1214 $this->setAdminUser();
1215 $fs = get_file_storage();
1216
1217 $file = self::create_draft_file();
1218 $options = array('subdirs' => 1,
1219 'maxbytes' => 1000,
1220 'maxfiles' => 0,
1221 'areamaxbytes' => 1000);
1222
1223 // Add new file.
1224 file_merge_files_from_draft_area_into_filearea($file->get_itemid(), $file->get_contextid(), 'user', 'private', 0, $options);
1225 $usercontext = context_user::instance($USER->id);
1226 // Check we only get the base directory, not a new file.
1227 $files = $fs->get_area_files($usercontext->id, 'user', 'private', 0);
1228 $this->assertCount(1, $files);
1229 $file = array_shift($files);
1230 $this->assertTrue($file->is_directory());
1231 }
02375710
JL
1232
1233 /**
1234 * Test file_get_draft_area_info.
1235 */
1236 public function test_file_get_draft_area_info() {
1237 global $USER;
1238
1239 $this->resetAfterTest(true);
1240 $this->setAdminUser();
1241 $fs = get_file_storage();
1242
1243 $filerecord = array(
1244 'filename' => 'one.txt',
1245 );
1246 $file = self::create_draft_file($filerecord);
1247 $size = $file->get_filesize();
1248 $draftitemid = $file->get_itemid();
1249 // Add another file.
1250 $filerecord = array(
1251 'itemid' => $draftitemid,
1252 'filename' => 'second.txt',
1253 );
1254 $file = self::create_draft_file($filerecord);
1255 $size += $file->get_filesize();
1256
1257 // Create directory.
1258 $usercontext = context_user::instance($USER->id);
1259 $dir = $fs->create_directory($usercontext->id, 'user', 'draft', $draftitemid, '/testsubdir/');
1260 // Add file to directory.
1261 $filerecord = array(
1262 'itemid' => $draftitemid,
1263 'filename' => 'third.txt',
1264 'filepath' => '/testsubdir/',
1265 );
1266 $file = self::create_draft_file($filerecord);
1267 $size += $file->get_filesize();
1268
1269 $fileinfo = file_get_draft_area_info($draftitemid);
1270 $this->assertEquals(3, $fileinfo['filecount']);
1271 $this->assertEquals($size, $fileinfo['filesize']);
bef201c7 1272 $this->assertEquals(1, $fileinfo['foldercount']); // Directory created.
02375710
JL
1273 $this->assertEquals($size, $fileinfo['filesize_without_references']);
1274
1275 // Now get files from just one folder.
1276 $fileinfo = file_get_draft_area_info($draftitemid, '/testsubdir/');
1277 $this->assertEquals(1, $fileinfo['filecount']);
1278 $this->assertEquals($file->get_filesize(), $fileinfo['filesize']);
1279 $this->assertEquals(0, $fileinfo['foldercount']); // No subdirectories inside the directory.
1280 $this->assertEquals($file->get_filesize(), $fileinfo['filesize_without_references']);
1281
1282 // Check we get the same results if we call file_get_file_area_info.
1283 $fileinfo = file_get_file_area_info($usercontext->id, 'user', 'draft', $draftitemid);
1284 $this->assertEquals(3, $fileinfo['filecount']);
1285 $this->assertEquals($size, $fileinfo['filesize']);
bef201c7 1286 $this->assertEquals(1, $fileinfo['foldercount']); // Directory created.
02375710
JL
1287 $this->assertEquals($size, $fileinfo['filesize_without_references']);
1288 }
1289
f74ac6e7 1290 /**
02375710
JL
1291 * Test file_get_file_area_info.
1292 */
1293 public function test_file_get_file_area_info() {
1294 global $USER;
1295
1296 $this->resetAfterTest(true);
1297 $this->setAdminUser();
1298 $fs = get_file_storage();
1299
1300 $filerecord = array(
1301 'filename' => 'one.txt',
1302 );
1303 $file = self::create_draft_file($filerecord);
1304 $size = $file->get_filesize();
1305 $draftitemid = $file->get_itemid();
1306 // Add another file.
1307 $filerecord = array(
1308 'itemid' => $draftitemid,
1309 'filename' => 'second.txt',
1310 );
1311 $file = self::create_draft_file($filerecord);
1312 $size += $file->get_filesize();
1313
1314 // Create directory.
1315 $usercontext = context_user::instance($USER->id);
1316 $dir = $fs->create_directory($usercontext->id, 'user', 'draft', $draftitemid, '/testsubdir/');
1317 // Add file to directory.
1318 $filerecord = array(
1319 'itemid' => $draftitemid,
1320 'filename' => 'third.txt',
1321 'filepath' => '/testsubdir/',
1322 );
1323 $file = self::create_draft_file($filerecord);
1324 $size += $file->get_filesize();
1325
1326 // Add files to user private file area.
1327 $options = array('subdirs' => 1, 'maxfiles' => 3);
1328 file_merge_files_from_draft_area_into_filearea($draftitemid, $file->get_contextid(), 'user', 'private', 0, $options);
1329
1330 $fileinfo = file_get_file_area_info($usercontext->id, 'user', 'private');
1331 $this->assertEquals(3, $fileinfo['filecount']);
1332 $this->assertEquals($size, $fileinfo['filesize']);
bef201c7 1333 $this->assertEquals(1, $fileinfo['foldercount']); // Directory created.
02375710
JL
1334 $this->assertEquals($size, $fileinfo['filesize_without_references']);
1335
1336 // Now get files from just one folder.
1337 $fileinfo = file_get_file_area_info($usercontext->id, 'user', 'private', 0, '/testsubdir/');
1338 $this->assertEquals(1, $fileinfo['filecount']);
1339 $this->assertEquals($file->get_filesize(), $fileinfo['filesize']);
1340 $this->assertEquals(0, $fileinfo['foldercount']); // No subdirectories inside the directory.
1341 $this->assertEquals($file->get_filesize(), $fileinfo['filesize_without_references']);
1342 }
cbb7c6a9
DC
1343}
1344
1345/**
1346 * Test-specific class to allow easier testing of curl functions.
9a1adda5
EL
1347 *
1348 * @copyright 2015 Dave Cooper
cbb7c6a9
DC
1349 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1350 */
9a1adda5 1351class testable_curl extends curl {
cbb7c6a9 1352 /**
9a1adda5
EL
1353 * Accessor for private options array using reflection.
1354 *
1355 * @return array
cbb7c6a9
DC
1356 */
1357 public function get_options() {
9a1adda5
EL
1358 // Access to private property.
1359 $rp = new ReflectionProperty('curl', 'options');
1360 $rp->setAccessible(true);
1361 return $rp->getValue($this);
1362 }
1363
1364 /**
1365 * Setter for private options array using reflection.
1366 *
1367 * @param array $options
1368 */
1369 public function set_options($options) {
1370 // Access to private property.
1371 $rp = new ReflectionProperty('curl', 'options');
1372 $rp->setAccessible(true);
1373 $rp->setValue($this, $options);
cbb7c6a9
DC
1374 }
1375
1376 /**
9a1adda5 1377 * Setter for individual option.
cbb7c6a9
DC
1378 * @param string $option
1379 * @param string $value
1380 */
1381 public function set_option($option, $value) {
9a1adda5
EL
1382 $options = $this->get_options();
1383 $options[$option] = $value;
1384 $this->set_options($options);
cbb7c6a9
DC
1385 }
1386
1387 /**
1388 * Unsets an option on the curl object
1389 * @param string $option
1390 */
1391 public function unset_option($option) {
9a1adda5
EL
1392 $options = $this->get_options();
1393 unset($options[$option]);
1394 $this->set_options($options);
cbb7c6a9
DC
1395 }
1396
1397 /**
9a1adda5 1398 * Wrapper to access the private curl::apply_opt() method using reflection.
cbb7c6a9
DC
1399 *
1400 * @param array $options
1401 * @return resource The curl handle
1402 */
1403 public function call_apply_opt($options = null) {
9a1adda5
EL
1404 // Access to private method.
1405 $rm = new ReflectionMethod('curl', 'apply_opt');
1406 $rm->setAccessible(true);
cbb7c6a9 1407 $ch = curl_init();
9a1adda5 1408 return $rm->invoke($this, $ch, $options);
cbb7c6a9 1409 }
a3d5830a 1410}