function FilterAdminTest::testFormatAdmin

Same name and namespace in other branches
  1. 11.x core/modules/filter/tests/src/Functional/FilterAdminTest.php \Drupal\Tests\filter\Functional\FilterAdminTest::testFormatAdmin()
  2. 10 core/modules/filter/tests/src/Functional/FilterAdminTest.php \Drupal\Tests\filter\Functional\FilterAdminTest::testFormatAdmin()
  3. 9 core/modules/filter/tests/src/Functional/FilterAdminTest.php \Drupal\Tests\filter\Functional\FilterAdminTest::testFormatAdmin()
  4. main core/modules/filter/tests/src/Functional/FilterAdminTest.php \Drupal\Tests\filter\Functional\FilterAdminTest::testFormatAdmin()

Tests the format administration functionality.

File

core/modules/filter/tests/src/Functional/FilterAdminTest.php, line 127

Class

FilterAdminTest
Thoroughly test the administrative interface of the filter module.

Namespace

Drupal\Tests\filter\Functional

Code

public function testFormatAdmin() {
  // Add text format.
  $this->drupalGet('admin/config/content/formats');
  $this->clickLink('Add text format');
  $format_id = mb_strtolower($this->randomMachineName());
  $name = $this->randomMachineName();
  $edit = [
    '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 = [
    "formats[{$format_id}][weight]" => 5,
  ];
  $this->drupalPostForm('admin/config/content/formats', $edit, t('Save'));
  $this->assertFieldByName("formats[{$format_id}][weight]", 5, 'Text format weight was saved.');
  // Edit text format.
  $this->drupalGet('admin/config/content/formats');
  $destination = Url::fromRoute('filter.admin_overview')->toString();
  $edit_href = Url::fromRoute('entity.filter_format.edit_form', [
    'filter_format' => $format_id,
  ], [
    'query' => [
      'destination' => $destination,
    ],
  ])->toString();
  $this->assertSession()
    ->linkByHrefExists($edit_href);
  $this->drupalGet('admin/config/content/formats/manage/' . $format_id);
  $this->drupalPostForm(NULL, [], 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, [], t('Disable'));
  // Verify that disabled text format no longer exists.
  $this->drupalGet('admin/config/content/formats/manage/' . $format_id);
  $this->assertSession()
    ->statusCodeEquals(404);
  // Attempt to create a format of the same machine name as the disabled
  // format but with a different human readable name.
  $edit = [
    '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 = [
    '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.', [
    '%name' => $name,
  ]));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.