Commit | Line | Data |
---|---|---|
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 | ||
17 | ||
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 | */ | |
26 | ||
27 | defined('MOODLE_INTERNAL') || die(); | |
28 | ||
29 | global $CFG; | |
30 | require_once($CFG->libdir . '/filelib.php'); | |
ac29403e | 31 | require_once($CFG->dirroot . '/repository/lib.php'); |
a3d5830a | 32 | |
67233725 | 33 | class filelib_testcase extends advanced_testcase { |
a3d5830a PS |
34 | public function test_format_postdata_for_curlcall() { |
35 | ||
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); | |
41 | ||
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); | |
47 | ||
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); | |
53 | ||
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); | |
71 | ||
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 | } | |
83 | ||
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 | } | |
67233725 DC |
89 | |
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; | |
98 | ||
99 | $this->resetAfterTest(true); | |
100 | ||
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)); | |
105 | ||
7051415c DC |
106 | $repositorypluginname = 'user'; |
107 | ||
108 | $args = array(); | |
109 | $args['type'] = $repositorypluginname; | |
110 | $repos = repository::get_instances($args); | |
111 | $userrepository = reset($repos); | |
112 | $this->assertInstanceOf('repository', $userrepository); | |
67233725 DC |
113 | |
114 | $fs = get_file_storage(); | |
7051415c | 115 | |
67233725 DC |
116 | $syscontext = context_system::instance(); |
117 | $component = 'core'; | |
118 | $filearea = 'unittest'; | |
119 | $itemid = 0; | |
120 | $filepath = '/'; | |
121 | $filename = 'test.txt'; | |
122 | $sourcefield = 'Copyright stuff'; | |
123 | ||
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); | |
67233725 | 134 | $originalfile = $fs->create_file_from_string($filerecord, 'Test content'); |
67233725 DC |
135 | $fileid = $originalfile->get_id(); |
136 | $this->assertInstanceOf('stored_file', $originalfile); | |
137 | ||
7051415c DC |
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); | |
149 | ||
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); | |
291a3d1f | 155 | $this->assertEquals($userrepository->id, $fileref->get_repository_id()); |
7051415c DC |
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()); | |
159 | ||
67233725 DC |
160 | $draftitemid = 0; |
161 | file_prepare_draft_area($draftitemid, $syscontext->id, $component, $filearea, $itemid); | |
162 | ||
163 | $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid); | |
7051415c | 164 | $this->assertEquals(3, count($draftfiles)); |
67233725 DC |
165 | |
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); | |
170 | ||
7051415c DC |
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()); | |
67233725 DC |
174 | |
175 | // change some information | |
176 | $author = 'Dongsheng Cai'; | |
177 | $draftfile->set_author($author); | |
7051415c | 178 | $newsourcefield = 'Get from Flickr'; |
67233725 DC |
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)); | |
186 | ||
187 | // Save changed file | |
188 | file_save_draft_area_files($draftitemid, $syscontext->id, $component, $filearea, $itemid); | |
189 | ||
190 | $file = $fs->get_file($syscontext->id, $component, $filearea, $itemid, $filepath, $filename); | |
191 | ||
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 | } | |
61506a0a DC |
199 | |
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; | |
208 | ||
209 | $this->resetAfterTest(true); | |
210 | ||
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)); | |
215 | ||
216 | $repositorypluginname = 'user'; | |
217 | ||
218 | $args = array(); | |
219 | $args['type'] = $repositorypluginname; | |
220 | $repos = repository::get_instances($args); | |
221 | $userrepository = reset($repos); | |
222 | $this->assertInstanceOf('repository', $userrepository); | |
223 | ||
224 | $fs = get_file_storage(); | |
225 | $syscontext = context_system::instance(); | |
226 | ||
227 | $filecontent = 'User file content'; | |
228 | ||
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(); | |
241 | ||
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); | |
291a3d1f | 253 | $this->assertEquals($userrepository->id, $fileref->get_repository_id()); |
61506a0a DC |
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()); | |
257 | ||
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); | |
266 | ||
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 | } | |
a3d5830a | 273 | } |