Index: modules/image/image.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/image/image.module,v
retrieving revision 1.34
diff -u -p -r1.34 image.module
--- modules/image/image.module	6 Mar 2010 06:31:24 -0000	1.34
+++ modules/image/image.module	18 Mar 2010 15:20:36 -0000
@@ -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, 
   if (!isset($name) && isset($isid)) {
     foreach ($styles as $name => $database_style) {
       if (isset($database_style['isid']) && $database_style['isid'] == $isid) {
+        $style = $database_style;
         break;
       }
     }
Index: modules/image/image.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/image/image.test,v
retrieving revision 1.15
diff -u -p -r1.15 image.test
--- modules/image/image.test	27 Feb 2010 07:52:03 -0000	1.15
+++ modules/image/image.test	18 Mar 2010 15:20:36 -0000
@@ -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'],
+      'object_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->randomString(),
+    );
+    $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 Image
 /**
  * Tests creation, deletion, and editing of image styles and effects.
  */
-class ImageAdminStylesUnitTest extends DrupalWebTestCase {
+class ImageAdminStylesUnitTest extends ImageFieldTestCase {
 
   function getInfo() {
     return array(
@@ -257,17 +334,6 @@ class ImageAdminStylesUnitTest extends D
   }
 
   /**
-   * 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 D
     $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'],
-      'object_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 replacment style.
     $edit = array(
-      'title' => $this->randomString(),
+      '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.'));
   }
 }
 
