MDL-38170 SimplePie: Cannot read https feeds through proxy
[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/>.
18 /**
19  * Unit tests for /lib/filelib.php.
20  *
21  * @package   core_files
22  * @category  phpunit
23  * @copyright 2009 Jerome Mouneyrac
24  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25  */
27 defined('MOODLE_INTERNAL') || die();
29 global $CFG;
30 require_once($CFG->libdir . '/filelib.php');
31 require_once($CFG->dirroot . '/repository/lib.php');
33 class filelib_testcase extends advanced_testcase {
34     public function test_format_postdata_for_curlcall() {
36         //POST params with just simple types
37         $postdatatoconvert =array( 'userid' => 1, 'roleid' => 22, 'name' => 'john');
38         $expectedresult = "userid=1&roleid=22&name=john";
39         $postdata = format_postdata_for_curlcall($postdatatoconvert);
40         $this->assertEquals($postdata, $expectedresult);
42         //POST params with a string containing & character
43         $postdatatoconvert =array( 'name' => 'john&emilie', 'roleid' => 22);
44         $expectedresult = "name=john%26emilie&roleid=22"; //urlencode: '%26' => '&'
45         $postdata = format_postdata_for_curlcall($postdatatoconvert);
46         $this->assertEquals($postdata, $expectedresult);
48         //POST params with an empty value
49         $postdatatoconvert =array( 'name' => null, 'roleid' => 22);
50         $expectedresult = "name=&roleid=22";
51         $postdata = format_postdata_for_curlcall($postdatatoconvert);
52         $this->assertEquals($postdata, $expectedresult);
54         //POST params with complex types
55         $postdatatoconvert =array( 'users' => array(
56             array(
57                 'id' => 2,
58                 'customfields' => array(
59                     array
60                     (
61                         'type' => 'Color',
62                         'value' => 'violet'
63                     )
64                 )
65             )
66         )
67         );
68         $expectedresult = "users[0][id]=2&users[0][customfields][0][type]=Color&users[0][customfields][0][value]=violet";
69         $postdata = format_postdata_for_curlcall($postdatatoconvert);
70         $this->assertEquals($postdata, $expectedresult);
72         //POST params with other complex types
73         $postdatatoconvert = array ('members' =>
74         array(
75             array('groupid' => 1, 'userid' => 1)
76         , array('groupid' => 1, 'userid' => 2)
77         )
78         );
79         $expectedresult = "members[0][groupid]=1&members[0][userid]=1&members[1][groupid]=1&members[1][userid]=2";
80         $postdata = format_postdata_for_curlcall($postdatatoconvert);
81         $this->assertEquals($postdata, $expectedresult);
82     }
84     public function test_download_file_content() {
85         $testhtml = "http://download.moodle.org/unittest/test.html";
86         $contents = download_file_content($testhtml);
87         $this->assertEquals('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
88     }
90     /**
91      * Testing prepare draft area
92      *
93      * @copyright 2012 Dongsheng Cai {@link http://dongsheng.org}
94      * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
95      */
96     public function test_prepare_draft_area() {
97         global $USER, $DB;
99         $this->resetAfterTest(true);
101         $generator = $this->getDataGenerator();
102         $user = $generator->create_user();
103         $usercontext = context_user::instance($user->id);
104         $USER = $DB->get_record('user', array('id'=>$user->id));
106         $repositorypluginname = 'user';
108         $args = array();
109         $args['type'] = $repositorypluginname;
110         $repos = repository::get_instances($args);
111         $userrepository = reset($repos);
112         $this->assertInstanceOf('repository', $userrepository);
114         $fs = get_file_storage();
116         $syscontext = context_system::instance();
117         $component = 'core';
118         $filearea  = 'unittest';
119         $itemid    = 0;
120         $filepath  = '/';
121         $filename  = 'test.txt';
122         $sourcefield = 'Copyright stuff';
124         $filerecord = array(
125             'contextid' => $syscontext->id,
126             'component' => $component,
127             'filearea'  => $filearea,
128             'itemid'    => $itemid,
129             'filepath'  => $filepath,
130             'filename'  => $filename,
131             'source'    => $sourcefield,
132         );
133         $ref = $fs->pack_reference($filerecord);
134         $originalfile = $fs->create_file_from_string($filerecord, 'Test content');
135         $fileid = $originalfile->get_id();
136         $this->assertInstanceOf('stored_file', $originalfile);
138         // create a user private file
139         $userfilerecord = new stdClass;
140         $userfilerecord->contextid = $usercontext->id;
141         $userfilerecord->component = 'user';
142         $userfilerecord->filearea  = 'private';
143         $userfilerecord->itemid    = 0;
144         $userfilerecord->filepath  = '/';
145         $userfilerecord->filename  = 'userfile.txt';
146         $userfilerecord->source    = 'test';
147         $userfile = $fs->create_file_from_string($userfilerecord, 'User file content');
148         $userfileref = $fs->pack_reference($userfilerecord);
150         $filerefrecord = clone((object)$filerecord);
151         $filerefrecord->filename = 'testref.txt';
152         // create a file reference
153         $fileref = $fs->create_file_from_reference($filerefrecord, $userrepository->id, $userfileref);
154         $this->assertInstanceOf('stored_file', $fileref);
155         $this->assertEquals($userrepository->id, $fileref->get_repository_id());
156         $this->assertEquals($userfile->get_contenthash(), $fileref->get_contenthash());
157         $this->assertEquals($userfile->get_filesize(), $fileref->get_filesize());
158         $this->assertRegExp('#' . $userfile->get_filename(). '$#', $fileref->get_reference_details());
160         $draftitemid = 0;
161         file_prepare_draft_area($draftitemid, $syscontext->id, $component, $filearea, $itemid);
163         $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid);
164         $this->assertEquals(3, count($draftfiles));
166         $draftfile = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $filepath, $filename);
167         $source = unserialize($draftfile->get_source());
168         $this->assertEquals($ref, $source->original);
169         $this->assertEquals($sourcefield, $source->source);
171         $draftfileref = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $filepath, $filerefrecord->filename);
172         $this->assertInstanceOf('stored_file', $draftfileref);
173         $this->assertEquals(true, $draftfileref->is_external_file());
175         // change some information
176         $author = 'Dongsheng Cai';
177         $draftfile->set_author($author);
178         $newsourcefield = 'Get from Flickr';
179         $license = 'GPLv3';
180         $draftfile->set_license($license);
181         // if you want to really just change source field, do this:
182         $source = unserialize($draftfile->get_source());
183         $newsourcefield = 'From flickr';
184         $source->source = $newsourcefield;
185         $draftfile->set_source(serialize($source));
187         // Save changed file
188         file_save_draft_area_files($draftitemid, $syscontext->id, $component, $filearea, $itemid);
190         $file = $fs->get_file($syscontext->id, $component, $filearea, $itemid, $filepath, $filename);
192         // Make sure it's the original file id
193         $this->assertEquals($fileid, $file->get_id());
194         $this->assertInstanceOf('stored_file', $file);
195         $this->assertEquals($author, $file->get_author());
196         $this->assertEquals($license, $file->get_license());
197         $this->assertEquals($newsourcefield, $file->get_source());
198     }
200     /**
201      * Testing deleting original files
202      *
203      * @copyright 2012 Dongsheng Cai {@link http://dongsheng.org}
204      * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
205      */
206     public function test_delete_original_file_from_draft() {
207         global $USER, $DB;
209         $this->resetAfterTest(true);
211         $generator = $this->getDataGenerator();
212         $user = $generator->create_user();
213         $usercontext = context_user::instance($user->id);
214         $USER = $DB->get_record('user', array('id'=>$user->id));
216         $repositorypluginname = 'user';
218         $args = array();
219         $args['type'] = $repositorypluginname;
220         $repos = repository::get_instances($args);
221         $userrepository = reset($repos);
222         $this->assertInstanceOf('repository', $userrepository);
224         $fs = get_file_storage();
225         $syscontext = context_system::instance();
227         $filecontent = 'User file content';
229         // create a user private file
230         $userfilerecord = new stdClass;
231         $userfilerecord->contextid = $usercontext->id;
232         $userfilerecord->component = 'user';
233         $userfilerecord->filearea  = 'private';
234         $userfilerecord->itemid    = 0;
235         $userfilerecord->filepath  = '/';
236         $userfilerecord->filename  = 'userfile.txt';
237         $userfilerecord->source    = 'test';
238         $userfile = $fs->create_file_from_string($userfilerecord, $filecontent);
239         $userfileref = $fs->pack_reference($userfilerecord);
240         $contenthash = $userfile->get_contenthash();
242         $filerecord = array(
243             'contextid' => $syscontext->id,
244             'component' => 'core',
245             'filearea'  => 'phpunit',
246             'itemid'    => 0,
247             'filepath'  => '/',
248             'filename'  => 'test.txt',
249         );
250         // create a file reference
251         $fileref = $fs->create_file_from_reference($filerecord, $userrepository->id, $userfileref);
252         $this->assertInstanceOf('stored_file', $fileref);
253         $this->assertEquals($userrepository->id, $fileref->get_repository_id());
254         $this->assertEquals($userfile->get_contenthash(), $fileref->get_contenthash());
255         $this->assertEquals($userfile->get_filesize(), $fileref->get_filesize());
256         $this->assertRegExp('#' . $userfile->get_filename(). '$#', $fileref->get_reference_details());
258         $draftitemid = 0;
259         file_prepare_draft_area($draftitemid, $usercontext->id, 'user', 'private', 0);
260         $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid);
261         $this->assertEquals(2, count($draftfiles));
262         $draftfile = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $userfilerecord->filepath, $userfilerecord->filename);
263         $draftfile->delete();
264         // Save changed file
265         file_save_draft_area_files($draftitemid, $usercontext->id, 'user', 'private', 0);
267         // The file reference should be a regular moodle file now
268         $fileref = $fs->get_file($syscontext->id, 'core', 'phpunit', 0, '/', 'test.txt');
269         $this->assertEquals(false, $fileref->is_external_file());
270         $this->assertEquals($contenthash, $fileref->get_contenthash());
271         $this->assertEquals($filecontent, $fileref->get_content());
272     }
274     /**
275      * Tests the strip_double_headers function in the curl class.
276      */
277     public function test_curl_strip_double_headers() {
278         // Example from issue tracker.
279         $mdl30648example = <<<EOF
280 HTTP/1.0 407 Proxy Authentication Required
281 Server: squid/2.7.STABLE9
282 Date: Thu, 08 Dec 2011 14:44:33 GMT
283 Content-Type: text/html
284 Content-Length: 1275
285 X-Squid-Error: ERR_CACHE_ACCESS_DENIED 0
286 Proxy-Authenticate: Basic realm="Squid proxy-caching web server"
287 X-Cache: MISS from homer.lancs.ac.uk
288 X-Cache-Lookup: NONE from homer.lancs.ac.uk:3128
289 Via: 1.0 homer.lancs.ac.uk:3128 (squid/2.7.STABLE9)
290 Connection: close
292 HTTP/1.0 200 OK
293 Server: Apache
294 X-Lb-Nocache: true
295 Cache-Control: private, max-age=15
296 ETag: "4d69af5d8ba873ea9192c489e151bd7b"
297 Content-Type: text/html
298 Date: Thu, 08 Dec 2011 14:44:53 GMT
299 Set-Cookie: BBC-UID=c4de2e109c8df6a51de627cee11b214bd4fb6054a030222488317afb31b343360MoodleBot/1.0; expires=Mon, 07-Dec-15 14:44:53 GMT; path=/; domain=bbc.co.uk
300 X-Cache-Action: MISS
301 X-Cache-Age: 0
302 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
303 X-Cache: MISS from ww
305 <html>...
306 EOF;
307         $mdl30648expected = <<<EOF
308 HTTP/1.0 200 OK
309 Server: Apache
310 X-Lb-Nocache: true
311 Cache-Control: private, max-age=15
312 ETag: "4d69af5d8ba873ea9192c489e151bd7b"
313 Content-Type: text/html
314 Date: Thu, 08 Dec 2011 14:44:53 GMT
315 Set-Cookie: BBC-UID=c4de2e109c8df6a51de627cee11b214bd4fb6054a030222488317afb31b343360MoodleBot/1.0; expires=Mon, 07-Dec-15 14:44:53 GMT; path=/; domain=bbc.co.uk
316 X-Cache-Action: MISS
317 X-Cache-Age: 0
318 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
319 X-Cache: MISS from ww
321 <html>...
322 EOF;
323         // For HTTP, replace the \n with \r\n.
324         $mdl30648example = preg_replace("~(?!<\r)\n~", "\r\n", $mdl30648example);
325         $mdl30648expected = preg_replace("~(?!<\r)\n~", "\r\n", $mdl30648expected);
327         // Test stripping works OK.
328         $this->assertEquals($mdl30648expected, curl::strip_double_headers($mdl30648example));
329         // Test it does nothing to the 'plain' data.
330         $this->assertEquals($mdl30648expected, curl::strip_double_headers($mdl30648expected));
332         // Example from OU proxy.
333         $httpsexample = <<<EOF
334 HTTP/1.0 200 Connection established
336 HTTP/1.1 200 OK
337 Date: Fri, 22 Feb 2013 17:14:23 GMT
338 Server: Apache/2
339 X-Powered-By: PHP/5.3.3-7+squeeze14
340 Content-Type: text/xml
341 Connection: close
342 Content-Encoding: gzip
343 Transfer-Encoding: chunked
345 <?xml version="1.0" encoding="ISO-8859-1" ?>
346 <rss version="2.0">...
347 EOF;
348         $httpsexpected = <<<EOF
349 HTTP/1.1 200 OK
350 Date: Fri, 22 Feb 2013 17:14:23 GMT
351 Server: Apache/2
352 X-Powered-By: PHP/5.3.3-7+squeeze14
353 Content-Type: text/xml
354 Connection: close
355 Content-Encoding: gzip
356 Transfer-Encoding: chunked
358 <?xml version="1.0" encoding="ISO-8859-1" ?>
359 <rss version="2.0">...
360 EOF;
361         // For HTTP, replace the \n with \r\n.
362         $httpsexample = preg_replace("~(?!<\r)\n~", "\r\n", $httpsexample);
363         $httpsexpected = preg_replace("~(?!<\r)\n~", "\r\n", $httpsexpected);
365         // Test stripping works OK.
366         $this->assertEquals($httpsexpected, curl::strip_double_headers($httpsexample));
367         // Test it does nothing to the 'plain' data.
368         $this->assertEquals($httpsexpected, curl::strip_double_headers($httpsexpected));
369     }