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 $this->installConfig(['system']);
28 // Update the config snapshot. This allows the parent::setUp() to write
29 // configuration files.
30 \Drupal::service('config.manager')->createSnapshot(\Drupal::service('config.storage'), \Drupal::service('config.storage.snapshot'));
31 $this->copyConfig($this->container->get('config.storage'), $this->container->get('config.storage.sync'));
35 * Tests config snapshot creation and updating.
37 public function testSnapshot() {
38 $active = $this->container->get('config.storage');
39 $sync = $this->container->get('config.storage.sync');
40 $snapshot = $this->container->get('config.storage.snapshot');
41 $config_manager = $this->container->get('config.manager');
42 $config_name = 'config_test.system';
46 $active_snapshot_comparer = new StorageComparer($active, $snapshot, $config_manager);
47 $sync_snapshot_comparer = new StorageComparer($sync, $snapshot, $config_manager);
49 // Verify that we have an initial snapshot that matches the active
50 // configuration. This has to be true as no config should be installed.
51 $this->assertFalse($active_snapshot_comparer->createChangelist()->hasChanges());
53 // Install the default config.
54 $this->installConfig(['config_test']);
55 // Although we have imported config this has not affected the snapshot.
56 $this->assertTrue($active_snapshot_comparer->reset()->hasChanges());
58 // Update the config snapshot.
59 \Drupal::service('config.manager')->createSnapshot($active, $snapshot);
61 // The snapshot and active config should now contain the same config
63 $this->assertFalse($active_snapshot_comparer->reset()->hasChanges());
65 // Change a configuration value in sync.
66 $sync_data = $this->config($config_name)->get();
67 $sync_data[$config_key] = $new_data;
68 $sync->write($config_name, $sync_data);
70 // Verify that active and snapshot match, and that sync doesn't match
72 $this->assertFalse($active_snapshot_comparer->reset()->hasChanges());
73 $this->assertTrue($sync_snapshot_comparer->createChangelist()->hasChanges());
75 // Import changed data from sync to active.
76 $this->configImporter()->import();
78 // Verify changed config was properly imported.
79 \Drupal::configFactory()->reset($config_name);
80 $this->assertIdentical($this->config($config_name)->get($config_key), $new_data);
82 // Verify that a new snapshot was created which and that it matches
84 $this->assertFalse($active_snapshot_comparer->reset()->hasChanges());