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;
10 @trigger_error(__NAMESPACE__ . '\EntityWithUriCacheTagsTestBase is deprecated in Drupal 8.6.x and will be removed before Drupal 9.0.0. Instead, use \Drupal\Tests\system\Functional\Entity\EntityWithUriCacheTagsTestBase. See https://www.drupal.org/node/2946549.', E_USER_DEPRECATED);
13 * Provides helper methods for Entity cache tags tests; for entities with URIs.
15 * @deprecated in Drupal 8.6.x and will be removed before Drupal 9.0.0.
16 * Use \Drupal\Tests\system\Functional\Entity\EntityWithUriCacheTagsTestBase.
18 * @see https://www.drupal.org/node/2946549
20 abstract class EntityWithUriCacheTagsTestBase extends EntityCacheTagsTestBase {
23 * Tests cache tags presence and invalidation of the entity at its URI.
25 * Tests the following cache tags:
26 * - "<entity type>_view"
27 * - "<entity_type>:<entity ID>"
29 public function testEntityUri() {
30 $entity_url = $this->entity->urlInfo();
31 $entity_type = $this->entity->getEntityTypeId();
33 // Selects the view mode that will be used.
34 $view_mode = $this->selectViewMode($entity_type);
36 // The default cache contexts for rendered entities.
37 $entity_cache_contexts = $this->getDefaultCacheContexts();
39 // Generate the standardized entity cache tags.
40 $cache_tag = $this->entity->getCacheTags();
41 $view_cache_tag = \Drupal::entityManager()->getViewBuilder($entity_type)->getCacheTags();
42 $render_cache_tag = 'rendered';
44 $this->pass("Test entity.", 'Debug');
45 $this->verifyPageCache($entity_url, 'MISS');
47 // Verify a cache hit, but also the presence of the correct cache tags.
48 $this->verifyPageCache($entity_url, 'HIT');
50 // Also verify the existence of an entity render cache entry, if this entity
51 // type supports render caching.
52 if (\Drupal::entityManager()->getDefinition($entity_type)->isRenderCacheable()) {
53 $cache_keys = ['entity_view', $entity_type, $this->entity->id(), $view_mode];
54 $cid = $this->createCacheId($cache_keys, $entity_cache_contexts);
55 $redirected_cid = NULL;
56 $additional_cache_contexts = $this->getAdditionalCacheContextsForEntity($this->entity);
57 if (count($additional_cache_contexts)) {
58 $redirected_cid = $this->createCacheId($cache_keys, Cache::mergeContexts($entity_cache_contexts, $additional_cache_contexts));
60 $expected_cache_tags = Cache::mergeTags($cache_tag, $view_cache_tag);
61 $expected_cache_tags = Cache::mergeTags($expected_cache_tags, $this->getAdditionalCacheTagsForEntity($this->entity));
62 $expected_cache_tags = Cache::mergeTags($expected_cache_tags, [$render_cache_tag]);
63 $this->verifyRenderCache($cid, $expected_cache_tags, $redirected_cid);
66 // Verify that after modifying the entity, there is a cache miss.
67 $this->pass("Test modification of entity.", 'Debug');
68 $this->entity->save();
69 $this->verifyPageCache($entity_url, 'MISS');
71 // Verify a cache hit.
72 $this->verifyPageCache($entity_url, 'HIT');
74 // Verify that after modifying the entity's display, there is a cache miss.
75 $this->pass("Test modification of entity's '$view_mode' display.", 'Debug');
76 $entity_display = entity_get_display($entity_type, $this->entity->bundle(), $view_mode);
77 $entity_display->save();
78 $this->verifyPageCache($entity_url, 'MISS');
80 // Verify a cache hit.
81 $this->verifyPageCache($entity_url, 'HIT');
83 if ($bundle_entity_type_id = $this->entity->getEntityType()->getBundleEntityType()) {
84 // Verify that after modifying the corresponding bundle entity, there is a
86 $this->pass("Test modification of entity's bundle entity.", 'Debug');
87 $bundle_entity = $this->container->get('entity_type.manager')
88 ->getStorage($bundle_entity_type_id)
89 ->load($this->entity->bundle());
90 $bundle_entity->save();
91 $this->verifyPageCache($entity_url, 'MISS');
93 // Verify a cache hit.
94 $this->verifyPageCache($entity_url, 'HIT');
97 if ($this->entity->getEntityType()->get('field_ui_base_route')) {
98 // Verify that after modifying a configurable field on the entity, there
100 $this->pass("Test modification of entity's configurable field.", 'Debug');
101 $field_storage_name = $this->entity->getEntityTypeId() . '.configurable_field';
102 $field_storage = FieldStorageConfig::load($field_storage_name);
103 $field_storage->save();
104 $this->verifyPageCache($entity_url, 'MISS');
106 // Verify a cache hit.
107 $this->verifyPageCache($entity_url, 'HIT');
109 // Verify that after modifying a configurable field on the entity, there
111 $this->pass("Test modification of entity's configurable field.", 'Debug');
112 $field_name = $this->entity->getEntityTypeId() . '.' . $this->entity->bundle() . '.configurable_field';
113 $field = FieldConfig::load($field_name);
115 $this->verifyPageCache($entity_url, 'MISS');
117 // Verify a cache hit.
118 $this->verifyPageCache($entity_url, 'HIT');
121 // Verify that after invalidating the entity's cache tag directly, there is
123 $this->pass("Test invalidation of entity's cache tag.", 'Debug');
124 Cache::invalidateTags($this->entity->getCacheTagsToInvalidate());
125 $this->verifyPageCache($entity_url, 'MISS');
127 // Verify a cache hit.
128 $this->verifyPageCache($entity_url, 'HIT');
130 // Verify that after invalidating the generic entity type's view cache tag
131 // directly, there is a cache miss.
132 $this->pass("Test invalidation of entity's 'view' cache tag.", 'Debug');
133 Cache::invalidateTags($view_cache_tag);
134 $this->verifyPageCache($entity_url, 'MISS');
136 // Verify a cache hit.
137 $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'];