3 namespace Drupal\image\Tests;
5 use Drupal\file\Entity\File;
6 use Drupal\simpletest\WebTestBase;
7 use Drupal\image\Entity\ImageStyle;
10 * Tests the file move function for images and image styles.
14 class FileMoveTest extends WebTestBase {
21 public static $modules = ['image'];
24 * Tests moving a randomly generated image.
26 public function testNormal() {
27 // Pick a file for testing.
28 $file = File::create((array) current($this->drupalGetTestFiles('image')));
30 // Create derivative image.
31 $styles = ImageStyle::loadMultiple();
32 $style = reset($styles);
33 $original_uri = $file->getFileUri();
34 $derivative_uri = $style->buildUri($original_uri);
35 $style->createDerivative($original_uri, $derivative_uri);
37 // Check if derivative image exists.
38 $this->assertTrue(file_exists($derivative_uri), 'Make sure derivative image is generated successfully.');
40 // Clone the object so we don't have to worry about the function changing
41 // our reference copy.
42 $desired_filepath = 'public://' . $this->randomMachineName();
43 $result = file_move(clone $file, $desired_filepath, FILE_EXISTS_ERROR);
45 // Check if image has been moved.
46 $this->assertTrue(file_exists($result->getFileUri()), 'Make sure image is moved successfully.');
48 // Check if derivative image has been flushed.
49 $this->assertFalse(file_exists($derivative_uri), 'Make sure derivative image has been flushed.');