function FieldWebTest::testTextRendering

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. 8.9.x core/modules/views/tests/src/Functional/Handler/FieldWebTest.php \Drupal\Tests\views\Functional\Handler\FieldWebTest::testTextRendering()
  3. 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 522

Class

FieldWebTest
Tests fields from within a UI.

Namespace

Drupal\Tests\views\Functional\Handler

Code

public function testTextRendering() : void {
    
    /** @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 = (string) $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 = (string) $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 = (string) $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 = (string) $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 = (string) $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 = (string) $renderer->executeInRenderContext(new RenderContext(), function () use ($name_field, $row) {
        return $name_field->advancedRender($row);
    });
    $this->assertSubString($output, $trimmed_name, "Make sure the trimmed output ({$trimmed_name}) appears in the rendered output ({$output}).");
    $this->assertNotSubString($output, $row->views_test_data_name, "Make sure the untrimmed value ({$row->views_test_data_name}) shouldn't appear in the rendered output ({$output}).");
    $name_field->options['alter']['max_length'] = 9;
    $output = (string) $renderer->executeInRenderContext(new RenderContext(), function () use ($name_field, $row) {
        return $name_field->advancedRender($row);
    });
    $this->assertSubString($output, $trimmed_name, "Make sure the untrimmed ({$trimmed_name}) output appears in the rendered 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 = (string) $renderer->executeInRenderContext(new RenderContext(), function () use ($name_field, $row) {
            return $name_field->advancedRender($row);
        });
        if ($tuple['trimmed']) {
            $this->assertNotSubString($output, $tuple['value'], "The untrimmed value ({$tuple['value']}) should not appear in the trimmed output ({$output}).");
        }
        if (!empty($tuple['trimmed_value'])) {
            $this->assertSubString($output, $tuple['trimmed_value'], "The trimmed value ({$tuple['trimmed_value']}) should appear in the trimmed 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 = (string) $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 = (string) $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 = (string) $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 = (string) $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');
}

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