FileMoveTest.php

Same filename in this branch
  1. 11.x core/tests/Drupal/KernelTests/Core/File/FileMoveTest.php
Same filename in other branches
  1. 9 core/modules/image/tests/src/Functional/FileMoveTest.php
  2. 9 core/tests/Drupal/KernelTests/Core/File/FileMoveTest.php
  3. 8.9.x core/modules/image/tests/src/Functional/FileMoveTest.php
  4. 8.9.x core/tests/Drupal/KernelTests/Core/File/FileMoveTest.php
  5. 10 core/modules/image/tests/src/Kernel/FileMoveTest.php
  6. 10 core/tests/Drupal/KernelTests/Core/File/FileMoveTest.php

Namespace

Drupal\Tests\image\Kernel

File

core/modules/image/tests/src/Kernel/FileMoveTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\Tests\image\Kernel;

use Drupal\Core\File\FileExists;
use Drupal\file\Entity\File;
use Drupal\file\FileRepository;
use Drupal\image\Entity\ImageStyle;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\TestFileCreationTrait;

/**
 * Tests the file move function for images and image styles.
 *
 * @group image
 */
class FileMoveTest extends KernelTestBase {
    use TestFileCreationTrait {
        getTestFiles as drupalGetTestFiles;
        compareFiles as drupalCompareFiles;
    }
    
    /**
     * {@inheritdoc}
     */
    protected static $modules = [
        'file',
        'image',
        'system',
        'user',
    ];
    
    /**
     * The file repository service.
     */
    protected FileRepository $fileRepository;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $this->installConfig([
            'system',
        ]);
        $this->installEntitySchema('file');
        $this->installEntitySchema('user');
        $this->installSchema('file', [
            'file_usage',
        ]);
        ImageStyle::create([
            'name' => 'main_style',
            'label' => 'Main',
        ])->save();
        $this->fileRepository = $this->container
            ->get('file.repository');
    }
    
    /**
     * Tests moving a randomly generated image.
     */
    public function testNormal() : void {
        // Pick a file for testing.
        $file = File::create((array) current($this->drupalGetTestFiles('image')));
        // Create derivative image.
        $styles = ImageStyle::loadMultiple();
        $style = reset($styles);
        $original_uri = $file->getFileUri();
        $derivative_uri = $style->buildUri($original_uri);
        $style->createDerivative($original_uri, $derivative_uri);
        // Check if derivative image exists.
        $this->assertFileExists($derivative_uri);
        // Clone the object, so we don't have to worry about the function changing
        // our reference copy.
        $desired_filepath = 'public://' . $this->randomMachineName();
        $result = $this->fileRepository
            ->move(clone $file, $desired_filepath, FileExists::Error);
        // Check if image has been moved.
        $this->assertFileExists($result->getFileUri());
        // Check if derivative image has been flushed.
        $this->assertFileDoesNotExist($derivative_uri);
    }

}

Classes

Title Deprecated Summary
FileMoveTest Tests the file move function for images and image styles.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.