FilterAdminTest.php 15.2 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Definition of Drupal\filter\Tests\FilterAdminTest.
 */

namespace Drupal\filter\Tests;

10
use Drupal\Component\Utility\String;
11 12
use Drupal\simpletest\WebTestBase;

13 14 15
/**
 * Tests the administrative functionality of the Filter module.
 */
16
class FilterAdminTest extends WebTestBase {
17 18

  /**
19
   * {@inheritdoc}
20
   */
21
  public static $modules = array('filter', 'node');
22

23 24 25
  /**
   * {@inheritdoc}
   */
26 27 28 29 30 31 32 33
  public static function getInfo() {
    return array(
      'name' => 'Filter administration functionality',
      'description' => 'Thoroughly test the administrative interface of the filter module.',
      'group' => 'Filter',
    );
  }

34 35 36
  /**
   * {@inheritdoc}
   */
37 38 39
  function setUp() {
    parent::setUp();

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));

    // Set up the filter formats used by this test.
    $basic_html_format = entity_create('filter_format', array(
      'format' => 'basic_html',
      'name' => 'Basic HTML',
      'filters' => array(
        'filter_html' => array(
          'status' => 1,
          'settings' => array(
            'allowed_html' => '<p> <br> <strong> <a> <em>',
          ),
        ),
      ),
    ));
    $basic_html_format->save();
    $restricted_html_format = entity_create('filter_format', array(
      'format' => 'restricted_html',
      'name' => 'Restricted HTML',
      'filters' => array(
        'filter_html' => array(
          'status' => TRUE,
          'weight' => -10,
          'settings' => array(
            'allowed_html' => '<p> <br> <strong> <a> <em> <h4>',
          ),
        ),
        'filter_autop' => array(
          'status' => TRUE,
          'weight' => 0,
        ),
        'filter_url' => array(
          'status' => TRUE,
          'weight' => 0,
        ),
        'filter_htmlcorrector' => array(
          'status' => TRUE,
          'weight' => 10,
        ),
      ),
    ));
    $restricted_html_format->save();
    $full_html_format = entity_create('filter_format', array(
      'format' => 'full_html',
      'name' => 'Full HTML',
      'weight' => 1,
      'filters' => array(),
    ));
    $full_html_format->save();

90 91
    $this->admin_user = $this->drupalCreateUser(array(
      'administer filters',
92 93 94
      $basic_html_format->getPermissionName(),
      $restricted_html_format->getPermissionName(),
      $full_html_format->getPermissionName(),
95 96 97
    ));

    $this->web_user = $this->drupalCreateUser(array('create page content', 'edit own page content'));
98 99
    user_role_grant_permissions('authenticated', array($basic_html_format->getPermissionName()));
    user_role_grant_permissions('anonymous', array($restricted_html_format->getPermissionName()));
100 101 102
    $this->drupalLogin($this->admin_user);
  }

103 104 105
  /**
   * Tests the format administration functionality.
   */
106 107 108 109 110 111 112 113 114 115
  function testFormatAdmin() {
    // Add text format.
    $this->drupalGet('admin/config/content/formats');
    $this->clickLink('Add text format');
    $format_id = drupal_strtolower($this->randomName());
    $name = $this->randomName();
    $edit = array(
      'format' => $format_id,
      'name' => $name,
    );
116
    $this->drupalPostForm(NULL, $edit, t('Save configuration'));
117 118 119

    // Verify default weight of the text format.
    $this->drupalGet('admin/config/content/formats');
120
    $this->assertFieldByName("formats[$format_id][weight]", 0, 'Text format weight was saved.');
121 122 123 124 125

    // Change the weight of the text format.
    $edit = array(
      "formats[$format_id][weight]" => 5,
    );
126
    $this->drupalPostForm('admin/config/content/formats', $edit, t('Save changes'));
127
    $this->assertFieldByName("formats[$format_id][weight]", 5, 'Text format weight was saved.');
128 129 130

    // Edit text format.
    $this->drupalGet('admin/config/content/formats');
131 132 133 134 135 136 137 138 139 140
    // Cannot use the assertNoLinkByHref method as it does partial url matching
    // and 'admin/config/content/formats/manage/' . $format_id . '/disable'
    // exists.
    // @todo: See https://drupal.org/node/2031223 for the above
    $edit_link = $this->xpath('//a[@href=:href]', array(
      ':href' => url('admin/config/content/formats/manage/' . $format_id)
    ));
    $this->assertTrue($edit_link, format_string('Link href %href found.',
      array('%href' => 'admin/config/content/formats/manage/' . $format_id)
    ));
141
    $this->drupalGet('admin/config/content/formats/manage/' . $format_id);
142
    $this->drupalPostForm(NULL, array(), t('Save configuration'));
143 144 145

    // Verify that the custom weight of the text format has been retained.
    $this->drupalGet('admin/config/content/formats');
146
    $this->assertFieldByName("formats[$format_id][weight]", 5, 'Text format weight was retained.');
147 148

    // Disable text format.
149 150
    $this->assertLinkByHref('admin/config/content/formats/manage/' . $format_id . '/disable');
    $this->drupalGet('admin/config/content/formats/manage/' . $format_id . '/disable');
151
    $this->drupalPostForm(NULL, array(), t('Disable'));
152 153

    // Verify that disabled text format no longer exists.
154
    $this->drupalGet('admin/config/content/formats/manage/' . $format_id);
155
    $this->assertResponse(404, 'Disabled text format no longer exists.');
156 157 158 159 160 161 162

    // Attempt to create a format of the same machine name as the disabled
    // format but with a different human readable name.
    $edit = array(
      'format' => $format_id,
      'name' => 'New format',
    );
163
    $this->drupalPostForm('admin/config/content/formats/add', $edit, t('Save configuration'));
164 165 166 167 168 169 170 171
    $this->assertText('The machine-readable name is already in use. It must be unique.');

    // Attempt to create a format of the same human readable name as the
    // disabled format but with a different machine name.
    $edit = array(
      'format' => 'new_format',
      'name' => $name,
    );
172
    $this->drupalPostForm('admin/config/content/formats/add', $edit, t('Save configuration'));
173 174 175 176 177 178
    $this->assertRaw(t('Text format names must be unique. A format named %name already exists.', array(
      '%name' => $name,
    )));
  }

  /**
179
   * Tests filter administration functionality.
180 181
   */
  function testFilterAdmin() {
182 183
    $first_filter = 'filter_autop';
    $second_filter = 'filter_url';
184

185
    $basic = 'basic_html';
186
    $restricted = 'restricted_html';
187 188 189 190
    $full = 'full_html';
    $plain = 'plain_text';

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

    // Verify access permissions to Full HTML format.
198
    $full_format = entity_load('filter_format', $full);
199 200
    $this->assertTrue($full_format->access('use', $this->admin_user), 'Admin user may use Full HTML.');
    $this->assertFalse($full_format->access('use', $this->web_user), 'Web user may not use Full HTML.');
201 202 203 204

    // Add an additional tag.
    $edit = array();
    $edit['filters[filter_html][settings][allowed_html]'] = '<a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <quote>';
205
    $this->drupalPostForm('admin/config/content/formats/manage/' . $restricted, $edit, t('Save configuration'));
206
    $this->assertUrl('admin/config/content/formats');
207
    $this->drupalGet('admin/config/content/formats/manage/' . $restricted);
208
    $this->assertFieldByName('filters[filter_html][settings][allowed_html]', $edit['filters[filter_html][settings][allowed_html]'], 'Allowed HTML tag added.');
209 210 211 212 213

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

    // Reorder filters.
    $edit = array();
    $edit['filters[' . $second_filter . '][weight]'] = 1;
    $edit['filters[' . $first_filter . '][weight]'] = 2;
220
    $this->drupalPostForm(NULL, $edit, t('Save configuration'));
221
    $this->assertUrl('admin/config/content/formats');
222
    $this->drupalGet('admin/config/content/formats/manage/' . $restricted);
223 224
    $this->assertFieldByName('filters[' . $second_filter . '][weight]', 1, 'Order saved successfully.');
    $this->assertFieldByName('filters[' . $first_filter . '][weight]', 2, 'Order saved successfully.');
225 226 227 228 229

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

232 233
    $filter_format = entity_load('filter_format', $restricted);
    foreach ($filter_format->filters() as $filter_name => $filter) {
234 235
      if ($filter_name == $second_filter || $filter_name == $first_filter) {
        $filters[] = $filter_name;
236 237
      }
    }
238 239
    // Ensure that the second filter is now before the first filter.
    $this->assertEqual($filter_format->filters($second_filter)->weight + 1, $filter_format->filters($first_filter)->weight, 'Order confirmed in configuration.');
240 241 242 243 244 245 246 247

    // Add format.
    $edit = array();
    $edit['format'] = drupal_strtolower($this->randomName());
    $edit['name'] = $this->randomName();
    $edit['roles[' . DRUPAL_AUTHENTICATED_RID . ']'] = 1;
    $edit['filters[' . $second_filter . '][status]'] = TRUE;
    $edit['filters[' . $first_filter . '][status]'] = TRUE;
248
    $this->drupalPostForm('admin/config/content/formats/add', $edit, t('Save configuration'));
249
    $this->assertUrl('admin/config/content/formats');
250
    $this->assertRaw(t('Added text format %format.', array('%format' => $edit['name'])), 'New filter created.');
251

252
    filter_formats_reset();
253
    $format = entity_load('filter_format', $edit['format']);
254
    $this->assertNotNull($format, 'Format found in database.');
255
    $this->drupalGet('admin/config/content/formats/manage/' . $format->format);
256 257 258
    $this->assertFieldByName('roles[' . DRUPAL_AUTHENTICATED_RID . ']', '', 'Role found.');
    $this->assertFieldByName('filters[' . $second_filter . '][status]', '', 'Line break filter found.');
    $this->assertFieldByName('filters[' . $first_filter . '][status]', '', 'Url filter found.');
259 260

    // Disable new filter.
261
    $this->drupalPostForm('admin/config/content/formats/manage/' . $format->format . '/disable', array(), t('Disable'));
262
    $this->assertUrl('admin/config/content/formats');
263
    $this->assertRaw(t('Disabled text format %format.', array('%format' => $edit['name'])), 'Format successfully disabled.');
264 265

    // Allow authenticated users on full HTML.
266
    $format = entity_load('filter_format', $full);
267 268 269
    $edit = array();
    $edit['roles[' . DRUPAL_ANONYMOUS_RID . ']'] = 0;
    $edit['roles[' . DRUPAL_AUTHENTICATED_RID . ']'] = 1;
270
    $this->drupalPostForm('admin/config/content/formats/manage/' . $full, $edit, t('Save configuration'));
271
    $this->assertUrl('admin/config/content/formats');
272
    $this->assertRaw(t('The text format %format has been updated.', array('%format' => $format->name)), 'Full HTML format successfully updated.');
273 274 275 276 277

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

    $this->drupalGet('node/add/page');
278
    $this->assertRaw('<option value="' . $full . '">Full HTML</option>', 'Full HTML filter accessible.');
279

280
    // Use basic HTML and see if it removes tags that are not allowed.
281 282 283 284 285
    $body = '<em>' . $this->randomName() . '</em>';
    $extra_text = 'text';
    $text = $body . '<random>' . $extra_text . '</random>';

    $edit = array();
286
    $edit['title[0][value]'] = $this->randomName();
287 288
    $edit['body[0][value]'] = $text;
    $edit['body[0][format]'] = $basic;
289
    $this->drupalPostForm('node/add/page', $edit, t('Save'));
290
    $this->assertRaw(t('Basic page %title has been created.', array('%title' => $edit['title[0][value]'])), 'Filtered node created.');
291

292
    $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
293
    $this->assertTrue($node, 'Node found in database.');
294

295
    $this->drupalGet('node/' . $node->id());
296
    $this->assertRaw($body . $extra_text, 'Filter removed invalid tag.');
297 298

    // Use plain text and see if it escapes all tags, whether allowed or not.
299 300
    // In order to test plain text, we have to enable the hidden variable for
    // "show_fallback_format", which displays plain text in the format list.
301
    \Drupal::config('filter.settings')
302 303
      ->set('always_show_fallback_choice', TRUE)
      ->save();
304
    $edit = array();
305
    $edit['body[0][format]'] = $plain;
306
    $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
307
    $this->drupalGet('node/' . $node->id());
308
    $this->assertText(String::checkPlain($text), 'The "Plain text" text format escapes all HTML tags.');
309
    \Drupal::config('filter.settings')
310 311
      ->set('always_show_fallback_choice', FALSE)
      ->save();
312 313 314 315 316 317 318 319

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

    // Clean up.
    // Allowed tags.
    $edit = array();
    $edit['filters[filter_html][settings][allowed_html]'] = '<a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>';
320
    $this->drupalPostForm('admin/config/content/formats/manage/' . $basic, $edit, t('Save configuration'));
321
    $this->assertUrl('admin/config/content/formats');
322
    $this->drupalGet('admin/config/content/formats/manage/' . $basic);
323
    $this->assertFieldByName('filters[filter_html][settings][allowed_html]', $edit['filters[filter_html][settings][allowed_html]'], 'Changes reverted.');
324 325 326 327

    // Full HTML.
    $edit = array();
    $edit['roles[' . DRUPAL_AUTHENTICATED_RID . ']'] = FALSE;
328
    $this->drupalPostForm('admin/config/content/formats/manage/' . $full, $edit, t('Save configuration'));
329
    $this->assertUrl('admin/config/content/formats');
330
    $this->assertRaw(t('The text format %format has been updated.', array('%format' => $format->name)), 'Full HTML format successfully reverted.');
331
    $this->drupalGet('admin/config/content/formats/manage/' . $full);
332
    $this->assertFieldByName('roles[' . DRUPAL_AUTHENTICATED_RID . ']', $edit['roles[' . DRUPAL_AUTHENTICATED_RID . ']'], 'Changes reverted.');
333 334 335 336 337

    // Filter order.
    $edit = array();
    $edit['filters[' . $second_filter . '][weight]'] = 2;
    $edit['filters[' . $first_filter . '][weight]'] = 1;
338
    $this->drupalPostForm('admin/config/content/formats/manage/' . $basic, $edit, t('Save configuration'));
339
    $this->assertUrl('admin/config/content/formats');
340
    $this->drupalGet('admin/config/content/formats/manage/' . $basic);
341 342
    $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.');
343 344 345 346 347 348 349 350 351 352
  }

  /**
   * Tests the URL filter settings form is properly validated.
   */
  function testUrlFilterAdmin() {
    // The form does not save with an invalid filter URL length.
    $edit = array(
      'filters[filter_url][settings][filter_url_length]' => $this->randomName(4),
    );
353
    $this->drupalPostForm('admin/config/content/formats/manage/basic_html', $edit, t('Save configuration'));
354
    $this->assertNoRaw(t('The text format %format has been updated.', array('%format' => 'Basic HTML')));
355
  }
356

357
}