Chris@16
|
1 <?php
|
Chris@16
|
2
|
Chris@16
|
3 namespace Drupal\Tests\image\Functional;
|
Chris@16
|
4
|
Chris@16
|
5 use Drupal\image\Entity\ImageStyle;
|
Chris@16
|
6 use Drupal\Tests\TestFileCreationTrait;
|
Chris@16
|
7
|
Chris@16
|
8 /**
|
Chris@16
|
9 * Tests flushing of image styles.
|
Chris@16
|
10 *
|
Chris@16
|
11 * @group image
|
Chris@16
|
12 */
|
Chris@16
|
13 class ImageStyleFlushTest extends ImageFieldTestBase {
|
Chris@16
|
14
|
Chris@16
|
15 use TestFileCreationTrait {
|
Chris@16
|
16 getTestFiles as drupalGetTestFiles;
|
Chris@16
|
17 compareFiles as drupalCompareFiles;
|
Chris@16
|
18 }
|
Chris@16
|
19
|
Chris@16
|
20 /**
|
Chris@16
|
21 * Given an image style and a wrapper, generate an image.
|
Chris@16
|
22 */
|
Chris@16
|
23 public function createSampleImage($style, $wrapper) {
|
Chris@16
|
24 static $file;
|
Chris@16
|
25
|
Chris@16
|
26 if (!isset($file)) {
|
Chris@16
|
27 $files = $this->drupalGetTestFiles('image');
|
Chris@16
|
28 $file = reset($files);
|
Chris@16
|
29 }
|
Chris@16
|
30
|
Chris@16
|
31 // Make sure we have an image in our wrapper testing file directory.
|
Chris@18
|
32 $source_uri = \Drupal::service('file_system')->copy($file->uri, $wrapper . '://');
|
Chris@16
|
33 // Build the derivative image.
|
Chris@16
|
34 $derivative_uri = $style->buildUri($source_uri);
|
Chris@16
|
35 $derivative = $style->createDerivative($source_uri, $derivative_uri);
|
Chris@16
|
36
|
Chris@16
|
37 return $derivative ? $derivative_uri : FALSE;
|
Chris@16
|
38 }
|
Chris@16
|
39
|
Chris@16
|
40 /**
|
Chris@16
|
41 * Count the number of images currently created for a style in a wrapper.
|
Chris@16
|
42 */
|
Chris@16
|
43 public function getImageCount($style, $wrapper) {
|
Chris@16
|
44 return count(file_scan_directory($wrapper . '://styles/' . $style->id(), '/.*/'));
|
Chris@16
|
45 }
|
Chris@16
|
46
|
Chris@16
|
47 /**
|
Chris@16
|
48 * General test to flush a style.
|
Chris@16
|
49 */
|
Chris@16
|
50 public function testFlush() {
|
Chris@16
|
51
|
Chris@16
|
52 // Setup a style to be created and effects to add to it.
|
Chris@16
|
53 $style_name = strtolower($this->randomMachineName(10));
|
Chris@16
|
54 $style_label = $this->randomString();
|
Chris@16
|
55 $style_path = 'admin/config/media/image-styles/manage/' . $style_name;
|
Chris@16
|
56 $effect_edits = [
|
Chris@16
|
57 'image_resize' => [
|
Chris@16
|
58 'data[width]' => 100,
|
Chris@16
|
59 'data[height]' => 101,
|
Chris@16
|
60 ],
|
Chris@16
|
61 'image_scale' => [
|
Chris@16
|
62 'data[width]' => 110,
|
Chris@16
|
63 'data[height]' => 111,
|
Chris@16
|
64 'data[upscale]' => 1,
|
Chris@16
|
65 ],
|
Chris@16
|
66 ];
|
Chris@16
|
67
|
Chris@16
|
68 // Add style form.
|
Chris@16
|
69 $edit = [
|
Chris@16
|
70 'name' => $style_name,
|
Chris@16
|
71 'label' => $style_label,
|
Chris@16
|
72 ];
|
Chris@16
|
73 $this->drupalPostForm('admin/config/media/image-styles/add', $edit, t('Create new style'));
|
Chris@16
|
74
|
Chris@16
|
75 // Add each sample effect to the style.
|
Chris@16
|
76 foreach ($effect_edits as $effect => $edit) {
|
Chris@16
|
77 // Add the effect.
|
Chris@16
|
78 $this->drupalPostForm($style_path, ['new' => $effect], t('Add'));
|
Chris@16
|
79 if (!empty($edit)) {
|
Chris@16
|
80 $this->drupalPostForm(NULL, $edit, t('Add effect'));
|
Chris@16
|
81 }
|
Chris@16
|
82 }
|
Chris@16
|
83
|
Chris@16
|
84 // Load the saved image style.
|
Chris@16
|
85 $style = ImageStyle::load($style_name);
|
Chris@16
|
86
|
Chris@16
|
87 // Create an image for the 'public' wrapper.
|
Chris@16
|
88 $image_path = $this->createSampleImage($style, 'public');
|
Chris@16
|
89 // Expecting to find 2 images, one is the sample.png image shown in
|
Chris@16
|
90 // image style preview.
|
Chris@16
|
91 $this->assertEqual($this->getImageCount($style, 'public'), 2, format_string('Image style %style image %file successfully generated.', ['%style' => $style->label(), '%file' => $image_path]));
|
Chris@16
|
92
|
Chris@16
|
93 // Create an image for the 'private' wrapper.
|
Chris@16
|
94 $image_path = $this->createSampleImage($style, 'private');
|
Chris@16
|
95 $this->assertEqual($this->getImageCount($style, 'private'), 1, format_string('Image style %style image %file successfully generated.', ['%style' => $style->label(), '%file' => $image_path]));
|
Chris@16
|
96
|
Chris@16
|
97 // Remove the 'image_scale' effect and updates the style, which in turn
|
Chris@16
|
98 // forces an image style flush.
|
Chris@16
|
99 $style_path = 'admin/config/media/image-styles/manage/' . $style->id();
|
Chris@16
|
100 $uuids = [];
|
Chris@16
|
101 foreach ($style->getEffects() as $uuid => $effect) {
|
Chris@16
|
102 $uuids[$effect->getPluginId()] = $uuid;
|
Chris@16
|
103 }
|
Chris@16
|
104 $this->drupalPostForm($style_path . '/effects/' . $uuids['image_scale'] . '/delete', [], t('Delete'));
|
Chris@16
|
105 $this->assertResponse(200);
|
Chris@17
|
106 $this->drupalPostForm($style_path, [], t('Save'));
|
Chris@16
|
107 $this->assertResponse(200);
|
Chris@16
|
108
|
Chris@16
|
109 // Post flush, expected 1 image in the 'public' wrapper (sample.png).
|
Chris@16
|
110 $this->assertEqual($this->getImageCount($style, 'public'), 1, format_string('Image style %style flushed correctly for %wrapper wrapper.', ['%style' => $style->label(), '%wrapper' => 'public']));
|
Chris@16
|
111
|
Chris@16
|
112 // Post flush, expected no image in the 'private' wrapper.
|
Chris@16
|
113 $this->assertEqual($this->getImageCount($style, 'private'), 0, format_string('Image style %style flushed correctly for %wrapper wrapper.', ['%style' => $style->label(), '%wrapper' => 'private']));
|
Chris@16
|
114 }
|
Chris@16
|
115
|
Chris@16
|
116 }
|