You are here

public function FieldWebTest::testTextRendering in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/views/tests/src/Functional/Handler/FieldWebTest.php \Drupal\Tests\views\Functional\Handler\FieldWebTest::testTextRendering()
  2. 10 core/modules/views/tests/src/Functional/Handler/FieldWebTest.php \Drupal\Tests\views\Functional\Handler\FieldWebTest::testTextRendering()

Tests trimming/read-more/ellipses.

File

core/modules/views/tests/src/Functional/Handler/FieldWebTest.php, line 507

Class

FieldWebTest
Tests fields from within a UI.

Namespace

Drupal\Tests\views\Functional\Handler

Code

public function testTextRendering() {

  /** @var \Drupal\Core\Render\RendererInterface $renderer */
  $renderer = \Drupal::service('renderer');
  $view = Views::getView('test_field_output');
  $view
    ->initHandlers();
  $name_field = $view->field['name'];

  // Tests stripping of html elements.
  $this
    ->executeView($view);
  $random_text = $this
    ->randomMachineName();
  $name_field->options['alter']['alter_text'] = TRUE;
  $name_field->options['alter']['text'] = $html_text = '<div class="views-test">' . $random_text . '</div>';
  $row = $view->result[0];
  $name_field->options['alter']['strip_tags'] = TRUE;
  $output = $renderer
    ->executeInRenderContext(new RenderContext(), function () use ($name_field, $row) {
    return $name_field
      ->advancedRender($row);
  });
  $this
    ->assertSubString($output, $random_text, 'Find text without html if stripping of views field output is enabled.');
  $this
    ->assertNotSubString($output, $html_text, 'Find no text with the html if stripping of views field output is enabled.');

  // Tests preserving of html tags.
  $name_field->options['alter']['preserve_tags'] = '<div>';
  $output = $renderer
    ->executeInRenderContext(new RenderContext(), function () use ($name_field, $row) {
    return $name_field
      ->advancedRender($row);
  });
  $this
    ->assertSubString($output, $random_text, 'Find text without html if stripping of views field output is enabled but a div is allowed.');
  $this
    ->assertSubString($output, $html_text, 'Find text with the html if stripping of views field output is enabled but a div is allowed.');
  $name_field->options['alter']['strip_tags'] = FALSE;
  $output = $renderer
    ->executeInRenderContext(new RenderContext(), function () use ($name_field, $row) {
    return $name_field
      ->advancedRender($row);
  });
  $this
    ->assertSubString($output, $random_text, 'Find text without html if stripping of views field output is disabled.');
  $this
    ->assertSubString($output, $html_text, 'Find text with the html if stripping of views field output is disabled.');

  // Tests for removing whitespace and the beginning and the end.
  $name_field->options['alter']['alter_text'] = FALSE;
  $views_test_data_name = $row->views_test_data_name;
  $row->views_test_data_name = '  ' . $views_test_data_name . '     ';
  $name_field->options['alter']['trim_whitespace'] = TRUE;
  $output = $renderer
    ->executeInRenderContext(new RenderContext(), function () use ($name_field, $row) {
    return $name_field
      ->advancedRender($row);
  });
  $this
    ->assertSubString($output, $views_test_data_name, 'Make sure the trimmed text can be found if trimming is enabled.');
  $this
    ->assertNotSubString($output, $row->views_test_data_name, 'Make sure the untrimmed text can be found if trimming is enabled.');
  $name_field->options['alter']['trim_whitespace'] = FALSE;
  $output = $renderer
    ->executeInRenderContext(new RenderContext(), function () use ($name_field, $row) {
    return $name_field
      ->advancedRender($row);
  });
  $this
    ->assertSubString($output, $views_test_data_name, 'Make sure the trimmed text can be found if trimming is disabled.');
  $this
    ->assertSubString($output, $row->views_test_data_name, 'Make sure the untrimmed text can be found  if trimming is disabled.');

  // Tests for trimming to a maximum length.
  $name_field->options['alter']['trim'] = TRUE;
  $name_field->options['alter']['word_boundary'] = FALSE;

  // Tests for simple trimming by string length.
  $row->views_test_data_name = $this
    ->randomMachineName(8);
  $name_field->options['alter']['max_length'] = 5;
  $trimmed_name = mb_substr($row->views_test_data_name, 0, 5);
  $output = $renderer
    ->executeInRenderContext(new RenderContext(), function () use ($name_field, $row) {
    return $name_field
      ->advancedRender($row);
  });
  $this
    ->assertSubString($output, $trimmed_name, new FormattableMarkup('Make sure the trimmed output (@trimmed) appears in the rendered output (@output).', [
    '@trimmed' => $trimmed_name,
    '@output' => $output,
  ]));
  $this
    ->assertNotSubString($output, $row->views_test_data_name, new FormattableMarkup("Make sure the untrimmed value (@untrimmed) shouldn't appear in the rendered output (@output).", [
    '@untrimmed' => $row->views_test_data_name,
    '@output' => $output,
  ]));
  $name_field->options['alter']['max_length'] = 9;
  $output = $renderer
    ->executeInRenderContext(new RenderContext(), function () use ($name_field, $row) {
    return $name_field
      ->advancedRender($row);
  });
  $this
    ->assertSubString($output, $trimmed_name, new FormattableMarkup('Make sure the untrimmed (@untrimmed) output appears in the rendered output  (@output).', [
    '@trimmed' => $trimmed_name,
    '@output' => $output,
  ]));

  // Take word_boundary into account for the tests.
  $name_field->options['alter']['max_length'] = 5;
  $name_field->options['alter']['word_boundary'] = TRUE;
  $random_text_2 = $this
    ->randomMachineName(2);
  $random_text_4 = $this
    ->randomMachineName(4);
  $random_text_8 = $this
    ->randomMachineName(8);
  $tuples = [
    // Create one string which doesn't fit at all into the limit.
    [
      'value' => $random_text_8,
      'trimmed_value' => '',
      'trimmed' => TRUE,
    ],
    // Create one string with two words which doesn't fit both into the limit.
    [
      'value' => $random_text_8 . ' ' . $random_text_8,
      'trimmed_value' => '',
      'trimmed' => TRUE,
    ],
    // Create one string which contains of two words, of which only the first
    // fits into the limit.
    [
      'value' => $random_text_4 . ' ' . $random_text_8,
      'trimmed_value' => $random_text_4,
      'trimmed' => TRUE,
    ],
    // Create one string which contains of two words, of which both fits into
    // the limit.
    [
      'value' => $random_text_2 . ' ' . $random_text_2,
      'trimmed_value' => $random_text_2 . ' ' . $random_text_2,
      'trimmed' => FALSE,
    ],
  ];
  foreach ($tuples as $tuple) {
    $row->views_test_data_name = $tuple['value'];
    $output = $renderer
      ->executeInRenderContext(new RenderContext(), function () use ($name_field, $row) {
      return $name_field
        ->advancedRender($row);
    });
    if ($tuple['trimmed']) {
      $this
        ->assertNotSubString($output, $tuple['value'], new FormattableMarkup('The untrimmed value (@untrimmed) should not appear in the trimmed output (@output).', [
        '@untrimmed' => $tuple['value'],
        '@output' => $output,
      ]));
    }
    if (!empty($tuple['trimmed_value'])) {
      $this
        ->assertSubString($output, $tuple['trimmed_value'], new FormattableMarkup('The trimmed value (@trimmed) should appear in the trimmed output (@output).', [
        '@trimmed' => $tuple['trimmed_value'],
        '@output' => $output,
      ]));
    }
  }

  // Tests for displaying a readmore link when the output got trimmed.
  $row->views_test_data_name = $this
    ->randomMachineName(8);
  $name_field->options['alter']['max_length'] = 5;
  $name_field->options['alter']['more_link'] = TRUE;
  $name_field->options['alter']['more_link_text'] = $more_text = $this
    ->randomMachineName();
  $name_field->options['alter']['more_link_path'] = $more_path = $this
    ->randomMachineName();
  $output = $renderer
    ->executeInRenderContext(new RenderContext(), function () use ($name_field, $row) {
    return $name_field
      ->advancedRender($row);
  });
  $this
    ->assertSubString($output, $more_text, 'Make sure a read more text is displayed if the output got trimmed');
  $this
    ->assertNotEmpty($this
    ->xpathContent($output, '//a[contains(@href, :path)]', [
    ':path' => $more_path,
  ]), 'Make sure the read more link points to the right destination.');
  $name_field->options['alter']['more_link'] = FALSE;
  $output = $renderer
    ->executeInRenderContext(new RenderContext(), function () use ($name_field, $row) {
    return $name_field
      ->advancedRender($row);
  });
  $this
    ->assertNotSubString($output, $more_text, 'Make sure no read more text appears.');
  $this
    ->assertEmpty($this
    ->xpathContent($output, '//a[contains(@href, :path)]', [
    ':path' => $more_path,
  ]), 'Make sure no read more link appears.');

  // Check for the ellipses.
  $row->views_test_data_name = $this
    ->randomMachineName(8);
  $name_field->options['alter']['max_length'] = 5;
  $output = $renderer
    ->executeInRenderContext(new RenderContext(), function () use ($name_field, $row) {
    return $name_field
      ->advancedRender($row);
  });
  $this
    ->assertSubString($output, '…', 'An ellipsis should appear if the output is trimmed');
  $name_field->options['alter']['max_length'] = 10;
  $output = $renderer
    ->executeInRenderContext(new RenderContext(), function () use ($name_field, $row) {
    return $name_field
      ->advancedRender($row);
  });
  $this
    ->assertNotSubString($output, '…', 'No ellipsis should appear if the output is not trimmed');
}