a367aaaeaa122a9cae41fb9fb551a7ac32921ae9
[moodle.git] / lib / tests / filelib_test.php
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/>.
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  */
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->libdir . '/filelib.php');
30 require_once($CFG->dirroot . '/repository/lib.php');
32 class core_filelib_testcase extends advanced_testcase {
33     public function test_format_postdata_for_curlcall() {
35         // POST params with just simple types.
36         $postdatatoconvert = array( 'userid' => 1, 'roleid' => 22, 'name' => 'john');
37         $expectedresult = "userid=1&roleid=22&name=john";
38         $postdata = format_postdata_for_curlcall($postdatatoconvert);
39         $this->assertEquals($expectedresult, $postdata);
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' => '&'.
44         $postdata = format_postdata_for_curlcall($postdatatoconvert);
45         $this->assertEquals($expectedresult, $postdata);
47         // POST params with an empty value.
48         $postdatatoconvert = array( 'name' => null, 'roleid' => 22);
49         $expectedresult = "name=&roleid=22";
50         $postdata = format_postdata_for_curlcall($postdatatoconvert);
51         $this->assertEquals($expectedresult, $postdata);
53         // POST params with complex types.
54         $postdatatoconvert = array( 'users' => array(
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);
69         $this->assertEquals($expectedresult, $postdata);
71         // POST params with other complex types.
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);
80         $this->assertEquals($expectedresult, $postdata);
81     }
83     public function test_download_file_content() {
84         global $CFG;
86         // Test http success first.
87         $testhtml = $this->getExternalTestFileUrl('/test.html');
89         $contents = download_file_content($testhtml);
90         $this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
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);
97         $this->assertSame(file_get_contents($tofile), $contents);
98         @unlink($tofile);
100         $result = download_file_content($testhtml, null, null, false, 300, 20, false, null, true);
101         $this->assertSame($contents, $result);
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));
107         $this->assertRegExp('|^HTTP/1\.[01] 200 OK$|', rtrim($response->response_code));
108         $this->assertSame($contents, $response->results);
109         $this->assertSame('', $response->error);
111         // Test https success.
112         $testhtml = $this->getExternalTestFileUrl('/test.html', true);
114         $contents = download_file_content($testhtml, null, null, false, 300, 20, true);
115         $this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
117         $contents = download_file_content($testhtml);
118         $this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
120         // Now 404.
121         $testhtml = $this->getExternalTestFileUrl('/test.html_nonexistent');
123         $contents = download_file_content($testhtml);
124         $this->assertFalse($contents);
125         $this->assertDebuggingCalled();
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));
131         $this->assertRegExp('|^HTTP/1\.[01] 404 Not Found$|', rtrim($response->response_code));
132         // Do not test the response starts with DOCTYPE here because some servers may return different headers.
133         $this->assertSame('', $response->error);
135         // Invalid url.
136         $testhtml = $this->getExternalTestFileUrl('/test.html');
137         $testhtml = str_replace('http://', 'ftp://', $testhtml);
139         $contents = download_file_content($testhtml);
140         $this->assertFalse($contents);
142         // Test standard redirects.
143         $testurl = $this->getExternalTestFileUrl('/test_redir.php');
145         $contents = download_file_content("$testurl?redir=2");
146         $this->assertSame('done', $contents);
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));
152         $this->assertRegExp('|^HTTP/1\.[01] 200 OK$|', rtrim($response->response_code));
153         $this->assertSame('done', $response->results);
154         $this->assertSame('', $response->error);
156         // Commented out this block if there are performance problems.
157         /*
158         $contents = download_file_content("$testurl?redir=6");
159         $this->assertFalse(false, $contents);
160         $this->assertDebuggingCalled();
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);
167         */
169         // Test relative redirects.
170         $testurl = $this->getExternalTestFileUrl('/test_relative_redir.php');
172         $contents = download_file_content("$testurl");
173         $this->assertSame('done', $contents);
175         $contents = download_file_content("$testurl?unused=xxx");
176         $this->assertSame('done', $contents);
177     }
179     /**
180      * Test curl basics.
181      */
182     public function test_curl_basics() {
183         global $CFG;
185         // Test HTTP success.
186         $testhtml = $this->getExternalTestFileUrl('/test.html');
188         $curl = new curl();
189         $contents = $curl->get($testhtml);
190         $this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
191         $this->assertSame(0, $curl->get_errno());
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);
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);
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     }
221     /**
222      * Test a curl basic request with security enabled.
223      */
224     public function test_curl_basics_with_security_helper() {
225         $this->resetAfterTest();
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.
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());
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());
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();
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'));
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));
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     }
262     public function test_curl_redirects() {
263         global $CFG;
265         // Test full URL redirects.
266         $testurl = $this->getExternalTestFileUrl('/test_redir.php');
268         $curl = new curl();
269         $contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>2));
270         $response = $curl->getResponse();
271         $this->assertSame('200 OK', reset($response));
272         $this->assertSame(0, $curl->get_errno());
273         $this->assertSame(2, $curl->info['redirect_count']);
274         $this->assertSame('done', $contents);
276         $curl = new curl();
277         $curl->emulateredirects = true;
278         $contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>2));
279         $response = $curl->getResponse();
280         $this->assertSame('200 OK', reset($response));
281         $this->assertSame(0, $curl->get_errno());
282         $this->assertSame(2, $curl->info['redirect_count']);
283         $this->assertSame('done', $contents);
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         }
295         $curl = new curl();
296         $contents = $curl->get("$testurl?redir=3", array(), array('CURLOPT_FOLLOWLOCATION'=>0));
297         $response = $curl->getResponse();
298         $this->assertSame($responsecode302, reset($response));
299         $this->assertSame(0, $curl->get_errno());
300         $this->assertSame(302, $curl->info['http_code']);
301         $this->assertSame('', $contents);
303         $curl = new curl();
304         $curl->emulateredirects = true;
305         $contents = $curl->get("$testurl?redir=3", array(), array('CURLOPT_FOLLOWLOCATION'=>0));
306         $response = $curl->getResponse();
307         $this->assertSame($responsecode302, reset($response));
308         $this->assertSame(0, $curl->get_errno());
309         $this->assertSame(302, $curl->info['http_code']);
310         $this->assertSame('', $contents);
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);
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);
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);
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);
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);
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);
364     }
366     public function test_curl_relative_redirects() {
367         // Test relative location redirects.
368         $testurl = $this->getExternalTestFileUrl('/test_relative_redir.php');
370         $curl = new curl();
371         $contents = $curl->get($testurl);
372         $response = $curl->getResponse();
373         $this->assertSame('200 OK', reset($response));
374         $this->assertSame(0, $curl->get_errno());
375         $this->assertSame(1, $curl->info['redirect_count']);
376         $this->assertSame('done', $contents);
378         $curl = new curl();
379         $curl->emulateredirects = true;
380         $contents = $curl->get($testurl);
381         $response = $curl->getResponse();
382         $this->assertSame('200 OK', reset($response));
383         $this->assertSame(0, $curl->get_errno());
384         $this->assertSame(1, $curl->info['redirect_count']);
385         $this->assertSame('done', $contents);
387         // Test different redirect types.
388         $testurl = $this->getExternalTestFileUrl('/test_relative_redir.php');
390         $curl = new curl();
391         $contents = $curl->get("$testurl?type=301");
392         $response = $curl->getResponse();
393         $this->assertSame('200 OK', reset($response));
394         $this->assertSame(0, $curl->get_errno());
395         $this->assertSame(1, $curl->info['redirect_count']);
396         $this->assertSame('done', $contents);
398         $curl = new curl();
399         $curl->emulateredirects = true;
400         $contents = $curl->get("$testurl?type=301");
401         $response = $curl->getResponse();
402         $this->assertSame('200 OK', reset($response));
403         $this->assertSame(0, $curl->get_errno());
404         $this->assertSame(1, $curl->info['redirect_count']);
405         $this->assertSame('done', $contents);
407         $curl = new curl();
408         $contents = $curl->get("$testurl?type=302");
409         $response = $curl->getResponse();
410         $this->assertSame('200 OK', reset($response));
411         $this->assertSame(0, $curl->get_errno());
412         $this->assertSame(1, $curl->info['redirect_count']);
413         $this->assertSame('done', $contents);
415         $curl = new curl();
416         $curl->emulateredirects = true;
417         $contents = $curl->get("$testurl?type=302");
418         $response = $curl->getResponse();
419         $this->assertSame('200 OK', reset($response));
420         $this->assertSame(0, $curl->get_errno());
421         $this->assertSame(1, $curl->info['redirect_count']);
422         $this->assertSame('done', $contents);
424         $curl = new curl();
425         $contents = $curl->get("$testurl?type=303");
426         $response = $curl->getResponse();
427         $this->assertSame('200 OK', reset($response));
428         $this->assertSame(0, $curl->get_errno());
429         $this->assertSame(1, $curl->info['redirect_count']);
430         $this->assertSame('done', $contents);
432         $curl = new curl();
433         $curl->emulateredirects = true;
434         $contents = $curl->get("$testurl?type=303");
435         $response = $curl->getResponse();
436         $this->assertSame('200 OK', reset($response));
437         $this->assertSame(0, $curl->get_errno());
438         $this->assertSame(1, $curl->info['redirect_count']);
439         $this->assertSame('done', $contents);
441         $curl = new curl();
442         $contents = $curl->get("$testurl?type=307");
443         $response = $curl->getResponse();
444         $this->assertSame('200 OK', reset($response));
445         $this->assertSame(0, $curl->get_errno());
446         $this->assertSame(1, $curl->info['redirect_count']);
447         $this->assertSame('done', $contents);
449         $curl = new curl();
450         $curl->emulateredirects = true;
451         $contents = $curl->get("$testurl?type=307");
452         $response = $curl->getResponse();
453         $this->assertSame('200 OK', reset($response));
454         $this->assertSame(0, $curl->get_errno());
455         $this->assertSame(1, $curl->info['redirect_count']);
456         $this->assertSame('done', $contents);
458         $curl = new curl();
459         $contents = $curl->get("$testurl?type=308");
460         $response = $curl->getResponse();
461         $this->assertSame('200 OK', reset($response));
462         $this->assertSame(0, $curl->get_errno());
463         $this->assertSame(1, $curl->info['redirect_count']);
464         $this->assertSame('done', $contents);
466         $curl = new curl();
467         $curl->emulateredirects = true;
468         $contents = $curl->get("$testurl?type=308");
469         $response = $curl->getResponse();
470         $this->assertSame('200 OK', reset($response));
471         $this->assertSame(0, $curl->get_errno());
472         $this->assertSame(1, $curl->info['redirect_count']);
473         $this->assertSame('done', $contents);
475     }
477     public function test_curl_proxybypass() {
478         global $CFG;
479         $testurl = $this->getExternalTestFileUrl('/test.html');
481         $oldproxy = $CFG->proxyhost;
482         $oldproxybypass = $CFG->proxybypass;
484         // Test without proxy bypass and inaccessible proxy.
485         $CFG->proxyhost = 'i.do.not.exist';
486         $CFG->proxybypass = '';
487         $curl = new curl();
488         $contents = $curl->get($testurl);
489         $this->assertNotEquals(0, $curl->get_errno());
490         $this->assertNotEquals('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
492         // Test with proxy bypass.
493         $testurlhost = parse_url($testurl, PHP_URL_HOST);
494         $CFG->proxybypass = $testurlhost;
495         $curl = new curl();
496         $contents = $curl->get($testurl);
497         $this->assertSame(0, $curl->get_errno());
498         $this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
500         $CFG->proxyhost = $oldproxy;
501         $CFG->proxybypass = $oldproxybypass;
502     }
504     public function test_curl_post() {
505         $testurl = $this->getExternalTestFileUrl('/test_post.php');
507         // Test post request.
508         $curl = new curl();
509         $contents = $curl->post($testurl, 'data=moodletest');
510         $response = $curl->getResponse();
511         $this->assertSame('200 OK', reset($response));
512         $this->assertSame(0, $curl->get_errno());
513         $this->assertSame('OK', $contents);
515         // Test 100 requests.
516         $curl = new curl();
517         $curl->setHeader('Expect: 100-continue');
518         $contents = $curl->post($testurl, 'data=moodletest');
519         $response = $curl->getResponse();
520         $this->assertSame('200 OK', reset($response));
521         $this->assertSame(0, $curl->get_errno());
522         $this->assertSame('OK', $contents);
523     }
525     public function test_curl_file() {
526         $this->resetAfterTest();
527         $testurl = $this->getExternalTestFileUrl('/test_file.php');
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);
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     }
548     public function test_curl_protocols() {
550         // HTTP and HTTPS requests were verified in previous requests. Now check
551         // that we can selectively disable some protocols.
552         $curl = new curl();
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);
560         $testurl = 'ftp://nowhere';
561         $curl->get($testurl);
562         $this->assertNotEmpty($curl->error);
563         $this->assertEquals(CURLE_UNSUPPORTED_PROTOCOL, $curl->errno);
565         $testurl = 'telnet://somewhere';
566         $curl->get($testurl);
567         $this->assertNotEmpty($curl->error);
568         $this->assertEquals(CURLE_UNSUPPORTED_PROTOCOL, $curl->errno);
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);
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);
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     }
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;
596         $this->resetAfterTest(true);
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));
603         $repositorypluginname = 'user';
605         $args = array();
606         $args['type'] = $repositorypluginname;
607         $repos = repository::get_instances($args);
608         $userrepository = reset($repos);
609         $this->assertInstanceOf('repository', $userrepository);
611         $fs = get_file_storage();
613         $syscontext = context_system::instance();
614         $component = 'core';
615         $filearea  = 'unittest';
616         $itemid    = 0;
617         $filepath  = '/';
618         $filename  = 'test.txt';
619         $sourcefield = 'Copyright stuff';
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);
631         $originalfile = $fs->create_file_from_string($filerecord, 'Test content');
632         $fileid = $originalfile->get_id();
633         $this->assertInstanceOf('stored_file', $originalfile);
635         // Create a user private file.
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);
647         $filerefrecord = clone((object)$filerecord);
648         $filerefrecord->filename = 'testref.txt';
650         // Create a file reference.
651         $fileref = $fs->create_file_from_reference($filerefrecord, $userrepository->id, $userfileref);
652         $this->assertInstanceOf('stored_file', $fileref);
653         $this->assertEquals($userrepository->id, $fileref->get_repository_id());
654         $this->assertSame($userfile->get_contenthash(), $fileref->get_contenthash());
655         $this->assertEquals($userfile->get_filesize(), $fileref->get_filesize());
656         $this->assertRegExp('#' . $userfile->get_filename(). '$#', $fileref->get_reference_details());
658         $draftitemid = 0;
659         file_prepare_draft_area($draftitemid, $syscontext->id, $component, $filearea, $itemid);
661         $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid);
662         $this->assertCount(3, $draftfiles);
664         $draftfile = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $filepath, $filename);
665         $source = unserialize($draftfile->get_source());
666         $this->assertSame($ref, $source->original);
667         $this->assertSame($sourcefield, $source->source);
669         $draftfileref = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $filepath, $filerefrecord->filename);
670         $this->assertInstanceOf('stored_file', $draftfileref);
671         $this->assertTrue($draftfileref->is_external_file());
673         // Change some information.
674         $author = 'Dongsheng Cai';
675         $draftfile->set_author($author);
676         $newsourcefield = 'Get from Flickr';
677         $license = 'GPLv3';
678         $draftfile->set_license($license);
679         // If you want to really just change source field, do this.
680         $source = unserialize($draftfile->get_source());
681         $newsourcefield = 'From flickr';
682         $source->source = $newsourcefield;
683         $draftfile->set_source(serialize($source));
685         // Save changed file.
686         file_save_draft_area_files($draftitemid, $syscontext->id, $component, $filearea, $itemid);
688         $file = $fs->get_file($syscontext->id, $component, $filearea, $itemid, $filepath, $filename);
690         // Make sure it's the original file id.
691         $this->assertEquals($fileid, $file->get_id());
692         $this->assertInstanceOf('stored_file', $file);
693         $this->assertSame($author, $file->get_author());
694         $this->assertSame($license, $file->get_license());
695         $this->assertEquals($newsourcefield, $file->get_source());
696     }
698     /**
699      * Testing deleting original files.
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;
707         $this->resetAfterTest(true);
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));
714         $repositorypluginname = 'user';
716         $args = array();
717         $args['type'] = $repositorypluginname;
718         $repos = repository::get_instances($args);
719         $userrepository = reset($repos);
720         $this->assertInstanceOf('repository', $userrepository);
722         $fs = get_file_storage();
723         $syscontext = context_system::instance();
725         $filecontent = 'User file content';
727         // Create a user private file.
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();
740         $filerecord = array(
741             'contextid' => $syscontext->id,
742             'component' => 'core',
743             'filearea'  => 'phpunit',
744             'itemid'    => 0,
745             'filepath'  => '/',
746             'filename'  => 'test.txt',
747         );
748         // Create a file reference.
749         $fileref = $fs->create_file_from_reference($filerecord, $userrepository->id, $userfileref);
750         $this->assertInstanceOf('stored_file', $fileref);
751         $this->assertEquals($userrepository->id, $fileref->get_repository_id());
752         $this->assertSame($userfile->get_contenthash(), $fileref->get_contenthash());
753         $this->assertEquals($userfile->get_filesize(), $fileref->get_filesize());
754         $this->assertRegExp('#' . $userfile->get_filename(). '$#', $fileref->get_reference_details());
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);
759         $this->assertCount(2, $draftfiles);
760         $draftfile = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $userfilerecord->filepath, $userfilerecord->filename);
761         $draftfile->delete();
762         // Save changed file.
763         file_save_draft_area_files($draftitemid, $usercontext->id, 'user', 'private', 0);
765         // The file reference should be a regular moodle file now.
766         $fileref = $fs->get_file($syscontext->id, 'core', 'phpunit', 0, '/', 'test.txt');
767         $this->assertFalse($fileref->is_external_file());
768         $this->assertSame($contenthash, $fileref->get_contenthash());
769         $this->assertEquals($filecontent, $fileref->get_content());
770     }
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
778 HTTP/1.0 407 Proxy Authentication Required
779 Server: squid/2.7.STABLE9
780 Date: Thu, 08 Dec 2011 14:44:33 GMT
781 Content-Type: text/html
782 Content-Length: 1275
783 X-Squid-Error: ERR_CACHE_ACCESS_DENIED 0
784 Proxy-Authenticate: Basic realm="Squid proxy-caching web server"
785 X-Cache: MISS from homer.lancs.ac.uk
786 X-Cache-Lookup: NONE from homer.lancs.ac.uk:3128
787 Via: 1.0 homer.lancs.ac.uk:3128 (squid/2.7.STABLE9)
788 Connection: close
790 HTTP/1.0 200 OK
791 Server: Apache
792 X-Lb-Nocache: true
793 Cache-Control: private, max-age=15, no-transform
794 ETag: "4d69af5d8ba873ea9192c489e151bd7b"
795 Content-Type: text/html
796 Date: Thu, 08 Dec 2011 14:44:53 GMT
797 Set-Cookie: BBC-UID=c4de2e109c8df6a51de627cee11b214bd4fb6054a030222488317afb31b343360MoodleBot/1.0; expires=Mon, 07-Dec-15 14:44:53 GMT; path=/; domain=bbc.co.uk
798 X-Cache-Action: MISS
799 X-Cache-Age: 0
800 Vary: Cookie,X-Country,X-Ip-is-uk-combined,X-Ip-is-advertise-combined,X-Ip_is_uk_combined,X-Ip_is_advertise_combined, X-GeoIP
801 X-Cache: MISS from ww
803 <html>...
804 EOF;
805         $mdl30648expected = <<<EOF
806 HTTP/1.0 200 OK
807 Server: Apache
808 X-Lb-Nocache: true
809 Cache-Control: private, max-age=15, no-transform
810 ETag: "4d69af5d8ba873ea9192c489e151bd7b"
811 Content-Type: text/html
812 Date: Thu, 08 Dec 2011 14:44:53 GMT
813 Set-Cookie: BBC-UID=c4de2e109c8df6a51de627cee11b214bd4fb6054a030222488317afb31b343360MoodleBot/1.0; expires=Mon, 07-Dec-15 14:44:53 GMT; path=/; domain=bbc.co.uk
814 X-Cache-Action: MISS
815 X-Cache-Age: 0
816 Vary: Cookie,X-Country,X-Ip-is-uk-combined,X-Ip-is-advertise-combined,X-Ip_is_uk_combined,X-Ip_is_advertise_combined, X-GeoIP
817 X-Cache: MISS from ww
819 <html>...
820 EOF;
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);
825         // Test stripping works OK.
826         $this->assertSame($mdl30648expected, curl::strip_double_headers($mdl30648example));
827         // Test it does nothing to the 'plain' data.
828         $this->assertSame($mdl30648expected, curl::strip_double_headers($mdl30648expected));
830         // Example from OU proxy.
831         $httpsexample = <<<EOF
832 HTTP/1.0 200 Connection established
834 HTTP/1.1 200 OK
835 Date: Fri, 22 Feb 2013 17:14:23 GMT
836 Server: Apache/2
837 X-Powered-By: PHP/5.3.3-7+squeeze14
838 Content-Type: text/xml
839 Connection: close
840 Content-Encoding: gzip
841 Transfer-Encoding: chunked
843 <?xml version="1.0" encoding="ISO-8859-1" ?>
844 <rss version="2.0">...
845 EOF;
846         $httpsexpected = <<<EOF
847 HTTP/1.1 200 OK
848 Date: Fri, 22 Feb 2013 17:14:23 GMT
849 Server: Apache/2
850 X-Powered-By: PHP/5.3.3-7+squeeze14
851 Content-Type: text/xml
852 Connection: close
853 Content-Encoding: gzip
854 Transfer-Encoding: chunked
856 <?xml version="1.0" encoding="ISO-8859-1" ?>
857 <rss version="2.0">...
858 EOF;
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);
863         // Test stripping works OK.
864         $this->assertSame($httpsexpected, curl::strip_double_headers($httpsexample));
865         // Test it does nothing to the 'plain' data.
866         $this->assertSame($httpsexpected, curl::strip_double_headers($httpsexpected));
867     }
869     /**
870      * Tests the get_mimetype_description function.
871      */
872     public function test_get_mimetype_description() {
873         $this->resetAfterTest();
875         // Test example type (.doc).
876         $this->assertEquals(get_string('application/msword', 'mimetypes'),
877                 get_mimetype_description(array('filename' => 'test.doc')));
879         // Test an unknown file type.
880         $this->assertEquals(get_string('document/unknown', 'mimetypes'),
881                 get_mimetype_description(array('filename' => 'test.frog')));
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')));
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')));
894         // Test custom description using multilang filter.
895         filter_manager::reset_caches();
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     }
905     /**
906      * Tests the get_mimetypes_array function.
907      */
908     public function test_get_mimetypes_array() {
909         $mimeinfo = get_mimetypes_array();
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']));
919         // Check the less common fields using other examples.
920         $this->assertEquals('image', $mimeinfo['png']['string']);
921         $this->assertEquals(true, $mimeinfo['txt']['defaulticon']);
922     }
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());
931         // Argument is null.
932         $this->assertEquals('application/octet-stream', get_mimetype_for_sending(null));
934         // Filename having no extension.
935         $this->assertEquals('application/octet-stream', get_mimetype_for_sending('filenamewithoutextension'));
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     }
948     /**
949      * Test curl agent settings.
950      */
951     public function test_curl_useragent() {
952         $curl = new testable_curl();
953         $options = $curl->get_options();
954         $this->assertNotEmpty($options);
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));
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));
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));
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));
976         $curl->unset_option('CURLOPT_USERAGENT');
977         $curl->call_apply_opt();
978         $this->assertTrue(in_array('User-Agent: MoodleBot/1.0', $curl->header));
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);
995     }
997     /**
998      * Test file_rewrite_pluginfile_urls.
999      */
1000     public function test_file_rewrite_pluginfile_urls() {
1002         $syscontext = context_system::instance();
1003         $originaltext = 'Fake test with an image <img src="@@PLUGINFILE@@/image.png">';
1005         // Do the rewrite.
1006         $finaltext = file_rewrite_pluginfile_urls($originaltext, 'pluginfile.php', $syscontext->id, 'user', 'private', 0);
1007         $this->assertContains("pluginfile.php", $finaltext);
1009         // Now undo.
1010         $options = array('reverse' => true);
1011         $finaltext = file_rewrite_pluginfile_urls($finaltext, 'pluginfile.php', $syscontext->id, 'user', 'private', 0, $options);
1013         // Compare the final text is the same that the original.
1014         $this->assertEquals($originaltext, $finaltext);
1015     }
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;
1026         self::setAdminUser();
1027         $fs = get_file_storage();
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         );
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';
1047         $file = $fs->create_file_from_string($filerecord, $content);
1048         $file->set_source($source);
1050         return $file;
1051     }
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;
1059         $this->resetAfterTest(true);
1060         $this->setAdminUser();
1061         $fs = get_file_storage();
1062         $usercontext = context_user::instance($USER->id);
1064         // Create a draft file.
1065         $filename = 'data.txt';
1066         $filerecord = array(
1067             'filename'  => $filename,
1068         );
1069         $file = self::create_draft_file($filerecord);
1070         $draftitemid = $file->get_itemid();
1072         $maxbytes = $CFG->userquota;
1073         $maxareabytes = $CFG->userquota;
1074         $options = array('subdirs' => 1,
1075                          'maxbytes' => $maxbytes,
1076                          'maxfiles' => -1,
1077                          'areamaxbytes' => $maxareabytes);
1079         // Add new file.
1080         file_merge_files_from_draft_area_into_filearea($draftitemid, $usercontext->id, 'user', 'private', 0, $options);
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);
1095         // Add two more files.
1096         $filerecord = array(
1097             'itemid'  => $draftitemid,
1098             'filename'  => 'second.txt',
1099         );
1100         self::create_draft_file($filerecord);
1101         $filerecord = array(
1102             'itemid'  => $draftitemid,
1103             'filename'  => 'third.txt',
1104         );
1105         $file = self::create_draft_file($filerecord);
1107         file_merge_files_from_draft_area_into_filearea($file->get_itemid(), $usercontext->id, 'user', 'private', 0, $options);
1109         $files = $fs->get_area_files($usercontext->id, 'user', 'private', 0);
1110         $this->assertCount(4, $files);
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);
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);
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);
1143         file_merge_files_from_draft_area_into_filearea($file->get_itemid(), $usercontext->id, 'user', 'private', 0, $options);
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);
1156     }
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;
1164         $this->resetAfterTest(true);
1165         $this->setAdminUser();
1166         $fs = get_file_storage();
1168         $file = self::create_draft_file();
1169         $options = array('subdirs' => 1,
1170                          'maxbytes' => 5,
1171                          'maxfiles' => -1,
1172                          'areamaxbytes' => 10);
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     }
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;
1187         $this->resetAfterTest(true);
1188         $this->setAdminUser();
1189         $fs = get_file_storage();
1191         $file = self::create_draft_file();
1192         $options = array('subdirs' => 1,
1193                          'maxbytes' => 1,
1194                          'maxfiles' => -1,
1195                          'areamaxbytes' => 100);
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     }
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;
1213         $this->resetAfterTest(true);
1214         $this->setAdminUser();
1215         $fs = get_file_storage();
1217         $file = self::create_draft_file();
1218         $options = array('subdirs' => 1,
1219                          'maxbytes' => 1000,
1220                          'maxfiles' => 0,
1221                          'areamaxbytes' => 1000);
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     }
1233     /**
1234      * Test file_get_draft_area_info.
1235      */
1236     public function test_file_get_draft_area_info() {
1237         global $USER;
1239         $this->resetAfterTest(true);
1240         $this->setAdminUser();
1241         $fs = get_file_storage();
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();
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();
1269         $fileinfo = file_get_draft_area_info($draftitemid);
1270         $this->assertEquals(3, $fileinfo['filecount']);
1271         $this->assertEquals($size, $fileinfo['filesize']);
1272         $this->assertEquals(2, $fileinfo['foldercount']);   // Base and directory created.
1273         $this->assertEquals($size, $fileinfo['filesize_without_references']);
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']);
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']);
1286         $this->assertEquals(2, $fileinfo['foldercount']);   // Base and directory created.
1287         $this->assertEquals($size, $fileinfo['filesize_without_references']);
1288     }
1290     /**
1291      * Test file_get_file_area_info.
1292      */
1293     public function test_file_get_file_area_info() {
1294         global $USER;
1296         $this->resetAfterTest(true);
1297         $this->setAdminUser();
1298         $fs = get_file_storage();
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();
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();
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);
1330         $fileinfo = file_get_file_area_info($usercontext->id, 'user', 'private');
1331         $this->assertEquals(3, $fileinfo['filecount']);
1332         $this->assertEquals($size, $fileinfo['filesize']);
1333         $this->assertEquals(2, $fileinfo['foldercount']);   // Base and directory created.
1334         $this->assertEquals($size, $fileinfo['filesize_without_references']);
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     }
1345 /**
1346  * Test-specific class to allow easier testing of curl functions.
1347  *
1348  * @copyright 2015 Dave Cooper
1349  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1350  */
1351 class testable_curl extends curl {
1352     /**
1353      * Accessor for private options array using reflection.
1354      *
1355      * @return array
1356      */
1357     public function get_options() {
1358         // Access to private property.
1359         $rp = new ReflectionProperty('curl', 'options');
1360         $rp->setAccessible(true);
1361         return $rp->getValue($this);
1362     }
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);
1374     }
1376     /**
1377      * Setter for individual option.
1378      * @param string $option
1379      * @param string $value
1380      */
1381     public function set_option($option, $value) {
1382         $options = $this->get_options();
1383         $options[$option] = $value;
1384         $this->set_options($options);
1385     }
1387     /**
1388      * Unsets an option on the curl object
1389      * @param string $option
1390      */
1391     public function unset_option($option) {
1392         $options = $this->get_options();
1393         unset($options[$option]);
1394         $this->set_options($options);
1395     }
1397     /**
1398      * Wrapper to access the private curl::apply_opt() method using reflection.
1399      *
1400      * @param array $options
1401      * @return resource The curl handle
1402      */
1403     public function call_apply_opt($options = null) {
1404         // Access to private method.
1405         $rm = new ReflectionMethod('curl', 'apply_opt');
1406         $rm->setAccessible(true);
1407         $ch = curl_init();
1408         return $rm->invoke($this, $ch, $options);
1409     }