You are here

function FilterAdminTest::testFormatAdmin in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/filter/src/Tests/FilterAdminTest.php \Drupal\filter\Tests\FilterAdminTest::testFormatAdmin()

Tests the format administration functionality.

File

core/modules/filter/src/Tests/FilterAdminTest.php, line 118
Contains \Drupal\filter\Tests\FilterAdminTest.

Class

FilterAdminTest
Thoroughly test the administrative interface of the filter module.

Namespace

Drupal\filter\Tests

Code

function testFormatAdmin() {

  // Add text format.
  $this
    ->drupalGet('admin/config/content/formats');
  $this
    ->clickLink('Add text format');
  $format_id = Unicode::strtolower($this
    ->randomMachineName());
  $name = $this
    ->randomMachineName();
  $edit = array(
    'format' => $format_id,
    'name' => $name,
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save configuration'));

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

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

  // Edit text format.
  $this
    ->drupalGet('admin/config/content/formats');

  // Cannot use the assertNoLinkByHref method as it does partial url matching
  // and 'admin/config/content/formats/manage/' . $format_id . '/disable'
  // exists.
  // @todo: See https://www.drupal.org/node/2031223 for the above.
  $edit_link = $this
    ->xpath('//a[@href=:href]', array(
    ':href' => \Drupal::url('entity.filter_format.edit_form', [
      'filter_format' => $format_id,
    ]),
  ));
  $this
    ->assertTrue($edit_link, format_string('Link href %href found.', array(
    '%href' => 'admin/config/content/formats/manage/' . $format_id,
  )));
  $this
    ->drupalGet('admin/config/content/formats/manage/' . $format_id);
  $this
    ->drupalPostForm(NULL, array(), t('Save configuration'));

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

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

  // Verify that disabled text format no longer exists.
  $this
    ->drupalGet('admin/config/content/formats/manage/' . $format_id);
  $this
    ->assertResponse(404, 'Disabled text format no longer exists.');

  // 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',
  );
  $this
    ->drupalPostForm('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 human readable name as the
  // disabled format but with a different machine name.
  $edit = array(
    'format' => 'new_format',
    'name' => $name,
  );
  $this
    ->drupalPostForm('admin/config/content/formats/add', $edit, t('Save configuration'));
  $this
    ->assertRaw(t('Text format names must be unique. A format named %name already exists.', array(
    '%name' => $name,
  )));
}