public function FieldWebTest::testTextRendering in Zircon Profile 8.0
Same name and namespace in other branches
- 8 core/modules/views/src/Tests/Handler/FieldWebTest.php \Drupal\views\Tests\Handler\FieldWebTest::testTextRendering()
Tests trimming/read-more/ellipses.
File
- core/
modules/ views/ src/ Tests/ Handler/ FieldWebTest.php, line 500 - Contains \Drupal\views\Tests\Handler\FieldWebTest.
Class
- FieldWebTest
- Tests fields from within a UI.
Namespace
Drupal\views\Tests\HandlerCode
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 = Unicode::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, format_string('Make sure the trimmed output (@trimmed) appears in the rendered output (@output).', array(
'@trimmed' => $trimmed_name,
'@output' => $output,
)));
$this
->assertNotSubString($output, $row->views_test_data_name, format_string("Make sure the untrimmed value (@untrimmed) shouldn't appear in the rendered output (@output).", array(
'@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, format_string('Make sure the untrimmed (@untrimmed) output appears in the rendered output (@output).', array(
'@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 = array(
// Create one string which doesn't fit at all into the limit.
array(
'value' => $random_text_8,
'trimmed_value' => '',
'trimmed' => TRUE,
),
// Create one string with two words which doesn't fit both into the limit.
array(
'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.
array(
'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.
array(
'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'], format_string('The untrimmed value (@untrimmed) should not appear in the trimmed output (@output).', array(
'@untrimmed' => $tuple['value'],
'@output' => $output,
)));
}
if (!empty($tuple['trimmed_value'])) {
$this
->assertSubString($output, $tuple['trimmed_value'], format_string('The trimmed value (@trimmed) should appear in the trimmed output (@output).', array(
'@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
->assertTrue($this
->xpathContent($output, '//a[contains(@href, :path)]', array(
':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
->assertFalse($this
->xpathContent($output, '//a[contains(@href, :path)]', array(
':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');
}