weekly release 3.7dev
[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);
b7801097 64 $fileinfo = external_api::clean_returnvalue(core_files_external::upload_returns(), $fileinfo);
873482fe
DW
65 // Get the created draft item id.
66 $itemid = $fileinfo['itemid'];
11f20be7
AA
67
68 // Make sure the file was created.
69 $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
70 $this->assertNotEmpty($file);
71
72 // Make sure no file exists.
11f20be7
AA
73 $filename = "Simple2.txt";
74 $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
75 $this->assertEmpty($file);
76
77 // Call the api to create a file.
57d16acd
AA
78 $fileinfo = core_files_external::upload($contextid, $component, $filearea, $itemid,
79 $filepath, $filename, $filecontent, $contextlevel, $instanceid);
b7801097 80 $fileinfo = external_api::clean_returnvalue(core_files_external::upload_returns(), $fileinfo);
873482fe
DW
81 $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
82 $this->assertNotEmpty($file);
11f20be7 83
57d16acd 84 // Let us try creating a file using contextlevel and instance id.
57d16acd
AA
85 $filename = "Simple5.txt";
86 $contextid = 0;
87 $contextlevel = "user";
88 $instanceid = $USER->id;
89 $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
90 $this->assertEmpty($file);
91 $fileinfo = core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath,
92 $filename, $filecontent, $contextlevel, $instanceid);
b7801097 93 $fileinfo = external_api::clean_returnvalue(core_files_external::upload_returns(), $fileinfo);
873482fe
DW
94 $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
95 $this->assertNotEmpty($file);
57d16acd 96
11f20be7 97 // Make sure the same file cannot be created again.
52f3e060 98 $this->expectException("moodle_exception");
57d16acd
AA
99 core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath,
100 $filename, $filecontent, $contextlevel, $instanceid);
11f20be7
AA
101 }
102
103 /*
104 * Make sure only user component is allowed in core_files_external::upload().
105 */
106 public function test_upload_param_component() {
107 global $USER;
108
109 $this->resetAfterTest();
110 $this->setAdminUser();
111 $context = context_user::instance($USER->id);
112 $contextid = $context->id;
113 $component = "backup";
f23e9b6b 114 $filearea = "draft";
11f20be7
AA
115 $itemid = 0;
116 $filepath = "/";
117 $filename = "Simple3.txt";
118 $filecontent = base64_encode("Let us create a nice simple file");
57d16acd
AA
119 $contextlevel = null;
120 $instanceid = null;
11f20be7
AA
121
122 // Make sure exception is thrown.
52f3e060 123 $this->expectException("coding_exception");
57d16acd
AA
124 core_files_external::upload($contextid, $component, $filearea, $itemid,
125 $filepath, $filename, $filecontent, $contextlevel, $instanceid);
11f20be7
AA
126 }
127
128 /*
f23e9b6b 129 * Make sure only draft areas are allowed in core_files_external::upload().
11f20be7
AA
130 */
131 public function test_upload_param_area() {
132 global $USER;
133
134 $this->resetAfterTest();
135 $this->setAdminUser();
136 $context = context_user::instance($USER->id);
137 $contextid = $context->id;
138 $component = "user";
139 $filearea = "draft";
873482fe 140 $itemid = file_get_unused_draft_itemid();
11f20be7
AA
141 $filepath = "/";
142 $filename = "Simple4.txt";
143 $filecontent = base64_encode("Let us create a nice simple file");
57d16acd
AA
144 $contextlevel = null;
145 $instanceid = null;
11f20be7 146
873482fe 147 // Make sure the file is created.
308ce5b4
MG
148 $fileinfo = core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent,
149 'user', $USER->id);
b7801097 150 $fileinfo = external_api::clean_returnvalue(core_files_external::upload_returns(), $fileinfo);
873482fe
DW
151 $browser = get_file_browser();
152 $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
71e38b12
AA
153 $this->assertNotEmpty($file);
154 }
196c8b71 155
9c3bfea0
AG
156 /**
157 * Test getting a list of files with and without a context ID.
158 */
196c8b71
AG
159 public function test_get_files() {
160 global $USER, $DB;
161
162 $this->resetAfterTest();
163
9c3bfea0 164 // Set the current user to be the administrator.
196c8b71 165 $this->setAdminUser();
0fe86bbd 166 $USER->email = 'test@example.com';
196c8b71 167
9c3bfea0 168 // Create a course.
196c8b71
AG
169 $course = $this->getDataGenerator()->create_course();
170 $record = new stdClass();
171 $record->course = $course->id;
172 $record->name = "Mod data upload test";
196c8b71
AG
173 $record->intro = "Some intro of some sort";
174
9c3bfea0 175 // Create a database module.
196c8b71
AG
176 $module = $this->getDataGenerator()->create_module('data', $record);
177
9c3bfea0 178 // Create a new field in the database activity.
196c8b71 179 $field = data_get_field_new('file', $module);
9c3bfea0 180 // Add more detail about the field.
196c8b71
AG
181 $fielddetail = new stdClass();
182 $fielddetail->d = $module->id;
183 $fielddetail->mode = 'add';
184 $fielddetail->type = 'file';
185 $fielddetail->sesskey = sesskey();
186 $fielddetail->name = 'Upload file';
187 $fielddetail->description = 'Some description';
188 $fielddetail->param3 = '0';
189
190 $field->define_field($fielddetail);
191 $field->insert_field();
192 $recordid = data_add_record($module);
193
9c3bfea0 194 // File information for the database module record.
196c8b71
AG
195 $datacontent = array();
196 $datacontent['fieldid'] = $field->field->id;
197 $datacontent['recordid'] = $recordid;
198 $datacontent['content'] = 'Simple4.txt';
199
9c3bfea0 200 // Insert the information about the file.
196c8b71 201 $contentid = $DB->insert_record('data_content', $datacontent);
9c3bfea0 202 // Required information for uploading a file.
74b63eae 203 $context = context_module::instance($module->cmid);
196c8b71
AG
204 $usercontext = context_user::instance($USER->id);
205 $component = 'mod_data';
206 $filearea = 'content';
207 $itemid = $contentid;
208 $filename = $datacontent['content'];
209 $filecontent = base64_encode("Let us create a nice simple file.");
210
211 $filerecord = array();
212 $filerecord['contextid'] = $context->id;
213 $filerecord['component'] = $component;
214 $filerecord['filearea'] = $filearea;
215 $filerecord['itemid'] = $itemid;
216 $filerecord['filepath'] = '/';
217 $filerecord['filename'] = $filename;
218
9c3bfea0 219 // Create an area to upload the file.
196c8b71 220 $fs = get_file_storage();
9c3bfea0 221 // Create a file from the string that we made earlier.
196c8b71 222 $file = $fs->create_file_from_string($filerecord, $filecontent);
9c3bfea0 223 $timemodified = $file->get_timemodified();
7310dcff
JL
224 $timecreated = $file->get_timemodified();
225 $filesize = $file->get_filesize();
196c8b71 226
9c3bfea0
AG
227 // Use the web service function to return the information about the file that we just uploaded.
228 // The first time is with a valid context ID.
196c8b71
AG
229 $filename = '';
230 $testfilelisting = core_files_external::get_files($context->id, $component, $filearea, $itemid, '/', $filename);
b7801097 231 $testfilelisting = external_api::clean_returnvalue(core_files_external::get_files_returns(), $testfilelisting);
196c8b71 232
9c3bfea0 233 // With the information that we have provided we should get an object exactly like the one below.
42e34c28 234 $coursecontext = context_course::instance($course->id);
196c8b71
AG
235 $testdata = array();
236 $testdata['parents'] = array();
237 $testdata['parents']['0'] = array('contextid' => 1,
238 'component' => null,
239 'filearea' => null,
240 'itemid' => null,
241 'filepath' => null,
242 'filename' => 'System');
243 $testdata['parents']['1'] = array('contextid' => 3,
244 'component' => null,
245 'filearea' => null,
246 'itemid' => null,
247 'filepath' => null,
248 'filename' => 'Miscellaneous');
42e34c28 249 $testdata['parents']['2'] = array('contextid' => $coursecontext->id,
196c8b71
AG
250 'component' => null,
251 'filearea' => null,
252 'itemid' => null,
253 'filepath' => null,
254 'filename' => 'Test course 1');
42e34c28 255 $testdata['parents']['3'] = array('contextid' => $context->id,
196c8b71
AG
256 'component' => null,
257 'filearea' => null,
258 'itemid' => null,
259 'filepath' => null,
260 'filename' => 'Mod data upload test (Database)');
42e34c28 261 $testdata['parents']['4'] = array('contextid' => $context->id,
196c8b71
AG
262 'component' => 'mod_data',
263 'filearea' => 'content',
264 'itemid' => null,
265 'filepath' => null,
266 'filename' => 'Fields');
267 $testdata['files'] = array();
42e34c28 268 $testdata['files']['0'] = array('contextid' => $context->id,
196c8b71
AG
269 'component' => 'mod_data',
270 'filearea' => 'content',
42e34c28 271 'itemid' => $itemid,
196c8b71
AG
272 'filepath' => '/',
273 'filename' => 'Simple4.txt',
55946a89 274 'url' => 'https://www.example.com/moodle/pluginfile.php/'.$context->id.'/mod_data/content/'.$itemid.'/Simple4.txt',
9c3bfea0 275 'isdir' => false,
7310dcff
JL
276 'timemodified' => $timemodified,
277 'timecreated' => $timecreated,
278 'filesize' => $filesize,
279 'author' => null,
280 'license' => null
281 );
9c3bfea0 282 // Make sure that they are the same.
42e34c28 283 $this->assertEquals($testdata, $testfilelisting);
196c8b71 284
9c3bfea0 285 // Try again but without the context. Minus one signals the function to use other variables to obtain the context.
196c8b71
AG
286 $nocontext = -1;
287 $modified = 0;
9c3bfea0 288 // Context level and instance ID are used to determine what the context is.
196c8b71 289 $contextlevel = 'module';
74b63eae 290 $instanceid = $module->cmid;
196c8b71 291 $testfilelisting = core_files_external::get_files($nocontext, $component, $filearea, $itemid, '/', $filename, $modified, $contextlevel, $instanceid);
b7801097
JL
292 $testfilelisting = external_api::clean_returnvalue(core_files_external::get_files_returns(), $testfilelisting);
293
196c8b71
AG
294 $this->assertEquals($testfilelisting, $testdata);
295 }
873482fe 296}