Index: modules/filter/filter.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/filter/filter.test,v
retrieving revision 1.33
diff -u -r1.33 filter.test
--- modules/filter/filter.test	25 Aug 2009 10:35:32 -0000	1.33
+++ modules/filter/filter.test	26 Aug 2009 05:22:03 -0000
@@ -746,3 +746,73 @@
     return $this->assertTrue(strpos(strtolower(decode_entities($haystack)), $needle) === FALSE, $message, $group);
   }
 }
+
+/**
+ * Tests for filter hook invocation.
+ */
+class FilterHooksTestCase extends DrupalWebTestCase {
+  function getInfo() {
+    return array(
+      'name' => 'Filter format hooks',
+      'description' => 'Test hooks for text formats insert/update/delete.',
+      'group' => 'Filter'
+    );
+  }
+
+  function setUp() {
+    parent::setUp('block', 'filter_test');
+    $admin_user = $this->drupalCreateUser(array('administer filters', 'administer blocks'));
+    $this->drupalLogin($admin_user);  	
+  }
+
+  /**
+   * Test that hooks are run correctly on creating, editing and deleting a text format.
+   */
+  function testFilterHooks() {
+    // Add a text format.
+    $name = $this->randomName();
+    $edit = array();
+    $edit['name'] = $name; 
+    $edit['roles[1]'] = TRUE;
+    $this->drupalPost('admin/settings/formats/add', $edit, t('Save configuration'));
+    $this->assertRaw(t('Added text format %format.', array('%format' => $name)), t('New format created.'));
+    $this->assertText(t('hook_filter_format_insert invoked.'), t('hook_filter_format_insert invoked.'));
+    
+    $format = db_query("SELECT format FROM {filter_format} WHERE name = :name", array(':name' => $name))->fetchField();
+   
+    // Update text format.
+    $edit = array();
+    $edit['roles[2]'] = TRUE;
+    $this->drupalPost('admin/settings/formats/' . $format, $edit, t('Save configuration'));
+    $this->assertRaw(t('The text format settings have been updated.'), t('Full HTML format successfully updated.'));
+    $this->assertText(t('hook_filter_format_update invoked.'), t('hook_filter_format_update invoked.'));
+    
+    // Add a new box by filling out the input form on the admin/structure/block/add page.
+    $box = array();
+    $box['info'] = $this->randomName(8);
+    $box['title'] = $this->randomName(8);
+    $box['body'] = $this->randomName(32);
+    // Use the format created.
+    $box['body_format'] = $format;
+    $this->drupalPost('admin/structure/block/add', $box, t('Save block'));
+    // Confirm that the box has been created, and then query the created bid.
+    $this->assertText(t('The block has been created.'), t('Box successfully created.'));
+    $bid = db_query("SELECT bid FROM {box} WHERE info = :info", array(':info' => $box['info']))->fetchField();
+    // Check to see if the box was created by checking that it's in the database..
+    $this->assertNotNull($bid, t('Box found in database'));
+    
+    // Delete text format.
+    $this->drupalPost('admin/settings/formats/delete/' . $format, array(), t('Delete'));
+    $this->assertRaw(t('Deleted text format %format.', array('%format' => $name)), t('Format successfully deleted.'));
+    $this->assertText(t('hook_filter_format_delete invoked.'), t('hook_filter_format_delete invoked.'));
+    
+    // Check if the deleted format was replace with the default format in a box that used it.
+    $current_format = db_select('box', 'b')
+      ->fields('b', array('format'))
+      ->condition('bid', $bid)
+      ->execute()
+      ->fetchField();
+    $default = variable_get('filter_default_format', 1);
+    $this->assertEqual($current_format, $default, t('Deleted text formar replaced with the default format.'));
+  }
+}
\ No newline at end of file
Index: modules/filter/filter.api.php
===================================================================
RCS file: /cvs/drupal/drupal/modules/filter/filter.api.php,v
retrieving revision 1.10
diff -u -r1.10 filter.api.php
--- modules/filter/filter.api.php	25 Aug 2009 10:35:32 -0000	1.10
+++ modules/filter/filter.api.php	26 Aug 2009 05:21:51 -0000
@@ -117,5 +117,64 @@
 }
 
 /**
+ * Inform other modules that a text format has been added.
+ *
+ * This hook allows you act when a text format has been inserted.
+ * 
+ * @see hook_filter_format_update().
+ * @see hook_filter_format_delete().
+ *
+ * @param $format
+ *   The format being updated.
+ *
+ */
+function hook_filter_format_insert($format) {
+  mymodule_cache_rebuild();
+}
+
+/**
+ * Inform other modules that a text format has been updated.
+ *
+ * This hook allows you act when a text format has been updated in any way,
+ * for example, when filters have been recofigured, disabled, or re-arranged
+ * within the format.
+ *
+ * @see hook_filter_format_insert().
+ * @see hook_filter_format_delete().
+ *  
+ * @param $format
+ *   The format being updated.
+ *
+ */
+function hook_filter_format_update($format) {
+  mymodule_cache_rebuild();
+} 
+
+/**
+ * Inform other modules that a text format has been deleted.
+ *
+ * This hook allows you act when a text format has been deleted.
+ * 
+ * It is recommended that you implement this hook if your module
+ * stores format references. Replace existing instances of the deleted
+ * format with the default format or other format.  
+ *
+ * @see hook_filter_format_update().
+ * @see hook_filter_format_delete().
+ *  *  
+ * @param $format
+ *   The format being deleted.
+ *
+ */
+function hook_filter_format_delete($format) {
+  $default = variable_get('filter_default_format', 1);
+  // Replace existing instances of the deleted format with the default format.
+  db_update('my_module_table')
+    ->fields(array('format' => $default))
+    ->condition('format', $format)
+    ->execute();
+} 
+
+/**
  * @} End of "addtogroup hooks".
  */
Index: modules/filter/filter.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/filter/filter.module,v
retrieving revision 1.280
diff -u -r1.280 filter.module
--- modules/filter/filter.module	26 Aug 2009 04:59:26 -0000	1.280
+++ modules/filter/filter.module	26 Aug 2009 05:21:58 -0000
@@ -194,6 +194,13 @@
   $query->execute();
 
   cache_clear_all($format->format . ':', 'cache_filter', TRUE);
+  
+  if ($status == SAVED_NEW) {
+    module_invoke_all('filter_format_insert', $format);
+  }
+  else {
+    module_invoke_all('filter_format_update', $format);
+  }
 
   return $status;
 }
Index: modules/block/block.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/block/block.module,v
retrieving revision 1.366
diff -u -r1.366 block.module
--- modules/block/block.module	24 Aug 2009 00:14:19 -0000	1.366
+++ modules/block/block.module	26 Aug 2009 05:21:31 -0000
@@ -855,3 +855,15 @@
   $variables['template_files'][] = 'block-' . $variables['block']->module;
   $variables['template_files'][] = 'block-' . $variables['block']->module . '-' . $variables['block']->delta;
 }
+
+/**
+ * Implement hook_filter_format_delete().
+ */
+function block_filter_format_delete($format) {
+  $default = variable_get('filter_default_format', 1);
+  // Replace existing instances of the deleted format with the default format.
+  db_update('box')
+    ->fields(array('format' => $default))
+    ->condition('format', $format)
+    ->execute();
+}
\ No newline at end of file
Index: modules/comment/comment.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/comment/comment.module,v
retrieving revision 1.761
diff -u -r1.761 comment.module
--- modules/comment/comment.module	25 Aug 2009 21:53:47 -0000	1.761
+++ modules/comment/comment.module	26 Aug 2009 05:21:45 -0000
@@ -2442,3 +2442,15 @@
   // Add comments to the description for admin/content.
   $items['admin/content']['description'] = "View, edit, and delete your site's content and comments.";
 }
+
+/**
+ * Implement hook_filter_format_delete().
+ */
+function comment_filter_format_delete($format) {
+  $default = variable_get('filter_default_format', 1);
+  // Replace existing instances of the deleted format with the default format.
+  db_update('comment')
+    ->fields(array('format' => $default))
+    ->condition('format', $format)
+    ->execute();
+}
\ No newline at end of file
Index: modules/simpletest/tests/filter_test.module
===================================================================
RCS file: modules/simpletest/tests/filter_test.module
diff -N modules/simpletest/tests/filter_test.module
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ modules/simpletest/tests/filter_test.module	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,28 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Test module for Filter hooks and functions not used in core.
+ */
+
+/**
+ * Implement hook_filter_format_insert().
+ */
+function filter_test_filter_format_insert($format) {
+  drupal_set_message(t('hook_filter_format_insert invoked.'));
+}
+
+/**
+ * Implement hook_filter_format_update().
+ */
+function filter_test_filter_format_update($format) {
+  drupal_set_message(t('hook_filter_format_update invoked.'));
+}
+
+/**
+ * Implement hook_filter_format_delete().
+ */
+function filter_test_filter_format_delete($format) {
+  drupal_set_message(t('hook_filter_format_delete invoked.'));
+}
Index: modules/simpletest/tests/filter_test.info
===================================================================
RCS file: modules/simpletest/tests/filter_test.info
diff -N modules/simpletest/tests/filter_test.info
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ modules/simpletest/tests/filter_test.info	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,8 @@
+; $Id$
+name = "Filter test module"
+description = "Tests filter hooks and functions."
+package = Testing
+version = VERSION
+core = 7.x
+files[] = filter_test.module
+hidden = TRUE
