Version 1
[yaffs-website] / web / core / modules / image / src / Tests / ImageStyleFlushTest.php
1 <?php
2
3 namespace Drupal\image\Tests;
4
5 use Drupal\image\Entity\ImageStyle;
6
7 /**
8  * Tests flushing of image styles.
9  *
10  * @group image
11  */
12 class ImageStyleFlushTest extends ImageFieldTestBase {
13
14   /**
15    * Given an image style and a wrapper, generate an image.
16    */
17   public function createSampleImage($style, $wrapper) {
18     static $file;
19
20     if (!isset($file)) {
21       $files = $this->drupalGetTestFiles('image');
22       $file = reset($files);
23     }
24
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);
30
31     return $derivative ? $derivative_uri : FALSE;
32   }
33
34   /**
35    * Count the number of images currently created for a style in a wrapper.
36    */
37   public function getImageCount($style, $wrapper) {
38     return count(file_scan_directory($wrapper . '://styles/' . $style->id(), '/.*/'));
39   }
40
41   /**
42    * General test to flush a style.
43    */
44   public function testFlush() {
45
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;
50     $effect_edits = [
51       'image_resize' => [
52         'data[width]' => 100,
53         'data[height]' => 101,
54       ],
55       'image_scale' => [
56         'data[width]' => 110,
57         'data[height]' => 111,
58         'data[upscale]' => 1,
59       ],
60     ];
61
62     // Add style form.
63     $edit = [
64       'name' => $style_name,
65       'label' => $style_label,
66     ];
67     $this->drupalPostForm('admin/config/media/image-styles/add', $edit, t('Create new style'));
68
69     // Add each sample effect to the style.
70     foreach ($effect_edits as $effect => $edit) {
71       // Add the effect.
72       $this->drupalPostForm($style_path, ['new' => $effect], t('Add'));
73       if (!empty($edit)) {
74         $this->drupalPostForm(NULL, $edit, t('Add effect'));
75       }
76     }
77
78     // Load the saved image style.
79     $style = ImageStyle::load($style_name);
80
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]));
86
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]));
90
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();
94     $uuids = [];
95     foreach ($style->getEffects() as $uuid => $effect) {
96       $uuids[$effect->getPluginId()] = $uuid;
97     }
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);
102
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']));
105
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']));
108   }
109
110 }