annotate core/modules/image/tests/src/Functional/ImageAdminStylesTest.php @ 19:fa3358dc1485 tip

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