View source
<?php
namespace Drupal\system\Tests\Entity;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Language\LanguageInterface;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;
abstract class EntityWithUriCacheTagsTestBase extends EntityCacheTagsTestBase {
public function testEntityUri() {
$entity_url = $this->entity
->urlInfo();
$entity_type = $this->entity
->getEntityTypeId();
$view_mode = $this
->selectViewMode($entity_type);
$entity_cache_contexts = [
'languages:' . LanguageInterface::TYPE_INTERFACE,
'theme',
'user.permissions',
];
$cache_tag = $this->entity
->getCacheTags();
$view_cache_tag = \Drupal::entityManager()
->getViewBuilder($entity_type)
->getCacheTags();
$render_cache_tag = 'rendered';
$this
->pass("Test entity.", 'Debug');
$this
->verifyPageCache($entity_url, 'MISS');
$this
->verifyPageCache($entity_url, 'HIT');
if (\Drupal::entityManager()
->getDefinition($entity_type)
->isRenderCacheable()) {
$cache_keys = [
'entity_view',
$entity_type,
$this->entity
->id(),
$view_mode,
];
$cid = $this
->createCacheId($cache_keys, $entity_cache_contexts);
$redirected_cid = NULL;
$additional_cache_contexts = $this
->getAdditionalCacheContextsForEntity($this->entity);
if (count($additional_cache_contexts)) {
$redirected_cid = $this
->createCacheId($cache_keys, Cache::mergeContexts($entity_cache_contexts, $additional_cache_contexts));
}
$expected_cache_tags = Cache::mergeTags($cache_tag, $view_cache_tag);
$expected_cache_tags = Cache::mergeTags($expected_cache_tags, $this
->getAdditionalCacheTagsForEntity($this->entity));
$expected_cache_tags = Cache::mergeTags($expected_cache_tags, array(
$render_cache_tag,
));
$this
->verifyRenderCache($cid, $expected_cache_tags, $redirected_cid);
}
$this
->pass("Test modification of entity.", 'Debug');
$this->entity
->save();
$this
->verifyPageCache($entity_url, 'MISS');
$this
->verifyPageCache($entity_url, 'HIT');
$this
->pass("Test modification of entity's '{$view_mode}' display.", 'Debug');
$entity_display = entity_get_display($entity_type, $this->entity
->bundle(), $view_mode);
$entity_display
->save();
$this
->verifyPageCache($entity_url, 'MISS');
$this
->verifyPageCache($entity_url, 'HIT');
if ($bundle_entity_type_id = $this->entity
->getEntityType()
->getBundleEntityType()) {
$this
->pass("Test modification of entity's bundle entity.", 'Debug');
$bundle_entity = entity_load($bundle_entity_type_id, $this->entity
->bundle());
$bundle_entity
->save();
$this
->verifyPageCache($entity_url, 'MISS');
$this
->verifyPageCache($entity_url, 'HIT');
}
if ($this->entity
->getEntityType()
->get('field_ui_base_route')) {
$this
->pass("Test modification of entity's configurable field.", 'Debug');
$field_storage_name = $this->entity
->getEntityTypeId() . '.configurable_field';
$field_storage = FieldStorageConfig::load($field_storage_name);
$field_storage
->save();
$this
->verifyPageCache($entity_url, 'MISS');
$this
->verifyPageCache($entity_url, 'HIT');
$this
->pass("Test modification of entity's configurable field.", 'Debug');
$field_name = $this->entity
->getEntityTypeId() . '.' . $this->entity
->bundle() . '.configurable_field';
$field = FieldConfig::load($field_name);
$field
->save();
$this
->verifyPageCache($entity_url, 'MISS');
$this
->verifyPageCache($entity_url, 'HIT');
}
$this
->pass("Test invalidation of entity's cache tag.", 'Debug');
Cache::invalidateTags($this->entity
->getCacheTagsToInvalidate());
$this
->verifyPageCache($entity_url, 'MISS');
$this
->verifyPageCache($entity_url, 'HIT');
$this
->pass("Test invalidation of entity's 'view' cache tag.", 'Debug');
Cache::invalidateTags($view_cache_tag);
$this
->verifyPageCache($entity_url, 'MISS');
$this
->verifyPageCache($entity_url, 'HIT');
$this
->pass('Test deletion of entity.', 'Debug');
$this->entity
->delete();
$this
->verifyPageCache($entity_url, 'MISS');
$this
->assertResponse(404);
}
}