diff --git modules/filter/filter.admin.inc modules/filter/filter.admin.inc index a1e79bc..4dd9c35 100644 --- modules/filter/filter.admin.inc +++ modules/filter/filter.admin.inc @@ -136,7 +136,7 @@ function filter_admin_format_form($form, &$form_state, $format) { '#default_value' => $format->format, '#maxlength' => 255, '#machine_name' => array( - 'exists' => 'filter_format_load', + 'exists' => 'filter_format_exists', ), '#disabled' => !empty($format->format), ); @@ -288,13 +288,13 @@ function theme_filter_admin_format_filter_order($variables) { * Validate text format form submissions. */ function filter_admin_format_form_validate($form, &$form_state) { - if (!isset($form_state['values']['format'])) { - $format_name = trim($form_state['values']['name']); - form_set_value($form['name'], $format_name, $form_state); - $result = db_query("SELECT format FROM {filter_format} WHERE name = :name", array(':name' => $format_name))->fetchField(); - if ($result) { - form_set_error('name', t('Text format names must be unique. A format named %name already exists.', array('%name' => $format_name))); - } + $format_format = trim($form_state['values']['format']); + form_set_value($form['format'], $format_format, $form_state); + $format_name = trim($form_state['values']['name']); + form_set_value($form['name'], $format_name, $form_state); + $result = db_query("SELECT format FROM {filter_format} WHERE name = :name AND format <> :format", array(':name' => $format_name, ':format' => $format_format))->fetchField(); + if ($result) { + form_set_error('name', t('Text format names must be unique. A format named %name already exists.', array('%name' => $format_name))); } } diff --git modules/filter/filter.module modules/filter/filter.module index a4ce97b..f0bb109 100644 --- modules/filter/filter.module +++ modules/filter/filter.module @@ -153,7 +153,11 @@ function _filter_disable_format_access($format) { * The format ID. * * @return - * A fully-populated text format object. + * A fully-populated text format object, if the requested format exists and + * is enabled. If the format does not exist, or exists in the database but + * has been marked as disabled, FALSE is returned. + * + * @see filter_format_exists() */ function filter_format_load($format_id) { $formats = filter_formats(); @@ -165,9 +169,10 @@ function filter_format_load($format_id) { * * @param $format * A format object using the properties: + * - 'format': A machine-readable name representing the ID of the text format + * to save. If this corresponds to an existing text format, that format + * will be updated; otherwise, a new format will be created. * - 'name': The title of the text format. - * - 'format': (optional) The internal ID of the text format. If omitted, a - * new text format is created. * - 'weight': (optional) The weight of the text format, which controls its * placement in text format lists. If omitted, the weight is set to 0. * - 'filters': (optional) An associative, multi-dimensional array of filters @@ -266,9 +271,9 @@ function filter_format_save($format) { * Disable a text format. * * There is no core facility to re-enable a disabled format. It is not deleted - * to keep information for contrib and to make sure the format auto increment - * id is never reused. As there might be content using the disabled format, - * this would lead to data corruption. + * to keep information for contrib and to make sure the format ID is never + * reused. As there might be content using the disabled format, this would lead + * to data corruption. * * @param $format * The text format object to be disabled. @@ -288,6 +293,23 @@ function filter_format_disable($format) { } /** + * Determines if a text format exists. + * + * @param $format_id + * The ID of the text format to check. + * + * @return + * TRUE if the text format exists, or FALSE if it does not. Note that for a + * format which has been disabled (and therefore is no longer available for + * use) but still exists in the database, TRUE will be returned. + * + * @see filter_format_load() + */ +function filter_format_exists($format_id) { + return (bool) db_query_range('SELECT 1 FROM {filter_format} WHERE format = :format', 0, 1, array(':format' => $format_id))->fetchField(); +} + +/** * Display a text format form title. */ function filter_admin_format_title($format) { diff --git modules/filter/filter.test modules/filter/filter.test index f27ed0a..af1d670 100644 --- modules/filter/filter.test +++ modules/filter/filter.test @@ -207,6 +207,22 @@ class FilterAdminTestCase extends DrupalWebTestCase { // Verify that disabled text format no longer exists. $this->drupalGet('admin/config/content/formats/' . $format->format); $this->assertResponse(404, t('Disabled text format no longer exists.')); + + // Attempt to create a format of the same machine name. + $edit = array( + 'format' => $format->format, + 'name' => 'New format', + ); + $this->drupalPost('admin/config/content/formats/add', $edit, t('Save configuration')); + $this->assertText('The machine-readable name is already in use. It must be unique.'); + + // Attempt to create a format of the same name. + $edit = array( + 'format' => 'new_format', + 'name' => $format->name, + ); + $this->drupalPost('admin/config/content/formats/add', $edit, t('Save configuration')); + $this->assertText('Text format names must be unique. A format named ' . check_plain($format->name) . ' already exists.'); } /** @@ -1082,7 +1098,7 @@ class FilterUnitTestCase extends DrupalUnitTestCase { $f = _filter_html('
', $filter); $this->assertNoNormalized($f, 'onerror', t('HTML filter should remove on* attributes on default.')); - $f = _filter_html(' ', $filter);
+ $f = _filter_html(' ', $filter);
$this->assertNoNormalized($f, 'onerror', t('HTML filter should remove empty on* attributes on default.'));
}
diff --git modules/simpletest/simpletest.info modules/simpletest/simpletest.info
index ab80c84..3cc5ede 100644
--- modules/simpletest/simpletest.info
+++ modules/simpletest/simpletest.info
@@ -41,6 +41,7 @@ files[] = tests/update.test
files[] = tests/xmlrpc.test
files[] = tests/upgrade/upgrade.test
files[] = tests/upgrade/upgrade.comment.test
+files[] = tests/upgrade/upgrade.filter.test
files[] = tests/upgrade/upgrade.node.test
files[] = tests/upgrade/upgrade.taxonomy.test
files[] = tests/upgrade/upgrade.upload.test
diff --git modules/simpletest/tests/upgrade/upgrade.filter.test modules/simpletest/tests/upgrade/upgrade.filter.test
new file mode 100644
index 0000000..2642921
--- /dev/null
+++ modules/simpletest/tests/upgrade/upgrade.filter.test
@@ -0,0 +1,56 @@
+ 'Filter format upgrade path',
+ 'description' => 'Verifies that filter formats and references to filter formats are converted properly.',
+ 'group' => 'Upgrade path',
+ );
+ }
+
+ public function setUp() {
+ // Path to the database dump.
+ $this->databaseDumpFiles = array(
+ drupal_get_path('module', 'simpletest') . '/tests/upgrade/drupal-6.filled.database.php',
+ );
+ parent::setUp();
+ }
+
+ /**
+ * Test a successful upgrade.
+ */
+ public function testFilterFormatUpgrade() {
+ $this->assertTrue($this->performUpgrade(), t('The upgrade was completed successfully.'));
+
+ $format = filter_format_load('1');
+ $this->assertTrue($format->format == '1', t('Filter format found.'));
+ $format->format = 'test_filter';
+ $format->name = 'Test filter';
+ filter_format_save($format);
+ $format = filter_format_load('test_filter');
+ $this->assertTrue($format->format == 'test_filter', t('Saved a filter format with machine name.'));
+
+ $account = user_load(4);
+ user_save($account, array('signature_format' => 'test_filter'));
+ $account = user_load(4);
+ $this->assertTrue($account->signature_format == 'test_filter', t('Signature format changed successfully to a filter format with machine name.'));
+
+ $delta = db_insert('block_custom')
+ ->fields(array(
+ 'body' => 'Test block',
+ 'info' => 'Test block',
+ 'format' => 'test_filter',
+ ))
+ ->execute();
+ $this->assertTrue($delta > 0, t('Created a custom block using a filter format with machine name.'));
+ }
+}