summaryrefslogtreecommitdiffstats
path: root/core/modules/system/src/Tests/Entity/EntityWithUriCacheTagsTestBase.php
blob: ad4f59b9faf43fa172fc12ba5cd23b33cecb437c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php

namespace Drupal\system\Tests\Entity;

use Drupal\Core\Cache\Cache;
use Drupal\Core\Language\LanguageInterface;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;

/**
 * Provides helper methods for Entity cache tags tests; for entities with URIs.
 */
abstract class EntityWithUriCacheTagsTestBase extends EntityCacheTagsTestBase {

  /**
   * Tests cache tags presence and invalidation of the entity at its URI.
   *
   * Tests the following cache tags:
   * - "<entity type>_view"
   * - "<entity_type>:<entity ID>"
   */
  public function testEntityUri() {
    $entity_url = $this->entity->urlInfo();
    $entity_type = $this->entity->getEntityTypeId();

    // Selects the view mode that will be used.
    $view_mode = $this->selectViewMode($entity_type);

    // The default cache contexts for rendered entities.
    $entity_cache_contexts = $this->getDefaultCacheContexts();

    // Generate the standardized entity cache tags.
    $cache_tag = $this->entity->getCacheTags();
    $view_cache_tag = \Drupal::entityManager()->getViewBuilder($entity_type)->getCacheTags();
    $render_cache_tag = 'rendered';


    $this->pass("Test entity.", 'Debug');
    $this->verifyPageCache($entity_url, 'MISS');

    // Verify a cache hit, but also the presence of the correct cache tags.
    $this->verifyPageCache($entity_url, 'HIT');

    // Also verify the existence of an entity render cache entry, if this entity
    // type supports render caching.
    if (\Drupal::entityManager()->getDefinition($entity_type)->isRenderCacheable()) {
      $cache_keys = ['entity_view', $entity_type, $this->entity->id(), $view_mode];
      $cid = $this->createCacheId($cache_keys, $entity_cache_contexts);
      $redirected_cid = NULL;
      $additional_cache_contexts = $this->getAdditionalCacheContextsForEntity($this->entity);
      if (count($additional_cache_contexts)) {
        $redirected_cid = $this->createCacheId($cache_keys, Cache::mergeContexts($entity_cache_contexts, $additional_cache_contexts));
      }
      $expected_cache_tags = Cache::mergeTags($cache_tag, $view_cache_tag);
      $expected_cache_tags = Cache::mergeTags($expected_cache_tags, $this->getAdditionalCacheTagsForEntity($this->entity));
      $expected_cache_tags = Cache::mergeTags($expected_cache_tags, array($render_cache_tag));
      $this->verifyRenderCache($cid, $expected_cache_tags, $redirected_cid);
    }

    // Verify that after modifying the entity, there is a cache miss.
    $this->pass("Test modification of entity.", 'Debug');
    $this->entity->save();
    $this->verifyPageCache($entity_url, 'MISS');

    // Verify a cache hit.
    $this->verifyPageCache($entity_url, 'HIT');


    // Verify that after modifying the entity's display, there is a cache miss.
    $this->pass("Test modification of entity's '$view_mode' display.", 'Debug');
    $entity_display = entity_get_display($entity_type, $this->entity->bundle(), $view_mode);
    $entity_display->save();
    $this->verifyPageCache($entity_url, 'MISS');

    // Verify a cache hit.
    $this->verifyPageCache($entity_url, 'HIT');


    if ($bundle_entity_type_id = $this->entity->getEntityType()->getBundleEntityType()) {
      // Verify that after modifying the corresponding bundle entity, there is a
      // cache miss.
      $this->pass("Test modification of entity's bundle entity.", 'Debug');
      $bundle_entity = entity_load($bundle_entity_type_id, $this->entity->bundle());
      $bundle_entity->save();
      $this->verifyPageCache($entity_url, 'MISS');

      // Verify a cache hit.
      $this->verifyPageCache($entity_url, 'HIT');
    }


    if ($this->entity->getEntityType()->get('field_ui_base_route')) {
      // Verify that after modifying a configurable field on the entity, there
      // is a cache miss.
      $this->pass("Test modification of entity's configurable field.", 'Debug');
      $field_storage_name = $this->entity->getEntityTypeId() . '.configurable_field';
      $field_storage = FieldStorageConfig::load($field_storage_name);
      $field_storage->save();
      $this->verifyPageCache($entity_url, 'MISS');

      // Verify a cache hit.
      $this->verifyPageCache($entity_url, 'HIT');

      // Verify that after modifying a configurable field on the entity, there
      // is a cache miss.
      $this->pass("Test modification of entity's configurable field.", 'Debug');
      $field_name = $this->entity->getEntityTypeId() . '.' . $this->entity->bundle() . '.configurable_field';
      $field = FieldConfig::load($field_name);
      $field->save();
      $this->verifyPageCache($entity_url, 'MISS');

      // Verify a cache hit.
      $this->verifyPageCache($entity_url, 'HIT');
    }


    // Verify that after invalidating the entity's cache tag directly, there is
    // a cache miss.
    $this->pass("Test invalidation of entity's cache tag.", 'Debug');
    Cache::invalidateTags($this->entity->getCacheTagsToInvalidate());
    $this->verifyPageCache($entity_url, 'MISS');

    // Verify a cache hit.
    $this->verifyPageCache($entity_url, 'HIT');


    // Verify that after invalidating the generic entity type's view cache tag
    // directly, there is a cache miss.
    $this->pass("Test invalidation of entity's 'view' cache tag.", 'Debug');
    Cache::invalidateTags($view_cache_tag);
    $this->verifyPageCache($entity_url, 'MISS');

    // Verify a cache hit.
    $this->verifyPageCache($entity_url, 'HIT');


    // Verify that after deleting the entity, there is a cache miss.
    $this->pass('Test deletion of entity.', 'Debug');
    $this->entity->delete();
    $this->verifyPageCache($entity_url, 'MISS');
    $this->assertResponse(404);
  }

  /**
   * Gets the default cache contexts for rendered entities.
   *
   * @return array
   *   The default cache contexts for rendered entities.
   */
  protected function getDefaultCacheContexts() {
    return ['languages:' . LanguageInterface::TYPE_INTERFACE, 'theme', 'user.permissions'];
  }

}