function FieldKernelTest::_testEmptyText

Same name in other branches
  1. 8.9.x core/modules/views/tests/src/Kernel/Handler/FieldKernelTest.php \Drupal\Tests\views\Kernel\Handler\FieldKernelTest::_testEmptyText()
  2. 10 core/modules/views/tests/src/Kernel/Handler/FieldKernelTest.php \Drupal\Tests\views\Kernel\Handler\FieldKernelTest::_testEmptyText()
  3. 11.x core/modules/views/tests/src/Kernel/Handler/FieldKernelTest.php \Drupal\Tests\views\Kernel\Handler\FieldKernelTest::_testEmptyText()

Tests the usage of the empty text.

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

File

core/modules/views/tests/src/Kernel/Handler/FieldKernelTest.php, line 680

Class

FieldKernelTest
Tests the generic field handler.

Namespace

Drupal\Tests\views\Kernel\Handler

Code

public 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->assertSame($empty_text, (string) $render, '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->assertSame("0", (string) $render, '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->assertSame($empty_text, (string) $render, '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->assertSame($alter_text, (string) $render, '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->assertSame($empty_text, (string) $render, 'If a field is empty, some rewrite text exists, and hide_alter_empty is checked, use the empty text.');
}

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