Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\image\Tests;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Utility\SafeMarkup;
|
Chris@0
|
6 use Drupal\Core\Entity\Entity\EntityViewDisplay;
|
Chris@0
|
7 use Drupal\image\Entity\ImageStyle;
|
Chris@0
|
8 use Drupal\image\ImageStyleInterface;
|
Chris@0
|
9 use Drupal\node\Entity\Node;
|
Chris@0
|
10 use Drupal\file\Entity\File;
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * Tests creation, deletion, and editing of image styles and effects.
|
Chris@0
|
14 *
|
Chris@0
|
15 * @group image
|
Chris@0
|
16 */
|
Chris@0
|
17 class ImageAdminStylesTest extends ImageFieldTestBase {
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * Given an image style, generate an image.
|
Chris@0
|
21 */
|
Chris@0
|
22 public function createSampleImage(ImageStyleInterface $style) {
|
Chris@0
|
23 static $file_path;
|
Chris@0
|
24
|
Chris@0
|
25 // First, we need to make sure we have an image in our testing
|
Chris@0
|
26 // file directory. Copy over an image on the first run.
|
Chris@0
|
27 if (!isset($file_path)) {
|
Chris@0
|
28 $files = $this->drupalGetTestFiles('image');
|
Chris@0
|
29 $file = reset($files);
|
Chris@0
|
30 $file_path = file_unmanaged_copy($file->uri);
|
Chris@0
|
31 }
|
Chris@0
|
32
|
Chris@0
|
33 return $style->buildUrl($file_path) ? $file_path : FALSE;
|
Chris@0
|
34 }
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * Count the number of images currently create for a style.
|
Chris@0
|
38 */
|
Chris@0
|
39 public function getImageCount(ImageStyleInterface $style) {
|
Chris@0
|
40 return count(file_scan_directory('public://styles/' . $style->id(), '/.*/'));
|
Chris@0
|
41 }
|
Chris@0
|
42
|
Chris@0
|
43 /**
|
Chris@0
|
44 * Test creating an image style with a numeric name and ensuring it can be
|
Chris@0
|
45 * applied to an image.
|
Chris@0
|
46 */
|
Chris@0
|
47 public function testNumericStyleName() {
|
Chris@0
|
48 $style_name = rand();
|
Chris@0
|
49 $style_label = $this->randomString();
|
Chris@0
|
50 $edit = [
|
Chris@0
|
51 'name' => $style_name,
|
Chris@0
|
52 'label' => $style_label,
|
Chris@0
|
53 ];
|
Chris@0
|
54 $this->drupalPostForm('admin/config/media/image-styles/add', $edit, t('Create new style'));
|
Chris@0
|
55 $this->assertRaw(t('Style %name was created.', ['%name' => $style_label]));
|
Chris@0
|
56 $options = image_style_options();
|
Chris@0
|
57 $this->assertTrue(array_key_exists($style_name, $options), format_string('Array key %key exists.', ['%key' => $style_name]));
|
Chris@0
|
58 }
|
Chris@0
|
59
|
Chris@0
|
60 /**
|
Chris@0
|
61 * General test to add a style, add/remove/edit effects to it, then delete it.
|
Chris@0
|
62 */
|
Chris@0
|
63 public function testStyle() {
|
Chris@0
|
64 $admin_path = 'admin/config/media/image-styles';
|
Chris@0
|
65
|
Chris@0
|
66 // Setup a style to be created and effects to add to it.
|
Chris@0
|
67 $style_name = strtolower($this->randomMachineName(10));
|
Chris@0
|
68 $style_label = $this->randomString();
|
Chris@0
|
69 $style_path = $admin_path . '/manage/' . $style_name;
|
Chris@0
|
70 $effect_edits = [
|
Chris@0
|
71 'image_resize' => [
|
Chris@0
|
72 'width' => 100,
|
Chris@0
|
73 'height' => 101,
|
Chris@0
|
74 ],
|
Chris@0
|
75 'image_scale' => [
|
Chris@0
|
76 'width' => 110,
|
Chris@0
|
77 'height' => 111,
|
Chris@0
|
78 'upscale' => 1,
|
Chris@0
|
79 ],
|
Chris@0
|
80 'image_scale_and_crop' => [
|
Chris@0
|
81 'width' => 120,
|
Chris@0
|
82 'height' => 121,
|
Chris@0
|
83 ],
|
Chris@0
|
84 'image_crop' => [
|
Chris@0
|
85 'width' => 130,
|
Chris@0
|
86 'height' => 131,
|
Chris@0
|
87 'anchor' => 'left-top',
|
Chris@0
|
88 ],
|
Chris@0
|
89 'image_desaturate' => [
|
Chris@0
|
90 // No options for desaturate.
|
Chris@0
|
91 ],
|
Chris@0
|
92 'image_rotate' => [
|
Chris@0
|
93 'degrees' => 5,
|
Chris@0
|
94 'random' => 1,
|
Chris@0
|
95 'bgcolor' => '#FFFF00',
|
Chris@0
|
96 ],
|
Chris@0
|
97 ];
|
Chris@0
|
98
|
Chris@0
|
99 // Add style form.
|
Chris@0
|
100
|
Chris@0
|
101 $edit = [
|
Chris@0
|
102 'name' => $style_name,
|
Chris@0
|
103 'label' => $style_label,
|
Chris@0
|
104 ];
|
Chris@0
|
105 $this->drupalPostForm($admin_path . '/add', $edit, t('Create new style'));
|
Chris@0
|
106 $this->assertRaw(t('Style %name was created.', ['%name' => $style_label]));
|
Chris@0
|
107
|
Chris@0
|
108 // Ensure that the expected entity operations are there.
|
Chris@0
|
109 $this->drupalGet($admin_path);
|
Chris@0
|
110 $this->assertLinkByHref($style_path);
|
Chris@0
|
111 $this->assertLinkByHref($style_path . '/flush');
|
Chris@0
|
112 $this->assertLinkByHref($style_path . '/delete');
|
Chris@0
|
113
|
Chris@0
|
114 // Add effect form.
|
Chris@0
|
115
|
Chris@0
|
116 // Add each sample effect to the style.
|
Chris@0
|
117 foreach ($effect_edits as $effect => $edit) {
|
Chris@0
|
118 $edit_data = [];
|
Chris@0
|
119 foreach ($edit as $field => $value) {
|
Chris@0
|
120 $edit_data['data[' . $field . ']'] = $value;
|
Chris@0
|
121 }
|
Chris@0
|
122 // Add the effect.
|
Chris@0
|
123 $this->drupalPostForm($style_path, ['new' => $effect], t('Add'));
|
Chris@0
|
124 if (!empty($edit)) {
|
Chris@0
|
125 $this->drupalPostForm(NULL, $edit_data, t('Add effect'));
|
Chris@0
|
126 }
|
Chris@0
|
127 }
|
Chris@0
|
128
|
Chris@0
|
129 // Load the saved image style.
|
Chris@0
|
130 $style = ImageStyle::load($style_name);
|
Chris@0
|
131
|
Chris@0
|
132 // Ensure that third party settings were added to the config entity.
|
Chris@0
|
133 // These are added by a hook_image_style_presave() implemented in
|
Chris@0
|
134 // image_module_test module.
|
Chris@0
|
135 $this->assertEqual('bar', $style->getThirdPartySetting('image_module_test', 'foo'), 'Third party settings were added to the image style.');
|
Chris@0
|
136
|
Chris@0
|
137 // Ensure that the image style URI matches our expected path.
|
Chris@0
|
138 $style_uri_path = $style->url();
|
Chris@0
|
139 $this->assertTrue(strpos($style_uri_path, $style_path) !== FALSE, 'The image style URI is correct.');
|
Chris@0
|
140
|
Chris@0
|
141 // Confirm that all effects on the image style have settings that match
|
Chris@0
|
142 // what was saved.
|
Chris@0
|
143 $uuids = [];
|
Chris@0
|
144 foreach ($style->getEffects() as $uuid => $effect) {
|
Chris@0
|
145 // Store the uuid for later use.
|
Chris@0
|
146 $uuids[$effect->getPluginId()] = $uuid;
|
Chris@0
|
147 $effect_configuration = $effect->getConfiguration();
|
Chris@0
|
148 foreach ($effect_edits[$effect->getPluginId()] as $field => $value) {
|
Chris@0
|
149 $this->assertEqual($value, $effect_configuration['data'][$field], SafeMarkup::format('The %field field in the %effect effect has the correct value of %value.', ['%field' => $field, '%effect' => $effect->getPluginId(), '%value' => $value]));
|
Chris@0
|
150 }
|
Chris@0
|
151 }
|
Chris@0
|
152
|
Chris@0
|
153 // Assert that every effect was saved.
|
Chris@0
|
154 foreach (array_keys($effect_edits) as $effect_name) {
|
Chris@0
|
155 $this->assertTrue(isset($uuids[$effect_name]), format_string(
|
Chris@0
|
156 'A %effect_name effect was saved with ID %uuid',
|
Chris@0
|
157 [
|
Chris@0
|
158 '%effect_name' => $effect_name,
|
Chris@0
|
159 '%uuid' => $uuids[$effect_name],
|
Chris@0
|
160 ]));
|
Chris@0
|
161 }
|
Chris@0
|
162
|
Chris@0
|
163 // Image style overview form (ordering and renaming).
|
Chris@0
|
164
|
Chris@0
|
165 // Confirm the order of effects is maintained according to the order we
|
Chris@0
|
166 // added the fields.
|
Chris@0
|
167 $effect_edits_order = array_keys($effect_edits);
|
Chris@0
|
168 $order_correct = TRUE;
|
Chris@0
|
169 $index = 0;
|
Chris@0
|
170 foreach ($style->getEffects() as $effect) {
|
Chris@0
|
171 if ($effect_edits_order[$index] != $effect->getPluginId()) {
|
Chris@0
|
172 $order_correct = FALSE;
|
Chris@0
|
173 }
|
Chris@0
|
174 $index++;
|
Chris@0
|
175 }
|
Chris@0
|
176 $this->assertTrue($order_correct, 'The order of the effects is correctly set by default.');
|
Chris@0
|
177
|
Chris@0
|
178 // Test the style overview form.
|
Chris@0
|
179 // Change the name of the style and adjust the weights of effects.
|
Chris@0
|
180 $style_name = strtolower($this->randomMachineName(10));
|
Chris@0
|
181 $style_label = $this->randomMachineName();
|
Chris@0
|
182 $weight = count($effect_edits);
|
Chris@0
|
183 $edit = [
|
Chris@0
|
184 'name' => $style_name,
|
Chris@0
|
185 'label' => $style_label,
|
Chris@0
|
186 ];
|
Chris@0
|
187 foreach ($style->getEffects() as $uuid => $effect) {
|
Chris@0
|
188 $edit['effects[' . $uuid . '][weight]'] = $weight;
|
Chris@0
|
189 $weight--;
|
Chris@0
|
190 }
|
Chris@0
|
191
|
Chris@0
|
192 // Create an image to make sure it gets flushed after saving.
|
Chris@0
|
193 $image_path = $this->createSampleImage($style);
|
Chris@0
|
194 $this->assertEqual($this->getImageCount($style), 1, format_string('Image style %style image %file successfully generated.', ['%style' => $style->label(), '%file' => $image_path]));
|
Chris@0
|
195
|
Chris@0
|
196 $this->drupalPostForm($style_path, $edit, t('Update style'));
|
Chris@0
|
197
|
Chris@0
|
198 // Note that after changing the style name, the style path is changed.
|
Chris@0
|
199 $style_path = 'admin/config/media/image-styles/manage/' . $style_name;
|
Chris@0
|
200
|
Chris@0
|
201 // Check that the URL was updated.
|
Chris@0
|
202 $this->drupalGet($style_path);
|
Chris@0
|
203 $this->assertTitle(t('Edit style @name | Drupal', ['@name' => $style_label]));
|
Chris@0
|
204 $this->assertResponse(200, format_string('Image style %original renamed to %new', ['%original' => $style->id(), '%new' => $style_name]));
|
Chris@0
|
205
|
Chris@0
|
206 // Check that the available image effects are properly sorted.
|
Chris@0
|
207 $option = $this->xpath('//select[@id=:id]//option', [':id' => 'edit-new--2']);
|
Chris@0
|
208 $this->assertTrue($option[1] == 'Ajax test', '"Ajax test" is the first selectable effect.');
|
Chris@0
|
209
|
Chris@0
|
210 // Check that the image was flushed after updating the style.
|
Chris@0
|
211 // This is especially important when renaming the style. Make sure that
|
Chris@0
|
212 // the old image directory has been deleted.
|
Chris@0
|
213 $this->assertEqual($this->getImageCount($style), 0, format_string('Image style %style was flushed after renaming the style and updating the order of effects.', ['%style' => $style->label()]));
|
Chris@0
|
214
|
Chris@0
|
215 // Load the style by the new name with the new weights.
|
Chris@0
|
216 $style = ImageStyle::load($style_name);
|
Chris@0
|
217
|
Chris@0
|
218 // Confirm the new style order was saved.
|
Chris@0
|
219 $effect_edits_order = array_reverse($effect_edits_order);
|
Chris@0
|
220 $order_correct = TRUE;
|
Chris@0
|
221 $index = 0;
|
Chris@0
|
222 foreach ($style->getEffects() as $effect) {
|
Chris@0
|
223 if ($effect_edits_order[$index] != $effect->getPluginId()) {
|
Chris@0
|
224 $order_correct = FALSE;
|
Chris@0
|
225 }
|
Chris@0
|
226 $index++;
|
Chris@0
|
227 }
|
Chris@0
|
228 $this->assertTrue($order_correct, 'The order of the effects is correctly set by default.');
|
Chris@0
|
229
|
Chris@0
|
230 // Image effect deletion form.
|
Chris@0
|
231
|
Chris@0
|
232 // Create an image to make sure it gets flushed after deleting an effect.
|
Chris@0
|
233 $image_path = $this->createSampleImage($style);
|
Chris@0
|
234 $this->assertEqual($this->getImageCount($style), 1, format_string('Image style %style image %file successfully generated.', ['%style' => $style->label(), '%file' => $image_path]));
|
Chris@0
|
235
|
Chris@0
|
236 // Delete the 'image_crop' effect from the style.
|
Chris@0
|
237 $this->drupalPostForm($style_path . '/effects/' . $uuids['image_crop'] . '/delete', [], t('Delete'));
|
Chris@0
|
238 // Confirm that the form submission was successful.
|
Chris@0
|
239 $this->assertResponse(200);
|
Chris@0
|
240 $image_crop_effect = $style->getEffect($uuids['image_crop']);
|
Chris@0
|
241 $this->assertRaw(t('The image effect %name has been deleted.', ['%name' => $image_crop_effect->label()]));
|
Chris@0
|
242 // Confirm that there is no longer a link to the effect.
|
Chris@0
|
243 $this->assertNoLinkByHref($style_path . '/effects/' . $uuids['image_crop'] . '/delete');
|
Chris@0
|
244 // Refresh the image style information and verify that the effect was
|
Chris@0
|
245 // actually deleted.
|
Chris@0
|
246 $entity_type_manager = $this->container->get('entity_type.manager');
|
Chris@0
|
247 $style = $entity_type_manager->getStorage('image_style')->loadUnchanged($style->id());
|
Chris@0
|
248 $this->assertFalse($style->getEffects()->has($uuids['image_crop']), format_string(
|
Chris@0
|
249 'Effect with ID %uuid no longer found on image style %style',
|
Chris@0
|
250 [
|
Chris@0
|
251 '%uuid' => $uuids['image_crop'],
|
Chris@0
|
252 '%style' => $style->label(),
|
Chris@0
|
253 ]));
|
Chris@0
|
254
|
Chris@0
|
255 // Additional test on Rotate effect, for transparent background.
|
Chris@0
|
256 $edit = [
|
Chris@0
|
257 'data[degrees]' => 5,
|
Chris@0
|
258 'data[random]' => 0,
|
Chris@0
|
259 'data[bgcolor]' => '',
|
Chris@0
|
260 ];
|
Chris@0
|
261 $this->drupalPostForm($style_path, ['new' => 'image_rotate'], t('Add'));
|
Chris@0
|
262 $this->drupalPostForm(NULL, $edit, t('Add effect'));
|
Chris@0
|
263 $entity_type_manager = $this->container->get('entity_type.manager');
|
Chris@0
|
264 $style = $entity_type_manager->getStorage('image_style')->loadUnchanged($style_name);
|
Chris@0
|
265 $this->assertEqual(count($style->getEffects()), 6, 'Rotate effect with transparent background was added.');
|
Chris@0
|
266
|
Chris@0
|
267 // Style deletion form.
|
Chris@0
|
268
|
Chris@0
|
269 // Delete the style.
|
Chris@0
|
270 $this->drupalPostForm($style_path . '/delete', [], t('Delete'));
|
Chris@0
|
271
|
Chris@0
|
272 // Confirm the style directory has been removed.
|
Chris@0
|
273 $directory = file_default_scheme() . '://styles/' . $style_name;
|
Chris@0
|
274 $this->assertFalse(is_dir($directory), format_string('Image style %style directory removed on style deletion.', ['%style' => $style->label()]));
|
Chris@0
|
275
|
Chris@0
|
276 $this->assertFalse(ImageStyle::load($style_name), format_string('Image style %style successfully deleted.', ['%style' => $style->label()]));
|
Chris@0
|
277
|
Chris@0
|
278 // Test empty text when there are no image styles.
|
Chris@0
|
279
|
Chris@0
|
280 // Delete all image styles.
|
Chris@0
|
281 foreach (ImageStyle::loadMultiple() as $image_style) {
|
Chris@0
|
282 $image_style->delete();
|
Chris@0
|
283 }
|
Chris@0
|
284
|
Chris@0
|
285 // Confirm that the empty text is correct on the image styles page.
|
Chris@0
|
286 $this->drupalGet($admin_path);
|
Chris@0
|
287 $this->assertRaw(t('There are currently no styles. <a href=":url">Add a new one</a>.', [
|
Chris@0
|
288 ':url' => \Drupal::url('image.style_add'),
|
Chris@0
|
289 ]));
|
Chris@0
|
290
|
Chris@0
|
291 }
|
Chris@0
|
292
|
Chris@0
|
293 /**
|
Chris@0
|
294 * Tests editing Ajax-enabled image effect forms.
|
Chris@0
|
295 */
|
Chris@0
|
296 public function testAjaxEnabledEffectForm() {
|
Chris@0
|
297 $admin_path = 'admin/config/media/image-styles';
|
Chris@0
|
298
|
Chris@0
|
299 // Setup a style to be created and effects to add to it.
|
Chris@0
|
300 $style_name = strtolower($this->randomMachineName(10));
|
Chris@0
|
301 $style_label = $this->randomString();
|
Chris@0
|
302 $style_path = $admin_path . '/manage/' . $style_name;
|
Chris@0
|
303 $effect_edit = [
|
Chris@0
|
304 'data[test_parameter]' => 100,
|
Chris@0
|
305 ];
|
Chris@0
|
306
|
Chris@0
|
307 // Add style form.
|
Chris@0
|
308 $edit = [
|
Chris@0
|
309 'name' => $style_name,
|
Chris@0
|
310 'label' => $style_label,
|
Chris@0
|
311 ];
|
Chris@0
|
312 $this->drupalPostForm($admin_path . '/add', $edit, t('Create new style'));
|
Chris@0
|
313 $this->assertRaw(t('Style %name was created.', ['%name' => $style_label]));
|
Chris@0
|
314
|
Chris@0
|
315 // Add two Ajax-enabled test effects.
|
Chris@0
|
316 $this->drupalPostForm($style_path, ['new' => 'image_module_test_ajax'], t('Add'));
|
Chris@0
|
317 $this->drupalPostForm(NULL, $effect_edit, t('Add effect'));
|
Chris@0
|
318 $this->drupalPostForm($style_path, ['new' => 'image_module_test_ajax'], t('Add'));
|
Chris@0
|
319 $this->drupalPostForm(NULL, $effect_edit, t('Add effect'));
|
Chris@0
|
320
|
Chris@0
|
321 // Load the saved image style.
|
Chris@0
|
322 $style = ImageStyle::load($style_name);
|
Chris@0
|
323
|
Chris@0
|
324 // Edit back the effects.
|
Chris@0
|
325 foreach ($style->getEffects() as $uuid => $effect) {
|
Chris@0
|
326 $effect_path = $admin_path . '/manage/' . $style_name . '/effects/' . $uuid;
|
Chris@0
|
327 $this->drupalGet($effect_path);
|
Chris@0
|
328 $this->drupalPostAjaxForm(NULL, $effect_edit, ['op' => t('Ajax refresh')]);
|
Chris@0
|
329 $this->drupalPostForm(NULL, $effect_edit, t('Update effect'));
|
Chris@0
|
330 }
|
Chris@0
|
331
|
Chris@0
|
332 }
|
Chris@0
|
333
|
Chris@0
|
334 /**
|
Chris@0
|
335 * Test deleting a style and choosing a replacement style.
|
Chris@0
|
336 */
|
Chris@0
|
337 public function testStyleReplacement() {
|
Chris@0
|
338 // Create a new style.
|
Chris@0
|
339 $style_name = strtolower($this->randomMachineName(10));
|
Chris@0
|
340 $style_label = $this->randomString();
|
Chris@0
|
341 $style = ImageStyle::create(['name' => $style_name, 'label' => $style_label]);
|
Chris@0
|
342 $style->save();
|
Chris@0
|
343 $style_path = 'admin/config/media/image-styles/manage/';
|
Chris@0
|
344
|
Chris@0
|
345 // Create an image field that uses the new style.
|
Chris@0
|
346 $field_name = strtolower($this->randomMachineName(10));
|
Chris@0
|
347 $this->createImageField($field_name, 'article');
|
Chris@0
|
348 entity_get_display('node', 'article', 'default')
|
Chris@0
|
349 ->setComponent($field_name, [
|
Chris@0
|
350 'type' => 'image',
|
Chris@0
|
351 'settings' => ['image_style' => $style_name],
|
Chris@0
|
352 ])
|
Chris@0
|
353 ->save();
|
Chris@0
|
354
|
Chris@0
|
355 // Create a new node with an image attached.
|
Chris@0
|
356 $test_image = current($this->drupalGetTestFiles('image'));
|
Chris@0
|
357 $nid = $this->uploadNodeImage($test_image, $field_name, 'article', $this->randomMachineName());
|
Chris@0
|
358 $node = Node::load($nid);
|
Chris@0
|
359
|
Chris@0
|
360 // Get node field original image URI.
|
Chris@0
|
361 $fid = $node->get($field_name)->target_id;
|
Chris@0
|
362 $original_uri = File::load($fid)->getFileUri();
|
Chris@0
|
363
|
Chris@0
|
364 // Test that image is displayed using newly created style.
|
Chris@0
|
365 $this->drupalGet('node/' . $nid);
|
Chris@0
|
366 $this->assertRaw(file_url_transform_relative($style->buildUrl($original_uri)), format_string('Image displayed using style @style.', ['@style' => $style_name]));
|
Chris@0
|
367
|
Chris@0
|
368 // Rename the style and make sure the image field is updated.
|
Chris@0
|
369 $new_style_name = strtolower($this->randomMachineName(10));
|
Chris@0
|
370 $new_style_label = $this->randomString();
|
Chris@0
|
371 $edit = [
|
Chris@0
|
372 'name' => $new_style_name,
|
Chris@0
|
373 'label' => $new_style_label,
|
Chris@0
|
374 ];
|
Chris@0
|
375 $this->drupalPostForm($style_path . $style_name, $edit, t('Update style'));
|
Chris@0
|
376 $this->assertText(t('Changes to the style have been saved.'), format_string('Style %name was renamed to %new_name.', ['%name' => $style_name, '%new_name' => $new_style_name]));
|
Chris@0
|
377 $this->drupalGet('node/' . $nid);
|
Chris@0
|
378
|
Chris@0
|
379 // Reload the image style using the new name.
|
Chris@0
|
380 $style = ImageStyle::load($new_style_name);
|
Chris@0
|
381 $this->assertRaw(file_url_transform_relative($style->buildUrl($original_uri)), 'Image displayed using style replacement style.');
|
Chris@0
|
382
|
Chris@0
|
383 // Delete the style and choose a replacement style.
|
Chris@0
|
384 $edit = [
|
Chris@0
|
385 'replacement' => 'thumbnail',
|
Chris@0
|
386 ];
|
Chris@0
|
387 $this->drupalPostForm($style_path . $new_style_name . '/delete', $edit, t('Delete'));
|
Chris@0
|
388 $message = t('The image style %name has been deleted.', ['%name' => $new_style_label]);
|
Chris@0
|
389 $this->assertRaw($message);
|
Chris@0
|
390
|
Chris@0
|
391 $replacement_style = ImageStyle::load('thumbnail');
|
Chris@0
|
392 $this->drupalGet('node/' . $nid);
|
Chris@0
|
393 $this->assertRaw(file_url_transform_relative($replacement_style->buildUrl($original_uri)), 'Image displayed using style replacement style.');
|
Chris@0
|
394 }
|
Chris@0
|
395
|
Chris@0
|
396 /**
|
Chris@0
|
397 * Verifies that editing an image effect does not cause it to be duplicated.
|
Chris@0
|
398 */
|
Chris@0
|
399 public function testEditEffect() {
|
Chris@0
|
400 // Add a scale effect.
|
Chris@0
|
401 $style_name = 'test_style_effect_edit';
|
Chris@0
|
402 $this->drupalGet('admin/config/media/image-styles/add');
|
Chris@0
|
403 $this->drupalPostForm(NULL, ['label' => 'Test style effect edit', 'name' => $style_name], t('Create new style'));
|
Chris@0
|
404 $this->drupalPostForm(NULL, ['new' => 'image_scale_and_crop'], t('Add'));
|
Chris@0
|
405 $this->drupalPostForm(NULL, ['data[width]' => '300', 'data[height]' => '200'], t('Add effect'));
|
Chris@0
|
406 $this->assertText(t('Scale and crop 300×200'));
|
Chris@0
|
407
|
Chris@0
|
408 // There should normally be only one edit link on this page initially.
|
Chris@0
|
409 $this->clickLink(t('Edit'));
|
Chris@0
|
410 $this->drupalPostForm(NULL, ['data[width]' => '360', 'data[height]' => '240'], t('Update effect'));
|
Chris@0
|
411 $this->assertText(t('Scale and crop 360×240'));
|
Chris@0
|
412
|
Chris@0
|
413 // Check that the previous effect is replaced.
|
Chris@0
|
414 $this->assertNoText(t('Scale and crop 300×200'));
|
Chris@0
|
415
|
Chris@0
|
416 // Add another scale effect.
|
Chris@0
|
417 $this->drupalGet('admin/config/media/image-styles/add');
|
Chris@0
|
418 $this->drupalPostForm(NULL, ['label' => 'Test style scale edit scale', 'name' => 'test_style_scale_edit_scale'], t('Create new style'));
|
Chris@0
|
419 $this->drupalPostForm(NULL, ['new' => 'image_scale'], t('Add'));
|
Chris@0
|
420 $this->drupalPostForm(NULL, ['data[width]' => '12', 'data[height]' => '19'], t('Add effect'));
|
Chris@0
|
421
|
Chris@0
|
422 // Edit the scale effect that was just added.
|
Chris@0
|
423 $this->clickLink(t('Edit'));
|
Chris@0
|
424 $this->drupalPostForm(NULL, ['data[width]' => '24', 'data[height]' => '19'], t('Update effect'));
|
Chris@0
|
425 $this->drupalPostForm(NULL, ['new' => 'image_scale'], t('Add'));
|
Chris@0
|
426
|
Chris@0
|
427 // Add another scale effect and make sure both exist.
|
Chris@0
|
428 $this->drupalPostForm(NULL, ['data[width]' => '12', 'data[height]' => '19'], t('Add effect'));
|
Chris@0
|
429 $this->assertText(t('Scale 24×19'));
|
Chris@0
|
430 $this->assertText(t('Scale 12×19'));
|
Chris@0
|
431
|
Chris@0
|
432 // Try to edit a nonexistent effect.
|
Chris@0
|
433 $uuid = $this->container->get('uuid');
|
Chris@0
|
434 $this->drupalGet('admin/config/media/image-styles/manage/' . $style_name . '/effects/' . $uuid->generate());
|
Chris@0
|
435 $this->assertResponse(404);
|
Chris@0
|
436 }
|
Chris@0
|
437
|
Chris@0
|
438 /**
|
Chris@0
|
439 * Test flush user interface.
|
Chris@0
|
440 */
|
Chris@0
|
441 public function testFlushUserInterface() {
|
Chris@0
|
442 $admin_path = 'admin/config/media/image-styles';
|
Chris@0
|
443
|
Chris@0
|
444 // Create a new style.
|
Chris@0
|
445 $style_name = strtolower($this->randomMachineName(10));
|
Chris@0
|
446 $style = ImageStyle::create(['name' => $style_name, 'label' => $this->randomString()]);
|
Chris@0
|
447 $style->save();
|
Chris@0
|
448
|
Chris@0
|
449 // Create an image to make sure it gets flushed.
|
Chris@0
|
450 $files = $this->drupalGetTestFiles('image');
|
Chris@0
|
451 $image_uri = $files[0]->uri;
|
Chris@0
|
452 $derivative_uri = $style->buildUri($image_uri);
|
Chris@0
|
453 $this->assertTrue($style->createDerivative($image_uri, $derivative_uri));
|
Chris@0
|
454 $this->assertEqual($this->getImageCount($style), 1);
|
Chris@0
|
455
|
Chris@0
|
456 // Go to image styles list page and check if the flush operation link
|
Chris@0
|
457 // exists.
|
Chris@0
|
458 $this->drupalGet($admin_path);
|
Chris@0
|
459 $flush_path = $admin_path . '/manage/' . $style_name . '/flush';
|
Chris@0
|
460 $this->assertLinkByHref($flush_path);
|
Chris@0
|
461
|
Chris@0
|
462 // Flush the image style derivatives using the user interface.
|
Chris@0
|
463 $this->drupalPostForm($flush_path, [], t('Flush'));
|
Chris@0
|
464
|
Chris@0
|
465 // The derivative image file should have been deleted.
|
Chris@0
|
466 $this->assertEqual($this->getImageCount($style), 0);
|
Chris@0
|
467 }
|
Chris@0
|
468
|
Chris@0
|
469 /**
|
Chris@0
|
470 * Tests image style configuration import that does a delete.
|
Chris@0
|
471 */
|
Chris@0
|
472 public function testConfigImport() {
|
Chris@0
|
473 // Create a new style.
|
Chris@0
|
474 $style_name = strtolower($this->randomMachineName(10));
|
Chris@0
|
475 $style_label = $this->randomString();
|
Chris@0
|
476 $style = ImageStyle::create(['name' => $style_name, 'label' => $style_label]);
|
Chris@0
|
477 $style->save();
|
Chris@0
|
478
|
Chris@0
|
479 // Create an image field that uses the new style.
|
Chris@0
|
480 $field_name = strtolower($this->randomMachineName(10));
|
Chris@0
|
481 $this->createImageField($field_name, 'article');
|
Chris@0
|
482 entity_get_display('node', 'article', 'default')
|
Chris@0
|
483 ->setComponent($field_name, [
|
Chris@0
|
484 'type' => 'image',
|
Chris@0
|
485 'settings' => ['image_style' => $style_name],
|
Chris@0
|
486 ])
|
Chris@0
|
487 ->save();
|
Chris@0
|
488
|
Chris@0
|
489 // Create a new node with an image attached.
|
Chris@0
|
490 $test_image = current($this->drupalGetTestFiles('image'));
|
Chris@0
|
491 $nid = $this->uploadNodeImage($test_image, $field_name, 'article', $this->randomMachineName());
|
Chris@0
|
492 $node = Node::load($nid);
|
Chris@0
|
493
|
Chris@0
|
494 // Get node field original image URI.
|
Chris@0
|
495 $fid = $node->get($field_name)->target_id;
|
Chris@0
|
496 $original_uri = File::load($fid)->getFileUri();
|
Chris@0
|
497
|
Chris@0
|
498 // Test that image is displayed using newly created style.
|
Chris@0
|
499 $this->drupalGet('node/' . $nid);
|
Chris@0
|
500 $this->assertRaw(file_url_transform_relative($style->buildUrl($original_uri)), format_string('Image displayed using style @style.', ['@style' => $style_name]));
|
Chris@0
|
501
|
Chris@0
|
502 // Copy config to sync, and delete the image style.
|
Chris@0
|
503 $sync = $this->container->get('config.storage.sync');
|
Chris@0
|
504 $active = $this->container->get('config.storage');
|
Chris@0
|
505 // Remove the image field from the display, to avoid a dependency error
|
Chris@0
|
506 // during import.
|
Chris@0
|
507 EntityViewDisplay::load('node.article.default')
|
Chris@0
|
508 ->removeComponent($field_name)
|
Chris@0
|
509 ->save();
|
Chris@0
|
510 $this->copyConfig($active, $sync);
|
Chris@0
|
511 $sync->delete('image.style.' . $style_name);
|
Chris@0
|
512 $this->configImporter()->import();
|
Chris@0
|
513
|
Chris@0
|
514 $this->assertFalse(ImageStyle::load($style_name), 'Style deleted after config import.');
|
Chris@0
|
515 $this->assertEqual($this->getImageCount($style), 0, 'Image style was flushed after being deleted by config import.');
|
Chris@0
|
516 }
|
Chris@0
|
517
|
Chris@0
|
518 /**
|
Chris@0
|
519 * Tests access for the image style listing.
|
Chris@0
|
520 */
|
Chris@0
|
521 public function testImageStyleAccess() {
|
Chris@0
|
522 $style = ImageStyle::create(['name' => 'style_foo', 'label' => $this->randomString()]);
|
Chris@0
|
523 $style->save();
|
Chris@0
|
524
|
Chris@0
|
525 $this->drupalGet('admin/config/media/image-styles');
|
Chris@0
|
526 $this->clickLink(t('Edit'));
|
Chris@0
|
527 $this->assertRaw(t('Select a new effect'));
|
Chris@0
|
528 }
|
Chris@0
|
529
|
Chris@0
|
530 }
|