View source
<?php
namespace Drupal\views\Tests\Entity;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\views\Tests\ViewTestBase;
use Drupal\views\Tests\ViewTestData;
use Drupal\views\Views;
class FieldEntityTest extends ViewTestBase {
use CommentTestTrait;
public static $testViews = array(
'test_field_get_entity',
);
public static $modules = array(
'node',
'comment',
);
protected function setUp($import_test_views = TRUE) {
parent::setUp(FALSE);
$this
->drupalCreateContentType(array(
'type' => 'page',
));
$this
->addDefaultCommentField('node', 'page');
ViewTestData::createTestViews(get_class($this), array(
'views_test_config',
));
}
public function testGetEntity() {
$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];
$entity = $view->field['cid']
->getEntity($row);
$this
->assertEqual($entity
->id(), $comment
->id(), 'Make sure the right comment entity got loaded.');
$entity = $view->field['nid']
->getEntity($row);
$this
->assertEqual($entity
->id(), $node
->id(), 'Make sure the right node entity got loaded.');
$entity = $view->field['uid']
->getEntity($row);
$this
->assertEqual($entity
->id(), $account
->id(), 'Make sure the right user entity got loaded.');
}
}