136b0f1f15d672e9982f30ae46878f92ccf900e5
[yaffs-website] / web / core / modules / image / src / Tests / ImageDimensionsTest.php
1 <?php
2
3 namespace Drupal\image\Tests;
4
5 use Drupal\image\Entity\ImageStyle;
6 use Drupal\simpletest\WebTestBase;
7
8 /**
9  * Tests that images have correct dimensions when styled.
10  *
11  * @group image
12  */
13 class ImageDimensionsTest extends WebTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['image', 'image_module_test'];
21
22   protected $profile = 'testing';
23
24   /**
25    * Test styled image dimensions cumulatively.
26    */
27   public function testImageDimensions() {
28     $image_factory = $this->container->get('image.factory');
29     // Create a working copy of the file.
30     $files = $this->drupalGetTestFiles('image');
31     $file = reset($files);
32     $original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
33
34     // Create a style.
35     /** @var $style \Drupal\image\ImageStyleInterface */
36     $style = ImageStyle::create(['name' => 'test', 'label' => 'Test']);
37     $style->save();
38     $generated_uri = 'public://styles/test/public/' . \Drupal::service('file_system')->basename($original_uri);
39     $url = file_url_transform_relative($style->buildUrl($original_uri));
40
41     $variables = [
42       '#theme' => 'image_style',
43       '#style_name' => 'test',
44       '#uri' => $original_uri,
45       '#width' => 40,
46       '#height' => 20,
47     ];
48     // Verify that the original image matches the hard-coded values.
49     $image_file = $image_factory->get($original_uri);
50     $this->assertEqual($image_file->getWidth(), $variables['#width']);
51     $this->assertEqual($image_file->getHeight(), $variables['#height']);
52
53     // Scale an image that is wider than it is high.
54     $effect = [
55       'id' => 'image_scale',
56       'data' => [
57         'width' => 120,
58         'height' => 90,
59         'upscale' => TRUE,
60       ],
61       'weight' => 0,
62     ];
63
64     $style->addImageEffect($effect);
65     $style->save();
66     $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="120" height="60" alt="" class="image-style-test" />');
67     $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
68     $this->drupalGet($this->getAbsoluteUrl($url));
69     $this->assertResponse(200, 'Image was generated at the URL.');
70     $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
71     $image_file = $image_factory->get($generated_uri);
72     $this->assertEqual($image_file->getWidth(), 120);
73     $this->assertEqual($image_file->getHeight(), 60);
74
75     // Rotate 90 degrees anticlockwise.
76     $effect = [
77       'id' => 'image_rotate',
78       'data' => [
79         'degrees' => -90,
80         'random' => FALSE,
81       ],
82       'weight' => 1,
83     ];
84
85     $style->addImageEffect($effect);
86     $style->save();
87     $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="60" height="120" alt="" class="image-style-test" />');
88     $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
89     $this->drupalGet($this->getAbsoluteUrl($url));
90     $this->assertResponse(200, 'Image was generated at the URL.');
91     $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
92     $image_file = $image_factory->get($generated_uri);
93     $this->assertEqual($image_file->getWidth(), 60);
94     $this->assertEqual($image_file->getHeight(), 120);
95
96     // Scale an image that is higher than it is wide (rotated by previous effect).
97     $effect = [
98       'id' => 'image_scale',
99       'data' => [
100         'width' => 120,
101         'height' => 90,
102         'upscale' => TRUE,
103       ],
104       'weight' => 2,
105     ];
106
107     $style->addImageEffect($effect);
108     $style->save();
109     $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="45" height="90" alt="" class="image-style-test" />');
110     $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
111     $this->drupalGet($this->getAbsoluteUrl($url));
112     $this->assertResponse(200, 'Image was generated at the URL.');
113     $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
114     $image_file = $image_factory->get($generated_uri);
115     $this->assertEqual($image_file->getWidth(), 45);
116     $this->assertEqual($image_file->getHeight(), 90);
117
118     // Test upscale disabled.
119     $effect = [
120       'id' => 'image_scale',
121       'data' => [
122         'width' => 400,
123         'height' => 200,
124         'upscale' => FALSE,
125       ],
126       'weight' => 3,
127     ];
128
129     $style->addImageEffect($effect);
130     $style->save();
131     $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="45" height="90" alt="" class="image-style-test" />');
132     $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
133     $this->drupalGet($this->getAbsoluteUrl($url));
134     $this->assertResponse(200, 'Image was generated at the URL.');
135     $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
136     $image_file = $image_factory->get($generated_uri);
137     $this->assertEqual($image_file->getWidth(), 45);
138     $this->assertEqual($image_file->getHeight(), 90);
139
140     // Add a desaturate effect.
141     $effect = [
142       'id' => 'image_desaturate',
143       'data' => [],
144       'weight' => 4,
145     ];
146
147     $style->addImageEffect($effect);
148     $style->save();
149     $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="45" height="90" alt="" class="image-style-test" />');
150     $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
151     $this->drupalGet($this->getAbsoluteUrl($url));
152     $this->assertResponse(200, 'Image was generated at the URL.');
153     $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
154     $image_file = $image_factory->get($generated_uri);
155     $this->assertEqual($image_file->getWidth(), 45);
156     $this->assertEqual($image_file->getHeight(), 90);
157
158     // Add a random rotate effect.
159     $effect = [
160       'id' => 'image_rotate',
161       'data' => [
162         'degrees' => 180,
163         'random' => TRUE,
164       ],
165       'weight' => 5,
166     ];
167
168     $style->addImageEffect($effect);
169     $style->save();
170     $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" alt="" class="image-style-test" />');
171     $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
172     $this->drupalGet($this->getAbsoluteUrl($url));
173     $this->assertResponse(200, 'Image was generated at the URL.');
174     $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
175
176     // Add a crop effect.
177     $effect = [
178       'id' => 'image_crop',
179       'data' => [
180         'width' => 30,
181         'height' => 30,
182         'anchor' => 'center-center',
183       ],
184       'weight' => 6,
185     ];
186
187     $style->addImageEffect($effect);
188     $style->save();
189     $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="30" height="30" alt="" class="image-style-test" />');
190     $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
191     $this->drupalGet($this->getAbsoluteUrl($url));
192     $this->assertResponse(200, 'Image was generated at the URL.');
193     $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
194     $image_file = $image_factory->get($generated_uri);
195     $this->assertEqual($image_file->getWidth(), 30);
196     $this->assertEqual($image_file->getHeight(), 30);
197
198     // Rotate to a non-multiple of 90 degrees.
199     $effect = [
200       'id' => 'image_rotate',
201       'data' => [
202         'degrees' => 57,
203         'random' => FALSE,
204       ],
205       'weight' => 7,
206     ];
207
208     $effect_id = $style->addImageEffect($effect);
209     $style->save();
210     // @todo Uncomment this once
211     //   https://www.drupal.org/project/drupal/issues/2670966 is resolved.
212     // $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="41" height="41" alt="" class="image-style-test" />');
213     $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
214     $this->drupalGet($this->getAbsoluteUrl($url));
215     $this->assertResponse(200, 'Image was generated at the URL.');
216     $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
217     $image_file = $image_factory->get($generated_uri);
218     // @todo Uncomment this once
219     //   https://www.drupal.org/project/drupal/issues/2670966 is resolved.
220     // $this->assertEqual($image_file->getWidth(), 41);
221     // $this->assertEqual($image_file->getHeight(), 41);
222
223     $effect_plugin = $style->getEffect($effect_id);
224     $style->deleteImageEffect($effect_plugin);
225
226     // Ensure that an effect can unset dimensions.
227     $effect = [
228       'id' => 'image_module_test_null',
229       'data' => [],
230       'weight' => 8,
231     ];
232
233     $style->addImageEffect($effect);
234     $style->save();
235     $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" alt="" class="image-style-test" />');
236
237     // Test URI dependent image effect.
238     $style = ImageStyle::create(['name' => 'test_uri', 'label' => 'Test URI']);
239     $effect = [
240       'id' => 'image_module_test_uri_dependent',
241       'data' => [],
242       'weight' => 0,
243     ];
244     $style->addImageEffect($effect);
245     $style->save();
246     $variables = [
247       '#theme' => 'image_style',
248       '#style_name' => 'test_uri',
249       '#uri' => $original_uri,
250       '#width' => 40,
251       '#height' => 20,
252     ];
253     // PNG original image. Should be resized to 100x100.
254     $generated_uri = 'public://styles/test_uri/public/' . \Drupal::service('file_system')->basename($original_uri);
255     $url = file_url_transform_relative($style->buildUrl($original_uri));
256     $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="100" height="100" alt="" class="image-style-test-uri" />');
257     $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
258     $this->drupalGet($this->getAbsoluteUrl($url));
259     $this->assertResponse(200, 'Image was generated at the URL.');
260     $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
261     $image_file = $image_factory->get($generated_uri);
262     $this->assertEqual($image_file->getWidth(), 100);
263     $this->assertEqual($image_file->getHeight(), 100);
264     // GIF original image. Should be resized to 50x50.
265     $file = $files[1];
266     $original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
267     $generated_uri = 'public://styles/test_uri/public/' . \Drupal::service('file_system')->basename($original_uri);
268     $url = file_url_transform_relative($style->buildUrl($original_uri));
269     $variables['#uri'] = $original_uri;
270     $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="50" height="50" alt="" class="image-style-test-uri" />');
271     $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
272     $this->drupalGet($this->getAbsoluteUrl($url));
273     $this->assertResponse(200, 'Image was generated at the URL.');
274     $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
275     $image_file = $image_factory->get($generated_uri);
276     $this->assertEqual($image_file->getWidth(), 50);
277     $this->assertEqual($image_file->getHeight(), 50);
278   }
279
280   /**
281    * Render an image style element.
282    *
283    * drupal_render() alters the passed $variables array by adding a new key
284    * '#printed' => TRUE. This prevents next call to re-render the element. We
285    * wrap drupal_render() in a helper protected method and pass each time a
286    * fresh array so that $variables won't get altered and the element is
287    * re-rendered each time.
288    */
289   protected function getImageTag($variables) {
290     return str_replace("\n", NULL, \Drupal::service('renderer')->renderRoot($variables));
291   }
292
293 }