summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorAngie Byron <webchick@24967.no-reply.drupal.org>2010-04-20 09:17:20 +0000
committerAngie Byron <webchick@24967.no-reply.drupal.org>2010-04-20 09:17:20 +0000
commit993117df998b0f59d459adde04555e33fac567f6 (patch)
treef70e0ba2fdcce2edc817b5594bd0d09c42d8aa4b /modules
parent894e35723c176c7d3e72b78403e217466e434d14 (diff)
downloadbrdo-993117df998b0f59d459adde04555e33fac567f6.tar.gz
brdo-993117df998b0f59d459adde04555e33fac567f6.tar.bz2
#713872 by eojthebrave, andypost: Fixed Image field formatters not updated when image style deleted and replacement format chosen.
Diffstat (limited to 'modules')
-rw-r--r--modules/image/image.module42
-rw-r--r--modules/image/image.test193
-rw-r--r--modules/node/tests/node_presave_test.info8
-rw-r--r--modules/node/tests/node_presave_test.module18
4 files changed, 182 insertions, 79 deletions
diff --git a/modules/image/image.module b/modules/image/image.module
index e2a906654..42641f816 100644
--- a/modules/image/image.module
+++ b/modules/image/image.module
@@ -354,6 +354,47 @@ function image_image_default_styles() {
}
/**
+ * Implements hook_image_style_save().
+ */
+function image_image_style_save($style) {
+ if (isset($style['old_name']) && $style['old_name'] != $style['name']) {
+ $instances = field_read_instances();
+ // Loop through all fields searching for image fields.
+ foreach ($instances as $instance) {
+ if ($instance['widget']['module'] == 'image') {
+ $instance_changed = FALSE;
+ foreach ($instance['display'] as $view_mode => $display) {
+ // Check if the formatter involves an image style.
+ $matches = array();
+ if (preg_match('/__([a-z0-9_]+)/', $display['type'], $matches)) {
+ // Update display information for any instance using the image
+ // style that was just deleted.
+ if ($style['old_name'] == $matches[1]) {
+ $instance['display'][$view_mode]['type'] = str_replace($style['old_name'], $style['name'], $display['type']);
+ $instance_changed = TRUE;
+ }
+ }
+ }
+ if ($instance['widget']['settings']['preview_image_style'] == $style['old_name']) {
+ $instance['widget']['settings']['preview_image_style'] = $style['name'];
+ $instance_changed = TRUE;
+ }
+ if ($instance_changed) {
+ field_update_instance($instance);
+ }
+ }
+ }
+ }
+}
+
+/**
+ * Implements hook_image_style_delete().
+ */
+function image_image_style_delete($style) {
+ image_image_style_save($style);
+}
+
+/**
* Clear cached versions of a specific file in all styles.
*
* @param $path
@@ -461,6 +502,7 @@ function image_style_load($name = NULL, $isid = NULL, $include = NULL) {
if (!isset($name) && isset($isid)) {
foreach ($styles as $name => $database_style) {
if (isset($database_style['isid']) && $database_style['isid'] == $isid) {
+ $style = $database_style;
break;
}
}
diff --git a/modules/image/image.test b/modules/image/image.test
index 42ec0ad4d..114cd59d5 100644
--- a/modules/image/image.test
+++ b/modules/image/image.test
@@ -28,6 +28,83 @@
*/
/**
+ * This class provides methods specifically for testing Image's field handling.
+ */
+class ImageFieldTestCase extends DrupalWebTestCase {
+ protected $admin_user;
+
+ function setUp() {
+ parent::setUp('image');
+ $this->admin_user = $this->drupalCreateUser(array('access content', 'access administration pages', 'administer site configuration', 'administer content types', 'administer nodes', 'create article content', 'edit any article content', 'delete any article content', 'administer image styles'));
+ $this->drupalLogin($this->admin_user);
+ }
+
+ /**
+ * Create a new image field.
+ *
+ * @param $name
+ * The name of the new field (all lowercase), exclude the "field_" prefix.
+ * @param $type_name
+ * The node type that this field will be added to.
+ * @param $field_settings
+ * A list of field settings that will be added to the defaults.
+ * @param $instance_settings
+ * A list of instance settings that will be added to the instance defaults.
+ * @param $widget_settings
+ * A list of widget settings that will be added to the widget defaults.
+ */
+ function createImageField($name, $type_name, $field_settings = array(), $instance_settings = array(), $widget_settings = array()) {
+ $field = array(
+ 'field_name' => $name,
+ 'type' => 'image',
+ 'settings' => array(),
+ 'cardinality' => !empty($field_settings['cardinality']) ? $field_settings['cardinality'] : 1,
+ );
+ $field['settings'] = array_merge($field['settings'], $field_settings);
+ field_create_field($field);
+
+ $instance = array(
+ 'field_name' => $field['field_name'],
+ 'entity_type' => 'node',
+ 'label' => $name,
+ 'bundle' => $type_name,
+ 'required' => !empty($instance_settings['required']),
+ 'settings' => array(),
+ 'widget' => array(
+ 'type' => 'image_image',
+ 'settings' => array(),
+ ),
+ );
+ $instance['settings'] = array_merge($instance['settings'], $instance_settings);
+ $instance['widget']['settings'] = array_merge($instance['widget']['settings'], $widget_settings);
+ return field_create_instance($instance);
+ }
+
+ /**
+ * Upload an image to a node.
+ *
+ * @param $image
+ * A file object representing the image to upload.
+ * @param $field_name
+ * Name of the image field the image should be attached to.
+ * @param $type
+ * The type of node to create.
+ */
+ function uploadNodeImage($image, $field_name, $type) {
+ $edit = array(
+ 'title' => $this->randomName(),
+ );
+ $edit['files[' . $field_name . '_' . LANGUAGE_NONE . '_0]'] = realpath($image->uri);
+ $this->drupalPost('node/add/' . $type, $edit, t('Save'));
+
+ // Retrieve ID of the newly created node from the current URL.
+ $matches = array();
+ preg_match('/node\/([0-9]+)/', $this->getUrl(), $matches);
+ return isset($matches[1]) ? $matches[1] : FALSE;
+ }
+}
+
+/**
* Tests the functions for generating paths and URLs for image styles.
*/
class ImageStylesPathAndUrlUnitTest extends DrupalWebTestCase {
@@ -246,7 +323,7 @@ class ImageEffectsUnitTest extends ImageToolkitTestCase {
/**
* Tests creation, deletion, and editing of image styles and effects.
*/
-class ImageAdminStylesUnitTest extends DrupalWebTestCase {
+class ImageAdminStylesUnitTest extends ImageFieldTestCase {
public static function getInfo() {
return array(
@@ -257,17 +334,6 @@ class ImageAdminStylesUnitTest extends DrupalWebTestCase {
}
/**
- * Implementation of setUp().
- */
- function setUp() {
- parent::setUp();
-
- // Create an administrative user.
- $this->admin_user = $this->drupalCreateUser(array('administer site configuration', 'administer image styles'));
- $this->drupalLogin($this->admin_user);
- }
-
- /**
* Given an image style, generate an image.
*/
function createSampleImage($style) {
@@ -503,82 +569,51 @@ class ImageAdminStylesUnitTest extends DrupalWebTestCase {
$this->assertEqual($effects[0]['name'], 'image_scale', t('The default effect still exists in the reverted style.'));
$this->assertFalse(array_key_exists(1, $effects), t('The added effect has been removed in the reverted style.'));
}
-}
-
-/**
- * This class provides methods specifically for testing Image's field handling.
- */
-class ImageFieldTestCase extends DrupalWebTestCase {
- protected $admin_user;
-
- function setUp() {
- parent::setUp('image');
- $this->admin_user = $this->drupalCreateUser(array('access content', 'access administration pages', 'administer site configuration', 'administer content types', 'administer nodes', 'create article content', 'edit any article content', 'delete any article content', 'administer image styles'));
- $this->drupalLogin($this->admin_user);
- }
/**
- * Create a new image field.
- *
- * @param $name
- * The name of the new field (all lowercase), exclude the "field_" prefix.
- * @param $type_name
- * The node type that this field will be added to.
- * @param $field_settings
- * A list of field settings that will be added to the defaults.
- * @param $instance_settings
- * A list of instance settings that will be added to the instance defaults.
- * @param $widget_settings
- * A list of widget settings that will be added to the widget defaults.
+ * Test deleting a style and choosing a replacement style.
*/
- function createImageField($name, $type_name, $field_settings = array(), $instance_settings = array(), $widget_settings = array()) {
- $field = array(
- 'field_name' => $name,
- 'type' => 'image',
- 'settings' => array(),
- 'cardinality' => !empty($field_settings['cardinality']) ? $field_settings['cardinality'] : 1,
- );
- $field['settings'] = array_merge($field['settings'], $field_settings);
- field_create_field($field);
+ function testStyleReplacement() {
+ // Create a new style.
+ $style_name = strtolower($this->randomName(10));
+ image_style_save(array('name' => $style_name));
+ $style_path = 'admin/config/media/image-styles/edit/' . $style_name;
- $instance = array(
- 'field_name' => $field['field_name'],
- 'entity_type' => 'node',
- 'label' => $name,
- 'bundle' => $type_name,
- 'required' => !empty($instance_settings['required']),
- 'settings' => array(),
- 'widget' => array(
- 'type' => 'image_image',
- 'settings' => array(),
- ),
+ // Create an image field that uses the new style.
+ $field_name = strtolower($this->randomName(10));
+ $instance = $this->createImageField($field_name, 'article');
+ $instance['display']['full']['type'] = 'image__' . $style_name;
+ field_update_instance($instance);
+
+ // Create a new node with an image attached.
+ $test_image = current($this->drupalGetTestFiles('image'));
+ $nid = $this->uploadNodeImage($test_image, $field_name, 'article');
+ $node = node_load($nid);
+
+ // Test that image is displayed using newly created style.
+ $this->drupalGet('node/' . $nid);
+ $this->assertRaw(image_style_url($style_name, $node->{$field_name}[LANGUAGE_NONE][0]['uri']), t('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));
+ $edit = array(
+ 'name' => $new_style_name,
);
- $instance['settings'] = array_merge($instance['settings'], $instance_settings);
- $instance['widget']['settings'] = array_merge($instance['widget']['settings'], $widget_settings);
- return field_create_instance($instance);
- }
+ $this->drupalPost('admin/config/media/image-styles/edit/' . $style_name, $edit, t('Update style'));
+ $this->assertText(t('Changes to the style have been saved.'), t('Style %name was renamed to %new_name.', array('%name' => $style_name, '%new_name' => $new_style_name)));
+ $this->drupalGet('node/' . $nid);
+ $this->assertRaw(image_style_url($new_style_name, $node->{$field_name}[LANGUAGE_NONE][0]['uri']), t('Image displayed using style replacement style.'));
- /**
- * Upload an image to a node.
- *
- * @param $image
- * A file object representing the image to upload.
- * @param $field_name
- * Name of the image field the image should be attached to.
- * @param $type
- * The type of node to create.
- */
- function uploadNodeImage($image, $field_name, $type) {
+ // Delete the style and choose a replacement style.
$edit = array(
- 'title' => $this->randomName(),
+ 'replacement' => 'thumbnail',
);
- $edit['files[' . $field_name . '_' . LANGUAGE_NONE . '_0]'] = realpath($image->uri);
- $this->drupalPost('node/add/' . $type, $edit, t('Save'));
+ $this->drupalPost('admin/config/media/image-styles/delete/' . $new_style_name, $edit, t('Delete'));
+ $message = t('Style %name was deleted.', array('%name' => $new_style_name));
+ $this->assertRaw($message, $message);
- // Retrieve ID of the newly created node from the current URL.
- $matches = array();
- preg_match('/node\/([0-9]+)/', $this->getUrl(), $matches);
- return isset($matches[1]) ? $matches[1] : FALSE;
+ $this->drupalGet('node/' . $nid);
+ $this->assertRaw(image_style_url('thumbnail', $node->{$field_name}[LANGUAGE_NONE][0]['uri']), t('Image displayed using style replacement style.'));
}
}
diff --git a/modules/node/tests/node_presave_test.info b/modules/node/tests/node_presave_test.info
new file mode 100644
index 000000000..e8a5c95ec
--- /dev/null
+++ b/modules/node/tests/node_presave_test.info
@@ -0,0 +1,8 @@
+; $Id$
+name = "Node module presave tests"
+description = "Support module for hook_node_presave testing."
+package = Testing
+version = VERSION
+core = 7.x
+files[] = node_presave_test.module
+hidden = TRUE
diff --git a/modules/node/tests/node_presave_test.module b/modules/node/tests/node_presave_test.module
new file mode 100644
index 000000000..a3256a52b
--- /dev/null
+++ b/modules/node/tests/node_presave_test.module
@@ -0,0 +1,18 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Dummy module implementing node related hooks to test API interaction with
+ * the Node module.
+ */
+
+/**
+ * Implements hook_node_presave().
+ */
+function node_presave_test_node_presave($node) {
+ if ($node->title == 'testing_node_presave') {
+ $node->created = 280299600; // Sun, 19 Nov 1978 05:00:00 GMT
+ $node->changed = 979534800; // Drupal 1.0 release.
+ }
+}