View source
<?php
namespace Drupal\KernelTests\Core\Entity;
use Drupal\Core\Entity\EntityViewBuilder;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Entity\Entity\EntityViewMode;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
class EntityViewBuilderTest extends EntityKernelTestBase {
use EntityReferenceTestTrait;
protected function setUp() : void {
parent::setUp();
$this
->installConfig([
'user',
'entity_test',
]);
Role::load(RoleInterface::ANONYMOUS_ID)
->grantPermission('view test entity')
->save();
}
public function testEntityViewBuilderCache() {
$renderer = $this->container
->get('renderer');
$cache_contexts_manager = \Drupal::service("cache_contexts_manager");
$cache = \Drupal::cache();
$request = \Drupal::request();
$request_method = $request->server
->get('REQUEST_METHOD');
$request
->setMethod('GET');
$entity_test = $this
->createTestEntity('entity_test');
$build = $this->container
->get('entity_type.manager')
->getViewBuilder('entity_test')
->view($entity_test, 'full');
$this
->assertNotEmpty($build['#cache']);
$this
->assertEquals([
'tags',
'contexts',
'max-age',
], array_keys($build['#cache']), 'The render array element of new (unsaved) entities is not cached, but does have cache tags set.');
$entity_test
->save();
$build = $this->container
->get('entity_type.manager')
->getViewBuilder('entity_test')
->view($entity_test, 'full');
$cid_parts = array_merge($build['#cache']['keys'], $cache_contexts_manager
->convertTokensToKeys([
'languages:' . LanguageInterface::TYPE_INTERFACE,
'theme',
'user.permissions',
])
->getKeys());
$cid = implode(':', $cid_parts);
$bin = $build['#cache']['bin'];
unset($build['#theme']);
$build['#markup'] = 'entity_render_test';
$renderer
->renderRoot($build);
$this
->assertNotEmpty($this->container
->get('cache.' . $bin)
->get($cid), 'The entity render element has been cached.');
$cache
->set('kittens', 'Kitten data', Cache::PERMANENT, $build['#cache']['tags']);
$entity_test
->save();
$this
->assertFalse($this->container
->get('cache.' . $bin)
->get($cid), 'The entity render cache has been cleared when the entity was saved.');
$this
->assertFalse($cache
->get('kittens'), 'The entity saving has invalidated cache tags.');
unset($build['#printed']);
$renderer
->renderRoot($build);
$this
->assertNotEmpty($this->container
->get('cache.' . $bin)
->get($cid), 'The entity render element has been cached.');
$entity_test
->delete();
$this
->assertFalse($this->container
->get('cache.' . $bin)
->get($cid), 'The entity render cache has been cleared when the entity was deleted.');
$request
->setMethod($request_method);
}
public function testEntityViewBuilderCacheWithReferences() {
$renderer = $this->container
->get('renderer');
$cache_contexts_manager = \Drupal::service("cache_contexts_manager");
$request = \Drupal::request();
$request_method = $request->server
->get('REQUEST_METHOD');
$request
->setMethod('GET');
$this
->createEntityReferenceField('entity_test', 'entity_test', 'reference_field', 'Reference', 'entity_test');
\Drupal::service('entity_display.repository')
->getViewDisplay('entity_test', 'entity_test', 'full')
->setComponent('reference_field', [
'type' => 'entity_reference_entity_view',
'settings' => [
'link' => FALSE,
],
])
->save();
$entity_test_reference = $this
->createTestEntity('entity_test');
$entity_test_reference
->save();
$build = $this->container
->get('entity_type.manager')
->getViewBuilder('entity_test')
->view($entity_test_reference, 'full');
$cid_parts = array_merge($build['#cache']['keys'], $cache_contexts_manager
->convertTokensToKeys([
'languages:' . LanguageInterface::TYPE_INTERFACE,
'theme',
'user.permissions',
])
->getKeys());
$cid_reference = implode(':', $cid_parts);
$bin_reference = $build['#cache']['bin'];
unset($build['#theme']);
$build['#markup'] = 'entity_render_test';
$renderer
->renderRoot($build);
$this
->assertNotEmpty($this->container
->get('cache.' . $bin_reference)
->get($cid_reference), 'The entity render element for the referenced entity has been cached.');
$entity_test = $this
->createTestEntity('entity_test');
$entity_test->reference_field->entity = $entity_test_reference;
$entity_test
->save();
$build = $this->container
->get('entity_type.manager')
->getViewBuilder('entity_test')
->view($entity_test, 'full');
$cid_parts = array_merge($build['#cache']['keys'], $cache_contexts_manager
->convertTokensToKeys([
'languages:' . LanguageInterface::TYPE_INTERFACE,
'theme',
'user.permissions',
])
->getKeys());
$cid = implode(':', $cid_parts);
$bin = $build['#cache']['bin'];
unset($build['#theme']);
$build['#markup'] = 'entity_render_test';
$renderer
->renderRoot($build);
$this
->assertNotEmpty($this->container
->get('cache.' . $bin)
->get($cid), 'The entity render element has been cached.');
$entity_test_reference
->save();
$this
->assertFalse($this->container
->get('cache.' . $bin)
->get($cid), 'The entity render cache has been cleared when the entity was deleted.');
$this
->assertFalse($this->container
->get('cache.' . $bin_reference)
->get($cid_reference), 'The entity render cache for the referenced entity has been cleared when the entity was deleted.');
$request
->setMethod($request_method);
}
public function testEntityViewBuilderCacheToggling() {
$entity_test = $this
->createTestEntity('entity_test');
$entity_test
->save();
$build = $this->container
->get('entity_type.manager')
->getViewBuilder('entity_test')
->view($entity_test, 'full');
$this
->assertNotEmpty($build['#cache']);
$this
->assertEquals([
'tags',
'contexts',
'max-age',
'keys',
'bin',
], array_keys($build['#cache']), 'A view mode with render cache enabled has the correct output (cache tags, keys, contexts, max-age and bin).');
$build = $this->container
->get('entity_type.manager')
->getViewBuilder('entity_test')
->view($entity_test, 'test');
$this
->assertNotEmpty($build['#cache']);
$this
->assertEquals([
'tags',
'contexts',
'max-age',
], array_keys($build['#cache']), 'A view mode with render cache disabled has the correct output (only cache tags, contexts and max-age).');
$this
->installEntitySchema('entity_test_label');
$entity_test_no_cache = $this
->createTestEntity('entity_test_label');
$entity_test_no_cache
->save();
$build = $this->container
->get('entity_type.manager')
->getViewBuilder('entity_test_label')
->view($entity_test_no_cache, 'full');
$this
->assertNotEmpty($build['#cache']);
$this
->assertEquals([
'tags',
'contexts',
'max-age',
], array_keys($build['#cache']), 'An entity type can opt out of render caching regardless of view mode configuration, but always has cache tags, contexts and max-age set.');
}
public function testEntityViewBuilderWeight() {
$renderer = $this->container
->get('renderer');
\Drupal::service('entity_display.repository')
->getViewDisplay('entity_test', 'entity_test', 'full')
->setComponent('label', [
'weight' => 20,
])
->save();
$entity_test = $this
->createTestEntity('entity_test');
$view = $this->container
->get('entity_type.manager')
->getViewBuilder('entity_test')
->view($entity_test, 'full');
$renderer
->renderRoot($view);
$this
->assertEquals(20, $view['label']['#weight'], 'The weight of a display component is respected.');
}
public function testViewField() {
Role::load(RoleInterface::ANONYMOUS_ID)
->grantPermission('view test entity translations')
->save();
$this
->enableModules([
'language',
'content_translation',
]);
$this
->installEntitySchema('entity_test_mul');
$en = ConfigurableLanguage::create([
'id' => 'en',
]);
$en
->save();
$es = ConfigurableLanguage::create([
'id' => 'es',
]);
$es
->save();
$this->container
->get('content_translation.manager')
->setEnabled('entity_test_mul', 'entity_test_mul', TRUE);
$this
->createEntityReferenceField('entity_test_mul', 'entity_test_mul', 'reference_field', 'Reference', 'entity_test_mul');
$field = FieldConfig::loadByName('entity_test_mul', 'entity_test_mul', 'reference_field');
$field
->set('translatable', FALSE)
->save();
FieldStorageConfig::create([
'field_name' => 'text',
'entity_type' => 'entity_test_mul',
'type' => 'string',
])
->save();
FieldConfig::create([
'field_name' => 'text',
'entity_type' => 'entity_test_mul',
'bundle' => 'entity_test_mul',
'label' => 'Translated text',
'translatable' => TRUE,
])
->save();
EntityViewMode::create([
'id' => 'entity_test_mul.full',
'targetEntityType' => 'entity_test_mul',
'status' => FALSE,
'enabled' => TRUE,
'label' => 'Full',
])
->save();
$display = EntityViewDisplay::create([
'targetEntityType' => 'entity_test_mul',
'bundle' => 'entity_test_mul',
'mode' => 'full',
'label' => 'My view mode',
'status' => TRUE,
])
->setComponent('reference_field', [
'type' => 'entity_reference_entity_view',
'settings' => [
'view_mode' => 'full',
],
])
->setComponent('text', [
'type' => 'string',
'region' => 'content',
]);
$display
->save();
$referenced_entity = $this
->createTestEntity('entity_test_mul');
$referenced_entity
->addTranslation('es', $referenced_entity
->getTranslation('en')
->toArray());
$referenced_entity
->set('text', 'Text in English');
$referenced_entity
->getTranslation('es')->text = 'Text in Spanish';
$main_entity = $this
->createTestEntity('entity_test_mul');
$main_entity
->addTranslation('es', $main_entity
->getTranslation('en')
->toArray());
$main_entity
->set('reference_field', $referenced_entity);
$view_builder = $this->container
->get('entity_type.manager')
->getViewBuilder('entity_test_mul');
$renderer = $this->container
->get('renderer');
$reference_field = $main_entity
->get('reference_field');
$reference_field_array_english = $view_builder
->viewField($reference_field, 'full');
$rendered_reference_field_english = $renderer
->renderRoot($reference_field_array_english);
$this
->assertStringContainsString('Text in English', (string) $rendered_reference_field_english);
\Drupal::service('language.default')
->set($es);
\Drupal::languageManager()
->reset();
\Drupal::languageManager()
->getCurrentLanguage();
$reference_field_array_spanish = $view_builder
->viewField($reference_field, 'full');
$rendered_reference_field_spanish = $renderer
->renderRoot($reference_field_array_spanish);
$this
->assertStringContainsString('Text in Spanish', (string) $rendered_reference_field_spanish);
}
protected function createTestEntity($entity_type) {
$data = [
'bundle' => $entity_type,
'name' => $this
->randomMachineName(),
];
return $this->container
->get('entity_type.manager')
->getStorage($entity_type)
->create($data);
}
public function testNoTemplate() {
$entity_type_manager = \Drupal::entityTypeManager();
$entity_type = $entity_type_manager
->getDefinition('entity_test_base_field_display');
$this
->assertTrue($entity_type
->hasViewBuilderClass());
$this
->assertEquals(EntityViewBuilder::class, $entity_type
->getViewBuilderClass());
$entity = $this
->createTestEntity('entity_test');
$build = $entity_type_manager
->getViewBuilder('entity_test')
->view($entity);
$this
->assertEquals($entity, $build['#entity_test']);
$this
->assertArrayNotHasKey('#theme', $build);
}
public function testExternalEntity() {
$this
->installEntitySchema('entity_test_external');
$renderer = $this->container
->get('renderer');
$entity_test = $this
->createTestEntity('entity_test_external');
$entity_test
->save();
$view = $this->container
->get('entity_type.manager')
->getViewBuilder('entity_test_external')
->view($entity_test);
$renderer
->renderRoot($view);
$this
->assertArrayNotHasKey('#contextual_links', $view);
}
}