2393b6d2699390a035c1ff7d126062d63b00b882
[yaffs-website] / web / core / modules / views / tests / src / Functional / Entity / FieldEntityTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Functional\Entity;
4
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;
12
13 /**
14  * Tests the field plugin base integration with the entity system.
15  *
16  * @group views
17  */
18 class FieldEntityTest extends ViewTestBase {
19
20   use CommentTestTrait;
21
22   /**
23    * Views used by this test.
24    *
25    * @var array
26    */
27   public static $testViews = ['test_field_get_entity'];
28
29   /**
30    * Modules to enable.
31    *
32    * @var array
33    */
34   public static $modules = ['node', 'comment'];
35
36   /**
37    * {@inheritdoc}
38    */
39   protected function setUp($import_test_views = TRUE) {
40     parent::setUp(FALSE);
41
42     $this->drupalCreateContentType(['type' => 'page']);
43     $this->addDefaultCommentField('node', 'page');
44
45     ViewTestData::createTestViews(get_class($this), ['views_test_config']);
46   }
47
48   /**
49    * Tests the getEntity method.
50    */
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.
54
55     $account = User::create(['name' => $this->randomMachineName(), 'bundle' => 'user']);
56     $account->save();
57
58     $node = Node::create([
59       'uid' => $account->id(),
60       'type' => 'page',
61       'title' => $this->randomString(),
62     ]);
63     $node->save();
64     $comment = Comment::create([
65       'uid' => $account->id(),
66       'entity_id' => $node->id(),
67       'entity_type' => 'node',
68       'field_name' => 'comment',
69     ]);
70     $comment->save();
71
72     $user = $this->drupalCreateUser(['access comments']);
73     $this->drupalLogin($user);
74
75     $view = Views::getView('test_field_get_entity');
76     $this->executeView($view);
77     $row = $view->result[0];
78
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.');
88   }
89
90 }