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