function FilterAdminTest::testFilterEnableAndDisable

Tests enabling and disabling of filters.

File

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

Class

FilterAdminTest
Thoroughly test the administrative interface of the filter module.

Namespace

Drupal\Tests\filter\Functional

Code

public function testFilterEnableAndDisable() : void {
  $filter_test = FilterFormat::create([
    'format' => 'filter_test',
    'name' => 'Filter test',
    'filters' => [
      'filter_html' => [
        'status' => TRUE,
        'weight' => -10,
        'settings' => [
          'allowed_html' => '<p> <br> <strong> <a> <em> <h4>',
        ],
      ],
    ],
  ]);
  $filter_test->save();
  // Create a node type and add a standard body field.
  $node_type = NodeType::create([
    'type' => $this->randomMachineName(),
    'name' => $this->randomString(),
  ]);
  $node_type->save();
  node_add_body_field($node_type, $this->randomString());
  // Create a new node of the new node type.
  $title = $this->randomString();
  $node = Node::create([
    'type' => $node_type->id(),
    'title' => $title,
  ]);
  $body_value = 'I belong to a filter that might be shut off!';
  $node->body->value = $body_value;
  $node->body->format = 'filter_test';
  $node->save();
  // Confirm the body field using the filter test is visible.
  $this->drupalGet($node->toUrl());
  $this->assertSession()
    ->pageTextContains($title);
  $this->assertSession()
    ->pageTextContains($body_value);
  $this->drupalGet('admin/config/content/formats');
  // Verify filter_test links.
  $this->assertSession()
    ->linkByHrefExists('/admin/config/content/formats/manage/filter_test/disable');
  $this->assertSession()
    ->linkByHrefNotExists('/admin/config/content/formats/manage/filter_test/enable');
  // Test the configure link appears for Filter test.
  $this->assertSession()
    ->elementExists('xpath', '//a[contains(@href, "/admin/config/content/formats/manage/filter_test") and text()="Configure"]');
  // Disable 'Filter test'.
  $this->getSession()
    ->getPage()
    ->find('css', '[href*="/admin/config/content/formats/manage/filter_test/disable"]')
    ->click();
  $this->assertSession()
    ->pageTextContains('Are you sure you want to disable the text format Filter test?');
  $this->getSession()
    ->getPage()
    ->find('css', '#edit-submit')
    ->click();
  // Verify filter_test links after filter_test is disabled.
  $this->assertSession()
    ->linkByHrefExists('/admin/config/content/formats/manage/filter_test/enable');
  $this->assertSession()
    ->linkByHrefNotExists('/admin/config/content/formats/manage/filter_test/disable');
  // Test the configure link doesn't appear for Filter test.
  $this->assertSession()
    ->elementNotExists('xpath', '//a[contains(@href, "/admin/config/content/formats/manage/filter_test") and text()="Configure"]');
  // Confirm the field using the now-disabled filter is not visible.
  $this->drupalGet($node->toUrl());
  $this->assertSession()
    ->statusCodeEquals(200);
  $this->assertSession()
    ->pageTextContains($title);
  $this->assertSession()
    ->pageTextNotContains($body_value);
  // Re-enable the filter that we disabled.
  $this->drupalGet('admin/config/content/formats');
  $this->getSession()
    ->getPage()
    ->find('css', '[href*="/admin/config/content/formats/manage/filter_test/enable"]')
    ->click();
  $this->assertSession()
    ->pageTextContains('Are you sure you want to enable the text format Filter test?');
  $this->getSession()
    ->getPage()
    ->find('css', '#edit-submit')
    ->click();
  // Confirm the presence of enable/disable operations has updated properly.
  $this->assertSession()
    ->linkByHrefExists('/admin/config/content/formats/manage/filter_test/disable');
  $this->assertSession()
    ->linkByHrefNotExists('/admin/config/content/formats/manage/filter_test/enable');
  $this->drupalGet($node->toUrl());
  $this->assertSession()
    ->pageTextContains($body_value);
}

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