View source
<?php
namespace Drupal\KernelTests\Core\Config;
use Drupal\config_override_test\Cache\PirateDayCacheContext;
use Drupal\KernelTests\KernelTestBase;
class CacheabilityMetadataConfigOverrideTest extends KernelTestBase {
protected static $modules = [
'block',
'block_content',
'config',
'config_override_test',
'path_alias',
'system',
'user',
];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('block_content');
$this
->installConfig([
'config_override_test',
]);
}
public function testConfigOverride() {
$GLOBALS['it_is_pirate_day'] = TRUE;
$config_factory = $this->container
->get('config.factory');
$config = $config_factory
->get('system.theme');
$theme = $config
->get('default');
$this
->assertEquals('pirate', $theme);
$this
->assertEquals([
'pirate_day',
], $config
->getCacheContexts());
$this
->assertEquals([
'config:system.theme',
'pirate-day-tag',
], $config
->getCacheTags());
$this
->assertEquals(PirateDayCacheContext::PIRATE_DAY_MAX_AGE, $config
->getCacheMaxAge());
}
public function testConfigEntityOverride() {
$GLOBALS['it_is_pirate_day'] = TRUE;
$entity_type_manager = $this->container
->get('entity_type.manager');
$block = $entity_type_manager
->getStorage('block')
->load('call_to_action');
$this
->assertEquals('Draw yer cutlasses!', $block
->label());
$this
->assertEquals([
'pirate_day',
], $block
->getCacheContexts());
$this
->assertEquals([
'config:block.block.call_to_action',
'pirate-day-tag',
], $block
->getCacheTags());
$this
->assertEquals(PirateDayCacheContext::PIRATE_DAY_MAX_AGE, $block
->getCacheMaxAge());
$this
->assertEquals([
'config:block.block.',
'pirate-day-tag',
], $block
->createDuplicate()
->getCacheTags());
$block
->set('id', 'call_to_looting')
->save();
$this
->assertEquals([
'pirate_day',
], $block
->getCacheContexts());
$this
->assertEquals([
'config:block.block.call_to_looting',
'pirate-day-tag',
], $block
->getCacheTags());
$this
->assertEquals(PirateDayCacheContext::PIRATE_DAY_MAX_AGE, $block
->getCacheMaxAge());
}
}