annotate core/modules/image/tests/src/Functional/ImageAdminStylesTest.php @ 17:129ea1e6d783

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