function RowEntityTest::testEntityRow

Same name in other branches
  1. 9 core/modules/views/tests/src/Kernel/Plugin/RowEntityTest.php \Drupal\Tests\views\Kernel\Plugin\RowEntityTest::testEntityRow()
  2. 8.9.x core/modules/views/tests/src/Kernel/Plugin/RowEntityTest.php \Drupal\Tests\views\Kernel\Plugin\RowEntityTest::testEntityRow()
  3. 10 core/modules/views/tests/src/Kernel/Plugin/RowEntityTest.php \Drupal\Tests\views\Kernel\Plugin\RowEntityTest::testEntityRow()

Tests the entity row handler.

File

core/modules/views/tests/src/Kernel/Plugin/RowEntityTest.php, line 51

Class

RowEntityTest
Tests the generic entity row plugin.

Namespace

Drupal\Tests\views\Kernel\Plugin

Code

public function testEntityRow() : void {
    $user = User::create([
        'name' => 'test user',
    ]);
    $user->save();
    $entity_test = EntityTest::create([
        'user_id' => $user->id(),
        'name' => 'test entity test',
    ]);
    $entity_test->save();
    // Ensure entities have different ids.
    if ($entity_test->id() == $user->id()) {
        $entity_test->delete();
        $entity_test = EntityTest::create([
            'user_id' => $user->id(),
            'name' => 'test entity test',
        ]);
        $entity_test->save();
    }
    $view = Views::getView('test_entity_row');
    $build = $view->preview();
    $this->render($build);
    $this->assertText('test entity test');
    $this->assertNoText('Member for');
    // Change the view to use a relationship to render the row.
    $view = Views::getView('test_entity_row');
    $display =& $view->storage
        ->getDisplay('default');
    $display['display_options']['row']['type'] = 'entity:user';
    $display['display_options']['row']['options']['relationship'] = 'user_id';
    $view->setDisplay('default');
    $build = $view->preview();
    $this->render($build);
    $this->assertNoText('test entity test');
    $this->assertText('Member for');
    // Tests the available view mode options.
    $form = [];
    $form_state = new FormState();
    $form_state->set('view', $view->storage);
    $view->rowPlugin
        ->buildOptionsForm($form, $form_state);
    $this->assertTrue(isset($form['view_mode']['#options']['default']), 'Ensure that the default view mode is available');
}

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