function FileMoveTest::testNormal

Same name and namespace in other branches
  1. 11.x core/modules/image/tests/src/Kernel/FileMoveTest.php \Drupal\Tests\image\Kernel\FileMoveTest::testNormal()

Tests moving a randomly generated image.

File

core/modules/image/tests/src/Kernel/FileMoveTest.php, line 63

Class

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

Namespace

Drupal\Tests\image\Kernel

Code

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);
}

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