Merge branch 'wip-MDL-40038-master' of git://github.com/abgreeve/moodle
[moodle.git] / files / tests / externallib_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  * PHPunit tests for external files API.
20  *
21  * @package    core_files
22  * @category   external
23  * @copyright  2013 Ankit Agarwal
24  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25  * @since Moodle 2.6
26  */
27 defined('MOODLE_INTERNAL') || die();
29 global $CFG;
31 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
32 require_once($CFG->dirroot . '/files/externallib.php');
34 class core_files_externallib_testcase extends advanced_testcase {
36     /*
37      * Test core_files_external::upload().
38      */
40     public function test_upload() {
41         global $USER;
43         $this->resetAfterTest();
44         $this->setAdminUser();
45         $context = context_user::instance($USER->id);
46         $contextid = $context->id;
47         $component = "user";
48         $filearea = "draft";
49         $itemid = 0;
50         $filepath = "/";
51         $filename = "Simple.txt";
52         $filecontent = base64_encode("Let us create a nice simple file");
53         $contextlevel = null;
54         $instanceid = null;
55         $browser = get_file_browser();
57         // Make sure no file exists.
58         $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
59         $this->assertEmpty($file);
61         // Call the api to create a file.
62         $fileinfo = core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath,
63                 $filename, $filecontent, $contextlevel, $instanceid);
64         // Get the created draft item id.
65         $itemid = $fileinfo['itemid'];
67         // Make sure the file was created.
68         $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
69         $this->assertNotEmpty($file);
71         // Make sure no file exists.
72         $filename = "Simple2.txt";
73         $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
74         $this->assertEmpty($file);
76         // Call the api to create a file.
77         $fileinfo = core_files_external::upload($contextid, $component, $filearea, $itemid,
78                 $filepath, $filename, $filecontent, $contextlevel, $instanceid);
79         $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
80         $this->assertNotEmpty($file);
82         // Let us try creating a file using contextlevel and instance id.
83         $filename = "Simple5.txt";
84         $contextid = 0;
85         $contextlevel = "user";
86         $instanceid = $USER->id;
87         $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
88         $this->assertEmpty($file);
89         $fileinfo = core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath,
90                 $filename, $filecontent, $contextlevel, $instanceid);
91         $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
92         $this->assertNotEmpty($file);
94         // Make sure the same file cannot be created again.
95         $this->setExpectedException("moodle_exception");
96         core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath,
97                 $filename, $filecontent, $contextlevel, $instanceid);
98     }
100     /*
101      * Make sure only user component is allowed in  core_files_external::upload().
102      */
103     public function test_upload_param_component() {
104         global $USER;
106         $this->resetAfterTest();
107         $this->setAdminUser();
108         $context = context_user::instance($USER->id);
109         $contextid = $context->id;
110         $component = "backup";
111         $filearea = "private";
112         $itemid = 0;
113         $filepath = "/";
114         $filename = "Simple3.txt";
115         $filecontent = base64_encode("Let us create a nice simple file");
116         $contextlevel = null;
117         $instanceid = null;
119         // Make sure exception is thrown.
120         $this->setExpectedException("coding_exception");
121         core_files_external::upload($contextid, $component, $filearea, $itemid,
122                 $filepath, $filename, $filecontent, $contextlevel, $instanceid);
123     }
125     /*
126      * Make sure only private or draft areas are allowed in  core_files_external::upload().
127      */
128     public function test_upload_param_area() {
129         global $USER;
131         $this->resetAfterTest();
132         $this->setAdminUser();
133         $context = context_user::instance($USER->id);
134         $contextid = $context->id;
135         $component = "user";
136         $filearea = "draft";
137         $itemid = file_get_unused_draft_itemid();
138         $filepath = "/";
139         $filename = "Simple4.txt";
140         $filecontent = base64_encode("Let us create a nice simple file");
141         $contextlevel = null;
142         $instanceid = null;
144         // Make sure the file is created.
145         @core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent);
146         $browser = get_file_browser();
147         $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
148         $this->assertNotEmpty($file);
149     }
151     /*
152      * Make sure core_files_external::upload() works without new parameters.
153      */
154     public function test_upload_without_new_param() {
155         global $USER;
157         $this->resetAfterTest();
158         $this->setAdminUser();
159         $context = context_user::instance($USER->id);
160         $contextid = $context->id;
161         $component = "user";
162         $filearea = "private";
163         $itemid = 0;
164         $filepath = "/";
165         $filename = "Simple4.txt";
166         $filecontent = base64_encode("Let us create a nice simple file");
168         @core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent);
170         // Assert debugging called (deprecation warning).
171         $this->assertDebuggingCalled();
173         // Make sure the file is created.
174         $browser = get_file_browser();
175         $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
176         $this->assertNotEmpty($file);
177     }
179     public function test_get_files() {
180         global $USER, $DB;
182         $this->resetAfterTest();
184         $this->setAdminUser();
185         $USER->email = 'test@moodle.com';
186         // print_object($USER);
188         $course = $this->getDataGenerator()->create_course();
189         $record = new stdClass();
190         $record->course = $course->id;
191         $record->name = "Mod data upload test";
193         $record->intro = "Some intro of some sort";
195         $module = $this->getDataGenerator()->create_module('data', $record);
197         $field = data_get_field_new('file', $module);
199         $fielddetail = new stdClass();
200         $fielddetail->d = $module->id;
201         $fielddetail->mode = 'add';
202         $fielddetail->type = 'file';
203         $fielddetail->sesskey = sesskey();
204         $fielddetail->name = 'Upload file';
205         $fielddetail->description = 'Some description';
206         $fielddetail->param3 = '0';
208         $field->define_field($fielddetail);
209         $field->insert_field();
210         $recordid = data_add_record($module);
212         $timemodified = $DB->get_field('data_records', 'timemodified', array('id' => $recordid));
214         $datacontent = array();
215         $datacontent['fieldid'] = $field->field->id;
216         $datacontent['recordid'] = $recordid;
217         $datacontent['content'] = 'Simple4.txt';
219         $contentid = $DB->insert_record('data_content', $datacontent);
221         $context = context_module::instance($module->id);
222         $usercontext = context_user::instance($USER->id);
223         $component = 'mod_data';
224         $filearea = 'content';
225         $itemid = $contentid;
226         $filename = $datacontent['content'];
227         $filecontent = base64_encode("Let us create a nice simple file.");
229         $filerecord = array();
230         $filerecord['contextid'] = $context->id;
231         $filerecord['component'] = $component;
232         $filerecord['filearea'] = $filearea;
233         $filerecord['itemid'] = $itemid;
234         $filerecord['filepath'] = '/';
235         $filerecord['filename'] = $filename;
237         $fs = get_file_storage();
238         $file = $fs->create_file_from_string($filerecord, $filecontent);
240         $filename = '';
241         $testfilelisting = core_files_external::get_files($context->id, $component, $filearea, $itemid, '/', $filename);
243         $testdata = array();
244         $testdata['parents'] = array();
245         $testdata['parents']['0'] = array('contextid' => 1,
246                                           'component' => null,
247                                           'filearea' => null,
248                                           'itemid' => null,
249                                           'filepath' => null,
250                                           'filename' => 'System');
251         $testdata['parents']['1'] = array('contextid' => 3,
252                                           'component' => null,
253                                           'filearea' => null,
254                                           'itemid' => null,
255                                           'filepath' => null,
256                                           'filename' => 'Miscellaneous');
257         $testdata['parents']['2'] = array('contextid' => 15,
258                                           'component' => null,
259                                           'filearea' => null,
260                                           'itemid' => null,
261                                           'filepath' => null,
262                                           'filename' => 'Test course 1');
263         $testdata['parents']['3'] = array('contextid' => 20,
264                                           'component' => null,
265                                           'filearea' => null,
266                                           'itemid' => null,
267                                           'filepath' => null,
268                                           'filename' => 'Mod data upload test (Database)');
269         $testdata['parents']['4'] = array('contextid' => 20,
270                                           'component' => 'mod_data',
271                                           'filearea' => 'content',
272                                           'itemid' => null,
273                                           'filepath' => null,
274                                           'filename' => 'Fields');
275         $testdata['files'] = array();
276         $testdata['files']['0'] = array('contextid' => 20,
277                                         'component' => 'mod_data',
278                                         'filearea' => 'content',
279                                         'itemid' => 1,
280                                         'filepath' => '/',
281                                         'filename' => 'Simple4.txt',
282                                         'url' => 'http://www.example.com/moodle/pluginfile.php/20/mod_data/content/1/Simple4.txt',
283                                         'isdir' => null,
284                                         'timemodified' => $timemodified);
286         $this->assertEquals($testfilelisting, $testdata);
288         // Try again but without the context.
289         $nocontext = -1;
290         $modified = 0;
291         $contextlevel = 'module';
292         $instanceid = $module->id;
293         $testfilelisting = core_files_external::get_files($nocontext, $component, $filearea, $itemid, '/', $filename, $modified, $contextlevel, $instanceid);
294         $this->assertEquals($testfilelisting, $testdata);
295     }