View source
<?php
namespace Drupal\Tests\config_sync\Kernel;
use Drupal\config_distro\Event\ConfigDistroEvents;
use Drupal\config_snapshot\ConfigSnapshotStorageTrait;
use Drupal\config_snapshot\Entity\ConfigSnapshot;
use Drupal\config_sync\ConfigSyncListerInterface;
use Drupal\config_sync\ConfigSyncSnapshotterInterface;
use Drupal\Core\Config\ConfigImporter;
use Drupal\Core\Config\StorageComparer;
use Drupal\Core\Config\StorageInterface;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\NodeType;
class UpdateModeTest extends KernelTestBase {
use ConfigSnapshotStorageTrait;
protected $preserveGlobalState = TRUE;
protected $testSnapshotStorage;
protected $configImporter;
protected $nodeTypeNames = [
1 => 'config_sync_test_1',
2 => 'config_sync_test_2',
3 => 'config_sync_test_3',
4 => 'config_sync_test_4',
];
protected $configNames = [];
public static $modules = [
'system',
'field',
'filter',
'text',
'user',
'node',
'config_distro',
'config_filter',
'config_merge',
'config_normalizer',
'config_provider',
'config_snapshot',
'config_update',
'config_sync',
'config_sync_test',
];
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('node');
$this
->installEntitySchema('config_snapshot');
$this
->installConfig([
'system',
'user',
'field',
'filter',
'text',
'node',
'config_sync_test',
]);
$this->container
->get('config_sync.snapshotter')
->refreshExtensionSnapshot('module', [
'config_sync_test',
], ConfigSyncSnapshotterInterface::SNAPSHOT_MODE_INSTALL);
$content_type_1 = NodeType::load($this->nodeTypeNames[1]);
$content_type_1
->set('name', 'Custom name')
->set('description', 'Prior description')
->set('help', 'Custom help')
->save();
$test_module_snapshot = ConfigSnapshot::load(ConfigSyncSnapshotterInterface::CONFIG_SNAPSHOT_SET . '.module.config_sync_test');
$this->configNames[1] = $content_type_1
->getEntityType()
->getConfigPrefix() . '.' . $content_type_1
->id();
$content_type_1_snapshot = $test_module_snapshot
->getItem(StorageInterface::DEFAULT_COLLECTION, $this->configNames[1]);
$content_type_1_snapshot['name'] = 'Prior name';
$content_type_1_snapshot['description'] = 'Prior description';
$test_module_snapshot
->setItem(StorageInterface::DEFAULT_COLLECTION, $this->configNames[1], $content_type_1_snapshot)
->save();
$content_type_2 = NodeType::load($this->nodeTypeNames[2]);
$this->configNames[2] = $content_type_2
->getEntityType()
->getConfigPrefix() . '.' . $content_type_2
->id();
$test_module_snapshot
->clearItem(StorageInterface::DEFAULT_COLLECTION, $this->configNames[2])
->save();
$content_type_2
->delete();
$content_type_3 = NodeType::load($this->nodeTypeNames[3]);
$this->configNames[3] = $content_type_3
->getEntityType()
->getConfigPrefix() . '.' . $content_type_3
->id();
$content_type_3
->delete($this->nodeTypeNames[3]);
$content_type_4 = NodeType::load($this->nodeTypeNames[4]);
$this->configNames[4] = $content_type_4
->getEntityType()
->getConfigPrefix() . '.' . $content_type_4
->id();
$content_type_4
->set('name', 'Custom name')
->save();
$this->testSnapshotStorage = $this
->getConfigSnapshotStorage(ConfigSyncSnapshotterInterface::CONFIG_SNAPSHOT_SET, 'module', 'config_sync_test');
$expected_snapshot_items = $this->configNames;
unset($expected_snapshot_items[2]);
$expected_snapshot_items = array_values($expected_snapshot_items);
$snapshot_items = $this->testSnapshotStorage
->listAll();
$this
->assertSame($snapshot_items, $expected_snapshot_items, 'Snapshot items match those expected.');
}
protected function setUpdateMode($update_mode) {
$this->container
->get('state')
->set('config_sync.update_mode', $update_mode);
$this->container
->get('kernel')
->rebuildContainer();
$storage_comparer = new StorageComparer($this->container
->get('config_distro.storage.distro'), $this->container
->get('config.storage'), $this->container
->get('config.manager'));
$this->configImporter = new ConfigImporter($storage_comparer
->createChangelist(), $this->container
->get('event_dispatcher'), $this->container
->get('config.manager'), $this->container
->get('lock'), $this->container
->get('config.typed'), $this->container
->get('module_handler'), $this->container
->get('module_installer'), $this->container
->get('theme_handler'), $this->container
->get('string_translation'));
}
public function testUpdateModeMerge() {
$this
->setUpdateMode(ConfigSyncListerInterface::UPDATE_MODE_MERGE);
$creates = $this->configImporter
->getUnprocessedConfiguration('create');
$updates = $this->configImporter
->getUnprocessedConfiguration('update');
$this
->assertEquals(0, count($this->configImporter
->getUnprocessedConfiguration('delete')), 'There are no configuration items to delete.');
$expected_creates = [
$this->configNames[2],
];
$this
->assertSame($creates, $expected_creates, 'Create operations match those expected.');
$expected_updates = [
$this->configNames[1],
];
$this
->assertSame($updates, $expected_updates, 'Update operations match those expected.');
$this->configImporter
->import();
$this->container
->get('event_dispatcher')
->dispatch(ConfigDistroEvents::IMPORT);
$node_type_1 = NodeType::load($this->nodeTypeNames[1]);
$this
->assertEquals('Custom name', $node_type_1
->label());
$this
->assertEquals('Provided description', $node_type_1
->get('description'));
$this
->assertEquals('Custom help', $node_type_1
->get('help'));
$this
->verifySnapshot();
}
public function testUpdateModePartialReset() {
$this
->setUpdateMode(ConfigSyncListerInterface::UPDATE_MODE_PARTIAL_RESET);
$creates = $this->configImporter
->getUnprocessedConfiguration('create');
$updates = $this->configImporter
->getUnprocessedConfiguration('update');
$this
->assertEquals(0, count($this->configImporter
->getUnprocessedConfiguration('delete')), 'There are no configuration items to delete.');
$expected_creates = [
$this->configNames[2],
];
$this
->assertSame($creates, $expected_creates, 'Create operations match those expected.');
$expected_updates = [
$this->configNames[1],
];
$this
->assertSame($updates, $expected_updates, 'Update operations match those expected.');
$this->configImporter
->import();
$this->container
->get('event_dispatcher')
->dispatch(ConfigDistroEvents::IMPORT);
$node_type_1 = NodeType::load($this->nodeTypeNames[1]);
$this
->assertEquals('Provided name', $node_type_1
->label());
$this
->assertEquals('Provided description', $node_type_1
->get('description'));
$this
->assertEquals('Provided help', $node_type_1
->get('help'));
$this
->verifySnapshot();
}
public function testUpdateModeFullReset() {
$this
->setUpdateMode(ConfigSyncListerInterface::UPDATE_MODE_FULL_RESET);
$creates = $this->configImporter
->getUnprocessedConfiguration('create');
$updates = $this->configImporter
->getUnprocessedConfiguration('update');
$this
->assertEquals(0, count($this->configImporter
->getUnprocessedConfiguration('delete')), 'There are no configuration items to delete.');
$expected_creates = [
$this->configNames[2],
$this->configNames[3],
];
$this
->assertSame($creates, $expected_creates, 'Create operations match those expected.');
$expected_updates = [
$this->configNames[1],
$this->configNames[4],
];
$this
->assertSame($updates, $expected_updates, 'Update operations match those expected.');
$this->configImporter
->import();
$this->container
->get('event_dispatcher')
->dispatch(ConfigDistroEvents::IMPORT);
foreach (array_keys($this->configNames) as $index) {
$node_type = NodeType::load($this->nodeTypeNames[$index]);
$this
->assertEquals('Provided name', $node_type
->label());
$this
->assertEquals('Provided description', $node_type
->get('description'));
$this
->assertEquals('Provided help', $node_type
->get('help'));
}
$this
->verifySnapshot();
}
protected function verifySnapshot() {
$expected_snapshot_items = array_values($this->configNames);
$test_snapshot_storage = $this
->getConfigSnapshotStorage(ConfigSyncSnapshotterInterface::CONFIG_SNAPSHOT_SET, 'module', 'config_sync_test');
$snapshot_items = $test_snapshot_storage
->listAll();
$this
->assertSame($snapshot_items, $expected_snapshot_items, 'Snapshot items match those expected.');
$extension_storage = $this->container
->get('config_update.extension_storage');
foreach (array_keys($this->configNames) as $index) {
$this
->assertEquals($extension_storage
->read($this->configNames[$index]), $test_snapshot_storage
->read($this->configNames[$index]));
}
}
}