function FieldRenderedEntityTest::testRenderedEntityWithoutAndWithField

Same name in other branches
  1. 11.x core/modules/views/tests/src/Kernel/Handler/FieldRenderedEntityTest.php \Drupal\Tests\views\Kernel\Handler\FieldRenderedEntityTest::testRenderedEntityWithoutAndWithField()

Tests the default rendered entity output.

File

core/modules/views/tests/src/Kernel/Handler/FieldRenderedEntityTest.php, line 114

Class

FieldRenderedEntityTest
Tests the core Drupal\views\Plugin\views\field\RenderedEntity handler.

Namespace

Drupal\Tests\views\Kernel\Handler

Code

public function testRenderedEntityWithoutAndWithField() : void {
    // First test without test_field displayed.
    \Drupal::currentUser()->setAccount($this->user);
    $display = EntityViewDisplay::load('entity_test.entity_test.foobar');
    $display->removeComponent('test_field')
        ->save();
    $view = Views::getView('test_field_entity_test_rendered');
    $build = [
        '#type' => 'view',
        '#name' => 'test_field_entity_test_rendered',
        '#view' => $view,
        '#display_id' => 'default',
    ];
    $renderer = \Drupal::service('renderer');
    $renderer->renderInIsolation($build);
    for ($i = 1; $i <= 3; $i++) {
        $view_field = (string) $view->style_plugin
            ->getField($i - 1, 'rendered_entity');
        $search_result = str_contains($view_field, "Test {$i}");
        $this->assertFalse($search_result, "The text 'Test {$i}' not found in the view.");
    }
    $this->assertConfigDependencies($view->storage);
    $this->assertCacheabilityMetadata($build);
    // Now show the test_field on the entity_test.entity_test.foobar view
    // display to confirm render is updated correctly.
    $display->setComponent('test_field', [
        'type' => 'string',
        'label' => 'above',
    ])
        ->save();
    // Need to reload the view because the rendered fields are statically cached
    // in the object.
    $view = Views::getView('test_field_entity_test_rendered');
    $build = [
        '#type' => 'view',
        '#name' => 'test_field_entity_test_rendered',
        '#view' => $view,
        '#display_id' => 'default',
    ];
    $renderer->renderInIsolation($build);
    for ($i = 1; $i <= 3; $i++) {
        $view_field = (string) $view->style_plugin
            ->getField($i - 1, 'rendered_entity');
        $search_result = str_contains($view_field, "Test {$i}");
        $this->assertTrue($search_result, "The text 'Test {$i}' found in the view.");
    }
    $this->assertConfigDependencies($view->storage);
    $this->assertCacheabilityMetadata($build);
}

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