EntityViewControllerTest.php in Drupal 8
File
core/modules/system/tests/src/Functional/Entity/EntityViewControllerTest.php
View source
<?php
namespace Drupal\Tests\system\Functional\Entity;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\Tests\BrowserTestBase;
class EntityViewControllerTest extends BrowserTestBase {
public static $modules = [
'entity_test',
];
protected $defaultTheme = 'classy';
protected $entities = [];
protected function setUp() {
parent::setUp();
for ($i = 0; $i < 2; $i++) {
$entity_test = $this
->createTestEntity('entity_test');
$entity_test
->save();
$this->entities[] = $entity_test;
}
$this
->drupalLogin($this
->drupalCreateUser([
'view test entity',
]));
}
public function testEntityViewController() {
$get_label_markup = function ($label) {
return '<h1 class="page-title">
<div class="field field--name-name field--type-string field--label-hidden field__item">' . $label . '</div>
</h1>';
};
foreach ($this->entities as $entity) {
$this
->drupalGet('entity_test/' . $entity
->id());
$this
->assertRaw($entity
->label());
$this
->assertRaw($get_label_markup($entity
->label()));
$this
->assertRaw('full');
$this
->drupalGet('entity_test_converter/' . $entity
->id());
$this
->assertRaw($entity
->label());
$this
->assertRaw('full');
$this
->drupalGet('entity_test_no_view_mode/' . $entity
->id());
$this
->assertRaw($entity
->label());
$this
->assertRaw('full');
}
$entity_test_rev = $this
->createTestEntity('entity_test_rev');
$entity_test_rev
->save();
$entity_test_rev->name->value = 'rev 2';
$entity_test_rev
->setNewRevision(TRUE);
$entity_test_rev
->isDefaultRevision(TRUE);
$entity_test_rev
->save();
$this
->drupalGet('entity_test_rev/' . $entity_test_rev
->id() . '/revision/' . $entity_test_rev->revision_id->value . '/view');
$this
->assertRaw($entity_test_rev
->label());
$this
->assertRaw($get_label_markup($entity_test_rev
->label()));
$this
->drupalGet('entity_test/invalid');
$this
->assertSession()
->statusCodeEquals(404);
}
public function testFieldItemAttributes() {
\Drupal::service('entity_display.repository')
->getViewDisplay('entity_test', 'entity_test')
->setComponent('field_test_text', [
'type' => 'text_default',
])
->save();
$test_value = $this
->randomMachineName();
$entity = EntityTest::create();
$entity->field_test_text = $test_value;
$entity
->save();
$this
->drupalGet('entity_test/' . $entity
->id());
$xpath = $this
->xpath('//div[@data-field-item-attr="foobar"]/p[text()=:value]', [
':value' => $test_value,
]);
$this
->assertNotEmpty($xpath, 'The field item attribute has been found in the rendered output of the field.');
\Drupal::service('module_installer')
->install([
'rdf',
]);
$this
->resetAll();
$mapping = rdf_get_mapping('entity_test', 'entity_test');
$mapping
->setFieldMapping('field_test_text', [
'properties' => [
'schema:text',
],
])
->save();
$this
->drupalGet('entity_test/' . $entity
->id());
$xpath = $this
->xpath('//div[@data-field-item-attr="foobar" and @property="schema:text"]/p[text()=:value]', [
':value' => $test_value,
]);
$this
->assertNotEmpty($xpath, 'The field item attributes from both modules have been found in the rendered output of the field.');
}
public function testEntityViewControllerViewBuilder() {
$entity_test = $this
->createTestEntity('entity_test_view_builder');
$entity_test
->save();
$this
->drupalGet('entity_test_view_builder/' . $entity_test
->id());
$this
->assertText($entity_test
->label());
}
protected function createTestEntity($entity_type) {
$data = [
'bundle' => $entity_type,
'name' => $this
->randomMachineName(),
];
return $this->container
->get('entity_type.manager')
->getStorage($entity_type)
->create($data);
}
}