ConfigEntityStaticCacheTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace Drupal\KernelTests\Core\Config;
  3. use Drupal\config_entity_static_cache_test\ConfigOverrider;
  4. use Drupal\KernelTests\KernelTestBase;
  5. /**
  6. * Tests the entity static cache when used by config entities.
  7. *
  8. * @group config
  9. */
  10. class ConfigEntityStaticCacheTest extends KernelTestBase {
  11. /**
  12. * Modules to enable.
  13. *
  14. * @var array
  15. */
  16. public static $modules = ['config_test', 'config_entity_static_cache_test'];
  17. /**
  18. * The type ID of the entity under test.
  19. *
  20. * @var string
  21. */
  22. protected $entityTypeId;
  23. /**
  24. * The entity ID of the entity under test.
  25. *
  26. * @var string
  27. */
  28. protected $entityId;
  29. /**
  30. * {@inheritdoc}
  31. */
  32. protected function setUp() {
  33. parent::setUp();
  34. $this->entityTypeId = 'config_test';
  35. $this->entityId = 'test_1';
  36. $this->container->get('entity_type.manager')
  37. ->getStorage($this->entityTypeId)
  38. ->create(['id' => $this->entityId, 'label' => 'Original label'])
  39. ->save();
  40. }
  41. /**
  42. * Tests that the static cache is working.
  43. */
  44. public function testCacheHit() {
  45. $storage = $this->container->get('entity_type.manager')
  46. ->getStorage($this->entityTypeId);
  47. $entity_1 = $storage->load($this->entityId);
  48. $entity_2 = $storage->load($this->entityId);
  49. // config_entity_static_cache_test_config_test_load() sets _loadStamp to a
  50. // random string. If they match, it means $entity_2 was retrieved from the
  51. // static cache rather than going through a separate load sequence.
  52. $this->assertSame($entity_1->_loadStamp, $entity_2->_loadStamp);
  53. }
  54. /**
  55. * Tests that the static cache is reset on entity save and delete.
  56. */
  57. public function testReset() {
  58. $storage = $this->container->get('entity_type.manager')
  59. ->getStorage($this->entityTypeId);
  60. $entity = $storage->load($this->entityId);
  61. // Ensure loading after a save retrieves the updated entity rather than an
  62. // obsolete cached one.
  63. $entity->label = 'New label';
  64. $entity->save();
  65. $entity = $storage->load($this->entityId);
  66. $this->assertIdentical($entity->label, 'New label');
  67. // Ensure loading after a delete retrieves NULL rather than an obsolete
  68. // cached one.
  69. $entity->delete();
  70. $this->assertNull($storage->load($this->entityId));
  71. }
  72. /**
  73. * Tests that the static cache is sensitive to config overrides.
  74. */
  75. public function testConfigOverride() {
  76. /** @var \Drupal\Core\Config\Entity\ConfigEntityStorage $storage */
  77. $storage = \Drupal::entityManager()->getStorage($this->entityTypeId);
  78. // Prime the cache prior to adding a config override.
  79. $storage->load($this->entityId);
  80. // Add the config override, and ensure that what is loaded is correct
  81. // despite the prior cache priming.
  82. \Drupal::configFactory()->addOverride(new ConfigOverrider());
  83. $entity_override = $storage->load($this->entityId);
  84. $this->assertIdentical($entity_override->label, 'Overridden label');
  85. // Load override free to ensure that loading the config entity again does
  86. // not return the overridden value.
  87. $entity_no_override = $storage->loadOverrideFree($this->entityId);
  88. $this->assertNotIdentical($entity_no_override->label, 'Overridden label');
  89. $this->assertNotIdentical($entity_override->_loadStamp, $entity_no_override->_loadStamp);
  90. // Reload the entity and ensure the cache is used.
  91. $this->assertIdentical($storage->loadOverrideFree($this->entityId)->_loadStamp, $entity_no_override->_loadStamp);
  92. // Enable overrides and reload the entity and ensure the cache is used.
  93. $this->assertIdentical($storage->load($this->entityId)->_loadStamp, $entity_override->_loadStamp);
  94. }
  95. }