3 namespace Drupal\image\Tests;
5 use Drupal\image\Entity\ImageStyle;
8 * Tests flushing of image styles.
12 class ImageStyleFlushTest extends ImageFieldTestBase {
15 * Given an image style and a wrapper, generate an image.
17 public function createSampleImage($style, $wrapper) {
21 $files = $this->drupalGetTestFiles('image');
22 $file = reset($files);
25 // Make sure we have an image in our wrapper testing file directory.
26 $source_uri = file_unmanaged_copy($file->uri, $wrapper . '://');
27 // Build the derivative image.
28 $derivative_uri = $style->buildUri($source_uri);
29 $derivative = $style->createDerivative($source_uri, $derivative_uri);
31 return $derivative ? $derivative_uri : FALSE;
35 * Count the number of images currently created for a style in a wrapper.
37 public function getImageCount($style, $wrapper) {
38 return count(file_scan_directory($wrapper . '://styles/' . $style->id(), '/.*/'));
42 * General test to flush a style.
44 public function testFlush() {
46 // Setup a style to be created and effects to add to it.
47 $style_name = strtolower($this->randomMachineName(10));
48 $style_label = $this->randomString();
49 $style_path = 'admin/config/media/image-styles/manage/' . $style_name;
53 'data[height]' => 101,
57 'data[height]' => 111,
64 'name' => $style_name,
65 'label' => $style_label,
67 $this->drupalPostForm('admin/config/media/image-styles/add', $edit, t('Create new style'));
69 // Add each sample effect to the style.
70 foreach ($effect_edits as $effect => $edit) {
72 $this->drupalPostForm($style_path, ['new' => $effect], t('Add'));
74 $this->drupalPostForm(NULL, $edit, t('Add effect'));
78 // Load the saved image style.
79 $style = ImageStyle::load($style_name);
81 // Create an image for the 'public' wrapper.
82 $image_path = $this->createSampleImage($style, 'public');
83 // Expecting to find 2 images, one is the sample.png image shown in
84 // image style preview.
85 $this->assertEqual($this->getImageCount($style, 'public'), 2, format_string('Image style %style image %file successfully generated.', ['%style' => $style->label(), '%file' => $image_path]));
87 // Create an image for the 'private' wrapper.
88 $image_path = $this->createSampleImage($style, 'private');
89 $this->assertEqual($this->getImageCount($style, 'private'), 1, format_string('Image style %style image %file successfully generated.', ['%style' => $style->label(), '%file' => $image_path]));
91 // Remove the 'image_scale' effect and updates the style, which in turn
92 // forces an image style flush.
93 $style_path = 'admin/config/media/image-styles/manage/' . $style->id();
95 foreach ($style->getEffects() as $uuid => $effect) {
96 $uuids[$effect->getPluginId()] = $uuid;
98 $this->drupalPostForm($style_path . '/effects/' . $uuids['image_scale'] . '/delete', [], t('Delete'));
99 $this->assertResponse(200);
100 $this->drupalPostForm($style_path, [], t('Update style'));
101 $this->assertResponse(200);
103 // Post flush, expected 1 image in the 'public' wrapper (sample.png).
104 $this->assertEqual($this->getImageCount($style, 'public'), 1, format_string('Image style %style flushed correctly for %wrapper wrapper.', ['%style' => $style->label(), '%wrapper' => 'public']));
106 // Post flush, expected no image in the 'private' wrapper.
107 $this->assertEqual($this->getImageCount($style, 'private'), 0, format_string('Image style %style flushed correctly for %wrapper wrapper.', ['%style' => $style->label(), '%wrapper' => 'private']));