Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [Versions 401 and 403]

   1  <?php
   2  // This file is part of Moodle - https://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;
  18  
  19  use advanced_testcase;
  20  use context_system;
  21  
  22  /**
  23   * Unit tests for lib/filestorage/stored_file.php.
  24   *
  25   * @package    core_files
  26   * @category   test
  27   * @covers     \stored_file
  28   * @copyright  2022 Mikhail Golenkov <mikhailgolenkov@catalyst-au.net>
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class stored_file_test extends advanced_testcase {
  32  
  33      /**
  34       * Test that the rotate_image() method does not rotate
  35       * an image that is not supposed to be rotated.
  36       * @covers ::rotate_image()
  37       */
  38      public function test_rotate_image_does_not_rotate_image() {
  39          global $CFG;
  40          $this->resetAfterTest();
  41  
  42          $filename = 'testimage.jpg';
  43          $filepath = $CFG->dirroot . '/lib/filestorage/tests/fixtures/' . $filename;
  44          $filerecord = [
  45              'contextid' => context_system::instance()->id,
  46              'component' => 'core',
  47              'filearea'  => 'unittest',
  48              'itemid'    => 0,
  49              'filepath'  => '/',
  50              'filename'  => $filename,
  51          ];
  52          $fs = get_file_storage();
  53          $storedfile = $fs->create_file_from_pathname($filerecord, $filepath);
  54  
  55          $result = $storedfile->rotate_image();
  56          $this->assertIsArray($result);
  57          $this->assertCount(2, $result);
  58          $this->assertFalse($result[0]);
  59          $this->assertFalse($result[1]);
  60      }
  61  
  62      /**
  63       * Test that the rotate_image() method rotates an image
  64       * that is supposed to be rotated.
  65       * @covers ::rotate_image()
  66       */
  67      public function test_rotate_image_rotates_image() {
  68          global $CFG;
  69          $this->resetAfterTest();
  70  
  71          // This image was manually rotated to be upside down. Also, Orientation, ExifImageWidth
  72          // and ExifImageLength EXIF tags were written into its metadata.
  73          // This is needed to make sure that this image will be rotated by stored_file::rotate_image()
  74          // and stored as a new rotated file.
  75          $filename = 'testimage_rotated.jpg';
  76          $filepath = $CFG->dirroot . '/lib/filestorage/tests/fixtures/' . $filename;
  77          $filerecord = [
  78              'contextid' => context_system::instance()->id,
  79              'component' => 'core',
  80              'filearea'  => 'unittest',
  81              'itemid'    => 0,
  82              'filepath'  => '/',
  83              'filename'  => $filename,
  84          ];
  85          $fs = get_file_storage();
  86          $storedfile = $fs->create_file_from_pathname($filerecord, $filepath);
  87  
  88          list ($rotateddata, $size) = $storedfile->rotate_image();
  89          $this->assertNotFalse($rotateddata);
  90          $this->assertIsArray($size);
  91          $this->assertEquals(1200, $size['width']);
  92          $this->assertEquals(297, $size['height']);
  93      }
  94  }