Updated Drupal to 8.6. This goes with the following updates because it's possible...
[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 @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);
11
12 /**
13  * Provides helper methods for Entity cache tags tests; for entities with URIs.
14  *
15  * @deprecated in Drupal 8.6.x and will be removed before Drupal 9.0.0.
16  * Use \Drupal\Tests\system\Functional\Entity\EntityWithUriCacheTagsTestBase.
17  *
18  * @see https://www.drupal.org/node/2946549
19  */
20 abstract class EntityWithUriCacheTagsTestBase extends EntityCacheTagsTestBase {
21
22   /**
23    * Tests cache tags presence and invalidation of the entity at its URI.
24    *
25    * Tests the following cache tags:
26    * - "<entity type>_view"
27    * - "<entity_type>:<entity ID>"
28    */
29   public function testEntityUri() {
30     $entity_url = $this->entity->urlInfo();
31     $entity_type = $this->entity->getEntityTypeId();
32
33     // Selects the view mode that will be used.
34     $view_mode = $this->selectViewMode($entity_type);
35
36     // The default cache contexts for rendered entities.
37     $entity_cache_contexts = $this->getDefaultCacheContexts();
38
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';
43
44     $this->pass("Test entity.", 'Debug');
45     $this->verifyPageCache($entity_url, 'MISS');
46
47     // Verify a cache hit, but also the presence of the correct cache tags.
48     $this->verifyPageCache($entity_url, 'HIT');
49
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));
59       }
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);
64     }
65
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');
70
71     // Verify a cache hit.
72     $this->verifyPageCache($entity_url, 'HIT');
73
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');
79
80     // Verify a cache hit.
81     $this->verifyPageCache($entity_url, 'HIT');
82
83     if ($bundle_entity_type_id = $this->entity->getEntityType()->getBundleEntityType()) {
84       // Verify that after modifying the corresponding bundle entity, there is a
85       // cache miss.
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');
92
93       // Verify a cache hit.
94       $this->verifyPageCache($entity_url, 'HIT');
95     }
96
97     if ($this->entity->getEntityType()->get('field_ui_base_route')) {
98       // Verify that after modifying a configurable field on the entity, there
99       // is a cache miss.
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');
105
106       // Verify a cache hit.
107       $this->verifyPageCache($entity_url, 'HIT');
108
109       // Verify that after modifying a configurable field on the entity, there
110       // is a cache miss.
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);
114       $field->save();
115       $this->verifyPageCache($entity_url, 'MISS');
116
117       // Verify a cache hit.
118       $this->verifyPageCache($entity_url, 'HIT');
119     }
120
121     // Verify that after invalidating the entity's cache tag directly, there is
122     // a cache miss.
123     $this->pass("Test invalidation of entity's cache tag.", 'Debug');
124     Cache::invalidateTags($this->entity->getCacheTagsToInvalidate());
125     $this->verifyPageCache($entity_url, 'MISS');
126
127     // Verify a cache hit.
128     $this->verifyPageCache($entity_url, 'HIT');
129
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');
135
136     // Verify a cache hit.
137     $this->verifyPageCache($entity_url, 'HIT');
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 }