3 namespace Drupal\KernelTests\Core\Config;
5 use Drupal\Core\Config\StorageComparer;
6 use Drupal\KernelTests\KernelTestBase;
9 * Tests config snapshot creation and updating.
13 class ConfigSnapshotTest extends KernelTestBase {
20 public static $modules = ['config_test', 'system'];
25 protected function setUp() {
27 // Update the config snapshot. This allows the parent::setUp() to write
28 // configuration files.
29 \Drupal::service('config.manager')->createSnapshot(\Drupal::service('config.storage'), \Drupal::service('config.storage.snapshot'));
30 $this->copyConfig($this->container->get('config.storage'), $this->container->get('config.storage.sync'));
34 * Tests config snapshot creation and updating.
36 public function testSnapshot() {
37 $active = $this->container->get('config.storage');
38 $sync = $this->container->get('config.storage.sync');
39 $snapshot = $this->container->get('config.storage.snapshot');
40 $config_manager = $this->container->get('config.manager');
41 $config_name = 'config_test.system';
45 $active_snapshot_comparer = new StorageComparer($active, $snapshot, $config_manager);
46 $sync_snapshot_comparer = new StorageComparer($sync, $snapshot, $config_manager);
48 // Verify that we have an initial snapshot that matches the active
49 // configuration. This has to be true as no config should be installed.
50 $this->assertFalse($active_snapshot_comparer->createChangelist()->hasChanges());
52 // Install the default config.
53 $this->installConfig(['config_test']);
54 // Although we have imported config this has not affected the snapshot.
55 $this->assertTrue($active_snapshot_comparer->reset()->hasChanges());
57 // Update the config snapshot.
58 \Drupal::service('config.manager')->createSnapshot($active, $snapshot);
60 // The snapshot and active config should now contain the same config
62 $this->assertFalse($active_snapshot_comparer->reset()->hasChanges());
64 // Change a configuration value in sync.
65 $sync_data = $this->config($config_name)->get();
66 $sync_data[$config_key] = $new_data;
67 $sync->write($config_name, $sync_data);
69 // Verify that active and snapshot match, and that sync doesn't match
71 $this->assertFalse($active_snapshot_comparer->reset()->hasChanges());
72 $this->assertTrue($sync_snapshot_comparer->createChangelist()->hasChanges());
74 // Import changed data from sync to active.
75 $this->configImporter()->import();
77 // Verify changed config was properly imported.
78 \Drupal::configFactory()->reset($config_name);
79 $this->assertIdentical($this->config($config_name)->get($config_key), $new_data);
81 // Verify that a new snapshot was created which and that it matches
83 $this->assertFalse($active_snapshot_comparer->reset()->hasChanges());