Index: modules/filter/filter.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/filter/filter.module,v
retrieving revision 1.289
diff -u -p -r1.289 filter.module
--- modules/filter/filter.module	12 Sep 2009 06:09:45 -0000	1.289
+++ modules/filter/filter.module	12 Sep 2009 17:33:13 -0000
@@ -157,19 +157,37 @@ 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) {
   // We store the roles as a string for ease of use.
   // We should always set all roles to TRUE when saving the default format.
   // We use leading and trailing comma's to allow easy substring matching.
-  $roles = array_filter($format->roles);
+  if (!isset($format->roles)) {
+    $format->roles = array();
+  }
+  $format->roles = array_filter($format->roles);
   if (!empty($format->format) && $format->format == variable_get('filter_default_format', 1)) {
     $roles = ',' . implode(',', array_keys(user_roles())) . ',';
   }
   else {
-    $roles = ',' . implode(',', array_keys($roles)) . ',';
+    $roles = ',' . implode(',', array_keys($format->roles)) . ',';
   }
+  // Backup user roles to restore them later.
+  $original_roles = $format->roles;
   $format->roles = $roles;
   $format->name = trim($format->name);
 
@@ -184,12 +202,17 @@ 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);
+  $filter_info = filter_get_filters();
+  if (!isset($format->filters)) {
+    $format->filters = array();
+  }
   $filters = $format->filters;
   foreach ($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'];
     // Only update settings if there are any.
     if (!empty($filter['settings'])) {
       $fields['settings'] = serialize($filter['settings']);
@@ -203,6 +226,9 @@ function filter_format_save($format) {
       ->execute();
   }
 
+  // Restore user roles before invoking module hooks.
+  $format->roles = $original_roles;
+
   if ($return == SAVED_NEW) {
     module_invoke_all('filter_format_insert', $format);
   }
@@ -450,6 +476,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);
       }
     }
@@ -537,22 +567,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.
@@ -690,11 +711,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.41
diff -u -p -r1.41 filter.test
--- modules/filter/filter.test	12 Sep 2009 06:09:45 -0000	1.41
+++ modules/filter/filter.test	12 Sep 2009 17:41:20 -0000
@@ -1,6 +1,88 @@
 <?php
 // $Id: filter.test,v 1.41 2009/09/12 06:09:45 dries Exp $
 
+/**
+ * Tests for text format and filter CRUD operations.
+ */
+class FilterCRUDTestCase extends DrupalWebTestCase {
+  protected $admin_user;
+
+  public static function getInfo() {
+    return array(
+      'name' => 'Filter CRUD operations',
+      'description' => 'Test creation, loading, updating, deleting of text formats and filters.',
+      'group' => 'Filter',
+    );
+  }
+
+  function setUp() {
+    parent::setUp();
+    $this->admin_user = $this->drupalCreateUser(array('administer filters'));
+    $this->drupalLogin($this->admin_user);
+  }
+
+  /**
+   * Test CRUD operations for text formats.
+   */
+  function testTextFormatCRUD() {
+    // Add a text format with minimum data only.
+    $format = new stdClass;
+    $format->name = 'Minimum format';
+    filter_format_save($format);
+    $this->verifyTextFormat($format);
+
+    // Add another text format specifying all possible properties.
+
+    // Alter some text format properties and save again.
+  }
+
+  /**
+   * Test CRUD operations for filters.
+   */
+  function testFilterCRUD() {
+    // @todo Erm. Come up with something sane here.
+  }
+
+  /**
+   * Verify that a text format is stored complete.
+   */
+  function verifyTextFormat($format) {
+    // Verify database record.
+    $db_format = db_select('filter_format', 'ff')
+      ->fields('ff')
+      ->condition('format', $format->format)
+      ->execute()
+      ->fetchObject();
+    $db_format->roles = trim($db_format->roles, ',');
+    if (!empty($db_format->roles)) {
+      $db_format->roles = array_fill_keys($db_format->roles, 1);
+    }
+    else {
+      $db_format->roles = array();
+    }
+    $this->assertTrue($db_format->format == $format->format, t('Database contains the saved text format.'));
+    $this->assertTrue($db_format->name == $format->name, t('Database contains the saved text format title.'));
+    $this->assertTrue($db_format->roles == $format->roles, t('Database contains the saved text format user roles.'));
+    $this->assertTrue($db_format->cache == $format->cache, t('Database contains the saved text format cache indicator.'));
+    $this->assertTrue($db_format->weight == $format->weight, t('Database contains the saved text format weight.'));
+
+    // Verify filter_format_load().
+    $filter_format = filter_format_load($format->format);
+    $filter_format->roles = trim($filter_format->roles, ',');
+    if (!empty($filter_format->roles)) {
+      $filter_format->roles = array_fill_keys($filter_format->roles, 1);
+    }
+    else {
+      $filter_format->roles = array();
+    }
+    $this->assertTrue($filter_format->format == $format->format, t('Loaded text format contains the saved text format.'));
+    $this->assertTrue($filter_format->name == $format->name, t('Loaded text format contains the saved text format title.'));
+    $this->assertTrue($filter_format->roles == $format->roles, t('Loaded text format contains the saved text format user roles.'));
+    $this->assertTrue($filter_format->cache == $format->cache, t('Loaded text format contains the saved text format cache indicator.'));
+    $this->assertTrue($filter_format->weight == $format->weight, t('Loaded text format contains the saved text format weight.'));
+  }
+}
+
 class FilterAdminTestCase extends DrupalWebTestCase {
   public static function getInfo() {
     return array(
