You are here

function FieldKernelTest::_testEmptyText in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/views/src/Tests/Handler/FieldKernelTest.php \Drupal\views\Tests\Handler\FieldKernelTest::_testEmptyText()

Tests the usage of the empty text.

1 call to FieldKernelTest::_testEmptyText()
FieldKernelTest::testEmpty in core/modules/views/src/Tests/Handler/FieldKernelTest.php
Tests everything related to empty output of a field.

File

core/modules/views/src/Tests/Handler/FieldKernelTest.php, line 651
Contains \Drupal\views\Tests\Handler\FieldKernelTest.

Class

FieldKernelTest
Tests the generic field handler.

Namespace

Drupal\views\Tests\Handler

Code

function _testEmptyText() {

  /** @var \Drupal\Core\Render\RendererInterface $renderer */
  $renderer = \Drupal::service('renderer');
  $view = Views::getView('test_view');
  $view
    ->initDisplay();
  $this
    ->executeView($view);
  $column_map_reversed = array_flip($this->columnMap);
  $view->row_index = 0;
  $empty_text = $view->field['name']->options['empty'] = $this
    ->randomMachineName();
  $view->result[0]->{$column_map_reversed['name']} = "";
  $render = $renderer
    ->executeInRenderContext(new RenderContext(), function () use ($view) {
    return $view->field['name']
      ->advancedRender($view->result[0]);
  });
  $this
    ->assertIdentical((string) $render, $empty_text, 'If a field is empty, the empty text should be used for the output.');
  $view->result[0]->{$column_map_reversed['name']} = "0";
  $render = $renderer
    ->executeInRenderContext(new RenderContext(), function () use ($view) {
    return $view->field['name']
      ->advancedRender($view->result[0]);
  });
  $this
    ->assertIdentical((string) $render, "0", 'If a field is 0 and empty_zero is not checked, the empty text should not be used for the output.');
  $view->result[0]->{$column_map_reversed['name']} = "0";
  $view->field['name']->options['empty_zero'] = TRUE;
  $render = $renderer
    ->executeInRenderContext(new RenderContext(), function () use ($view) {
    return $view->field['name']
      ->advancedRender($view->result[0]);
  });
  $this
    ->assertIdentical((string) $render, $empty_text, 'If a field is 0 and empty_zero is checked, the empty text should be used for the output.');
  $view->result[0]->{$column_map_reversed['name']} = "";
  $view->field['name']->options['alter']['alter_text'] = TRUE;
  $alter_text = $view->field['name']->options['alter']['text'] = $this
    ->randomMachineName();
  $view->field['name']->options['hide_alter_empty'] = FALSE;
  $render = $renderer
    ->executeInRenderContext(new RenderContext(), function () use ($view) {
    return $view->field['name']
      ->advancedRender($view->result[0]);
  });
  $this
    ->assertIdentical((string) $render, $alter_text, 'If a field is empty, some rewrite text exists, but hide_alter_empty is not checked, render the rewrite text.');
  $view->field['name']->options['hide_alter_empty'] = TRUE;
  $render = $renderer
    ->executeInRenderContext(new RenderContext(), function () use ($view) {
    return $view->field['name']
      ->advancedRender($view->result[0]);
  });
  $this
    ->assertIdentical((string) $render, $empty_text, 'If a field is empty, some rewrite text exists, and hide_alter_empty is checked, use the empty text.');
}