You are here

public function NodeFilterTest::testTeaserFilter in Field Formatter Filter 8

Same name and namespace in other branches
  1. 2.0.x tests/src/Kernel/NodeFilterTest.php \Drupal\Tests\field_formatter_filter\Kernel\NodeFilterTest::testTeaserFilter()

Tests applying the text formatter to node teasers.

File

tests/src/Kernel/NodeFilterTest.php, line 109

Class

NodeFilterTest
Tests applying the filter formatter to a node.

Namespace

Drupal\Tests\field_formatter_filter\Kernel

Code

public function testTeaserFilter() {
  $node = $this
    ->createTestNode('fff_test_type');

  // When the node is set to promoted, we expect to see its teaser
  // on the /node page. Once you have views.module on at least.
  // But that would require BrowserTestBase
  // instead of KernelTestBase.
  // Artificially just call the render stack directly instead.
  $build = $this->container
    ->get('entity.manager')
    ->getViewBuilder('node')
    ->view($node, 'full');
  $output = \Drupal::service('renderer')
    ->renderRoot($build);

  // When rendered in 'full' mode, the bad markup should be there.
  $this
    ->assertTrue((bool) preg_match("/the real content of the body text/", $output), 'Full view of node contains expected markup');
  $this
    ->assertTrue((bool) preg_match("/<img/", $output), 'Full view of node contains messy markup');

  // But when rendered as a teaser, it must NOT be there.
  $build = $this->container
    ->get('entity.manager')
    ->getViewBuilder('node')
    ->view($node, 'teaser');
  $output = \Drupal::service('renderer')
    ->renderRoot($build);
  $this
    ->assertTrue((bool) preg_match("/the real content of the body text/", $output), 'Teaser view of node contains expected markup');
  $this
    ->assertFalse((bool) preg_match("/<img/", $output), 'Teaser view of node does not contain messy markup');
}