Merge branch 'MDL-43936' of git://github.com/timhunt/moodle
[moodle.git] / files / tests / externallib_test.php
CommitLineData
11f20be7
AA
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 * 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 */
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30
31require_once($CFG->dirroot . '/webservice/tests/helpers.php');
32require_once($CFG->dirroot . '/files/externallib.php');
33
8252b7c2 34class core_files_externallib_testcase extends advanced_testcase {
11f20be7
AA
35
36 /*
37 * Test core_files_external::upload().
38 */
39
40 public function test_upload() {
41 global $USER;
42
43 $this->resetAfterTest();
44 $this->setAdminUser();
45 $context = context_user::instance($USER->id);
46 $contextid = $context->id;
47 $component = "user";
873482fe 48 $filearea = "draft";
11f20be7
AA
49 $itemid = 0;
50 $filepath = "/";
51 $filename = "Simple.txt";
52 $filecontent = base64_encode("Let us create a nice simple file");
57d16acd
AA
53 $contextlevel = null;
54 $instanceid = null;
11f20be7
AA
55 $browser = get_file_browser();
56
57 // Make sure no file exists.
58 $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
59 $this->assertEmpty($file);
60
61 // Call the api to create a file.
873482fe 62 $fileinfo = core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath,
57d16acd 63 $filename, $filecontent, $contextlevel, $instanceid);
873482fe
DW
64 // Get the created draft item id.
65 $itemid = $fileinfo['itemid'];
11f20be7
AA
66
67 // Make sure the file was created.
68 $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
69 $this->assertNotEmpty($file);
70
71 // Make sure no file exists.
11f20be7
AA
72 $filename = "Simple2.txt";
73 $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
74 $this->assertEmpty($file);
75
76 // Call the api to create a file.
57d16acd
AA
77 $fileinfo = core_files_external::upload($contextid, $component, $filearea, $itemid,
78 $filepath, $filename, $filecontent, $contextlevel, $instanceid);
873482fe
DW
79 $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
80 $this->assertNotEmpty($file);
11f20be7 81
57d16acd 82 // Let us try creating a file using contextlevel and instance id.
57d16acd
AA
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);
873482fe
DW
91 $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
92 $this->assertNotEmpty($file);
57d16acd 93
11f20be7
AA
94 // Make sure the same file cannot be created again.
95 $this->setExpectedException("moodle_exception");
57d16acd
AA
96 core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath,
97 $filename, $filecontent, $contextlevel, $instanceid);
11f20be7
AA
98 }
99
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;
105
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");
57d16acd
AA
116 $contextlevel = null;
117 $instanceid = null;
11f20be7
AA
118
119 // Make sure exception is thrown.
120 $this->setExpectedException("coding_exception");
57d16acd
AA
121 core_files_external::upload($contextid, $component, $filearea, $itemid,
122 $filepath, $filename, $filecontent, $contextlevel, $instanceid);
11f20be7
AA
123 }
124
125 /*
873482fe 126 * Make sure only private or draft areas are allowed in core_files_external::upload().
11f20be7
AA
127 */
128 public function test_upload_param_area() {
129 global $USER;
130
131 $this->resetAfterTest();
132 $this->setAdminUser();
133 $context = context_user::instance($USER->id);
134 $contextid = $context->id;
135 $component = "user";
136 $filearea = "draft";
873482fe 137 $itemid = file_get_unused_draft_itemid();
11f20be7
AA
138 $filepath = "/";
139 $filename = "Simple4.txt";
140 $filecontent = base64_encode("Let us create a nice simple file");
57d16acd
AA
141 $contextlevel = null;
142 $instanceid = null;
11f20be7 143
873482fe
DW
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);
11f20be7
AA
149 }
150
71e38b12
AA
151 /*
152 * Make sure core_files_external::upload() works without new parameters.
153 */
154 public function test_upload_without_new_param() {
155 global $USER;
156
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");
167
71e38b12 168 @core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent);
873482fe
DW
169
170 // Assert debugging called (deprecation warning).
171 $this->assertDebuggingCalled();
172
173 // Make sure the file is created.
71e38b12
AA
174 $browser = get_file_browser();
175 $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
176 $this->assertNotEmpty($file);
177 }
196c8b71 178
9c3bfea0
AG
179 /**
180 * Test getting a list of files with and without a context ID.
181 */
196c8b71
AG
182 public function test_get_files() {
183 global $USER, $DB;
184
185 $this->resetAfterTest();
186
9c3bfea0 187 // Set the current user to be the administrator.
196c8b71
AG
188 $this->setAdminUser();
189 $USER->email = 'test@moodle.com';
196c8b71 190
9c3bfea0 191 // Create a course.
196c8b71
AG
192 $course = $this->getDataGenerator()->create_course();
193 $record = new stdClass();
194 $record->course = $course->id;
195 $record->name = "Mod data upload test";
196c8b71
AG
196 $record->intro = "Some intro of some sort";
197
9c3bfea0 198 // Create a database module.
196c8b71
AG
199 $module = $this->getDataGenerator()->create_module('data', $record);
200
9c3bfea0 201 // Create a new field in the database activity.
196c8b71 202 $field = data_get_field_new('file', $module);
9c3bfea0 203 // Add more detail about the field.
196c8b71
AG
204 $fielddetail = new stdClass();
205 $fielddetail->d = $module->id;
206 $fielddetail->mode = 'add';
207 $fielddetail->type = 'file';
208 $fielddetail->sesskey = sesskey();
209 $fielddetail->name = 'Upload file';
210 $fielddetail->description = 'Some description';
211 $fielddetail->param3 = '0';
212
213 $field->define_field($fielddetail);
214 $field->insert_field();
215 $recordid = data_add_record($module);
216
9c3bfea0 217 // File information for the database module record.
196c8b71
AG
218 $datacontent = array();
219 $datacontent['fieldid'] = $field->field->id;
220 $datacontent['recordid'] = $recordid;
221 $datacontent['content'] = 'Simple4.txt';
222
9c3bfea0 223 // Insert the information about the file.
196c8b71 224 $contentid = $DB->insert_record('data_content', $datacontent);
9c3bfea0 225 // Required information for uploading a file.
74b63eae 226 $context = context_module::instance($module->cmid);
196c8b71
AG
227 $usercontext = context_user::instance($USER->id);
228 $component = 'mod_data';
229 $filearea = 'content';
230 $itemid = $contentid;
231 $filename = $datacontent['content'];
232 $filecontent = base64_encode("Let us create a nice simple file.");
233
234 $filerecord = array();
235 $filerecord['contextid'] = $context->id;
236 $filerecord['component'] = $component;
237 $filerecord['filearea'] = $filearea;
238 $filerecord['itemid'] = $itemid;
239 $filerecord['filepath'] = '/';
240 $filerecord['filename'] = $filename;
241
9c3bfea0 242 // Create an area to upload the file.
196c8b71 243 $fs = get_file_storage();
9c3bfea0 244 // Create a file from the string that we made earlier.
196c8b71 245 $file = $fs->create_file_from_string($filerecord, $filecontent);
9c3bfea0 246 $timemodified = $file->get_timemodified();
196c8b71 247
9c3bfea0
AG
248 // Use the web service function to return the information about the file that we just uploaded.
249 // The first time is with a valid context ID.
196c8b71
AG
250 $filename = '';
251 $testfilelisting = core_files_external::get_files($context->id, $component, $filearea, $itemid, '/', $filename);
252
9c3bfea0 253 // With the information that we have provided we should get an object exactly like the one below.
196c8b71
AG
254 $testdata = array();
255 $testdata['parents'] = array();
256 $testdata['parents']['0'] = array('contextid' => 1,
257 'component' => null,
258 'filearea' => null,
259 'itemid' => null,
260 'filepath' => null,
261 'filename' => 'System');
262 $testdata['parents']['1'] = array('contextid' => 3,
263 'component' => null,
264 'filearea' => null,
265 'itemid' => null,
266 'filepath' => null,
267 'filename' => 'Miscellaneous');
268 $testdata['parents']['2'] = array('contextid' => 15,
269 'component' => null,
270 'filearea' => null,
271 'itemid' => null,
272 'filepath' => null,
273 'filename' => 'Test course 1');
274 $testdata['parents']['3'] = array('contextid' => 20,
275 'component' => null,
276 'filearea' => null,
277 'itemid' => null,
278 'filepath' => null,
279 'filename' => 'Mod data upload test (Database)');
280 $testdata['parents']['4'] = array('contextid' => 20,
281 'component' => 'mod_data',
282 'filearea' => 'content',
283 'itemid' => null,
284 'filepath' => null,
285 'filename' => 'Fields');
286 $testdata['files'] = array();
287 $testdata['files']['0'] = array('contextid' => 20,
288 'component' => 'mod_data',
289 'filearea' => 'content',
9c3bfea0 290 'itemid' => '1',
196c8b71
AG
291 'filepath' => '/',
292 'filename' => 'Simple4.txt',
293 'url' => 'http://www.example.com/moodle/pluginfile.php/20/mod_data/content/1/Simple4.txt',
9c3bfea0 294 'isdir' => false,
196c8b71 295 'timemodified' => $timemodified);
9c3bfea0 296 // Make sure that they are the same.
196c8b71
AG
297 $this->assertEquals($testfilelisting, $testdata);
298
9c3bfea0 299 // Try again but without the context. Minus one signals the function to use other variables to obtain the context.
196c8b71
AG
300 $nocontext = -1;
301 $modified = 0;
9c3bfea0 302 // Context level and instance ID are used to determine what the context is.
196c8b71 303 $contextlevel = 'module';
74b63eae 304 $instanceid = $module->cmid;
196c8b71
AG
305 $testfilelisting = core_files_external::get_files($nocontext, $component, $filearea, $itemid, '/', $filename, $modified, $contextlevel, $instanceid);
306 $this->assertEquals($testfilelisting, $testdata);
307 }
873482fe 308}