3 namespace Drupal\system\Tests\Entity;
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Language\LanguageInterface;
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\field\Entity\FieldConfig;
11 * Provides helper methods for Entity cache tags tests; for entities with URIs.
13 abstract class EntityWithUriCacheTagsTestBase extends EntityCacheTagsTestBase {
16 * Tests cache tags presence and invalidation of the entity at its URI.
18 * Tests the following cache tags:
19 * - "<entity type>_view"
20 * - "<entity_type>:<entity ID>"
22 public function testEntityUri() {
23 $entity_url = $this->entity->urlInfo();
24 $entity_type = $this->entity->getEntityTypeId();
26 // Selects the view mode that will be used.
27 $view_mode = $this->selectViewMode($entity_type);
29 // The default cache contexts for rendered entities.
30 $entity_cache_contexts = $this->getDefaultCacheContexts();
32 // Generate the standardized entity cache tags.
33 $cache_tag = $this->entity->getCacheTags();
34 $view_cache_tag = \Drupal::entityManager()->getViewBuilder($entity_type)->getCacheTags();
35 $render_cache_tag = 'rendered';
38 $this->pass("Test entity.", 'Debug');
39 $this->verifyPageCache($entity_url, 'MISS');
41 // Verify a cache hit, but also the presence of the correct cache tags.
42 $this->verifyPageCache($entity_url, 'HIT');
44 // Also verify the existence of an entity render cache entry, if this entity
45 // type supports render caching.
46 if (\Drupal::entityManager()->getDefinition($entity_type)->isRenderCacheable()) {
47 $cache_keys = ['entity_view', $entity_type, $this->entity->id(), $view_mode];
48 $cid = $this->createCacheId($cache_keys, $entity_cache_contexts);
49 $redirected_cid = NULL;
50 $additional_cache_contexts = $this->getAdditionalCacheContextsForEntity($this->entity);
51 if (count($additional_cache_contexts)) {
52 $redirected_cid = $this->createCacheId($cache_keys, Cache::mergeContexts($entity_cache_contexts, $additional_cache_contexts));
54 $expected_cache_tags = Cache::mergeTags($cache_tag, $view_cache_tag);
55 $expected_cache_tags = Cache::mergeTags($expected_cache_tags, $this->getAdditionalCacheTagsForEntity($this->entity));
56 $expected_cache_tags = Cache::mergeTags($expected_cache_tags, [$render_cache_tag]);
57 $this->verifyRenderCache($cid, $expected_cache_tags, $redirected_cid);
60 // Verify that after modifying the entity, there is a cache miss.
61 $this->pass("Test modification of entity.", 'Debug');
62 $this->entity->save();
63 $this->verifyPageCache($entity_url, 'MISS');
65 // Verify a cache hit.
66 $this->verifyPageCache($entity_url, 'HIT');
69 // Verify that after modifying the entity's display, there is a cache miss.
70 $this->pass("Test modification of entity's '$view_mode' display.", 'Debug');
71 $entity_display = entity_get_display($entity_type, $this->entity->bundle(), $view_mode);
72 $entity_display->save();
73 $this->verifyPageCache($entity_url, 'MISS');
75 // Verify a cache hit.
76 $this->verifyPageCache($entity_url, 'HIT');
79 if ($bundle_entity_type_id = $this->entity->getEntityType()->getBundleEntityType()) {
80 // Verify that after modifying the corresponding bundle entity, there is a
82 $this->pass("Test modification of entity's bundle entity.", 'Debug');
83 $bundle_entity = $this->container->get('entity_type.manager')
84 ->getStorage($bundle_entity_type_id)
85 ->load($this->entity->bundle());
86 $bundle_entity->save();
87 $this->verifyPageCache($entity_url, 'MISS');
89 // Verify a cache hit.
90 $this->verifyPageCache($entity_url, 'HIT');
94 if ($this->entity->getEntityType()->get('field_ui_base_route')) {
95 // Verify that after modifying a configurable field on the entity, there
97 $this->pass("Test modification of entity's configurable field.", 'Debug');
98 $field_storage_name = $this->entity->getEntityTypeId() . '.configurable_field';
99 $field_storage = FieldStorageConfig::load($field_storage_name);
100 $field_storage->save();
101 $this->verifyPageCache($entity_url, 'MISS');
103 // Verify a cache hit.
104 $this->verifyPageCache($entity_url, 'HIT');
106 // Verify that after modifying a configurable field on the entity, there
108 $this->pass("Test modification of entity's configurable field.", 'Debug');
109 $field_name = $this->entity->getEntityTypeId() . '.' . $this->entity->bundle() . '.configurable_field';
110 $field = FieldConfig::load($field_name);
112 $this->verifyPageCache($entity_url, 'MISS');
114 // Verify a cache hit.
115 $this->verifyPageCache($entity_url, 'HIT');
119 // Verify that after invalidating the entity's cache tag directly, there is
121 $this->pass("Test invalidation of entity's cache tag.", 'Debug');
122 Cache::invalidateTags($this->entity->getCacheTagsToInvalidate());
123 $this->verifyPageCache($entity_url, 'MISS');
125 // Verify a cache hit.
126 $this->verifyPageCache($entity_url, 'HIT');
129 // Verify that after invalidating the generic entity type's view cache tag
130 // directly, there is a cache miss.
131 $this->pass("Test invalidation of entity's 'view' cache tag.", 'Debug');
132 Cache::invalidateTags($view_cache_tag);
133 $this->verifyPageCache($entity_url, 'MISS');
135 // Verify a cache hit.
136 $this->verifyPageCache($entity_url, 'HIT');
139 // Verify that after deleting the entity, there is a cache miss.
140 $this->pass('Test deletion of entity.', 'Debug');
141 $this->entity->delete();
142 $this->verifyPageCache($entity_url, 'MISS');
143 $this->assertResponse(404);
147 * Gets the default cache contexts for rendered entities.
150 * The default cache contexts for rendered entities.
152 protected function getDefaultCacheContexts() {
153 return ['languages:' . LanguageInterface::TYPE_INTERFACE, 'theme', 'user.permissions'];