1 image.test public ImageAdminStylesUnitTest::testDefaultStyle()

Test to override, edit, then revert a style.

File

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

Class

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

Code

public function testDefaultStyle() {
  // Setup a style to be created and effects to add to it.
  $style_name = 'thumbnail';
  $style_label = 'Thumbnail (100x100)';
  $edit_path = 'admin/config/media/image-styles/configure/' . $style_name;
  $delete_path = 'admin/config/media/image-styles/delete/' . $style_name;
  $revert_path = 'admin/config/media/image-styles/revert/' . $style_name;

  // Ensure deleting a default is not possible.
  $this->backdropGet($delete_path);
  $this->assertText(t('Page not found'), 'Default styles may not be deleted.');

  // Ensure that editing a default name is not possible.
  $this->backdropGet($edit_path);
  $disabled_field = $this->xpath('//input[@id=:id and @disabled="disabled"]', array(':id' => 'edit-name'));
  $this->assertTrue($disabled_field, 'Default styles may not be renamed.');
  $this->assertField('edit-actions-submit', 'Default styles may be edited.');
  $this->assertField('edit-add', 'Default styles may have new effects added.');

  // Create an image to make sure the default works before overriding.
  backdrop_static_reset('image_styles');
  $style = image_style_load($style_name);
  $image_path = $this->createSampleImage($style);
  $this->assertEqual($this->getImageCount($style), 1, format_string('Image style %style image %file successfully generated.', array('%style' => $style['name'], '%file' => $image_path)));

  // Add sample effect to the overridden style.
  $this->backdropPost($edit_path, array('new' => 'image_desaturate'), t('Add'));
  backdrop_static_reset('image_styles');
  $style = image_style_load($style_name);

  // Verify that effects attached to the style have an ieid now.
  foreach ($style['effects'] as $effect) {
    $this->assertTrue(isset($effect['ieid']), format_string('The %effect effect has an ieid.', array('%effect' => $effect['name'])));
  }

  // The style should now have 2 effect, the original scale provided by core
  // and the desaturate effect we added in the override.
  $effects = array_values($style['effects']);
  $this->assertEqual($effects[0]['name'], 'image_scale', 'The default effect still exists in the overridden style.');
  $this->assertEqual($effects[1]['name'], 'image_desaturate', 'The added effect exists in the overridden style.');

  // Check that we are able to rename an overridden style.
  $this->backdropGet($edit_path);
  $disabled_field = $this->xpath('//input[@id=:id and @disabled="disabled"]', array(':id' => 'edit-name'));
  $this->assertTrue($disabled_field, 'Overridden styles may not be renamed.');

  // Create an image to ensure the override works properly.
  $image_path = $this->createSampleImage($style);
  $this->assertEqual($this->getImageCount($style), 1, format_string('Image style %style image %file successfully generated.', array('%style' => $style['label'], '%file' => $image_path)));

  // Revert the image style.
  $this->backdropPost($revert_path, array(), t('Revert'));
  backdrop_static_reset('image_styles');
  $style = image_style_load($style_name);

  // The style should now have the single effect for scale.
  $effects = array_values($style['effects']);
  $this->assertEqual($effects[0]['name'], 'image_scale', 'The default effect still exists in the reverted style.');
  $this->assertFalse(array_key_exists(1, $effects), 'The added effect has been removed in the reverted style.');
}