View source
<?php
namespace Drupal\Tests\config_split\Kernel;
use Drupal\Core\Config\MemoryStorage;
use Drupal\Core\Config\StorageInterface;
use Drupal\KernelTests\KernelTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\config_filter\Kernel\ConfigStorageTestTrait;
class IndividualExportImportTest extends KernelTestBase {
use ConfigStorageTestTrait;
use SplitTestTrait;
public static $modules = [
'system',
'language',
'user',
'node',
'field',
'text',
'config',
'config_test',
'config_split',
'config_filter',
];
protected $split;
protected $io;
protected $t;
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('user');
$this
->installEntitySchema('node');
$this
->installConfig([
'system',
'field',
'config_test',
]);
ConfigurableLanguage::createFromLangcode('fr')
->save();
ConfigurableLanguage::createFromLangcode('de')
->save();
$extension = $this->container
->get('config.storage')
->read('core.extension');
$extension['module'] = module_config_sort($extension['module']);
$this->container
->get('config.storage')
->write('core.extension', $extension);
$this->split = $this
->createSplitConfig($this
->randomMachineName(), [
'module' => [
'config_test' => 0,
],
]);
$this->io = new class {
public $calls;
public function __call($name, $arguments) {
$this->calls[] = [
$name => $arguments,
];
return TRUE;
}
};
$this->t = static function (string $s, $args = []) : string {
return strtr($s, $args);
};
}
public function testExportEmptyFiles() {
static::assertEmpty($this
->getSyncFileStorage()
->listAll());
static::assertEmpty($this
->getSyncFileStorage()
->getAllCollectionNames());
static::assertEmpty($this
->getSplitSourceStorage($this->split)
->listAll());
static::assertEmpty($this
->getSplitSourceStorage($this->split)
->getAllCollectionNames());
static::assertEmpty($this
->getSplitPreviewStorage($this->split)
->listAll());
static::assertEmpty($this
->getSplitPreviewStorage($this->split)
->getAllCollectionNames());
$this->container
->get('config_split.cli')
->ioExport($this->split
->getName(), $this->io, $this->t);
static::assertEmpty($this
->getSyncFileStorage()
->listAll());
static::assertEmpty($this
->getSyncFileStorage()
->getAllCollectionNames());
static::assertStorageEquals($this
->getSplitExpectationStorage(), $this
->getSplitSourceStorage($this->split));
static::assertCount(2, $this->io->calls);
static::assertEquals([
'confirm',
], array_keys($this->io->calls[0]));
static::assertEquals([
'success' => [
'Configuration successfully exported.',
],
], $this->io->calls[1]);
}
public function testExportNonEmptyFiles() {
$random = $this
->getRandomDataStorage();
$this
->copyConfig($random, $this
->getSyncFileStorage());
$this
->copyConfig($this
->getRandomDataStorage(), $this
->getSplitSourceStorage($this->split));
$this->container
->get('config_split.cli')
->ioExport($this->split
->getName(), $this->io, $this->t);
static::assertStorageEquals($random, $this
->getSyncFileStorage());
static::assertStorageEquals($this
->getSplitExpectationStorage(), $this
->getSplitSourceStorage($this->split));
static::assertCount(2, $this->io->calls);
static::assertEquals([
'confirm',
], array_keys($this->io->calls[0]));
static::assertEquals([
'success' => [
'Configuration successfully exported.',
],
], $this->io->calls[1]);
}
public function testImport() {
$storage = $this
->getSplitSourceStorage($this->split);
$this
->copyConfig($this
->getSplitExpectationStorage(), $storage);
static::assertEmpty($this
->getSyncFileStorage()
->listAll());
static::assertEmpty($this
->getSyncFileStorage()
->getAllCollectionNames());
$this->container
->get('config_split.cli')
->ioImport($this->split
->getName(), $this->io, $this->t);
static::assertCount(2, $this->io->calls);
static::assertEquals([
'confirm',
], array_keys($this->io->calls[0]));
if (isset($this->io->calls[1]['error'])) {
static::assertEquals([
'',
], $this->io->calls[1]['error']);
}
static::assertEquals([
'text' => [
'There are no changes to import.',
],
], $this->io->calls[1]);
$data = $storage
->read('config_test.system');
$data['foo'] = 'test split';
$storage
->write('config_test.system', $data);
$this->io->calls = [];
$this->container
->get('config_split.cli')
->ioImport($this->split
->getName(), $this->io, $this->t);
static::assertEquals($data, $this
->getActiveStorage()
->read('config_test.system'));
static::assertCount(2, $this->io->calls);
static::assertEquals([
'confirm',
], array_keys($this->io->calls[0]));
static::assertEquals([
'success' => [
'Configuration successfully imported.',
],
], $this->io->calls[1]);
}
protected function getSplitExpectationStorage() : StorageInterface {
$expected = new MemoryStorage();
foreach (array_merge($this
->getActiveStorage()
->getAllCollectionNames(), [
StorageInterface::DEFAULT_COLLECTION,
]) as $collection) {
$active = $this
->getActiveStorage()
->createCollection($collection);
$expected = $expected
->createCollection($collection);
foreach ($active
->listAll() as $name) {
if (strpos($name, 'config_test') !== FALSE) {
$expected
->write($name, $active
->read($name));
}
}
}
return $expected;
}
protected function getRandomDataStorage() : StorageInterface {
$random = new MemoryStorage();
$collections = array_merge($this
->getActiveStorage()
->getAllCollectionNames(), [
StorageInterface::DEFAULT_COLLECTION,
$this
->randomMachineName(),
]);
foreach ($collections as $collection) {
$random = $random
->createCollection($collection);
$size = random_int(4, 10);
for ($i = 0; $i < $size; $i++) {
$random
->write($this
->randomMachineName(), (array) $this
->randomObject());
}
}
return $random;
}
}