filter.test 8.79 KB
Newer Older
1 2 3
<?php
// $Id$

4
class FilterAdminTestCase extends DrupalWebTestCase {
5
  public static function getInfo() {
6 7 8 9 10 11 12 13 14 15
    return array(
      'name' => t('Filter administration functionality'),
      'description' => t('Thoroughly test the administrative interface of the filter module.'),
      'group' => t('Filter'),
    );
  }

  /**
   * Test filter administration functionality.
   */
16
  function testFilterAdmin() {
17 18 19 20 21 22 23 24 25 26 27 28 29 30
    $first_filter = 2; // URL filter.
    $second_filter = 1; // Line filter.

    // Create users.
    $admin_user = $this->drupalCreateUser(array('administer filters'));
    $web_user = $this->drupalCreateUser(array('create page content'));

    $this->drupalLogin($admin_user);

    list($filtered, $full) = $this->checkFilterFormats();

    // Change default filter.
    $edit = array();
    $edit['default'] = $full;
31
    $this->drupalPost('admin/settings/filter', $edit, t('Save changes'));
32 33
    $this->assertText(t('Default format updated.'), t('Default filter updated successfully.'));

34
    $this->assertNoRaw('admin/settings/filter/delete/' . $full, t('Delete link not found.'));
35 36 37

    // Add an additional tag.
    $edit = array();
38
    $edit['allowed_html_1'] = '<a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>' . ' <quote>'; // Adding <quote> tag.
39
    $this->drupalPost('admin/settings/filter/' . $filtered . '/configure', $edit, t('Save configuration'));
40 41 42 43
    $this->assertText(t('The configuration options have been saved.'), t('Allowed HTML tag added.'));

    $this->assertRaw(htmlentities($edit['allowed_html_1']), t('Tag displayed.'));

44
    $result = db_query('SELECT * FROM {cache_filter}')->fetchObject();
45 46 47 48
    $this->assertFalse($result, t('Cache cleared.'));

    // Reorder filters.
    $edit = array();
49 50
    $edit['weights[filter/' . $second_filter . ']'] = 1;
    $edit['weights[filter/' . $first_filter . ']'] = 2;
51
    $this->drupalPost('admin/settings/filter/' . $filtered . '/order', $edit, t('Save configuration'));
52 53
    $this->assertText(t('The filter ordering has been saved.'), t('Order saved successfully.'));

54
    $result = db_query('SELECT * FROM {filter} WHERE format = :format ORDER BY weight ASC', array(':format' => $filtered));
55
    $filters = array();
56
    foreach ($result as $filter) {
57 58 59 60 61 62 63 64 65 66
      if ($filter->delta == $second_filter || $filter->delta == $first_filter) {
        $filters[] = $filter;
      }
    }
    $this->assertTrue(($filters[0]->delta == $second_filter && $filters[1]->delta == $first_filter), t('Order confirmed.'));

    // Add filter.
    $edit = array();
    $edit['name'] = $this->randomName();
    $edit['roles[2]'] = TRUE;
67 68
    $edit['filters[filter/' . $second_filter . ']'] = TRUE;
    $edit['filters[filter/' . $first_filter . ']'] = TRUE;
69 70
    $this->drupalPost('admin/settings/filter/add', $edit, t('Save configuration'));
    $this->assertRaw(t('Added text format %format.', array('%format' => $edit['name'])), t('New filter created.'));
71 72 73 74 75 76

    $format = $this->getFilter($edit['name']);
    $this->assertNotNull($format, t('Format found in database.'));

    if ($format !== NULL) {
      $this->assertFieldByName('roles[2]', '', t('Role found.'));
77 78
      $this->assertFieldByName('filters[filter/' . $second_filter . ']', '', t('Line break filter found.'));
      $this->assertFieldByName('filters[filter/' . $first_filter . ']', '', t('Url filter found.'));
79 80

      // Delete new filter.
81 82
      $this->drupalPost('admin/settings/filter/delete/' . $format->format, array(), t('Delete'));
      $this->assertRaw(t('Deleted text format %format.', array('%format' => $edit['name'])), t('Format successfully deleted.'));
83 84 85 86 87
    }

    // Change default filter back.
    $edit = array();
    $edit['default'] = $filtered;
88
    $this->drupalPost('admin/settings/filter', $edit, t('Save changes'));
89 90
    $this->assertText(t('Default format updated.'), t('Default filter updated successfully.'));

91
    $this->assertNoRaw('admin/settings/filter/delete/' . $filtered, t('Delete link not found.'));
92 93 94 95

    // Allow authenticated users on full HTML.
    $edit = array();
    $edit['roles[2]'] = TRUE;
96 97
    $this->drupalPost('admin/settings/filter/' . $full, $edit, t('Save configuration'));
    $this->assertText(t('The text format settings have been updated.'), t('Full HTML format successfully updated.'));
98 99 100 101 102 103

    // Switch user.
    $this->drupalLogout();
    $this->drupalLogin($web_user);

    $this->drupalGet('node/add/page');
104
    $this->assertRaw('<option value="' . $full . '">Full HTML</option>', t('Full HTML filter accessible.'));
105 106 107 108 109 110 111

    // Use filtered HTML and see if it removes tags that arn't allowed.
    $body = $this->randomName();
    $extra_text = 'text';

    $edit = array();
    $edit['title'] = $this->randomName();
112
    $edit['body'] = $body . '<random>' . $extra_text . '</random>';
113
    $edit['body_format'] = $filtered;
114 115 116
    $this->drupalPost('node/add/page', $edit, t('Save'));
    $this->assertRaw(t('Page %title has been created.', array('%title' => $edit['title'])), t('Filtered node created.'));

117
    $node = $this->drupalGetNodeByTitle($edit['title']);
118 119
    $this->assertTrue($node, t('Node found in database.'));

120
    $this->drupalGet('node/' . $node->nid);
121 122 123 124 125 126 127
    $this->assertText($body . $extra_text, t('Filter removed invalid tag.'));

    // Switch user.
    $this->drupalLogout();
    $this->drupalLogin($admin_user);

    // Clean up.
128
    // Allowed tags.
129 130
    $edit = array();
    $edit['allowed_html_1'] = '<a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>';
131
    $this->drupalPost('admin/settings/filter/' . $filtered . '/configure', $edit, t('Save configuration'));
132 133
    $this->assertText(t('The configuration options have been saved.'), t('Changes reverted.'));

134
    // Full HTML.
135 136
    $edit = array();
    $edit['roles[2]'] = FALSE;
137 138
    $this->drupalPost('admin/settings/filter/' . $full, $edit, t('Save configuration'));
    $this->assertText(t('The text format settings have been updated.'), t('Full HTML format successfully reverted.'));
139

140
    // Filter order.
141
    $edit = array();
142 143
    $edit['weights[filter/' . $second_filter . ']'] = 2;
    $edit['weights[filter/' . $first_filter . ']'] = 1;
144
    $this->drupalPost('admin/settings/filter/' . $filtered . '/order', $edit, t('Save configuration'));
145 146 147 148 149 150 151 152 153
    $this->assertText(t('The filter ordering has been saved.'), t('Order successfully reverted.'));
  }

  /**
   * Query the database to get the two basic formats.
   *
   * @return Array Array containing filtered and full filter ids.
   */
  function checkFilterFormats() {
154
    $result = db_query('SELECT format, name FROM {filter_format}');
155 156 157

    $filtered = -1;
    $full = -1;
158
    foreach ($result as $format) {
159 160 161
      if ($format->name == 'Filtered HTML') {
        $filtered = $format->format;
      }
162
      elseif ($format->name == 'Full HTML') {
163 164 165 166 167 168 169 170 171 172 173 174 175 176
        $full = $format->format;
      }
    }

    return array($filtered, $full);
  }

  /**
   * Get filter by name.
   *
   * @param string $name Name of filter to find.
   * @return object Filter object.
   */
  function getFilter($name) {
177
    return db_query("SELECT * FROM {filter_format} WHERE name = :name", array(':name' => $name))->fetchObject();
178 179
  }
}
180 181 182 183

class FilterTestCase extends DrupalWebTestCase {
  protected $format;

184
  public static function getInfo() {
185 186
    return array(
      'name' => t('Core filters'),
187
      'description' => t('Filter each filter individually: Convert URLs into links, Convert line breaks, Correct broken HTML, Escape all HTML, Limit allowed HTML tags.'),
188 189 190 191 192 193
      'group' => t('Filter'),
    );
  }

  function setUp() {
    parent::setUp();
194 195 196

    $admin_user = $this->drupalCreateUser(array('administer filters', 'create page content'));
    $this->drupalLogin($admin_user);
197 198 199 200 201 202
  }

  /**
   * Test the line break filter
   */
  function testLineBreakFilter() {
203 204
    $f = _filter_autop('<p><div>  </div></p>');
    $this->assertEqual(substr_count($f, '<p>'), substr_count($f, '</p>'), t('Make sure line breaking produces matching paragraph tags.'));
Dries's avatar
Dries committed
205

206 207 208 209 210
    $f = _filter_autop('<div><p>  </p></div>');
    $this->assertEqual(substr_count($f, '<p>'), substr_count($f, '</p>'), t('Make sure line breaking produces matching paragraph tags.'));

    $f = _filter_autop('<blockquote><pre>aaa</pre></blockquote>');
    $this->assertEqual(substr_count($f, '<p>'), substr_count($f, '</p>'), t('Make sure line breaking produces matching paragraph tags.'));
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
  }

  /**
   * Test the HTML filter
   */
  function testHtmlFilter() {

  }

  function createFormat($filter) {
    $edit = array(
      'name' => $this->randomName(),
      'roles[2]' => TRUE,
      'filters[filter/' . $filter . ']' => TRUE,
    );
226
    $this->drupalPost('admin/settings/filter/add', $edit, t('Save configuration'));
227
    return db_query("SELECT * FROM {filter_format} WHERE name = :name", array(':name' => $edit['name']))->fetchObject();
228 229 230 231 232
  }

  function deleteFormat($format) {
    if ($format !== NULL) {
      // Delete new filter.
233
      $this->drupalPost('admin/settings/filter/delete/' . $format->format, array(), t('Delete'));
234 235 236
    }
  }
}