FieldEntityTest.php in Drupal 9
File
core/modules/views/tests/src/Functional/Entity/FieldEntityTest.php
View source
<?php
namespace Drupal\Tests\views\Functional\Entity;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\node\Entity\Node;
use Drupal\user\Entity\User;
use Drupal\Tests\views\Functional\ViewTestBase;
use Drupal\views\Tests\ViewTestData;
use Drupal\views\Views;
use Drupal\comment\Entity\Comment;
class FieldEntityTest extends ViewTestBase {
use CommentTestTrait;
public static $testViews = [
'test_field_get_entity',
];
protected static $modules = [
'node',
'comment',
];
protected $defaultTheme = 'stark';
protected function setUp($import_test_views = TRUE) : void {
parent::setUp(FALSE);
$this
->drupalCreateContentType([
'type' => 'page',
]);
$this
->addDefaultCommentField('node', 'page');
ViewTestData::createTestViews(static::class, [
'views_test_config',
]);
}
public function testGetEntity() {
$account = User::create([
'name' => $this
->randomMachineName(),
'bundle' => 'user',
]);
$account
->save();
$node = Node::create([
'uid' => $account
->id(),
'type' => 'page',
'title' => $this
->randomString(),
]);
$node
->save();
$comment = Comment::create([
'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];
$entity = $view->field['cid']
->getEntity($row);
$this
->assertEquals($comment
->id(), $entity
->id(), 'Make sure the right comment entity got loaded.');
$entity = $view->field['nid']
->getEntity($row);
$this
->assertEquals($node
->id(), $entity
->id(), 'Make sure the right node entity got loaded.');
$entity = $view->field['uid']
->getEntity($row);
$this
->assertEquals($account
->id(), $entity
->id(), 'Make sure the right user entity got loaded.');
}
}
Classes
Name |
Description |
FieldEntityTest |
Tests the field plugin base integration with the entity system. |