f29b88acd1410322035d6701ba6b84a9d9b6b036
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Config / ConfigDiffTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Config;
4
5 use Drupal\KernelTests\KernelTestBase;
6
7 /**
8  * Calculating the difference between two sets of configuration.
9  *
10  * @group config
11  */
12 class ConfigDiffTest extends KernelTestBase {
13
14   /**
15    * Modules to enable.
16    *
17    * @var array
18    */
19   public static $modules = ['config_test', 'system'];
20
21   /**
22    * Tests calculating the difference between two sets of configuration.
23    */
24   public function testDiff() {
25     $active = $this->container->get('config.storage');
26     $sync = $this->container->get('config.storage.sync');
27     $config_name = 'config_test.system';
28     $change_key = 'foo';
29     $remove_key = '404';
30     $add_key = 'biff';
31     $add_data = 'bangpow';
32     $change_data = 'foobar';
33
34     // Install the default config.
35     $this->installConfig(['config_test']);
36     $original_data = \Drupal::config($config_name)->get();
37
38     // Change a configuration value in sync.
39     $sync_data = $original_data;
40     $sync_data[$change_key] = $change_data;
41     $sync_data[$add_key] = $add_data;
42     $sync->write($config_name, $sync_data);
43
44     // Verify that the diff reflects a change.
45     $diff = \Drupal::service('config.manager')->diff($active, $sync, $config_name);
46     $edits = $diff->getEdits();
47     $this->assertYamlEdit($edits, $change_key, 'change',
48       [$change_key . ': ' . $original_data[$change_key]],
49       [$change_key . ': ' . $change_data]);
50
51     // Reset data back to original, and remove a key
52     $sync_data = $original_data;
53     unset($sync_data[$remove_key]);
54     $sync->write($config_name, $sync_data);
55
56     // Verify that the diff reflects a removed key.
57     $diff = \Drupal::service('config.manager')->diff($active, $sync, $config_name);
58     $edits = $diff->getEdits();
59     $this->assertYamlEdit($edits, $change_key, 'copy');
60     $this->assertYamlEdit($edits, $remove_key, 'delete',
61       [$remove_key . ': ' . $original_data[$remove_key]],
62       FALSE
63     );
64
65     // Reset data back to original and add a key
66     $sync_data = $original_data;
67     $sync_data[$add_key] = $add_data;
68     $sync->write($config_name, $sync_data);
69
70     // Verify that the diff reflects an added key.
71     $diff = \Drupal::service('config.manager')->diff($active, $sync, $config_name);
72     $edits = $diff->getEdits();
73     $this->assertYamlEdit($edits, $change_key, 'copy');
74     $this->assertYamlEdit($edits, $add_key, 'add', FALSE, [$add_key . ': ' . $add_data]);
75
76     // Test diffing a renamed config entity.
77     $test_entity_id = $this->randomMachineName();
78     $test_entity = \Drupal::entityTypeManager()->getStorage('config_test')->create([
79       'id' => $test_entity_id,
80       'label' => $this->randomMachineName(),
81     ]);
82     $test_entity->save();
83     $data = $active->read('config_test.dynamic.' . $test_entity_id);
84     $sync->write('config_test.dynamic.' . $test_entity_id, $data);
85     $config_name = 'config_test.dynamic.' . $test_entity_id;
86     $diff = \Drupal::service('config.manager')->diff($active, $sync, $config_name, $config_name);
87     // Prove the fields match.
88     $edits = $diff->getEdits();
89     $this->assertEqual($edits[0]->type, 'copy', 'The first item in the diff is a copy.');
90     $this->assertEqual(count($edits), 1, 'There is one item in the diff');
91
92     // Rename the entity.
93     $new_test_entity_id = $this->randomMachineName();
94     $test_entity->set('id', $new_test_entity_id);
95     $test_entity->save();
96
97     $diff = \Drupal::service('config.manager')->diff($active, $sync, 'config_test.dynamic.' . $new_test_entity_id, $config_name);
98     $edits = $diff->getEdits();
99     $this->assertYamlEdit($edits, 'uuid', 'copy');
100     $this->assertYamlEdit($edits, 'id', 'change',
101       ['id: ' . $new_test_entity_id],
102       ['id: ' . $test_entity_id]);
103     $this->assertYamlEdit($edits, 'label', 'copy');
104     $this->assertEqual($edits[2]->type, 'copy', 'The third item in the diff is a copy.');
105     $this->assertEqual(count($edits), 3, 'There are three items in the diff.');
106   }
107
108   /**
109    * Tests calculating the difference between two sets of config collections.
110    */
111   public function testCollectionDiff() {
112     /** @var \Drupal\Core\Config\StorageInterface $active */
113     $active = $this->container->get('config.storage');
114     /** @var \Drupal\Core\Config\StorageInterface $sync */
115     $sync = $this->container->get('config.storage.sync');
116     $active_test_collection = $active->createCollection('test');
117     $sync_test_collection = $sync->createCollection('test');
118
119     $config_name = 'config_test.test';
120     $data = ['foo' => 'bar'];
121
122     $active->write($config_name, $data);
123     $sync->write($config_name, $data);
124     $active_test_collection->write($config_name, $data);
125     $sync_test_collection->write($config_name, ['foo' => 'baz']);
126
127     // Test the fields match in the default collection diff.
128     $diff = \Drupal::service('config.manager')->diff($active, $sync, $config_name);
129     $edits = $diff->getEdits();
130     $this->assertEqual($edits[0]->type, 'copy', 'The first item in the diff is a copy.');
131     $this->assertEqual(count($edits), 1, 'There is one item in the diff');
132
133     // Test that the differences are detected when diffing the collection.
134     $diff = \Drupal::service('config.manager')->diff($active, $sync, $config_name, NULL, 'test');
135     $edits = $diff->getEdits();
136     $this->assertYamlEdit($edits, 'foo', 'change', ['foo: bar'], ['foo: baz']);
137   }
138
139   /**
140    * Helper method to test that an edit is found in a diff'd YAML file.
141    *
142    * @param array $edits
143    *   A list of edits.
144    * @param string $field
145    *   The field key that is being asserted.
146    * @param string $type
147    *   The type of edit that is being asserted.
148    * @param mixed $orig
149    *   (optional) The original value of of the edit. If not supplied, assertion
150    *   is skipped.
151    * @param mixed $closing
152    *   (optional) The closing value of of the edit. If not supplied, assertion
153    *   is skipped.
154    */
155   protected function assertYamlEdit(array $edits, $field, $type, $orig = NULL, $closing = NULL) {
156     $match = FALSE;
157     foreach ($edits as $edit) {
158       // Choose which section to search for the field.
159       $haystack = $type == 'add' ? $edit->closing : $edit->orig;
160       // Look through each line and try and find the key.
161       if (is_array($haystack)) {
162         foreach ($haystack as $item) {
163           if (strpos($item, $field . ':') === 0) {
164             $match = TRUE;
165             // Assert that the edit is of the type specified.
166             $this->assertEqual($edit->type, $type, "The $field item in the diff is a $type");
167             // If an original value was given, assert that it matches.
168             if (isset($orig)) {
169               $this->assertIdentical($edit->orig, $orig, "The original value for key '$field' is correct.");
170             }
171             // If a closing value was given, assert that it matches.
172             if (isset($closing)) {
173               $this->assertIdentical($edit->closing, $closing, "The closing value for key '$field' is correct.");
174             }
175             // Break out of the search entirely.
176             break 2;
177           }
178         }
179       }
180     }
181
182     // If we didn't match anything, fail.
183     if (!$match) {
184       $this->fail("$field edit was not matched");
185     }
186   }
187
188 }