ImportStorageTransformerTest.php in Drupal 10
File
core/tests/Drupal/KernelTests/Core/Config/ImportStorageTransformerTest.php
View source
<?php
namespace Drupal\KernelTests\Core\Config;
use Drupal\Core\Config\ConfigImporter;
use Drupal\Core\Config\ImportStorageTransformer;
use Drupal\Core\Config\MemoryStorage;
use Drupal\Core\Config\StorageTransformerException;
use Drupal\Core\Lock\NullLockBackend;
use Drupal\KernelTests\KernelTestBase;
class ImportStorageTransformerTest extends KernelTestBase {
protected static $modules = [
'system',
'config_transformer_test',
];
protected function setUp() : void {
parent::setUp();
$this
->installConfig([
'system',
]);
}
public function testTransform() {
$rawConfig = $this
->config('system.site')
->getRawData();
$storage = new MemoryStorage();
$this
->copyConfig($this->container
->get('config.storage'), $storage);
$import = $this->container
->get('config.import_transformer')
->transform($storage);
$config = $import
->read('system.site');
$this
->assertEquals($rawConfig['name'] . ' Arrr', $config['name']);
$this
->assertEquals($rawConfig['slogan'], $config['slogan']);
$config['name'] = 'New name';
$config['slogan'] = 'New slogan';
$storage
->write('system.site', $config);
$import = $this->container
->get('config.import_transformer')
->transform($storage);
$config = $import
->read('system.site');
$this
->assertEquals($rawConfig['name'] . ' Arrr', $config['name']);
$this
->assertEquals('New slogan', $config['slogan']);
}
public function testTransformLocked() {
$lock = $this
->createMock('Drupal\\Core\\Lock\\LockBackendInterface');
$lock
->expects($this
->exactly(2))
->method('acquire')
->with(ImportStorageTransformer::LOCK_NAME)
->will($this
->returnValue(FALSE));
$lock
->expects($this
->once())
->method('wait')
->with(ImportStorageTransformer::LOCK_NAME);
$transformer = new ImportStorageTransformer($this->container
->get('event_dispatcher'), $this->container
->get('database'), $lock, new NullLockBackend());
$this
->expectException(StorageTransformerException::class);
$this
->expectExceptionMessage("Cannot acquire config import transformer lock.");
$transformer
->transform(new MemoryStorage());
}
public function testTransformWhileImporting() {
$storage = $this->container
->get('config.import_transformer')
->transform($this->container
->get('config.storage'));
$lock = $this
->createMock('Drupal\\Core\\Lock\\LockBackendInterface');
$lock
->expects($this
->once())
->method('lockMayBeAvailable')
->with(ConfigImporter::LOCK_NAME)
->will($this
->returnValue(FALSE));
$transformer = new ImportStorageTransformer($this->container
->get('event_dispatcher'), $this->container
->get('database'), new NullLockBackend(), $lock);
$import = $transformer
->transform(new MemoryStorage());
$this
->assertEquals($storage
->listAll(), $import
->listAll());
$this
->assertNotEmpty($import
->read('system.site'));
}
}