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';
37 $this->pass("Test entity.", 'Debug');
38 $this->verifyPageCache($entity_url, 'MISS');
40 // Verify a cache hit, but also the presence of the correct cache tags.
41 $this->verifyPageCache($entity_url, 'HIT');
43 // Also verify the existence of an entity render cache entry, if this entity
44 // type supports render caching.
45 if (\Drupal::entityManager()->getDefinition($entity_type)->isRenderCacheable()) {
46 $cache_keys = ['entity_view', $entity_type, $this->entity->id(), $view_mode];
47 $cid = $this->createCacheId($cache_keys, $entity_cache_contexts);
48 $redirected_cid = NULL;
49 $additional_cache_contexts = $this->getAdditionalCacheContextsForEntity($this->entity);
50 if (count($additional_cache_contexts)) {
51 $redirected_cid = $this->createCacheId($cache_keys, Cache::mergeContexts($entity_cache_contexts, $additional_cache_contexts));
53 $expected_cache_tags = Cache::mergeTags($cache_tag, $view_cache_tag);
54 $expected_cache_tags = Cache::mergeTags($expected_cache_tags, $this->getAdditionalCacheTagsForEntity($this->entity));
55 $expected_cache_tags = Cache::mergeTags($expected_cache_tags, [$render_cache_tag]);
56 $this->verifyRenderCache($cid, $expected_cache_tags, $redirected_cid);
59 // Verify that after modifying the entity, there is a cache miss.
60 $this->pass("Test modification of entity.", 'Debug');
61 $this->entity->save();
62 $this->verifyPageCache($entity_url, 'MISS');
64 // Verify a cache hit.
65 $this->verifyPageCache($entity_url, 'HIT');
67 // Verify that after modifying the entity's display, there is a cache miss.
68 $this->pass("Test modification of entity's '$view_mode' display.", 'Debug');
69 $entity_display = entity_get_display($entity_type, $this->entity->bundle(), $view_mode);
70 $entity_display->save();
71 $this->verifyPageCache($entity_url, 'MISS');
73 // Verify a cache hit.
74 $this->verifyPageCache($entity_url, 'HIT');
76 if ($bundle_entity_type_id = $this->entity->getEntityType()->getBundleEntityType()) {
77 // Verify that after modifying the corresponding bundle entity, there is a
79 $this->pass("Test modification of entity's bundle entity.", 'Debug');
80 $bundle_entity = $this->container->get('entity_type.manager')
81 ->getStorage($bundle_entity_type_id)
82 ->load($this->entity->bundle());
83 $bundle_entity->save();
84 $this->verifyPageCache($entity_url, 'MISS');
86 // Verify a cache hit.
87 $this->verifyPageCache($entity_url, 'HIT');
90 if ($this->entity->getEntityType()->get('field_ui_base_route')) {
91 // Verify that after modifying a configurable field on the entity, there
93 $this->pass("Test modification of entity's configurable field.", 'Debug');
94 $field_storage_name = $this->entity->getEntityTypeId() . '.configurable_field';
95 $field_storage = FieldStorageConfig::load($field_storage_name);
96 $field_storage->save();
97 $this->verifyPageCache($entity_url, 'MISS');
99 // Verify a cache hit.
100 $this->verifyPageCache($entity_url, 'HIT');
102 // Verify that after modifying a configurable field on the entity, there
104 $this->pass("Test modification of entity's configurable field.", 'Debug');
105 $field_name = $this->entity->getEntityTypeId() . '.' . $this->entity->bundle() . '.configurable_field';
106 $field = FieldConfig::load($field_name);
108 $this->verifyPageCache($entity_url, 'MISS');
110 // Verify a cache hit.
111 $this->verifyPageCache($entity_url, 'HIT');
114 // Verify that after invalidating the entity's cache tag directly, there is
116 $this->pass("Test invalidation of entity's cache tag.", 'Debug');
117 Cache::invalidateTags($this->entity->getCacheTagsToInvalidate());
118 $this->verifyPageCache($entity_url, 'MISS');
120 // Verify a cache hit.
121 $this->verifyPageCache($entity_url, 'HIT');
123 // Verify that after invalidating the generic entity type's view cache tag
124 // directly, there is a cache miss.
125 $this->pass("Test invalidation of entity's 'view' cache tag.", 'Debug');
126 Cache::invalidateTags($view_cache_tag);
127 $this->verifyPageCache($entity_url, 'MISS');
129 // Verify a cache hit.
130 $this->verifyPageCache($entity_url, 'HIT');
132 // Verify that after deleting the entity, there is a cache miss.
133 $this->pass('Test deletion of entity.', 'Debug');
134 $this->entity->delete();
135 $this->verifyPageCache($entity_url, 'MISS');
136 $this->assertResponse(404);
140 * Gets the default cache contexts for rendered entities.
143 * The default cache contexts for rendered entities.
145 protected function getDefaultCacheContexts() {
146 return ['languages:' . LanguageInterface::TYPE_INTERFACE, 'theme', 'user.permissions'];