ConfigEntityStaticCacheTest.php in Drupal 10
File
core/tests/Drupal/KernelTests/Core/Config/ConfigEntityStaticCacheTest.php
View source
<?php
namespace Drupal\KernelTests\Core\Config;
use Drupal\config_entity_static_cache_test\ConfigOverrider;
use Drupal\KernelTests\KernelTestBase;
class ConfigEntityStaticCacheTest extends KernelTestBase {
protected static $modules = [
'config_test',
'config_entity_static_cache_test',
];
protected $entityTypeId;
protected $entityId;
protected function setUp() : void {
parent::setUp();
$this->entityTypeId = 'config_test';
$this->entityId = 'test_1';
$this->container
->get('entity_type.manager')
->getStorage($this->entityTypeId)
->create([
'id' => $this->entityId,
'label' => 'Original label',
])
->save();
}
public function testCacheHit() {
$storage = $this->container
->get('entity_type.manager')
->getStorage($this->entityTypeId);
$entity_1 = $storage
->load($this->entityId);
$entity_2 = $storage
->load($this->entityId);
$this
->assertSame($entity_1->_loadStamp, $entity_2->_loadStamp);
}
public function testReset() {
$storage = $this->container
->get('entity_type.manager')
->getStorage($this->entityTypeId);
$entity = $storage
->load($this->entityId);
$entity->label = 'New label';
$entity
->save();
$entity = $storage
->load($this->entityId);
$this
->assertSame('New label', $entity->label);
$entity
->delete();
$this
->assertNull($storage
->load($this->entityId));
}
public function testConfigOverride() {
$storage = \Drupal::entityTypeManager()
->getStorage($this->entityTypeId);
$storage
->load($this->entityId);
\Drupal::configFactory()
->addOverride(new ConfigOverrider());
$entity_override = $storage
->load($this->entityId);
$this
->assertSame('Overridden label', $entity_override->label);
$entity_no_override = $storage
->loadOverrideFree($this->entityId);
$this
->assertNotSame('Overridden label', $entity_no_override->label);
$this
->assertNotSame($entity_override->_loadStamp, $entity_no_override->_loadStamp);
$this
->assertSame($entity_no_override->_loadStamp, $storage
->loadOverrideFree($this->entityId)->_loadStamp);
$this
->assertSame($entity_override->_loadStamp, $storage
->load($this->entityId)->_loadStamp);
}
}