Pull merge.
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Config / ConfigEntityStaticCacheTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Config;
4
5 use Drupal\config_entity_static_cache_test\ConfigOverrider;
6 use Drupal\KernelTests\KernelTestBase;
7
8 /**
9  * Tests the entity static cache when used by config entities.
10  *
11  * @group config
12  */
13 class ConfigEntityStaticCacheTest extends KernelTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['config_test', 'config_entity_static_cache_test'];
21
22   /**
23    * The type ID of the entity under test.
24    *
25    * @var string
26    */
27   protected $entityTypeId;
28
29   /**
30    * The entity ID of the entity under test.
31    *
32    * @var string
33    */
34   protected $entityId;
35
36   /**
37    * {@inheritdoc}
38    */
39   protected function setUp() {
40     parent::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'])
46       ->save();
47   }
48
49   /**
50    * Tests that the static cache is working.
51    */
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);
61   }
62
63   /**
64    * Tests that the static cache is reset on entity save and delete.
65    */
66   public function testReset() {
67     $storage = $this->container->get('entity_type.manager')
68       ->getStorage($this->entityTypeId);
69     $entity = $storage->load($this->entityId);
70
71     // Ensure loading after a save retrieves the updated entity rather than an
72     // obsolete cached one.
73     $entity->label = 'New label';
74     $entity->save();
75     $entity = $storage->load($this->entityId);
76     $this->assertIdentical($entity->label, 'New label');
77
78     // Ensure loading after a delete retrieves NULL rather than an obsolete
79     // cached one.
80     $entity->delete();
81     $this->assertNull($storage->load($this->entityId));
82   }
83
84   /**
85    * Tests that the static cache is sensitive to config overrides.
86    */
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);
92
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');
98
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);
104
105     // Reload the entity and ensure the cache is used.
106     $this->assertIdentical($storage->loadOverrideFree($this->entityId)->_loadStamp, $entity_no_override->_loadStamp);
107
108     // Enable overrides and reload the entity and ensure the cache is used.
109     $this->assertIdentical($storage->load($this->entityId)->_loadStamp, $entity_override->_loadStamp);
110   }
111
112 }