function FilterAdminTest::testDisabledFormat

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

Tests whether a field using a disabled format is rendered.

File

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

Class

FilterAdminTest
Thoroughly test the administrative interface of the filter module.

Namespace

Drupal\Tests\filter\Functional

Code

public function testDisabledFormat() : void {
  // 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 text format with a filter that returns a static string.
  $format = FilterFormat::create([
    'name' => $this->randomString(),
    'format' => $format_id = $this->randomMachineName(),
  ]);
  $format->setFilterConfig('filter_static_text', [
    'status' => TRUE,
  ]);
  $format->save();
  // Create a new node of the new node type.
  $node = Node::create([
    'type' => $node_type->id(),
    'title' => $this->randomString(),
  ]);
  $body_value = $this->randomString();
  $node->body->value = $body_value;
  $node->body->format = $format_id;
  $node->save();
  // The format is used and we should see the static text instead of the body
  // value.
  $this->drupalGet($node->toUrl());
  $this->assertSession()
    ->pageTextContains('filtered text');
  // Disable the format.
  $format->disable()
    ->save();
  $this->drupalGet($node->toUrl());
  // The format is not used anymore.
  $this->assertSession()
    ->pageTextNotContains('filtered text');
  // The text is not displayed unfiltered or escaped.
  $this->assertSession()
    ->responseNotContains($body_value);
  $this->assertSession()
    ->assertNoEscaped($body_value);
  // Visit the dblog report page.
  $this->drupalLogin($this->adminUser);
  $this->drupalGet('admin/reports/dblog');
  // The correct message has been logged.
  $this->assertSession()
    ->pageTextContains(sprintf('Disabled text format: %s.', $format_id));
  // Programmatically change the text format to something random so we trigger
  // the missing text format message.
  $format_id = $this->randomMachineName();
  $node->body->format = $format_id;
  $node->save();
  $this->drupalGet($node->toUrl());
  // The text is not displayed unfiltered or escaped.
  $this->assertSession()
    ->responseNotContains($body_value);
  $this->assertSession()
    ->assertNoEscaped($body_value);
  // Visit the dblog report page.
  $this->drupalGet('admin/reports/dblog');
  // The missing text format message has been logged.
  $this->assertSession()
    ->pageTextContains(sprintf('Missing text format: %s.', $format_id));
}

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