You are here

public function FieldEntityTest::testGetEntity in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/views/src/Tests/Entity/FieldEntityTest.php \Drupal\views\Tests\Entity\FieldEntityTest::testGetEntity()

Tests the getEntity method.

File

core/modules/views/src/Tests/Entity/FieldEntityTest.php, line 53
Contains \Drupal\views\Tests\Entity\FieldEntityTest.

Class

FieldEntityTest
Tests the field plugin base integration with the entity system.

Namespace

Drupal\views\Tests\Entity

Code

public function testGetEntity() {

  // The view is a view of comments, their nodes and their authors, so there
  // are three layers of entities.
  $account = entity_create('user', array(
    'name' => $this
      ->randomMachineName(),
    'bundle' => 'user',
  ));
  $account
    ->save();
  $node = entity_create('node', array(
    'uid' => $account
      ->id(),
    'type' => 'page',
    'title' => $this
      ->randomString(),
  ));
  $node
    ->save();
  $comment = entity_create('comment', array(
    'uid' => $account
      ->id(),
    'entity_id' => $node
      ->id(),
    'entity_type' => 'node',
    'field_name' => 'comment',
  ));
  $comment
    ->save();
  $user = $this
    ->drupalCreateUser([
    'access comments',
  ]);
  $this
    ->drupalLogin($user);
  $view = Views::getView('test_field_get_entity');
  $this
    ->executeView($view);
  $row = $view->result[0];

  // Tests entities on the base level.
  $entity = $view->field['cid']
    ->getEntity($row);
  $this
    ->assertEqual($entity
    ->id(), $comment
    ->id(), 'Make sure the right comment entity got loaded.');

  // Tests entities as relationship on first level.
  $entity = $view->field['nid']
    ->getEntity($row);
  $this
    ->assertEqual($entity
    ->id(), $node
    ->id(), 'Make sure the right node entity got loaded.');

  // Tests entities as relationships on second level.
  $entity = $view->field['uid']
    ->getEntity($row);
  $this
    ->assertEqual($entity
    ->id(), $account
    ->id(), 'Make sure the right user entity got loaded.');
}