1 image.test public ImageAdminStylesUnitTest::testStyleReplacement()

Test deleting a style and choosing a replacement style.

File

core/modules/image/tests/image.test, line 691
Tests for image.module.

Class

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

Code

public function testStyleReplacement() {
  // Create a new style.
  $style_name = strtolower($this->randomName(10));
  $style_label = $this->randomName();
  image_style_save(array('name' => $style_name, 'label' => $style_label));
  $style_path = 'admin/config/media/image-styles/configure/' . $style_name;

  // Create an image field that uses the new style.
  $field_name = strtolower($this->randomName(10));
  $this->createImageField($field_name, 'post');
  $instance = field_info_instance('node', $field_name, 'post');
  $instance['display']['default']['type'] = 'image';
  $instance['display']['default']['settings']['image_style'] = $style_name;
  field_update_instance($instance);

  // Create a new node with an image attached.
  $test_image = current($this->backdropGetTestFiles('image'));
  $nid = $this->uploadNodeImage($test_image, $field_name, 'post');
  $node = node_load($nid);

  // Test that image is displayed using newly created style.
  $this->backdropGet('node/' . $nid);
  $this->assertRaw(check_plain(image_style_url($style_name, $node->{$field_name}[LANGUAGE_NONE][0]['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->randomName(10));
  $new_style_label = $this->randomName();
  $edit = array(
    'name' => $new_style_name,
    'label' => $new_style_label,
  );
  $this->backdropPost('admin/config/media/image-styles/configure/' . $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->backdropGet('node/' . $nid);
  $this->assertRaw(check_plain(image_style_url($new_style_name, $node->{$field_name}[LANGUAGE_NONE][0]['uri'])), format_string('Image displayed using style replacement style.'));

  // Delete the style and choose a replacement style.
  $edit = array(
    'replacement' => 'thumbnail',
  );
  $this->backdropPost('admin/config/media/image-styles/delete/' . $new_style_name, $edit, t('Delete'));
  $message = t('Style %label was deleted.', array('%label' => $new_style_label));
  $this->assertRaw($message, format_string('Style %label was deleted.', array('%label' => $new_style_label)));

  $this->backdropGet('node/' . $nid);
  $this->assertRaw(check_plain(image_style_url('thumbnail', $node->{$field_name}[LANGUAGE_NONE][0]['uri'])), format_string('Image displayed using style replacement style.'));
}