645638d83239ab51485db64af724c62320a5d037
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Config / ConfigSnapshotTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Config;
4
5 use Drupal\Core\Config\StorageComparer;
6 use Drupal\KernelTests\KernelTestBase;
7
8 /**
9  * Tests config snapshot creation and updating.
10  *
11  * @group config
12  */
13 class ConfigSnapshotTest extends KernelTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['config_test', 'system'];
21
22   /**
23    * {@inheritdoc}
24    */
25   protected function setUp() {
26     parent::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'));
32   }
33
34   /**
35    * Tests config snapshot creation and updating.
36    */
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';
43     $config_key = 'foo';
44     $new_data = 'foobar';
45
46     $active_snapshot_comparer = new StorageComparer($active, $snapshot, $config_manager);
47     $sync_snapshot_comparer = new StorageComparer($sync, $snapshot, $config_manager);
48
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());
52
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());
57
58     // Update the config snapshot.
59     \Drupal::service('config.manager')->createSnapshot($active, $snapshot);
60
61     // The snapshot and active config should now contain the same config
62     // objects.
63     $this->assertFalse($active_snapshot_comparer->reset()->hasChanges());
64
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);
69
70     // Verify that active and snapshot match, and that sync doesn't match
71     // active.
72     $this->assertFalse($active_snapshot_comparer->reset()->hasChanges());
73     $this->assertTrue($sync_snapshot_comparer->createChangelist()->hasChanges());
74
75     // Import changed data from sync to active.
76     $this->configImporter()->import();
77
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);
81
82     // Verify that a new snapshot was created which and that it matches
83     // the active config.
84     $this->assertFalse($active_snapshot_comparer->reset()->hasChanges());
85   }
86
87 }