comparison core/modules/image/src/Tests/ImageDimensionsTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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
177 // Add a crop effect.
178 $effect = [
179 'id' => 'image_crop',
180 'data' => [
181 'width' => 30,
182 'height' => 30,
183 'anchor' => 'center-center',
184 ],
185 'weight' => 6,
186 ];
187
188 $style->addImageEffect($effect);
189 $style->save();
190 $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="30" height="30" alt="" class="image-style-test" />');
191 $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
192 $this->drupalGet($this->getAbsoluteUrl($url));
193 $this->assertResponse(200, 'Image was generated at the URL.');
194 $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
195 $image_file = $image_factory->get($generated_uri);
196 $this->assertEqual($image_file->getWidth(), 30);
197 $this->assertEqual($image_file->getHeight(), 30);
198
199 // Rotate to a non-multiple of 90 degrees.
200 $effect = [
201 'id' => 'image_rotate',
202 'data' => [
203 'degrees' => 57,
204 'random' => FALSE,
205 ],
206 'weight' => 7,
207 ];
208
209 $effect_id = $style->addImageEffect($effect);
210 $style->save();
211 $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="41" height="41" alt="" class="image-style-test" />');
212 $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
213 $this->drupalGet($this->getAbsoluteUrl($url));
214 $this->assertResponse(200, 'Image was generated at the URL.');
215 $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
216 $image_file = $image_factory->get($generated_uri);
217 $this->assertEqual($image_file->getWidth(), 41);
218 $this->assertEqual($image_file->getHeight(), 41);
219
220 $effect_plugin = $style->getEffect($effect_id);
221 $style->deleteImageEffect($effect_plugin);
222
223 // Ensure that an effect can unset dimensions.
224 $effect = [
225 'id' => 'image_module_test_null',
226 'data' => [],
227 'weight' => 8,
228 ];
229
230 $style->addImageEffect($effect);
231 $style->save();
232 $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" alt="" class="image-style-test" />');
233
234 // Test URI dependent image effect.
235 $style = ImageStyle::create(['name' => 'test_uri', 'label' => 'Test URI']);
236 $effect = [
237 'id' => 'image_module_test_uri_dependent',
238 'data' => [],
239 'weight' => 0,
240 ];
241 $style->addImageEffect($effect);
242 $style->save();
243 $variables = [
244 '#theme' => 'image_style',
245 '#style_name' => 'test_uri',
246 '#uri' => $original_uri,
247 '#width' => 40,
248 '#height' => 20,
249 ];
250 // PNG original image. Should be resized to 100x100.
251 $generated_uri = 'public://styles/test_uri/public/' . \Drupal::service('file_system')->basename($original_uri);
252 $url = file_url_transform_relative($style->buildUrl($original_uri));
253 $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="100" height="100" alt="" class="image-style-test-uri" />');
254 $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
255 $this->drupalGet($this->getAbsoluteUrl($url));
256 $this->assertResponse(200, 'Image was generated at the URL.');
257 $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
258 $image_file = $image_factory->get($generated_uri);
259 $this->assertEqual($image_file->getWidth(), 100);
260 $this->assertEqual($image_file->getHeight(), 100);
261 // GIF original image. Should be resized to 50x50.
262 $file = $files[1];
263 $original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
264 $generated_uri = 'public://styles/test_uri/public/' . \Drupal::service('file_system')->basename($original_uri);
265 $url = file_url_transform_relative($style->buildUrl($original_uri));
266 $variables['#uri'] = $original_uri;
267 $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="50" height="50" alt="" class="image-style-test-uri" />');
268 $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
269 $this->drupalGet($this->getAbsoluteUrl($url));
270 $this->assertResponse(200, 'Image was generated at the URL.');
271 $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
272 $image_file = $image_factory->get($generated_uri);
273 $this->assertEqual($image_file->getWidth(), 50);
274 $this->assertEqual($image_file->getHeight(), 50);
275 }
276
277 /**
278 * Render an image style element.
279 *
280 * drupal_render() alters the passed $variables array by adding a new key
281 * '#printed' => TRUE. This prevents next call to re-render the element. We
282 * wrap drupal_render() in a helper protected method and pass each time a
283 * fresh array so that $variables won't get altered and the element is
284 * re-rendered each time.
285 */
286 protected function getImageTag($variables) {
287 return str_replace("\n", NULL, \Drupal::service('renderer')->renderRoot($variables));
288 }
289
290 }