Index: modules/filter/filter.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/filter/filter.module,v
retrieving revision 1.291
diff -u -p -r1.291 filter.module
--- modules/filter/filter.module	20 Sep 2009 07:32:18 -0000	1.291
+++ modules/filter/filter.module	25 Sep 2009 21:18:39 -0000
@@ -165,9 +165,22 @@ function filter_format_load($format) {
  * Save a text format object to the database.
  *
  * @param $format
- *   A format object.
+ *   A format object using the properties:
+ *   - 'name': The title of the text format.
+ *   - 'format': (optional) The internal ID of the text format. If omitted, a
+ *     new text format is created.
+ *   - 'roles': (optional) An associative array containing the roles allowed to
+ *     access/use the text format.
+ *   - 'filters': (optional) An associative, multi-dimensional array of filters
+ *     assigned to the text format, using the properties:
+ *     - 'weight': The weight of the filter in the text format.
+ *     - 'status': A boolean indicating whether the filter is enabled in the
+ *       text format.
+ *     - 'module': The name of the module implementing the filter.
+ *     - 'settings': (optional) An array of configured settings for the filter.
+ *       See hook_filter_info() for details.
  */
-function filter_format_save($format) {
+function filter_format_save(&$format) {
   $format->name = trim($format->name);
 
   // Add a new text format.
@@ -180,13 +193,18 @@ function filter_format_save($format) {
 
   // Get the filters currently active in the format, to add new filters
   // to the bottom.
-  $current = filter_list_format($format->format);
-  $filters = $format->filters;
-  foreach ($filters as $name => $filter) {
+  $current = filter_list_format($format->format, TRUE);
+  $filter_info = filter_get_filters();
+  if (!isset($format->filters)) {
+    $format->filters = array();
+  }
+  foreach ($format->filters as $name => $filter) {
     $fields = array();
     // Add new filters to the bottom.
     $fields['weight'] = isset($current[$name]->weight) ? $current[$name]->weight : 10;
     $fields['status'] = $filter['status'];
+    $fields['module'] = $filter_info[$name]['module'];
+    $format->filters[$name]['module'] = $filter_info[$name]['module'];
     // Only update settings if there are any.
     if (!empty($filter['settings'])) {
       $fields['settings'] = serialize($filter['settings']);
@@ -568,6 +586,10 @@ function filter_get_filters() {
     foreach (module_implements('filter_info') as $module) {
       $info = module_invoke($module, 'filter_info');
       if (isset($info) && is_array($info)) {
+        // Assign the name of the module implementing the filters.
+        foreach (array_keys($info) as $name) {
+          $info[$name]['module'] = $module;
+        }
         $filters = array_merge($filters, $info);
       }
     }
@@ -648,22 +670,13 @@ function filter_list_format($format, $in
 }
 
 /**
- * @name Filtering functions
- * @{
- * Modules which need to have content filtered can use these functions to
- * interact with the filter system.
- *
- * For more info, see the hook_filter() documentation.
+ * Run all the enabled filters on a piece of text.
  *
- * Note: because filters can inject JavaScript or execute PHP code, security is
- * vital here. When a user supplies a $format, you should validate it with
+ * Note: Because filters can inject JavaScript or execute PHP code, security is
+ * vital here. When a user supplies a $format, you should validate it using
  * filter_access($format) before accepting/using it. This is normally done in
- * the validation stage of the node system. You should for example never make a
+ * the validation stage of the Form API. You should for example never make a
  * preview of content in a disallowed format.
- */
-
-/**
- * Run all the enabled filters on a piece of text.
  *
  * @param $text
  *   The text to be filtered.
@@ -813,11 +826,6 @@ function filter_access($format, $account
 }
 
 /**
- * @} End of "Filtering functions".
- */
-
-
-/**
  * Helper function for fetching filter tips.
  */
 function _filter_tips($format, $long = FALSE) {
Index: modules/filter/filter.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/filter/filter.test,v
retrieving revision 1.43
diff -u -p -r1.43 filter.test
--- modules/filter/filter.test	25 Sep 2009 14:06:09 -0000	1.43
+++ modules/filter/filter.test	25 Sep 2009 21:26:44 -0000
@@ -1,6 +1,142 @@
 <?php
 // $Id: filter.test,v 1.43 2009/09/25 14:06:09 dries Exp $
 
+/**
+ * Tests for text format and filter CRUD operations.
+ */
+class FilterCRUDTestCase extends DrupalWebTestCase {
+  public static function getInfo() {
+    return array(
+      'name' => 'Filter CRUD operations',
+      'description' => 'Test creation, loading, updating, deleting of text formats and filters.',
+      'group' => 'Filter',
+    );
+  }
+
+  /**
+   * Test CRUD operations for text formats and filters.
+   */
+  function testTextFormatCRUD() {
+    // Add a text format with minimum data only.
+    $format = new stdClass;
+    $format->name = 'Empty format';
+    filter_format_save($format);
+    $this->verifyTextFormat($format);
+    $this->verifyFilters($format);
+
+    // Add another text format specifying all possible properties.
+    $format = new stdClass;
+    $format->name = 'Custom format';
+    $format->filters = array(
+      'filter_url' => array(
+        'status' => 1,
+        'settings' => array(
+          'filter_url_length' => 30,
+        ),
+      ),
+    );
+    filter_format_save($format);
+    $this->verifyTextFormat($format);
+    $this->verifyFilters($format);
+
+    // Alter some text format properties and save again.
+    $format->name = 'Altered format';
+    $format->filters['filter_url']['status'] = 0;
+    $format->filters['filter_autop']['status'] = 1;
+    filter_format_save($format);
+    $this->verifyTextFormat($format);
+    $this->verifyFilters($format);
+
+    // Delete the text format.
+    filter_format_delete($format);
+    $db_format = db_query("SELECT * FROM {filter_format} WHERE format = :format", array(':format' => $format->format))->fetchObject();
+    $this->assertFalse($db_format, t('Database: Deleted text format no longer exists.'));
+    $db_filters = db_query("SELECT * FROM {filter} WHERE format = :format", array(':format' => $format->format))->fetchObject();
+    $this->assertFalse($db_filters, t('Database: Filters for deleted text format no longer exist.'));
+    $formats = filter_formats();
+    $this->assertTrue(!isset($formats[$format->format]), t('filter_formats: Deleted text format no longer exists.'));
+  }
+
+  /**
+   * Verify that a text format is properly stored.
+   */
+  function verifyTextFormat($format) {
+    $t_args = array('%format' => $format->name);
+    // Verify text format database record.
+    $db_format = db_select('filter_format', 'ff')
+      ->fields('ff')
+      ->condition('format', $format->format)
+      ->execute()
+      ->fetchObject();
+    $this->assertEqual($db_format->format, $format->format, t('Database: Proper format id for text format %format.', $t_args));
+    $this->assertEqual($db_format->name, $format->name, t('Database: Proper title for text format %format.', $t_args));
+    $this->assertEqual($db_format->cache, $format->cache, t('Database: Proper cache indicator for text format %format.', $t_args));
+    $this->assertEqual($db_format->weight, $format->weight, t('Database: Proper weight for text format %format.', $t_args));
+
+    // Verify filter_format_load().
+    $filter_format = filter_format_load($format->format);
+    $this->assertEqual($filter_format->format, $format->format, t('filter_format_load: Proper format id for text format %format.', $t_args));
+    $this->assertEqual($filter_format->name, $format->name, t('filter_format_load: Proper title for text format %format.', $t_args));
+    $this->assertEqual($filter_format->cache, $format->cache, t('filter_format_load: Proper cache indicator for text format %format.', $t_args));
+    $this->assertEqual($filter_format->weight, $format->weight, t('filter_format_load: Proper weight for text format %format.', $t_args));
+  }
+
+  /**
+   * Verify that filters are properly stored for a text format.
+   */
+  function verifyFilters($format) {
+    // Verify filter database records.
+    $filters = db_query("SELECT * FROM {filter} WHERE format = :format", array(':format' => $format->format))->fetchAllAssoc('name');
+    $format_filters = $format->filters;
+    foreach ($filters as $name => $filter) {
+      $t_args = array('%format' => $format->name, '%filter' => $name);
+      // Check whether the filter is contained in the saved $format.
+      if (isset($format_filters[$name])) {
+        // Verify that filter status is properly stored.
+        $this->assertEqual($filter->status, $format_filters[$name]['status'], t('Database: Proper status for %filter in text format %format.', $t_args));
+        // Verify that filter settings were properly stored.
+        $this->assertEqual(unserialize($filter->settings), isset($format_filters[$name]['settings']) ? $format_filters[$name]['settings'] : array(), t('Database: Proper filter settings for %filter in text format %format.', $t_args));
+      }
+      // Otherwise, the stored filter must be disabled.
+      else {
+        $this->assertTrue($filter->status == 0, t('Database: Proper status for disabled %filter in text format %format.', $t_args));
+      }
+      // Verify that each filter has a module name assigned.
+      $this->assertTrue(!empty($filter->module), t('Database: Proper module name for %filter in text format %format.', $t_args));
+
+      // Remove the filter from the copy of saved $format to check whether all
+      // filters have been processed later.
+      unset($format_filters[$name]);
+    }
+    $this->assertTrue(empty($format_filters), t('Database contains values for all filters in the saved format.'));
+
+    // Verify filter_list_format().
+    $filters = filter_list_format($format->format, TRUE);
+    $format_filters = $format->filters;
+    foreach ($filters as $name => $filter) {
+      $t_args = array('%format' => $format->name, '%filter' => $name);
+      // Check whether the filter is contained in the saved $format.
+      if (isset($format_filters[$name])) {
+        // Verify that filter status is properly stored.
+        $this->assertEqual($filter->status, $format_filters[$name]['status'], t('filter_list_format: Proper status for %filter in text format %format.', $t_args));
+        // Verify that filter settings were properly stored.
+        $this->assertEqual($filter->settings, isset($format_filters[$name]['settings']) ? $format_filters[$name]['settings'] : array(), t('filter_list_format: Proper filter settings for %filter in text format %format.', $t_args));
+      }
+      // Otherwise, the stored filter must be disabled.
+      else {
+        $this->assertTrue($filter->status == 0, t('filter_list_format: Proper status for disabled %filter in text format %format.', $t_args));
+      }
+      // Verify that each filter has a module name assigned.
+      $this->assertTrue(!empty($filter->module), t('filter_list_format: Proper module name for %filter in text format %format.', $t_args));
+
+      // Remove the filter from the copy of saved $format to check whether all
+      // filters have been processed later.
+      unset($format_filters[$name]);
+    }
+    $this->assertTrue(empty($format_filters), t('filter_list_format: Loaded filters contain values for all filters in the saved format.'));
+  }
+}
+
 class FilterAdminTestCase extends DrupalWebTestCase {
   public static function getInfo() {
     return array(
