| 1 | 
           efrain | 
           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 | 
           namespace core_files;
  | 
        
        
            | 
            | 
           18 | 
              | 
        
        
            | 
            | 
           19 | 
           use core_external\external_api;
  | 
        
        
            | 
            | 
           20 | 
           use core_files\external\delete\draft;
  | 
        
        
            | 
            | 
           21 | 
           use core_files\external\get\unused_draft;
  | 
        
        
            | 
            | 
           22 | 
           use core_files_external;
  | 
        
        
            | 
            | 
           23 | 
              | 
        
        
            | 
            | 
           24 | 
           defined('MOODLE_INTERNAL') || die();
  | 
        
        
            | 
            | 
           25 | 
              | 
        
        
            | 
            | 
           26 | 
           global $CFG;
  | 
        
        
            | 
            | 
           27 | 
              | 
        
        
            | 
            | 
           28 | 
           require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  | 
        
        
            | 
            | 
           29 | 
           require_once($CFG->dirroot . '/files/externallib.php');
  | 
        
        
            | 
            | 
           30 | 
              | 
        
        
            | 
            | 
           31 | 
           /**
  | 
        
        
            | 
            | 
           32 | 
            * PHPunit tests for external files API.
  | 
        
        
            | 
            | 
           33 | 
            *
  | 
        
        
            | 
            | 
           34 | 
            * @package    core_files
  | 
        
        
            | 
            | 
           35 | 
            * @category   external
  | 
        
        
            | 
            | 
           36 | 
            * @copyright  2013 Ankit Agarwal
  | 
        
        
            | 
            | 
           37 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           38 | 
            * @since Moodle 2.6
  | 
        
        
            | 
            | 
           39 | 
            */
  | 
        
        
           | 1441 | 
           ariadna | 
           40 | 
           final class externallib_test extends \advanced_testcase {
  | 
        
        
           | 1 | 
           efrain | 
           41 | 
              | 
        
        
            | 
            | 
           42 | 
               /*
  | 
        
        
            | 
            | 
           43 | 
                * Test core_files_external::upload().
  | 
        
        
            | 
            | 
           44 | 
                */
  | 
        
        
            | 
            | 
           45 | 
              | 
        
        
           | 11 | 
           efrain | 
           46 | 
               public function test_upload(): void {
  | 
        
        
           | 1 | 
           efrain | 
           47 | 
                   global $USER;
  | 
        
        
            | 
            | 
           48 | 
              | 
        
        
            | 
            | 
           49 | 
                   $this->resetAfterTest();
  | 
        
        
            | 
            | 
           50 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           51 | 
                   $context = \context_user::instance($USER->id);
  | 
        
        
            | 
            | 
           52 | 
                   $contextid = $context->id;
  | 
        
        
            | 
            | 
           53 | 
                   $component = "user";
  | 
        
        
            | 
            | 
           54 | 
                   $filearea = "draft";
  | 
        
        
            | 
            | 
           55 | 
                   $itemid = 0;
  | 
        
        
            | 
            | 
           56 | 
                   $filepath = "/";
  | 
        
        
            | 
            | 
           57 | 
                   $filename = "Simple.txt";
  | 
        
        
            | 
            | 
           58 | 
                   $filecontent = base64_encode("Let us create a nice simple file");
  | 
        
        
            | 
            | 
           59 | 
                   $contextlevel = null;
  | 
        
        
            | 
            | 
           60 | 
                   $instanceid = null;
  | 
        
        
            | 
            | 
           61 | 
                   $browser = get_file_browser();
  | 
        
        
            | 
            | 
           62 | 
              | 
        
        
            | 
            | 
           63 | 
                   // Make sure no file exists.
  | 
        
        
            | 
            | 
           64 | 
                   $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
  | 
        
        
            | 
            | 
           65 | 
                   $this->assertEmpty($file);
  | 
        
        
            | 
            | 
           66 | 
              | 
        
        
            | 
            | 
           67 | 
                   // Call the api to create a file.
  | 
        
        
            | 
            | 
           68 | 
                   $fileinfo = core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath,
  | 
        
        
            | 
            | 
           69 | 
                           $filename, $filecontent, $contextlevel, $instanceid);
  | 
        
        
            | 
            | 
           70 | 
                   $fileinfo = external_api::clean_returnvalue(core_files_external::upload_returns(), $fileinfo);
  | 
        
        
            | 
            | 
           71 | 
                   // Get the created draft item id.
  | 
        
        
            | 
            | 
           72 | 
                   $itemid = $fileinfo['itemid'];
  | 
        
        
            | 
            | 
           73 | 
              | 
        
        
            | 
            | 
           74 | 
                   // Make sure the file was created.
  | 
        
        
            | 
            | 
           75 | 
                   $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
  | 
        
        
            | 
            | 
           76 | 
                   $this->assertNotEmpty($file);
  | 
        
        
            | 
            | 
           77 | 
              | 
        
        
            | 
            | 
           78 | 
                   // Make sure no file exists.
  | 
        
        
            | 
            | 
           79 | 
                   $filename = "Simple2.txt";
  | 
        
        
            | 
            | 
           80 | 
                   $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
  | 
        
        
            | 
            | 
           81 | 
                   $this->assertEmpty($file);
  | 
        
        
            | 
            | 
           82 | 
              | 
        
        
            | 
            | 
           83 | 
                   // Call the api to create a file.
  | 
        
        
            | 
            | 
           84 | 
                   $fileinfo = core_files_external::upload($contextid, $component, $filearea, $itemid,
  | 
        
        
            | 
            | 
           85 | 
                           $filepath, $filename, $filecontent, $contextlevel, $instanceid);
  | 
        
        
            | 
            | 
           86 | 
                   $fileinfo = external_api::clean_returnvalue(core_files_external::upload_returns(), $fileinfo);
  | 
        
        
            | 
            | 
           87 | 
                   $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
  | 
        
        
            | 
            | 
           88 | 
                   $this->assertNotEmpty($file);
  | 
        
        
            | 
            | 
           89 | 
              | 
        
        
            | 
            | 
           90 | 
                   // Let us try creating a file using contextlevel and instance id.
  | 
        
        
            | 
            | 
           91 | 
                   $filename = "Simple5.txt";
  | 
        
        
            | 
            | 
           92 | 
                   $contextid = 0;
  | 
        
        
            | 
            | 
           93 | 
                   $contextlevel = "user";
  | 
        
        
            | 
            | 
           94 | 
                   $instanceid = $USER->id;
  | 
        
        
            | 
            | 
           95 | 
                   $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
  | 
        
        
            | 
            | 
           96 | 
                   $this->assertEmpty($file);
  | 
        
        
            | 
            | 
           97 | 
                   $fileinfo = core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath,
  | 
        
        
            | 
            | 
           98 | 
                           $filename, $filecontent, $contextlevel, $instanceid);
  | 
        
        
            | 
            | 
           99 | 
                   $fileinfo = external_api::clean_returnvalue(core_files_external::upload_returns(), $fileinfo);
  | 
        
        
            | 
            | 
           100 | 
                   $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
  | 
        
        
            | 
            | 
           101 | 
                   $this->assertNotEmpty($file);
  | 
        
        
            | 
            | 
           102 | 
              | 
        
        
            | 
            | 
           103 | 
                   // Make sure the same file cannot be created again.
  | 
        
        
            | 
            | 
           104 | 
                   $this->expectException("moodle_exception");
  | 
        
        
            | 
            | 
           105 | 
                   core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath,
  | 
        
        
            | 
            | 
           106 | 
                           $filename, $filecontent, $contextlevel, $instanceid);
  | 
        
        
            | 
            | 
           107 | 
               }
  | 
        
        
            | 
            | 
           108 | 
              | 
        
        
            | 
            | 
           109 | 
               /*
  | 
        
        
            | 
            | 
           110 | 
                * Make sure only user component is allowed in  core_files_external::upload().
  | 
        
        
            | 
            | 
           111 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           112 | 
               public function test_upload_param_component(): void {
  | 
        
        
           | 1 | 
           efrain | 
           113 | 
                   global $USER;
  | 
        
        
            | 
            | 
           114 | 
              | 
        
        
            | 
            | 
           115 | 
                   $this->resetAfterTest();
  | 
        
        
            | 
            | 
           116 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           117 | 
                   $context = \context_user::instance($USER->id);
  | 
        
        
            | 
            | 
           118 | 
                   $contextid = $context->id;
  | 
        
        
            | 
            | 
           119 | 
                   $component = "backup";
  | 
        
        
            | 
            | 
           120 | 
                   $filearea = "draft";
  | 
        
        
            | 
            | 
           121 | 
                   $itemid = 0;
  | 
        
        
            | 
            | 
           122 | 
                   $filepath = "/";
  | 
        
        
            | 
            | 
           123 | 
                   $filename = "Simple3.txt";
  | 
        
        
            | 
            | 
           124 | 
                   $filecontent = base64_encode("Let us create a nice simple file");
  | 
        
        
            | 
            | 
           125 | 
                   $contextlevel = null;
  | 
        
        
            | 
            | 
           126 | 
                   $instanceid = null;
  | 
        
        
            | 
            | 
           127 | 
              | 
        
        
            | 
            | 
           128 | 
                   // Make sure exception is thrown.
  | 
        
        
            | 
            | 
           129 | 
                   $this->expectException("coding_exception");
  | 
        
        
            | 
            | 
           130 | 
                   core_files_external::upload($contextid, $component, $filearea, $itemid,
  | 
        
        
            | 
            | 
           131 | 
                           $filepath, $filename, $filecontent, $contextlevel, $instanceid);
  | 
        
        
            | 
            | 
           132 | 
               }
  | 
        
        
            | 
            | 
           133 | 
              | 
        
        
            | 
            | 
           134 | 
               /*
  | 
        
        
            | 
            | 
           135 | 
                * Make sure only draft areas are allowed in  core_files_external::upload().
  | 
        
        
            | 
            | 
           136 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           137 | 
               public function test_upload_param_area(): void {
  | 
        
        
           | 1 | 
           efrain | 
           138 | 
                   global $USER;
  | 
        
        
            | 
            | 
           139 | 
              | 
        
        
            | 
            | 
           140 | 
                   $this->resetAfterTest();
  | 
        
        
            | 
            | 
           141 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           142 | 
                   $context = \context_user::instance($USER->id);
  | 
        
        
            | 
            | 
           143 | 
                   $contextid = $context->id;
  | 
        
        
            | 
            | 
           144 | 
                   $component = "user";
  | 
        
        
            | 
            | 
           145 | 
                   $filearea = "draft";
  | 
        
        
            | 
            | 
           146 | 
                   $itemid = file_get_unused_draft_itemid();
  | 
        
        
            | 
            | 
           147 | 
                   $filepath = "/";
  | 
        
        
            | 
            | 
           148 | 
                   $filename = "Simple4.txt";
  | 
        
        
            | 
            | 
           149 | 
                   $filecontent = base64_encode("Let us create a nice simple file");
  | 
        
        
            | 
            | 
           150 | 
                   $contextlevel = null;
  | 
        
        
            | 
            | 
           151 | 
                   $instanceid = null;
  | 
        
        
            | 
            | 
           152 | 
              | 
        
        
            | 
            | 
           153 | 
                   // Make sure the file is created.
  | 
        
        
            | 
            | 
           154 | 
                   $fileinfo = core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent,
  | 
        
        
            | 
            | 
           155 | 
                       'user', $USER->id);
  | 
        
        
            | 
            | 
           156 | 
                   $fileinfo = external_api::clean_returnvalue(core_files_external::upload_returns(), $fileinfo);
  | 
        
        
            | 
            | 
           157 | 
                   $browser = get_file_browser();
  | 
        
        
            | 
            | 
           158 | 
                   $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
  | 
        
        
            | 
            | 
           159 | 
                   $this->assertNotEmpty($file);
  | 
        
        
            | 
            | 
           160 | 
               }
  | 
        
        
            | 
            | 
           161 | 
              | 
        
        
            | 
            | 
           162 | 
               /**
  | 
        
        
            | 
            | 
           163 | 
                * Test getting a list of files with and without a context ID.
  | 
        
        
            | 
            | 
           164 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           165 | 
               public function test_get_files(): void {
  | 
        
        
           | 1 | 
           efrain | 
           166 | 
                   global $USER, $DB;
  | 
        
        
            | 
            | 
           167 | 
              | 
        
        
            | 
            | 
           168 | 
                   $this->resetAfterTest();
  | 
        
        
            | 
            | 
           169 | 
              | 
        
        
            | 
            | 
           170 | 
                   // Set the current user to be the administrator.
  | 
        
        
            | 
            | 
           171 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           172 | 
                   $USER->email = 'test@example.com';
  | 
        
        
            | 
            | 
           173 | 
              | 
        
        
            | 
            | 
           174 | 
                   // Create a course.
  | 
        
        
            | 
            | 
           175 | 
                   $course = $this->getDataGenerator()->create_course();
  | 
        
        
            | 
            | 
           176 | 
                   $record = new \stdClass();
  | 
        
        
            | 
            | 
           177 | 
                   $record->course = $course->id;
  | 
        
        
            | 
            | 
           178 | 
                   $record->name = "Mod data upload test";
  | 
        
        
            | 
            | 
           179 | 
                   $record->intro = "Some intro of some sort";
  | 
        
        
            | 
            | 
           180 | 
              | 
        
        
            | 
            | 
           181 | 
                   // Create a database module.
  | 
        
        
            | 
            | 
           182 | 
                   $module = $this->getDataGenerator()->create_module('data', $record);
  | 
        
        
            | 
            | 
           183 | 
              | 
        
        
            | 
            | 
           184 | 
                   // Create a new field in the database activity.
  | 
        
        
            | 
            | 
           185 | 
                   $field = data_get_field_new('file', $module);
  | 
        
        
            | 
            | 
           186 | 
                   // Add more detail about the field.
  | 
        
        
            | 
            | 
           187 | 
                   $fielddetail = new \stdClass();
  | 
        
        
            | 
            | 
           188 | 
                   $fielddetail->d = $module->id;
  | 
        
        
            | 
            | 
           189 | 
                   $fielddetail->mode = 'add';
  | 
        
        
            | 
            | 
           190 | 
                   $fielddetail->type = 'file';
  | 
        
        
            | 
            | 
           191 | 
                   $fielddetail->sesskey = sesskey();
  | 
        
        
            | 
            | 
           192 | 
                   $fielddetail->name = 'Upload file';
  | 
        
        
            | 
            | 
           193 | 
                   $fielddetail->description = 'Some description';
  | 
        
        
            | 
            | 
           194 | 
                   $fielddetail->param3 = '0';
  | 
        
        
            | 
            | 
           195 | 
              | 
        
        
            | 
            | 
           196 | 
                   $field->define_field($fielddetail);
  | 
        
        
            | 
            | 
           197 | 
                   $field->insert_field();
  | 
        
        
            | 
            | 
           198 | 
                   $recordid = data_add_record($module);
  | 
        
        
            | 
            | 
           199 | 
              | 
        
        
            | 
            | 
           200 | 
                   // File information for the database module record.
  | 
        
        
            | 
            | 
           201 | 
                   $datacontent = array();
  | 
        
        
            | 
            | 
           202 | 
                   $datacontent['fieldid'] = $field->field->id;
  | 
        
        
            | 
            | 
           203 | 
                   $datacontent['recordid'] = $recordid;
  | 
        
        
            | 
            | 
           204 | 
                   $datacontent['content'] = 'Simple4.txt';
  | 
        
        
            | 
            | 
           205 | 
              | 
        
        
            | 
            | 
           206 | 
                   // Insert the information about the file.
  | 
        
        
            | 
            | 
           207 | 
                   $contentid = $DB->insert_record('data_content', $datacontent);
  | 
        
        
            | 
            | 
           208 | 
                   // Required information for uploading a file.
  | 
        
        
            | 
            | 
           209 | 
                   $context = \context_module::instance($module->cmid);
  | 
        
        
            | 
            | 
           210 | 
                   $usercontext = \context_user::instance($USER->id);
  | 
        
        
            | 
            | 
           211 | 
                   $component = 'mod_data';
  | 
        
        
            | 
            | 
           212 | 
                   $filearea = 'content';
  | 
        
        
            | 
            | 
           213 | 
                   $itemid = $contentid;
  | 
        
        
            | 
            | 
           214 | 
                   $filename = $datacontent['content'];
  | 
        
        
            | 
            | 
           215 | 
                   $filecontent = base64_encode("Let us create a nice simple file.");
  | 
        
        
            | 
            | 
           216 | 
              | 
        
        
            | 
            | 
           217 | 
                   $filerecord = array();
  | 
        
        
            | 
            | 
           218 | 
                   $filerecord['contextid'] = $context->id;
  | 
        
        
            | 
            | 
           219 | 
                   $filerecord['component'] = $component;
  | 
        
        
            | 
            | 
           220 | 
                   $filerecord['filearea'] = $filearea;
  | 
        
        
            | 
            | 
           221 | 
                   $filerecord['itemid'] = $itemid;
  | 
        
        
            | 
            | 
           222 | 
                   $filerecord['filepath'] = '/';
  | 
        
        
            | 
            | 
           223 | 
                   $filerecord['filename'] = $filename;
  | 
        
        
            | 
            | 
           224 | 
              | 
        
        
            | 
            | 
           225 | 
                   // Create an area to upload the file.
  | 
        
        
            | 
            | 
           226 | 
                   $fs = get_file_storage();
  | 
        
        
            | 
            | 
           227 | 
                   // Create a file from the string that we made earlier.
  | 
        
        
            | 
            | 
           228 | 
                   $file = $fs->create_file_from_string($filerecord, $filecontent);
  | 
        
        
            | 
            | 
           229 | 
                   $timemodified = $file->get_timemodified();
  | 
        
        
            | 
            | 
           230 | 
                   $timecreated = $file->get_timemodified();
  | 
        
        
            | 
            | 
           231 | 
                   $filesize = $file->get_filesize();
  | 
        
        
            | 
            | 
           232 | 
              | 
        
        
            | 
            | 
           233 | 
                   // Use the web service function to return the information about the file that we just uploaded.
  | 
        
        
            | 
            | 
           234 | 
                   // The first time is with a valid context ID.
  | 
        
        
            | 
            | 
           235 | 
                   $filename = '';
  | 
        
        
            | 
            | 
           236 | 
                   $testfilelisting = core_files_external::get_files($context->id, $component, $filearea, $itemid, '/', $filename);
  | 
        
        
            | 
            | 
           237 | 
                   $testfilelisting = external_api::clean_returnvalue(core_files_external::get_files_returns(), $testfilelisting);
  | 
        
        
            | 
            | 
           238 | 
              | 
        
        
            | 
            | 
           239 | 
                   // With the information that we have provided we should get an object exactly like the one below.
  | 
        
        
            | 
            | 
           240 | 
                   $coursecontext = \context_course::instance($course->id);
  | 
        
        
            | 
            | 
           241 | 
                   $testdata = array();
  | 
        
        
            | 
            | 
           242 | 
                   $testdata['parents'] = array();
  | 
        
        
            | 
            | 
           243 | 
                   $testdata['parents']['0'] = array('contextid' => 1,
  | 
        
        
            | 
            | 
           244 | 
                                                     'component' => null,
  | 
        
        
            | 
            | 
           245 | 
                                                     'filearea' => null,
  | 
        
        
            | 
            | 
           246 | 
                                                     'itemid' => null,
  | 
        
        
            | 
            | 
           247 | 
                                                     'filepath' => null,
  | 
        
        
            | 
            | 
           248 | 
                                                     'filename' => 'System');
  | 
        
        
            | 
            | 
           249 | 
                   $testdata['parents']['1'] = array('contextid' => 3,
  | 
        
        
            | 
            | 
           250 | 
                                                     'component' => null,
  | 
        
        
            | 
            | 
           251 | 
                                                     'filearea' => null,
  | 
        
        
            | 
            | 
           252 | 
                                                     'itemid' => null,
  | 
        
        
            | 
            | 
           253 | 
                                                     'filepath' => null,
  | 
        
        
            | 
            | 
           254 | 
                                                     'filename' => get_string('defaultcategoryname'));
  | 
        
        
            | 
            | 
           255 | 
                   $testdata['parents']['2'] = array('contextid' => $coursecontext->id,
  | 
        
        
            | 
            | 
           256 | 
                                                     'component' => null,
  | 
        
        
            | 
            | 
           257 | 
                                                     'filearea' => null,
  | 
        
        
            | 
            | 
           258 | 
                                                     'itemid' => null,
  | 
        
        
            | 
            | 
           259 | 
                                                     'filepath' => null,
  | 
        
        
            | 
            | 
           260 | 
                                                     'filename' => 'Test course 1');
  | 
        
        
            | 
            | 
           261 | 
                   $testdata['parents']['3'] = array('contextid' => $context->id,
  | 
        
        
            | 
            | 
           262 | 
                                                     'component' => null,
  | 
        
        
            | 
            | 
           263 | 
                                                     'filearea' => null,
  | 
        
        
            | 
            | 
           264 | 
                                                     'itemid' => null,
  | 
        
        
            | 
            | 
           265 | 
                                                     'filepath' => null,
  | 
        
        
            | 
            | 
           266 | 
                                                     'filename' => 'Mod data upload test (Database)');
  | 
        
        
            | 
            | 
           267 | 
                   $testdata['parents']['4'] = array('contextid' => $context->id,
  | 
        
        
            | 
            | 
           268 | 
                                                     'component' => 'mod_data',
  | 
        
        
            | 
            | 
           269 | 
                                                     'filearea' => 'content',
  | 
        
        
            | 
            | 
           270 | 
                                                     'itemid' => null,
  | 
        
        
            | 
            | 
           271 | 
                                                     'filepath' => null,
  | 
        
        
            | 
            | 
           272 | 
                                                     'filename' => 'Fields');
  | 
        
        
            | 
            | 
           273 | 
                   $testdata['files'] = array();
  | 
        
        
            | 
            | 
           274 | 
                   $testdata['files']['0'] = array('contextid' => $context->id,
  | 
        
        
            | 
            | 
           275 | 
                                                   'component' => 'mod_data',
  | 
        
        
            | 
            | 
           276 | 
                                                   'filearea' => 'content',
  | 
        
        
            | 
            | 
           277 | 
                                                   'itemid' => $itemid,
  | 
        
        
            | 
            | 
           278 | 
                                                   'filepath' => '/',
  | 
        
        
            | 
            | 
           279 | 
                                                   'filename' => 'Simple4.txt',
  | 
        
        
            | 
            | 
           280 | 
                                                   'url' => 'https://www.example.com/moodle/pluginfile.php/'.$context->id.'/mod_data/content/'.$itemid.'/Simple4.txt',
  | 
        
        
            | 
            | 
           281 | 
                                                   'isdir' => false,
  | 
        
        
            | 
            | 
           282 | 
                                                   'timemodified' => $timemodified,
  | 
        
        
            | 
            | 
           283 | 
                                                   'timecreated' => $timecreated,
  | 
        
        
            | 
            | 
           284 | 
                                                   'filesize' => $filesize,
  | 
        
        
            | 
            | 
           285 | 
                                                   'author' => null,
  | 
        
        
            | 
            | 
           286 | 
                                                   'license' => null
  | 
        
        
            | 
            | 
           287 | 
                                                   );
  | 
        
        
            | 
            | 
           288 | 
                   // Make sure that they are the same.
  | 
        
        
            | 
            | 
           289 | 
                   $this->assertEquals($testdata, $testfilelisting);
  | 
        
        
            | 
            | 
           290 | 
              | 
        
        
            | 
            | 
           291 | 
                   // Try again but without the context. Minus one signals the function to use other variables to obtain the context.
  | 
        
        
            | 
            | 
           292 | 
                   $nocontext = -1;
  | 
        
        
            | 
            | 
           293 | 
                   $modified = 0;
  | 
        
        
            | 
            | 
           294 | 
                   // Context level and instance ID are used to determine what the context is.
  | 
        
        
            | 
            | 
           295 | 
                   $contextlevel = 'module';
  | 
        
        
            | 
            | 
           296 | 
                   $instanceid = $module->cmid;
  | 
        
        
            | 
            | 
           297 | 
                   $testfilelisting = core_files_external::get_files($nocontext, $component, $filearea, $itemid, '/', $filename, $modified, $contextlevel, $instanceid);
  | 
        
        
            | 
            | 
           298 | 
                   $testfilelisting = external_api::clean_returnvalue(core_files_external::get_files_returns(), $testfilelisting);
  | 
        
        
            | 
            | 
           299 | 
              | 
        
        
            | 
            | 
           300 | 
                   $this->assertEquals($testfilelisting, $testdata);
  | 
        
        
            | 
            | 
           301 | 
               }
  | 
        
        
            | 
            | 
           302 | 
              | 
        
        
            | 
            | 
           303 | 
               /**
  | 
        
        
            | 
            | 
           304 | 
                * Test delete draft files
  | 
        
        
            | 
            | 
           305 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           306 | 
               public function test_delete_draft_files(): void {
  | 
        
        
           | 1 | 
           efrain | 
           307 | 
                   global $USER;
  | 
        
        
            | 
            | 
           308 | 
              | 
        
        
            | 
            | 
           309 | 
                   $this->resetAfterTest();
  | 
        
        
            | 
            | 
           310 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           311 | 
              | 
        
        
            | 
            | 
           312 | 
                   // Add files to user draft area.
  | 
        
        
            | 
            | 
           313 | 
                   $draftitemid = file_get_unused_draft_itemid();
  | 
        
        
            | 
            | 
           314 | 
                   $context = \context_user::instance($USER->id);
  | 
        
        
            | 
            | 
           315 | 
                   $filerecordinline = array(
  | 
        
        
            | 
            | 
           316 | 
                       'contextid' => $context->id,
  | 
        
        
            | 
            | 
           317 | 
                       'component' => 'user',
  | 
        
        
            | 
            | 
           318 | 
                       'filearea'  => 'draft',
  | 
        
        
            | 
            | 
           319 | 
                       'itemid'    => $draftitemid,
  | 
        
        
            | 
            | 
           320 | 
                       'filepath'  => '/',
  | 
        
        
            | 
            | 
           321 | 
                       'filename'  => 'faketxt.txt',
  | 
        
        
            | 
            | 
           322 | 
                   );
  | 
        
        
            | 
            | 
           323 | 
                   $fs = get_file_storage();
  | 
        
        
            | 
            | 
           324 | 
                   $fs->create_file_from_string($filerecordinline, 'fake txt contents 1.');
  | 
        
        
            | 
            | 
           325 | 
              | 
        
        
            | 
            | 
           326 | 
                   // Now create a folder with a file inside.
  | 
        
        
            | 
            | 
           327 | 
                   $fs->create_directory($context->id, 'user', 'draft', $draftitemid, '/fakefolder/');
  | 
        
        
            | 
            | 
           328 | 
                   $filerecordinline['filepath'] = '/fakefolder/';
  | 
        
        
            | 
            | 
           329 | 
                   $filerecordinline['filename'] = 'fakeimage.png';
  | 
        
        
            | 
            | 
           330 | 
                   $fs->create_file_from_string($filerecordinline, 'img...');
  | 
        
        
            | 
            | 
           331 | 
              | 
        
        
            | 
            | 
           332 | 
                   // Check two files were created (one file and one directory).
  | 
        
        
            | 
            | 
           333 | 
                   $files = core_files_external::get_files($context->id, 'user', 'draft', $draftitemid, '/', '');
  | 
        
        
            | 
            | 
           334 | 
                   $files = external_api::clean_returnvalue(core_files_external::get_files_returns(), $files);
  | 
        
        
            | 
            | 
           335 | 
                   $this->assertCount(2, $files['files']);
  | 
        
        
            | 
            | 
           336 | 
              | 
        
        
            | 
            | 
           337 | 
                   // Check the folder has one file.
  | 
        
        
            | 
            | 
           338 | 
                   $files = core_files_external::get_files($context->id, 'user', 'draft', $draftitemid, '/fakefolder/', '');
  | 
        
        
            | 
            | 
           339 | 
                   $files = external_api::clean_returnvalue(core_files_external::get_files_returns(), $files);
  | 
        
        
            | 
            | 
           340 | 
                   $this->assertCount(1, $files['files']);
  | 
        
        
            | 
            | 
           341 | 
              | 
        
        
            | 
            | 
           342 | 
                   // Delete a file and a folder.
  | 
        
        
            | 
            | 
           343 | 
                   $filestodelete = [
  | 
        
        
            | 
            | 
           344 | 
                       ['filepath' => '/', 'filename' => 'faketxt.txt'],
  | 
        
        
            | 
            | 
           345 | 
                       ['filepath' => '/fakefolder/', 'filename' => ''],
  | 
        
        
            | 
            | 
           346 | 
                   ];
  | 
        
        
            | 
            | 
           347 | 
                   $paths = draft::execute($draftitemid, $filestodelete);
  | 
        
        
            | 
            | 
           348 | 
                   $paths = external_api::clean_returnvalue(draft::execute_returns(), $paths);
  | 
        
        
            | 
            | 
           349 | 
              | 
        
        
            | 
            | 
           350 | 
                   // Check everything was deleted.
  | 
        
        
            | 
            | 
           351 | 
                   $files = core_files_external::get_files($context->id, 'user', 'draft', $draftitemid, '/', '');
  | 
        
        
            | 
            | 
           352 | 
                   $files = external_api::clean_returnvalue(core_files_external::get_files_returns(), $files);
  | 
        
        
            | 
            | 
           353 | 
                   $this->assertCount(0, $files['files']);
  | 
        
        
            | 
            | 
           354 | 
               }
  | 
        
        
            | 
            | 
           355 | 
              | 
        
        
            | 
            | 
           356 | 
               /**
  | 
        
        
            | 
            | 
           357 | 
                * Test get_unused_draft_itemid.
  | 
        
        
            | 
            | 
           358 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           359 | 
               public function test_get_unused_draft_itemid(): void {
  | 
        
        
           | 1 | 
           efrain | 
           360 | 
                   global $USER;
  | 
        
        
            | 
            | 
           361 | 
              | 
        
        
            | 
            | 
           362 | 
                   $this->resetAfterTest();
  | 
        
        
            | 
            | 
           363 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           364 | 
              | 
        
        
            | 
            | 
           365 | 
                   // Add files to user draft area.
  | 
        
        
            | 
            | 
           366 | 
                   $result = unused_draft::execute();
  | 
        
        
            | 
            | 
           367 | 
                   $result = external_api::clean_returnvalue(unused_draft::execute_returns(), $result);
  | 
        
        
            | 
            | 
           368 | 
              | 
        
        
            | 
            | 
           369 | 
                   $filerecordinline = [
  | 
        
        
            | 
            | 
           370 | 
                       'contextid' => $result['contextid'],
  | 
        
        
            | 
            | 
           371 | 
                       'component' => $result['component'],
  | 
        
        
            | 
            | 
           372 | 
                       'filearea'  => $result['filearea'],
  | 
        
        
            | 
            | 
           373 | 
                       'itemid'    => $result['itemid'],
  | 
        
        
            | 
            | 
           374 | 
                       'filepath'  => '/',
  | 
        
        
            | 
            | 
           375 | 
                       'filename'  => 'faketxt.txt',
  | 
        
        
            | 
            | 
           376 | 
                   ];
  | 
        
        
            | 
            | 
           377 | 
                   $fs = get_file_storage();
  | 
        
        
            | 
            | 
           378 | 
                   $fs->create_file_from_string($filerecordinline, 'fake txt contents 1.');
  | 
        
        
            | 
            | 
           379 | 
              | 
        
        
            | 
            | 
           380 | 
                   // Now create a folder with a file inside.
  | 
        
        
            | 
            | 
           381 | 
                   $fs->create_directory($result['contextid'], $result['component'], $result['filearea'], $result['itemid'], '/fakefolder/');
  | 
        
        
            | 
            | 
           382 | 
                   $filerecordinline['filepath'] = '/fakefolder/';
  | 
        
        
            | 
            | 
           383 | 
                   $filerecordinline['filename'] = 'fakeimage.png';
  | 
        
        
            | 
            | 
           384 | 
                   $fs->create_file_from_string($filerecordinline, 'img...');
  | 
        
        
            | 
            | 
           385 | 
              | 
        
        
            | 
            | 
           386 | 
                   $context = \context_user::instance($USER->id);
  | 
        
        
            | 
            | 
           387 | 
                   // Check two files were created (one file and one directory).
  | 
        
        
            | 
            | 
           388 | 
                   $files = core_files_external::get_files($context->id, 'user', 'draft', $result['itemid'], '/', '');
  | 
        
        
            | 
            | 
           389 | 
                   $files = external_api::clean_returnvalue(core_files_external::get_files_returns(), $files);
  | 
        
        
            | 
            | 
           390 | 
                   $this->assertCount(2, $files['files']);
  | 
        
        
            | 
            | 
           391 | 
               }
  | 
        
        
            | 
            | 
           392 | 
           }
  |