3 namespace Drupal\Tests\views\Functional\Entity;
5 use Drupal\comment\Tests\CommentTestTrait;
6 use Drupal\node\Entity\Node;
7 use Drupal\user\Entity\User;
8 use Drupal\Tests\views\Functional\ViewTestBase;
9 use Drupal\views\Tests\ViewTestData;
10 use Drupal\views\Views;
11 use Drupal\comment\Entity\Comment;
14 * Tests the field plugin base integration with the entity system.
18 class FieldEntityTest extends ViewTestBase {
23 * Views used by this test.
27 public static $testViews = ['test_field_get_entity'];
34 public static $modules = ['node', 'comment'];
39 protected function setUp($import_test_views = TRUE) {
42 $this->drupalCreateContentType(['type' => 'page']);
43 $this->addDefaultCommentField('node', 'page');
45 ViewTestData::createTestViews(get_class($this), ['views_test_config']);
49 * Tests the getEntity method.
51 public function testGetEntity() {
52 // The view is a view of comments, their nodes and their authors, so there
53 // are three layers of entities.
55 $account = User::create(['name' => $this->randomMachineName(), 'bundle' => 'user']);
58 $node = Node::create([
59 'uid' => $account->id(),
61 'title' => $this->randomString(),
64 $comment = Comment::create([
65 'uid' => $account->id(),
66 'entity_id' => $node->id(),
67 'entity_type' => 'node',
68 'field_name' => 'comment'
72 $user = $this->drupalCreateUser(['access comments']);
73 $this->drupalLogin($user);
75 $view = Views::getView('test_field_get_entity');
76 $this->executeView($view);
77 $row = $view->result[0];
79 // Tests entities on the base level.
80 $entity = $view->field['cid']->getEntity($row);
81 $this->assertEqual($entity->id(), $comment->id(), 'Make sure the right comment entity got loaded.');
82 // Tests entities as relationship on first level.
83 $entity = $view->field['nid']->getEntity($row);
84 $this->assertEqual($entity->id(), $node->id(), 'Make sure the right node entity got loaded.');
85 // Tests entities as relationships on second level.
86 $entity = $view->field['uid']->getEntity($row);
87 $this->assertEqual($entity->id(), $account->id(), 'Make sure the right user entity got loaded.');