Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / image / tests / src / Functional / ImageStyleFlushTest.php
1 <?php
2
3 namespace Drupal\Tests\image\Functional;
4
5 use Drupal\image\Entity\ImageStyle;
6 use Drupal\Tests\TestFileCreationTrait;
7
8 /**
9  * Tests flushing of image styles.
10  *
11  * @group image
12  */
13 class ImageStyleFlushTest extends ImageFieldTestBase {
14
15   use TestFileCreationTrait {
16     getTestFiles as drupalGetTestFiles;
17     compareFiles as drupalCompareFiles;
18   }
19
20   /**
21    * Given an image style and a wrapper, generate an image.
22    */
23   public function createSampleImage($style, $wrapper) {
24     static $file;
25
26     if (!isset($file)) {
27       $files = $this->drupalGetTestFiles('image');
28       $file = reset($files);
29     }
30
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);
36
37     return $derivative ? $derivative_uri : FALSE;
38   }
39
40   /**
41    * Count the number of images currently created for a style in a wrapper.
42    */
43   public function getImageCount($style, $wrapper) {
44     return count(file_scan_directory($wrapper . '://styles/' . $style->id(), '/.*/'));
45   }
46
47   /**
48    * General test to flush a style.
49    */
50   public function testFlush() {
51
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;
56     $effect_edits = [
57       'image_resize' => [
58         'data[width]' => 100,
59         'data[height]' => 101,
60       ],
61       'image_scale' => [
62         'data[width]' => 110,
63         'data[height]' => 111,
64         'data[upscale]' => 1,
65       ],
66     ];
67
68     // Add style form.
69     $edit = [
70       'name' => $style_name,
71       'label' => $style_label,
72     ];
73     $this->drupalPostForm('admin/config/media/image-styles/add', $edit, t('Create new style'));
74
75     // Add each sample effect to the style.
76     foreach ($effect_edits as $effect => $edit) {
77       // Add the effect.
78       $this->drupalPostForm($style_path, ['new' => $effect], t('Add'));
79       if (!empty($edit)) {
80         $this->drupalPostForm(NULL, $edit, t('Add effect'));
81       }
82     }
83
84     // Load the saved image style.
85     $style = ImageStyle::load($style_name);
86
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]));
92
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]));
96
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();
100     $uuids = [];
101     foreach ($style->getEffects() as $uuid => $effect) {
102       $uuids[$effect->getPluginId()] = $uuid;
103     }
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);
108
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']));
111
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']));
114   }
115
116 }