74c110380696edc8fd8870abd3219c851f793e6a
[yaffs-website] / web / core / modules / system / src / Tests / Entity / EntityWithUriCacheTagsTestBase.php
1 <?php
2
3 namespace Drupal\system\Tests\Entity;
4
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Language\LanguageInterface;
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\field\Entity\FieldConfig;
9
10 /**
11  * Provides helper methods for Entity cache tags tests; for entities with URIs.
12  */
13 abstract class EntityWithUriCacheTagsTestBase extends EntityCacheTagsTestBase {
14
15   /**
16    * Tests cache tags presence and invalidation of the entity at its URI.
17    *
18    * Tests the following cache tags:
19    * - "<entity type>_view"
20    * - "<entity_type>:<entity ID>"
21    */
22   public function testEntityUri() {
23     $entity_url = $this->entity->urlInfo();
24     $entity_type = $this->entity->getEntityTypeId();
25
26     // Selects the view mode that will be used.
27     $view_mode = $this->selectViewMode($entity_type);
28
29     // The default cache contexts for rendered entities.
30     $entity_cache_contexts = $this->getDefaultCacheContexts();
31
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';
36
37
38     $this->pass("Test entity.", 'Debug');
39     $this->verifyPageCache($entity_url, 'MISS');
40
41     // Verify a cache hit, but also the presence of the correct cache tags.
42     $this->verifyPageCache($entity_url, 'HIT');
43
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));
53       }
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);
58     }
59
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');
64
65     // Verify a cache hit.
66     $this->verifyPageCache($entity_url, 'HIT');
67
68
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');
74
75     // Verify a cache hit.
76     $this->verifyPageCache($entity_url, 'HIT');
77
78
79     if ($bundle_entity_type_id = $this->entity->getEntityType()->getBundleEntityType()) {
80       // Verify that after modifying the corresponding bundle entity, there is a
81       // cache miss.
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');
88
89       // Verify a cache hit.
90       $this->verifyPageCache($entity_url, 'HIT');
91     }
92
93
94     if ($this->entity->getEntityType()->get('field_ui_base_route')) {
95       // Verify that after modifying a configurable field on the entity, there
96       // is a cache miss.
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');
102
103       // Verify a cache hit.
104       $this->verifyPageCache($entity_url, 'HIT');
105
106       // Verify that after modifying a configurable field on the entity, there
107       // is a cache miss.
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);
111       $field->save();
112       $this->verifyPageCache($entity_url, 'MISS');
113
114       // Verify a cache hit.
115       $this->verifyPageCache($entity_url, 'HIT');
116     }
117
118
119     // Verify that after invalidating the entity's cache tag directly, there is
120     // a cache miss.
121     $this->pass("Test invalidation of entity's cache tag.", 'Debug');
122     Cache::invalidateTags($this->entity->getCacheTagsToInvalidate());
123     $this->verifyPageCache($entity_url, 'MISS');
124
125     // Verify a cache hit.
126     $this->verifyPageCache($entity_url, 'HIT');
127
128
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');
134
135     // Verify a cache hit.
136     $this->verifyPageCache($entity_url, 'HIT');
137
138
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);
144   }
145
146   /**
147    * Gets the default cache contexts for rendered entities.
148    *
149    * @return array
150    *   The default cache contexts for rendered entities.
151    */
152   protected function getDefaultCacheContexts() {
153     return ['languages:' . LanguageInterface::TYPE_INTERFACE, 'theme', 'user.permissions'];
154   }
155
156 }