Security update for Core, with self-updated composer
[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     $this->pass("Test entity.", 'Debug');
38     $this->verifyPageCache($entity_url, 'MISS');
39
40     // Verify a cache hit, but also the presence of the correct cache tags.
41     $this->verifyPageCache($entity_url, 'HIT');
42
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));
52       }
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);
57     }
58
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');
63
64     // Verify a cache hit.
65     $this->verifyPageCache($entity_url, 'HIT');
66
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');
72
73     // Verify a cache hit.
74     $this->verifyPageCache($entity_url, 'HIT');
75
76     if ($bundle_entity_type_id = $this->entity->getEntityType()->getBundleEntityType()) {
77       // Verify that after modifying the corresponding bundle entity, there is a
78       // cache miss.
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');
85
86       // Verify a cache hit.
87       $this->verifyPageCache($entity_url, 'HIT');
88     }
89
90     if ($this->entity->getEntityType()->get('field_ui_base_route')) {
91       // Verify that after modifying a configurable field on the entity, there
92       // is a cache miss.
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');
98
99       // Verify a cache hit.
100       $this->verifyPageCache($entity_url, 'HIT');
101
102       // Verify that after modifying a configurable field on the entity, there
103       // is a cache miss.
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);
107       $field->save();
108       $this->verifyPageCache($entity_url, 'MISS');
109
110       // Verify a cache hit.
111       $this->verifyPageCache($entity_url, 'HIT');
112     }
113
114     // Verify that after invalidating the entity's cache tag directly, there is
115     // a cache miss.
116     $this->pass("Test invalidation of entity's cache tag.", 'Debug');
117     Cache::invalidateTags($this->entity->getCacheTagsToInvalidate());
118     $this->verifyPageCache($entity_url, 'MISS');
119
120     // Verify a cache hit.
121     $this->verifyPageCache($entity_url, 'HIT');
122
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');
128
129     // Verify a cache hit.
130     $this->verifyPageCache($entity_url, 'HIT');
131
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);
137   }
138
139   /**
140    * Gets the default cache contexts for rendered entities.
141    *
142    * @return array
143    *   The default cache contexts for rendered entities.
144    */
145   protected function getDefaultCacheContexts() {
146     return ['languages:' . LanguageInterface::TYPE_INTERFACE, 'theme', 'user.permissions'];
147   }
148
149 }