[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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 */ 27 defined('MOODLE_INTERNAL') || die(); 28 29 global $CFG; 30 31 require_once($CFG->dirroot . '/webservice/tests/helpers.php'); 32 require_once($CFG->dirroot . '/files/externallib.php'); 33 34 class core_files_externallib_testcase extends advanced_testcase { 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"; 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(); 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. 62 $fileinfo = core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, 63 $filename, $filecontent, $contextlevel, $instanceid); 64 $fileinfo = external_api::clean_returnvalue(core_files_external::upload_returns(), $fileinfo); 65 // Get the created draft item id. 66 $itemid = $fileinfo['itemid']; 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. 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. 78 $fileinfo = core_files_external::upload($contextid, $component, $filearea, $itemid, 79 $filepath, $filename, $filecontent, $contextlevel, $instanceid); 80 $fileinfo = external_api::clean_returnvalue(core_files_external::upload_returns(), $fileinfo); 81 $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename); 82 $this->assertNotEmpty($file); 83 84 // Let us try creating a file using contextlevel and instance id. 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); 93 $fileinfo = external_api::clean_returnvalue(core_files_external::upload_returns(), $fileinfo); 94 $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename); 95 $this->assertNotEmpty($file); 96 97 // Make sure the same file cannot be created again. 98 $this->expectException("moodle_exception"); 99 core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, 100 $filename, $filecontent, $contextlevel, $instanceid); 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"; 114 $filearea = "draft"; 115 $itemid = 0; 116 $filepath = "/"; 117 $filename = "Simple3.txt"; 118 $filecontent = base64_encode("Let us create a nice simple file"); 119 $contextlevel = null; 120 $instanceid = null; 121 122 // Make sure exception is thrown. 123 $this->expectException("coding_exception"); 124 core_files_external::upload($contextid, $component, $filearea, $itemid, 125 $filepath, $filename, $filecontent, $contextlevel, $instanceid); 126 } 127 128 /* 129 * Make sure only draft areas are allowed in core_files_external::upload(). 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"; 140 $itemid = file_get_unused_draft_itemid(); 141 $filepath = "/"; 142 $filename = "Simple4.txt"; 143 $filecontent = base64_encode("Let us create a nice simple file"); 144 $contextlevel = null; 145 $instanceid = null; 146 147 // Make sure the file is created. 148 $fileinfo = @core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent); 149 $fileinfo = external_api::clean_returnvalue(core_files_external::upload_returns(), $fileinfo); 150 $browser = get_file_browser(); 151 $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename); 152 $this->assertNotEmpty($file); 153 } 154 155 /** 156 * Test getting a list of files with and without a context ID. 157 */ 158 public function test_get_files() { 159 global $USER, $DB; 160 161 $this->resetAfterTest(); 162 163 // Set the current user to be the administrator. 164 $this->setAdminUser(); 165 $USER->email = 'test@example.com'; 166 167 // Create a course. 168 $course = $this->getDataGenerator()->create_course(); 169 $record = new stdClass(); 170 $record->course = $course->id; 171 $record->name = "Mod data upload test"; 172 $record->intro = "Some intro of some sort"; 173 174 // Create a database module. 175 $module = $this->getDataGenerator()->create_module('data', $record); 176 177 // Create a new field in the database activity. 178 $field = data_get_field_new('file', $module); 179 // Add more detail about the field. 180 $fielddetail = new stdClass(); 181 $fielddetail->d = $module->id; 182 $fielddetail->mode = 'add'; 183 $fielddetail->type = 'file'; 184 $fielddetail->sesskey = sesskey(); 185 $fielddetail->name = 'Upload file'; 186 $fielddetail->description = 'Some description'; 187 $fielddetail->param3 = '0'; 188 189 $field->define_field($fielddetail); 190 $field->insert_field(); 191 $recordid = data_add_record($module); 192 193 // File information for the database module record. 194 $datacontent = array(); 195 $datacontent['fieldid'] = $field->field->id; 196 $datacontent['recordid'] = $recordid; 197 $datacontent['content'] = 'Simple4.txt'; 198 199 // Insert the information about the file. 200 $contentid = $DB->insert_record('data_content', $datacontent); 201 // Required information for uploading a file. 202 $context = context_module::instance($module->cmid); 203 $usercontext = context_user::instance($USER->id); 204 $component = 'mod_data'; 205 $filearea = 'content'; 206 $itemid = $contentid; 207 $filename = $datacontent['content']; 208 $filecontent = base64_encode("Let us create a nice simple file."); 209 210 $filerecord = array(); 211 $filerecord['contextid'] = $context->id; 212 $filerecord['component'] = $component; 213 $filerecord['filearea'] = $filearea; 214 $filerecord['itemid'] = $itemid; 215 $filerecord['filepath'] = '/'; 216 $filerecord['filename'] = $filename; 217 218 // Create an area to upload the file. 219 $fs = get_file_storage(); 220 // Create a file from the string that we made earlier. 221 $file = $fs->create_file_from_string($filerecord, $filecontent); 222 $timemodified = $file->get_timemodified(); 223 $timecreated = $file->get_timemodified(); 224 $filesize = $file->get_filesize(); 225 226 // Use the web service function to return the information about the file that we just uploaded. 227 // The first time is with a valid context ID. 228 $filename = ''; 229 $testfilelisting = core_files_external::get_files($context->id, $component, $filearea, $itemid, '/', $filename); 230 $testfilelisting = external_api::clean_returnvalue(core_files_external::get_files_returns(), $testfilelisting); 231 232 // With the information that we have provided we should get an object exactly like the one below. 233 $coursecontext = context_course::instance($course->id); 234 $testdata = array(); 235 $testdata['parents'] = array(); 236 $testdata['parents']['0'] = array('contextid' => 1, 237 'component' => null, 238 'filearea' => null, 239 'itemid' => null, 240 'filepath' => null, 241 'filename' => 'System'); 242 $testdata['parents']['1'] = array('contextid' => 3, 243 'component' => null, 244 'filearea' => null, 245 'itemid' => null, 246 'filepath' => null, 247 'filename' => 'Miscellaneous'); 248 $testdata['parents']['2'] = array('contextid' => $coursecontext->id, 249 'component' => null, 250 'filearea' => null, 251 'itemid' => null, 252 'filepath' => null, 253 'filename' => 'Test course 1'); 254 $testdata['parents']['3'] = array('contextid' => $context->id, 255 'component' => null, 256 'filearea' => null, 257 'itemid' => null, 258 'filepath' => null, 259 'filename' => 'Mod data upload test (Database)'); 260 $testdata['parents']['4'] = array('contextid' => $context->id, 261 'component' => 'mod_data', 262 'filearea' => 'content', 263 'itemid' => null, 264 'filepath' => null, 265 'filename' => 'Fields'); 266 $testdata['files'] = array(); 267 $testdata['files']['0'] = array('contextid' => $context->id, 268 'component' => 'mod_data', 269 'filearea' => 'content', 270 'itemid' => $itemid, 271 'filepath' => '/', 272 'filename' => 'Simple4.txt', 273 'url' => 'http://www.example.com/moodle/pluginfile.php/'.$context->id.'/mod_data/content/'.$itemid.'/Simple4.txt', 274 'isdir' => false, 275 'timemodified' => $timemodified, 276 'timecreated' => $timecreated, 277 'filesize' => $filesize, 278 'author' => null, 279 'license' => null 280 ); 281 // Make sure that they are the same. 282 $this->assertEquals($testdata, $testfilelisting); 283 284 // Try again but without the context. Minus one signals the function to use other variables to obtain the context. 285 $nocontext = -1; 286 $modified = 0; 287 // Context level and instance ID are used to determine what the context is. 288 $contextlevel = 'module'; 289 $instanceid = $module->cmid; 290 $testfilelisting = core_files_external::get_files($nocontext, $component, $filearea, $itemid, '/', $filename, $modified, $contextlevel, $instanceid); 291 $testfilelisting = external_api::clean_returnvalue(core_files_external::get_files_returns(), $testfilelisting); 292 293 $this->assertEquals($testfilelisting, $testdata); 294 } 295 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |