You are here

function ImageAdminStylesTest::testStyleReplacement in Zircon Profile 8.0

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

Test deleting a style and choosing a replacement style.

File

core/modules/image/src/Tests/ImageAdminStylesTest.php, line 281
Contains \Drupal\image\Tests\ImageAdminStylesTest.

Class

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

Namespace

Drupal\image\Tests

Code

function testStyleReplacement() {

  // Create a new style.
  $style_name = strtolower($this
    ->randomMachineName(10));
  $style_label = $this
    ->randomString();
  $style = entity_create('image_style', array(
    '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');
  entity_get_display('node', 'article', 'default')
    ->setComponent($field_name, array(
    'type' => 'image',
    'settings' => array(
      '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.
  $this
    ->drupalGet('node/' . $nid);
  $this
    ->assertRaw($style
    ->buildUrl($original_uri), format_string('Image displayed using style @style.', array(
    '@style' => $style_name,
  )));

  // Rename the style and make sure the image field is updated.
  $new_style_name = strtolower($this
    ->randomMachineName(10));
  $new_style_label = $this
    ->randomString();
  $edit = array(
    'name' => $new_style_name,
    'label' => $new_style_label,
  );
  $this
    ->drupalPostForm($style_path . $style_name, $edit, t('Update style'));
  $this
    ->assertText(t('Changes to the style have been saved.'), format_string('Style %name was renamed to %new_name.', array(
    '%name' => $style_name,
    '%new_name' => $new_style_name,
  )));
  $this
    ->drupalGet('node/' . $nid);

  // Reload the image style using the new name.
  $style = ImageStyle::load($new_style_name);
  $this
    ->assertRaw($style
    ->buildUrl($original_uri), 'Image displayed using style replacement style.');

  // Delete the style and choose a replacement style.
  $edit = array(
    'replacement' => 'thumbnail',
  );
  $this
    ->drupalPostForm($style_path . $new_style_name . '/delete', $edit, t('Delete'));
  $message = t('The image style %name has been deleted.', array(
    '%name' => $new_style_label,
  ));
  $this
    ->assertRaw($message);
  $replacement_style = ImageStyle::load('thumbnail');
  $this
    ->drupalGet('node/' . $nid);
  $this
    ->assertRaw($replacement_style
    ->buildUrl($original_uri), 'Image displayed using style replacement style.');
}