3 namespace Drupal\KernelTests\Core\Config;
5 use Drupal\config_entity_static_cache_test\ConfigOverrider;
6 use Drupal\KernelTests\KernelTestBase;
9 * Tests the entity static cache when used by config entities.
13 class ConfigEntityStaticCacheTest extends KernelTestBase {
20 public static $modules = ['config_test', 'config_entity_static_cache_test'];
23 * The type ID of the entity under test.
27 protected $entityTypeId;
30 * The entity ID of the entity under test.
39 protected function setUp() {
41 $this->entityTypeId = 'config_test';
42 $this->entityId = 'test_1';
43 $this->container->get('entity_type.manager')
44 ->getStorage($this->entityTypeId)
45 ->create(['id' => $this->entityId, 'label' => 'Original label'])
50 * Tests that the static cache is working.
52 public function testCacheHit() {
53 $storage = $this->container->get('entity_type.manager')
54 ->getStorage($this->entityTypeId);
55 $entity_1 = $storage->load($this->entityId);
56 $entity_2 = $storage->load($this->entityId);
57 // config_entity_static_cache_test_config_test_load() sets _loadStamp to a
58 // random string. If they match, it means $entity_2 was retrieved from the
59 // static cache rather than going through a separate load sequence.
60 $this->assertSame($entity_1->_loadStamp, $entity_2->_loadStamp);
64 * Tests that the static cache is reset on entity save and delete.
66 public function testReset() {
67 $storage = $this->container->get('entity_type.manager')
68 ->getStorage($this->entityTypeId);
69 $entity = $storage->load($this->entityId);
71 // Ensure loading after a save retrieves the updated entity rather than an
72 // obsolete cached one.
73 $entity->label = 'New label';
75 $entity = $storage->load($this->entityId);
76 $this->assertIdentical($entity->label, 'New label');
78 // Ensure loading after a delete retrieves NULL rather than an obsolete
81 $this->assertNull($storage->load($this->entityId));
85 * Tests that the static cache is sensitive to config overrides.
87 public function testConfigOverride() {
88 /** @var \Drupal\Core\Config\Entity\ConfigEntityStorage $storage */
89 $storage = \Drupal::entityManager()->getStorage($this->entityTypeId);
90 // Prime the cache prior to adding a config override.
91 $storage->load($this->entityId);
93 // Add the config override, and ensure that what is loaded is correct
94 // despite the prior cache priming.
95 \Drupal::configFactory()->addOverride(new ConfigOverrider());
96 $entity_override = $storage->load($this->entityId);
97 $this->assertIdentical($entity_override->label, 'Overridden label');
99 // Load override free to ensure that loading the config entity again does
100 // not return the overridden value.
101 $entity_no_override = $storage->loadOverrideFree($this->entityId);
102 $this->assertNotIdentical($entity_no_override->label, 'Overridden label');
103 $this->assertNotIdentical($entity_override->_loadStamp, $entity_no_override->_loadStamp);
105 // Reload the entity and ensure the cache is used.
106 $this->assertIdentical($storage->loadOverrideFree($this->entityId)->_loadStamp, $entity_no_override->_loadStamp);
108 // Enable overrides and reload the entity and ensure the cache is used.
109 $this->assertIdentical($storage->load($this->entityId)->_loadStamp, $entity_override->_loadStamp);