3 namespace Drupal\Tests\image\Functional;
5 use Drupal\image\Entity\ImageStyle;
6 use Drupal\Tests\TestFileCreationTrait;
9 * Tests flushing of image styles.
13 class ImageStyleFlushTest extends ImageFieldTestBase {
15 use TestFileCreationTrait {
16 getTestFiles as drupalGetTestFiles;
17 compareFiles as drupalCompareFiles;
21 * Given an image style and a wrapper, generate an image.
23 public function createSampleImage($style, $wrapper) {
27 $files = $this->drupalGetTestFiles('image');
28 $file = reset($files);
31 // Make sure we have an image in our wrapper testing file directory.
32 $source_uri = file_unmanaged_copy($file->uri, $wrapper . '://');
33 // Build the derivative image.
34 $derivative_uri = $style->buildUri($source_uri);
35 $derivative = $style->createDerivative($source_uri, $derivative_uri);
37 return $derivative ? $derivative_uri : FALSE;
41 * Count the number of images currently created for a style in a wrapper.
43 public function getImageCount($style, $wrapper) {
44 return count(file_scan_directory($wrapper . '://styles/' . $style->id(), '/.*/'));
48 * General test to flush a style.
50 public function testFlush() {
52 // Setup a style to be created and effects to add to it.
53 $style_name = strtolower($this->randomMachineName(10));
54 $style_label = $this->randomString();
55 $style_path = 'admin/config/media/image-styles/manage/' . $style_name;
59 'data[height]' => 101,
63 'data[height]' => 111,
70 'name' => $style_name,
71 'label' => $style_label,
73 $this->drupalPostForm('admin/config/media/image-styles/add', $edit, t('Create new style'));
75 // Add each sample effect to the style.
76 foreach ($effect_edits as $effect => $edit) {
78 $this->drupalPostForm($style_path, ['new' => $effect], t('Add'));
80 $this->drupalPostForm(NULL, $edit, t('Add effect'));
84 // Load the saved image style.
85 $style = ImageStyle::load($style_name);
87 // Create an image for the 'public' wrapper.
88 $image_path = $this->createSampleImage($style, 'public');
89 // Expecting to find 2 images, one is the sample.png image shown in
90 // image style preview.
91 $this->assertEqual($this->getImageCount($style, 'public'), 2, format_string('Image style %style image %file successfully generated.', ['%style' => $style->label(), '%file' => $image_path]));
93 // Create an image for the 'private' wrapper.
94 $image_path = $this->createSampleImage($style, 'private');
95 $this->assertEqual($this->getImageCount($style, 'private'), 1, format_string('Image style %style image %file successfully generated.', ['%style' => $style->label(), '%file' => $image_path]));
97 // Remove the 'image_scale' effect and updates the style, which in turn
98 // forces an image style flush.
99 $style_path = 'admin/config/media/image-styles/manage/' . $style->id();
101 foreach ($style->getEffects() as $uuid => $effect) {
102 $uuids[$effect->getPluginId()] = $uuid;
104 $this->drupalPostForm($style_path . '/effects/' . $uuids['image_scale'] . '/delete', [], t('Delete'));
105 $this->assertResponse(200);
106 $this->drupalPostForm($style_path, [], t('Save'));
107 $this->assertResponse(200);
109 // Post flush, expected 1 image in the 'public' wrapper (sample.png).
110 $this->assertEqual($this->getImageCount($style, 'public'), 1, format_string('Image style %style flushed correctly for %wrapper wrapper.', ['%style' => $style->label(), '%wrapper' => 'public']));
112 // Post flush, expected no image in the 'private' wrapper.
113 $this->assertEqual($this->getImageCount($style, 'private'), 0, format_string('Image style %style flushed correctly for %wrapper wrapper.', ['%style' => $style->label(), '%wrapper' => 'private']));