View source
<?php
namespace Drupal\system\Tests\Entity;
use Drupal\Core\Language\LanguageInterface;
use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
use Drupal\Core\Cache\Cache;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
class EntityViewBuilderTest extends EntityUnitTestBase {
use EntityReferenceTestTrait;
protected function setUp() {
parent::setUp();
$this
->installConfig(array(
'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.manager')
->getViewBuilder('entity_test')
->view($entity_test, 'full');
$this
->assertTrue(isset($build['#cache']) && array_keys($build['#cache']) == [
'tags',
'contexts',
'max-age',
], '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.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
->assertTrue($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
->assertTrue($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');
entity_get_display('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.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
->assertTrue($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.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
->assertTrue($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.manager')
->getViewBuilder('entity_test')
->view($entity_test, 'full');
$this
->assertTrue(isset($build['#cache']) && array_keys($build['#cache']) == [
'tags',
'contexts',
'max-age',
'keys',
'bin',
], 'A view mode with render cache enabled has the correct output (cache tags, keys, contexts, max-age and bin).');
$build = $this->container
->get('entity.manager')
->getViewBuilder('entity_test')
->view($entity_test, 'test');
$this
->assertTrue(isset($build['#cache']) && array_keys($build['#cache']) == [
'tags',
'contexts',
'max-age',
], 'A view mode with render cache disabled has the correct output (only cache tags, contexts and max-age).');
$entity_test_no_cache = $this
->createTestEntity('entity_test_label');
$entity_test_no_cache
->save();
$build = $this->container
->get('entity.manager')
->getViewBuilder('entity_test_label')
->view($entity_test_no_cache, 'full');
$this
->assertTrue(isset($build['#cache']) && array_keys($build['#cache']) == [
'tags',
'contexts',
'max-age',
], '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');
entity_get_display('entity_test', 'entity_test', 'full')
->setComponent('label', array(
'weight' => 20,
))
->save();
$entity_test = $this
->createTestEntity('entity_test');
$view = $this->container
->get('entity.manager')
->getViewBuilder('entity_test')
->view($entity_test, 'full');
$renderer
->renderRoot($view);
$this
->assertEqual($view['label']['#weight'], 20, 'The weight of a display component is respected.');
}
protected function createTestEntity($entity_type) {
$data = array(
'bundle' => $entity_type,
'name' => $this
->randomMachineName(),
);
return $this->container
->get('entity.manager')
->getStorage($entity_type)
->create($data);
}
}