1 filter.test FilterAdminTestCase::testFilterAdmin()

Tests filter administration functionality.

File

core/modules/filter/tests/filter.test, line 298
Tests for filter.module.

Class

FilterAdminTestCase
Tests the administrative functionality of the Filter module.

Code

function testFilterAdmin() {
  // URL filter.
  $first_filter = 'filter_url';
  // Line filter.
  $second_filter = 'filter_autop';

  $filtered = 'filtered_html';
  $full = 'full_html';
  $plain = 'plain_text';

  // Check that the fallback format exists and cannot be disabled.
  $this->assertTrue($plain == filter_fallback_format(), 'The fallback format is set to plain text.');
  $this->backdropGet('admin/config/content/formats');
  $this->assertNoRaw('admin/config/content/formats/' . $plain . '/disable', 'Disable link for the fallback format not found.');
  $this->backdropGet('admin/config/content/formats/' . $plain . '/disable');
  $this->assertResponse(403, 'The fallback format cannot be disabled.');

  // Verify access permissions to Raw HTML (full_html) format.
  $this->assertTrue(filter_access(filter_format_load($full), $this->admin_user), 'Admin user may use Raw HTML.');
  $this->assertFalse(filter_access(filter_format_load($full), $this->web_user), 'Web user may not use Raw HTML.');

  // Add an additional tag.
  $edit = array();
  $edit['allowed_html'] = '<a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <quote>';
  $this->backdropPost('admin/config/content/formats/' . $filtered . '/filter-settings/filter_html', $edit, t('Update'));
  $this->backdropPost(NULL, array(), t('Save configuration'));
  $this->backdropGet('admin/config/content/formats/' . $filtered);
  $this->assertFieldByName('allowed_html', $edit['allowed_html'], 'Allowed HTML tag added.');

  $result = db_query('SELECT * FROM {cache_filter}')->fetchObject();
  $this->assertFalse($result, 'Cache cleared.');

  $elements = $this->xpath('//select[@name=:first]/following::select[@name=:second]', array(
    ':first' => 'filters[' . $first_filter . '][weight]',
    ':second' => 'filters[' . $second_filter . '][weight]',
  ));
  $this->assertTrue(!empty($elements), 'Order confirmed in admin interface.');

  // Reorder filters.
  $edit = array();
  $edit['filters[' . $second_filter . '][weight]'] = 1;
  $edit['filters[' . $first_filter . '][weight]'] = 2;
  $this->backdropPost(NULL, $edit, t('Save configuration'));
  $this->backdropGet('admin/config/content/formats/' . $filtered);
  $this->assertFieldByName('filters[' . $second_filter . '][weight]', 1, 'Order saved successfully.');
  $this->assertFieldByName('filters[' . $first_filter . '][weight]', 2, 'Order saved successfully.');

  $elements = $this->xpath('//select[@name=:first]/following::select[@name=:second]', array(
    ':first' => 'filters[' . $second_filter . '][weight]',
    ':second' => 'filters[' . $first_filter . '][weight]',
  ));
  $this->assertTrue(!empty($elements), 'Reorder confirmed in admin interface.');

  $config_filters = config_get('filter.format.' . $filtered, 'filters');
  backdrop_sort($config_filters, array('weight'));
  $filters = array();
  foreach ($config_filters as $filter_name => $filter) {
    if ($filter_name == $second_filter || $filter_name == $first_filter) {
      $filter['name'] = $filter_name;
      $filters[] = $filter;
    }
  }
  $this->assertTrue(($filters[0]['name'] == $second_filter && $filters[1]['name'] == $first_filter), 'Order confirmed in database.');

  // Add format.
  $edit = array();
  $edit['format'] = backdrop_strtolower($this->randomName());
  $edit['name'] = $this->randomName();
  $edit['roles[authenticated]'] = 1;
  $edit['filters[' . $second_filter . '][status]'] = TRUE;
  $edit['filters[' . $first_filter . '][status]'] = TRUE;
  $this->backdropPost('admin/config/content/formats/add', $edit, t('Save configuration'));
  $this->assertRaw(t('Added text format %format.', array('%format' => $edit['name'])), 'New filter created.');

  backdrop_static_reset('filter_formats');
  $format = filter_format_load($edit['format']);
  $this->assertNotNull($format, 'Format found in config.');
  $this->backdropGet('admin/config/content/formats/' . $edit['format']);

  $this->assertFieldByName('roles[authenticated]', '', 'Role found.');
  $this->assertFieldByName('filters[' . $second_filter . '][status]', '', 'Line break filter found.');
  $this->assertFieldByName('filters[' . $first_filter . '][status]', '', 'Url filter found.');

  // Order the new text format and check its position in the markup.
  $edit_weight = array();
  $edit_weight['formats[' . $format->format . '][weight]'] = -10;
  $edit_weight['formats[filtered_html][weight]'] = -9;
  $edit_weight['formats[full_html][weight]'] = -8;
  $edit_weight['formats[plain_text][weight]'] = 0;
  $this->backdropPost('admin/config/content/formats', $edit_weight, t('Save changes'));

  $elements = $this->xpath('//select[@name=:first]/following::select[@name=:second]/following::select[@name=:third]/following::select[@name=:fourth]', array(
    ':first' => 'formats[' . $format->format . '][weight]',
    ':second' => 'formats[filtered_html][weight]',
    ':third' => 'formats[full_html][weight]',
    ':fourth' => 'formats[plain_text][weight]',
  ));
  $this->assertTrue(!empty($elements), 'Text format order confirmed in admin interface with new format at top.');

  // Disable the new text format.
  $this->backdropPost('admin/config/content/formats/' . $format->format . '/disable', array(), t('Disable'));
  $this->assertRaw(t('Disabled text format %format.', array('%format' => $edit['name'])), 'Format successfully disabled.');

  // Check that the disabled text format is at the bottom of the list.
  $elements = $this->xpath('//select[@name=:first]/following::select[@name=:second]/following::select[@name=:third]/following::select[@name=:fourth]', array(
    ':first' => 'formats[filtered_html][weight]',
    ':second' => 'formats[full_html][weight]',
    ':third' => 'formats[plain_text][weight]',
    ':fourth' => 'formats[' . $format->format . '][weight]',
  ));
  $this->assertTrue(!empty($elements), 'Text format order confirmed in admin interface with disabled format at bottom.');

  // Allow authenticated users on Raw HTML (full_html).
  $format = filter_format_load($full);
  $edit = array();
  $edit['roles[anonymous]'] = 0;
  $edit['roles[authenticated]'] = 1;
  $this->backdropPost('admin/config/content/formats/' . $full, $edit, t('Save configuration'));
  $this->assertRaw(t('Updated text format %format.', array('%format' => $format->name)), 'Raw HTML format successfully updated.');

  // Switch user.
  $this->backdropLogout();
  $this->backdropLogin($this->web_user);

  $this->backdropGet('node/add/page');
  // Ensure Raw HTML (full_html) is allowed.
  $this->assertRaw('<option value="' . $full . '">Raw HTML</option>', 'Raw HTML filter accessible.');

  // Switch user.
  $this->backdropLogout();
  $this->backdropLogin($this->admin_user);

  // Clean up.
  // Allowed tags.
  $edit = array();
  $edit['allowed_html'] = '<a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>';
  $this->backdropPost('admin/config/content/formats/' . $filtered . '/filter-settings/filter_html', $edit, t('Update'));
  $this->backdropPost(NULL, array(), t('Save configuration'));
  $this->backdropGet('admin/config/content/formats/' . $filtered);
  $this->assertFieldByName('allowed_html', $edit['allowed_html'], 'Allowed HTML tag added.');

  // Raw HTML (full_html).
  $edit = array();
  $edit['roles[authenticated]'] = FALSE;
  $this->backdropPost('admin/config/content/formats/' . $full, $edit, t('Save configuration'));
  $this->assertRaw(t('Updated text format %format.', array('%format' => $format->name)), 'Raw HTML format successfully reverted.');
  $this->backdropGet('admin/config/content/formats/' . $full);
  $this->assertFieldByName('roles[authenticated]', $edit['roles[authenticated]'], 'Changes reverted.');

  // Filter order.
  $edit = array();
  $edit['filters[' . $second_filter . '][weight]'] = 2;
  $edit['filters[' . $first_filter . '][weight]'] = 1;
  $this->backdropPost('admin/config/content/formats/' . $filtered, $edit, t('Save configuration'));
  $this->backdropGet('admin/config/content/formats/' . $filtered);
  $this->assertFieldByName('filters[' . $second_filter . '][weight]', $edit['filters[' . $second_filter . '][weight]'], 'Changes reverted.');
  $this->assertFieldByName('filters[' . $first_filter . '][weight]', $edit['filters[' . $first_filter . '][weight]'], 'Changes reverted.');

  // Switch user.
  $this->backdropLogout();
  $this->backdropLogin($this->web_user);

  // Use filtered HTML and see if it removes tags that are not allowed.
  $body = '<em>' . $this->randomName() . '</em>';
  $extra_text = 'text';
  $text = $body . '<random>' . $extra_text . '</random>';

  $edit = array();
  $langcode = LANGUAGE_NONE;
  $edit["title"] = $this->randomName();
  $edit["body[$langcode][0][value]"] = $text;
  $edit["body[$langcode][0][format]"] = $filtered;
  $this->backdropPost('node/add/page', $edit, t('Save'));
  $this->assertRaw(t('Page %title has been created.', array('%title' => $edit["title"])), 'Filtered node created.');

  $node = $this->backdropGetNodeByTitle($edit["title"]);
  $this->assertTrue($node, 'Node found in database.');

  $this->backdropGet('node/' . $node->nid);
  $this->assertRaw($body . $extra_text, 'Filter removed invalid tag.');

  // Use plain text and see if it escapes all tags, whether allowed or not.
  foreach (filter_formats() as $format) {
    if ($format->format != filter_fallback_format()) {
      filter_format_disable($format);
    }
  }

  $edit = array();
  $langcode = LANGUAGE_NONE;
  $edit["title"] = $this->randomName();
  $edit["body[$langcode][0][value]"] = $text;
  $this->backdropPost('node/add/page', $edit, t('Save'));
  $this->assertText(check_plain($text), 'The "Plain text" text format escapes all HTML tags.');
}