class FileMoveTest

Same name in this branch
  1. 9 core/tests/Drupal/KernelTests/Core/File/FileMoveTest.php \Drupal\KernelTests\Core\File\FileMoveTest
Same name and namespace in other branches
  1. 7.x modules/simpletest/tests/file.test \FileMoveTest
  2. 11.x core/modules/image/tests/src/Kernel/FileMoveTest.php \Drupal\Tests\image\Kernel\FileMoveTest
  3. 11.x core/tests/Drupal/KernelTests/Core/File/FileMoveTest.php \Drupal\KernelTests\Core\File\FileMoveTest
  4. 10 core/modules/image/tests/src/Kernel/FileMoveTest.php \Drupal\Tests\image\Kernel\FileMoveTest
  5. 10 core/tests/Drupal/KernelTests/Core/File/FileMoveTest.php \Drupal\KernelTests\Core\File\FileMoveTest
  6. 8.9.x core/modules/image/tests/src/Functional/FileMoveTest.php \Drupal\Tests\image\Functional\FileMoveTest
  7. 8.9.x core/tests/Drupal/KernelTests/Core/File/FileMoveTest.php \Drupal\KernelTests\Core\File\FileMoveTest

Tests the file move function for images and image styles.

@group image

Hierarchy

Expanded class hierarchy of FileMoveTest

File

core/modules/image/tests/src/Functional/FileMoveTest.php, line 16

Namespace

Drupal\Tests\image\Functional
View source
class FileMoveTest extends BrowserTestBase {
  use TestFileCreationTrait {
    getTestFiles as drupalGetTestFiles;
    compareFiles as drupalCompareFiles;
  }
  
  /**
   * Modules to enable.
   *
   * @var array
   */
  protected static $modules = [
    'image',
  ];
  
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';
  
  /**
   * The file repository service.
   *
   * @var \Drupal\file\FileRepository
   */
  protected $fileRepository;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->fileRepository = $this->container
      ->get('file.repository');
  }
  
  /**
   * Tests moving a randomly generated image.
   */
  public function testNormal() {
    // 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, FileSystemInterface::EXISTS_ERROR);
    // Check if image has been moved.
    $this->assertFileExists($result->getFileUri());
    // Check if derivative image has been flushed.
    $this->assertFileDoesNotExist($derivative_uri);
  }

}

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