You are here

public function ImageAdminStylesTest::testStyleReplacement in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/image/tests/src/Functional/ImageAdminStylesTest.php \Drupal\Tests\image\Functional\ImageAdminStylesTest::testStyleReplacement()
  2. 9 core/modules/image/tests/src/Functional/ImageAdminStylesTest.php \Drupal\Tests\image\Functional\ImageAdminStylesTest::testStyleReplacement()

Tests deleting a style and choosing a replacement style.

File

core/modules/image/tests/src/Functional/ImageAdminStylesTest.php, line 318

Class

ImageAdminStylesTest
Tests creation, deletion, and editing of image styles and effects.

Namespace

Drupal\Tests\image\Functional

Code

public function testStyleReplacement() {

  // Create a new style.
  $style_name = strtolower($this
    ->randomMachineName(10));
  $style_label = $this
    ->randomString();
  $style = ImageStyle::create([
    'name' => $style_name,
    'label' => $style_label,
  ]);
  $style
    ->save();
  $style_path = 'admin/config/media/image-styles/manage/';

  // Create an image field that uses the new style.
  $field_name = strtolower($this
    ->randomMachineName(10));
  $this
    ->createImageField($field_name, 'article');
  \Drupal::service('entity_display.repository')
    ->getViewDisplay('node', 'article')
    ->setComponent($field_name, [
    'type' => 'image',
    'settings' => [
      'image_style' => $style_name,
    ],
  ])
    ->save();

  // Create a new node with an image attached.
  $test_image = current($this
    ->drupalGetTestFiles('image'));
  $nid = $this
    ->uploadNodeImage($test_image, $field_name, 'article', $this
    ->randomMachineName());
  $node = Node::load($nid);

  // Get node field original image URI.
  $fid = $node
    ->get($field_name)->target_id;
  $original_uri = File::load($fid)
    ->getFileUri();

  // Test that image is displayed using newly created style.

  /** @var \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator */
  $file_url_generator = \Drupal::service('file_url_generator');
  $this
    ->drupalGet('node/' . $nid);
  $this
    ->assertSession()
    ->responseContains($file_url_generator
    ->transformRelative($style
    ->buildUrl($original_uri)));

  // Rename the style and make sure the image field is updated.
  $new_style_name = strtolower($this
    ->randomMachineName(10));
  $new_style_label = $this
    ->randomString();
  $edit = [
    'name' => $new_style_name,
    'label' => $new_style_label,
  ];
  $this
    ->drupalGet($style_path . $style_name);
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->statusMessageContains('Changes to the style have been saved.', 'status');
  $this
    ->drupalGet('node/' . $nid);

  // Reload the image style using the new name.
  $style = ImageStyle::load($new_style_name);
  $this
    ->assertSession()
    ->responseContains($file_url_generator
    ->transformRelative($style
    ->buildUrl($original_uri)));

  // Delete the style and choose a replacement style.
  $edit = [
    'replacement' => 'thumbnail',
  ];
  $this
    ->drupalGet($style_path . $new_style_name . '/delete');
  $this
    ->submitForm($edit, 'Delete');
  $this
    ->assertSession()
    ->statusMessageContains("The image style {$new_style_label} has been deleted.", 'status');
  $replacement_style = ImageStyle::load('thumbnail');
  $this
    ->drupalGet('node/' . $nid);
  $this
    ->assertSession()
    ->responseContains($file_url_generator
    ->transformRelative($replacement_style
    ->buildUrl($original_uri)));
}