Commit 993117df authored by webchick's avatar webchick

#713872 by eojthebrave, andypost: Fixed Image field formatters not updated...

#713872 by eojthebrave, andypost: Fixed Image field formatters not updated when image style deleted and replacement format chosen.
parent 894e3572
...@@ -353,6 +353,47 @@ function image_image_default_styles() { ...@@ -353,6 +353,47 @@ function image_image_default_styles() {
return $styles; return $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. * Clear cached versions of a specific file in all styles.
* *
...@@ -461,6 +502,7 @@ function image_style_load($name = NULL, $isid = NULL, $include = NULL) { ...@@ -461,6 +502,7 @@ function image_style_load($name = NULL, $isid = NULL, $include = NULL) {
if (!isset($name) && isset($isid)) { if (!isset($name) && isset($isid)) {
foreach ($styles as $name => $database_style) { foreach ($styles as $name => $database_style) {
if (isset($database_style['isid']) && $database_style['isid'] == $isid) { if (isset($database_style['isid']) && $database_style['isid'] == $isid) {
$style = $database_style;
break; break;
} }
} }
......
...@@ -27,6 +27,83 @@ ...@@ -27,6 +27,83 @@
* image_filter_keyword() * image_filter_keyword()
*/ */
/**
* 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. * Tests the functions for generating paths and URLs for image styles.
*/ */
...@@ -246,7 +323,7 @@ class ImageEffectsUnitTest extends ImageToolkitTestCase { ...@@ -246,7 +323,7 @@ class ImageEffectsUnitTest extends ImageToolkitTestCase {
/** /**
* Tests creation, deletion, and editing of image styles and effects. * Tests creation, deletion, and editing of image styles and effects.
*/ */
class ImageAdminStylesUnitTest extends DrupalWebTestCase { class ImageAdminStylesUnitTest extends ImageFieldTestCase {
public static function getInfo() { public static function getInfo() {
return array( return array(
...@@ -256,17 +333,6 @@ class ImageAdminStylesUnitTest extends DrupalWebTestCase { ...@@ -256,17 +333,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. * Given an image style, generate an image.
*/ */
...@@ -503,82 +569,51 @@ class ImageAdminStylesUnitTest extends DrupalWebTestCase { ...@@ -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->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->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. * Test deleting a style and choosing a replacement style.
*/ */
class ImageFieldTestCase extends DrupalWebTestCase { function testStyleReplacement() {
protected $admin_user; // 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;
function setUp() { // Create an image field that uses the new style.
parent::setUp('image'); $field_name = strtolower($this->randomName(10));
$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')); $instance = $this->createImageField($field_name, 'article');
$this->drupalLogin($this->admin_user); $instance['display']['full']['type'] = 'image__' . $style_name;
} field_update_instance($instance);
/** // Create a new node with an image attached.
* Create a new image field. $test_image = current($this->drupalGetTestFiles('image'));
* $nid = $this->uploadNodeImage($test_image, $field_name, 'article');
* @param $name $node = node_load($nid);
* 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( // Test that image is displayed using newly created style.
'field_name' => $field['field_name'], $this->drupalGet('node/' . $nid);
'entity_type' => 'node', $this->assertRaw(image_style_url($style_name, $node->{$field_name}[LANGUAGE_NONE][0]['uri']), t('Image displayed using style @style.', array('@style' => $style_name)));
'label' => $name,
'bundle' => $type_name, // Rename the style and make sure the image field is updated.
'required' => !empty($instance_settings['required']), $new_style_name = strtolower($this->randomName(10));
'settings' => array(), $edit = array(
'widget' => array( 'name' => $new_style_name,
'type' => 'image_image',
'settings' => array(),
),
); );
$instance['settings'] = array_merge($instance['settings'], $instance_settings); $this->drupalPost('admin/config/media/image-styles/edit/' . $style_name, $edit, t('Update style'));
$instance['widget']['settings'] = array_merge($instance['widget']['settings'], $widget_settings); $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)));
return field_create_instance($instance); $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.'));
/** // Delete the style and choose a 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) {
$edit = array( $edit = array(
'title' => $this->randomName(), 'replacement' => 'thumbnail',
); );
$edit['files[' . $field_name . '_' . LANGUAGE_NONE . '_0]'] = realpath($image->uri); $this->drupalPost('admin/config/media/image-styles/delete/' . $new_style_name, $edit, t('Delete'));
$this->drupalPost('node/add/' . $type, $edit, t('Save')); $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. $this->drupalGet('node/' . $nid);
$matches = array(); $this->assertRaw(image_style_url('thumbnail', $node->{$field_name}[LANGUAGE_NONE][0]['uri']), t('Image displayed using style replacement style.'));
preg_match('/node\/([0-9]+)/', $this->getUrl(), $matches);
return isset($matches[1]) ? $matches[1] : FALSE;
} }
} }
......
; $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
<?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.
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment