| 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\external;
  | 
        
        
            | 
            | 
           18 | 
              | 
        
        
            | 
            | 
           19 | 
           use advanced_testcase;
  | 
        
        
            | 
            | 
           20 | 
           use context_user;
  | 
        
        
            | 
            | 
           21 | 
              | 
        
        
            | 
            | 
           22 | 
           /**
  | 
        
        
            | 
            | 
           23 | 
            * Unit tests for stored file exporter
  | 
        
        
            | 
            | 
           24 | 
            *
  | 
        
        
            | 
            | 
           25 | 
            * @package     core_files
  | 
        
        
            | 
            | 
           26 | 
            * @covers      \core_files\external\stored_file_exporter
  | 
        
        
            | 
            | 
           27 | 
            * @copyright   2023 Paul Holden <paulh@moodle.com>
  | 
        
        
            | 
            | 
           28 | 
            * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           29 | 
            */
  | 
        
        
            | 
            | 
           30 | 
           class stored_file_exporter_test extends advanced_testcase {
  | 
        
        
            | 
            | 
           31 | 
              | 
        
        
            | 
            | 
           32 | 
               /**
  | 
        
        
            | 
            | 
           33 | 
                * Test exported data structure
  | 
        
        
            | 
            | 
           34 | 
                */
  | 
        
        
            | 
            | 
           35 | 
               public function test_export(): void {
  | 
        
        
            | 
            | 
           36 | 
                   global $PAGE, $USER, $CFG;
  | 
        
        
            | 
            | 
           37 | 
              | 
        
        
            | 
            | 
           38 | 
                   $this->resetAfterTest();
  | 
        
        
            | 
            | 
           39 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           40 | 
              | 
        
        
            | 
            | 
           41 | 
                   $contextuser = context_user::instance($USER->id);
  | 
        
        
            | 
            | 
           42 | 
              | 
        
        
            | 
            | 
           43 | 
                   $file = get_file_storage()->create_file_from_string([
  | 
        
        
            | 
            | 
           44 | 
                       'contextid' => $contextuser->id,
  | 
        
        
            | 
            | 
           45 | 
                       'userid' => $USER->id,
  | 
        
        
            | 
            | 
           46 | 
                       'component' => 'user',
  | 
        
        
            | 
            | 
           47 | 
                       'filearea' => 'draft',
  | 
        
        
            | 
            | 
           48 | 
                       'itemid' => file_get_unused_draft_itemid(),
  | 
        
        
            | 
            | 
           49 | 
                       'filepath' => '/',
  | 
        
        
            | 
            | 
           50 | 
                       'filename' => 'Hi.txt',
  | 
        
        
            | 
            | 
           51 | 
                   ], 'Hello');
  | 
        
        
            | 
            | 
           52 | 
              | 
        
        
            | 
            | 
           53 | 
                   $exporter = new stored_file_exporter($file, ['context' => $contextuser]);
  | 
        
        
            | 
            | 
           54 | 
                   $export = $exporter->export($PAGE->get_renderer('core'));
  | 
        
        
            | 
            | 
           55 | 
              | 
        
        
            | 
            | 
           56 | 
                   $this->assertEquals((object) [
  | 
        
        
            | 
            | 
           57 | 
                       'contextid' => $file->get_contextid(),
  | 
        
        
            | 
            | 
           58 | 
                       'component' => $file->get_component(),
  | 
        
        
            | 
            | 
           59 | 
                       'filearea' => $file->get_filearea(),
  | 
        
        
            | 
            | 
           60 | 
                       'itemid' => $file->get_itemid(),
  | 
        
        
            | 
            | 
           61 | 
                       'filepath' => $file->get_filepath(),
  | 
        
        
            | 
            | 
           62 | 
                       'filename' => $file->get_filename(),
  | 
        
        
            | 
            | 
           63 | 
                       'isdir' => false,
  | 
        
        
            | 
            | 
           64 | 
                       'isimage' => false,
  | 
        
        
            | 
            | 
           65 | 
                       'timemodified' => $file->get_timemodified(),
  | 
        
        
            | 
            | 
           66 | 
                       'timecreated' => $file->get_timecreated(),
  | 
        
        
            | 
            | 
           67 | 
                       'filesize' => $file->get_filesize(),
  | 
        
        
            | 
            | 
           68 | 
                       'author' => $file->get_author(),
  | 
        
        
            | 
            | 
           69 | 
                       'license' => $file->get_license(),
  | 
        
        
            | 
            | 
           70 | 
                       'filenameshort' => $file->get_filename(),
  | 
        
        
            | 
            | 
           71 | 
                       'filesizeformatted' => display_size($file->get_filesize()),
  | 
        
        
            | 
            | 
           72 | 
                       'icon' => 'f/text',
  | 
        
        
            | 
            | 
           73 | 
                       'timecreatedformatted' => userdate($file->get_timecreated()),
  | 
        
        
            | 
            | 
           74 | 
                       'timemodifiedformatted' => userdate($file->get_timemodified()),
  | 
        
        
            | 
            | 
           75 | 
                       'url' => "{$CFG->wwwroot}/pluginfile.php/{$contextuser->id}/user/draft/{$file->get_itemid()}/Hi.txt?forcedownload=1",
  | 
        
        
            | 
            | 
           76 | 
                   ], $export);
  | 
        
        
            | 
            | 
           77 | 
               }
  | 
        
        
            | 
            | 
           78 | 
              | 
        
        
            | 
            | 
           79 | 
               /**
  | 
        
        
            | 
            | 
           80 | 
                * Data provider for {@see test_export_filenameshort}
  | 
        
        
            | 
            | 
           81 | 
                *
  | 
        
        
            | 
            | 
           82 | 
                * @return array[]
  | 
        
        
            | 
            | 
           83 | 
                */
  | 
        
        
            | 
            | 
           84 | 
               public static function export_filenameshort_provider(): array {
  | 
        
        
            | 
            | 
           85 | 
                   return [
  | 
        
        
            | 
            | 
           86 | 
                       // Long filenames (30 characters), with extensions of varying length.
  | 
        
        
            | 
            | 
           87 | 
                       ['Lorem ipsum dolor sit amet sit.c', 'Lorem ipsum dolor sit...c'],
  | 
        
        
            | 
            | 
           88 | 
                       ['Lorem ipsum dolor sit amet sit.txt', 'Lorem ipsum dolor s...txt'],
  | 
        
        
            | 
            | 
           89 | 
                       ['Lorem ipsum dolor sit amet sit.docx', 'Lorem ipsum dolor ...docx'],
  | 
        
        
            | 
            | 
           90 | 
                       // Multi-byte filenames.
  | 
        
        
            | 
            | 
           91 | 
                       ['Мазитов А.З. практика тусур.py', 'Мазитов А.З. практик...py'],
  | 
        
        
            | 
            | 
           92 | 
                   ];
  | 
        
        
            | 
            | 
           93 | 
               }
  | 
        
        
            | 
            | 
           94 | 
              | 
        
        
            | 
            | 
           95 | 
               /**
  | 
        
        
            | 
            | 
           96 | 
                * Test exporting shortened filename
  | 
        
        
            | 
            | 
           97 | 
                *
  | 
        
        
            | 
            | 
           98 | 
                * @param string $filename
  | 
        
        
            | 
            | 
           99 | 
                * @param string $expected
  | 
        
        
            | 
            | 
           100 | 
                *
  | 
        
        
            | 
            | 
           101 | 
                * @dataProvider export_filenameshort_provider
  | 
        
        
            | 
            | 
           102 | 
                */
  | 
        
        
            | 
            | 
           103 | 
               public function test_export_filenameshort(string $filename, string $expected): void {
  | 
        
        
            | 
            | 
           104 | 
                   global $PAGE, $USER;
  | 
        
        
            | 
            | 
           105 | 
              | 
        
        
            | 
            | 
           106 | 
                   $this->resetAfterTest();
  | 
        
        
            | 
            | 
           107 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           108 | 
              | 
        
        
            | 
            | 
           109 | 
                   $contextuser = context_user::instance($USER->id);
  | 
        
        
            | 
            | 
           110 | 
              | 
        
        
            | 
            | 
           111 | 
                   $file = get_file_storage()->create_file_from_string([
  | 
        
        
            | 
            | 
           112 | 
                       'contextid' => $contextuser->id,
  | 
        
        
            | 
            | 
           113 | 
                       'userid' => $USER->id,
  | 
        
        
            | 
            | 
           114 | 
                       'component' => 'user',
  | 
        
        
            | 
            | 
           115 | 
                       'filearea' => 'draft',
  | 
        
        
            | 
            | 
           116 | 
                       'itemid' => file_get_unused_draft_itemid(),
  | 
        
        
            | 
            | 
           117 | 
                       'filepath' => '/',
  | 
        
        
            | 
            | 
           118 | 
                       'filename' => $filename,
  | 
        
        
            | 
            | 
           119 | 
                   ], 'Hello');
  | 
        
        
            | 
            | 
           120 | 
              | 
        
        
            | 
            | 
           121 | 
                   $exporter = new stored_file_exporter($file, ['context' => $contextuser]);
  | 
        
        
            | 
            | 
           122 | 
                   $export = $exporter->export($PAGE->get_renderer('core'));
  | 
        
        
            | 
            | 
           123 | 
              | 
        
        
            | 
            | 
           124 | 
                   $this->assertEquals($expected, $export->filenameshort);
  | 
        
        
            | 
            | 
           125 | 
               }
  | 
        
        
            | 
            | 
           126 | 
           }
  |