d9ce27f75d3049b1a61d2dcaf6706311eac9f9ba
[yaffs-website] / web / core / modules / image / src / Tests / FileMoveTest.php
1 <?php
2
3 namespace Drupal\image\Tests;
4
5 use Drupal\file\Entity\File;
6 use Drupal\simpletest\WebTestBase;
7 use Drupal\image\Entity\ImageStyle;
8
9 /**
10  * Tests the file move function for images and image styles.
11  *
12  * @group image
13  */
14 class FileMoveTest extends WebTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = ['image'];
22
23   /**
24    * Tests moving a randomly generated image.
25    */
26   public function testNormal() {
27     // Pick a file for testing.
28     $file = File::create((array) current($this->drupalGetTestFiles('image')));
29
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);
36
37     // Check if derivative image exists.
38     $this->assertTrue(file_exists($derivative_uri), 'Make sure derivative image is generated successfully.');
39
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);
44
45     // Check if image has been moved.
46     $this->assertTrue(file_exists($result->getFileUri()), 'Make sure image is moved successfully.');
47
48     // Check if derivative image has been flushed.
49     $this->assertFalse(file_exists($derivative_uri), 'Make sure derivative image has been flushed.');
50   }
51
52 }