3 namespace Drupal\Tests\Core\Config;
5 use Drupal\Component\Uuid\Php;
6 use Drupal\Core\Config\StorageComparer;
7 use Drupal\Tests\UnitTestCase;
10 * @coversDefaultClass \Drupal\Core\Config\StorageComparer
13 class StorageComparerTest extends UnitTestCase {
16 * @var \Drupal\Core\Config\StorageInterface|\PHPUnit_Framework_MockObject_MockObject
18 protected $sourceStorage;
21 * @var \Drupal\Core\Config\StorageInterface|\PHPUnit_Framework_MockObject_MockObject
23 protected $targetStorage;
26 * @var \Drupal\Core\Config\ConfigManager|\PHPUnit_Framework_MockObject_MockObject
28 protected $configManager;
31 * The storage comparer to test.
33 * @var \Drupal\Core\Config\StorageComparer
35 protected $storageComparer;
38 * An array of test configuration data keyed by configuration name.
42 protected $configData;
44 protected function setUp() {
45 $this->sourceStorage = $this->getMock('Drupal\Core\Config\StorageInterface');
46 $this->targetStorage = $this->getMock('Drupal\Core\Config\StorageInterface');
47 $this->configManager = $this->getMock('Drupal\Core\Config\ConfigManagerInterface');
48 $this->storageComparer = new StorageComparer($this->sourceStorage, $this->targetStorage, $this->configManager);
51 protected function getConfigData() {
53 // Mock data using minimal data to use ConfigDependencyManger.
55 // Simple config that controls configuration sync.
58 'uuid' => $uuid->generate(),
60 // Config entity which requires another config entity.
61 'field.field.node.article.body' => [
62 'id' => 'node.article.body',
63 'uuid' => $uuid->generate(),
66 'field.storage.node.body',
70 // Config entity which is required by another config entity.
71 'field.storage.node.body' => [
73 'uuid' => $uuid->generate(),
80 // Config entity not which has no dependencies on configuration.
81 'views.view.test_view' => [
83 'uuid' => $uuid->generate(),
91 'system.performance' => [
92 'stale_file_threshold' => 2592000,
96 return $this->configData;
100 * @covers ::createChangelist
102 public function testCreateChangelistNoChange() {
103 $config_data = $this->getConfigData();
104 $config_files = array_keys($config_data);
105 $this->sourceStorage->expects($this->once())
107 ->will($this->returnValue($config_files));
108 $this->targetStorage->expects($this->once())
110 ->will($this->returnValue($config_files));
111 $this->sourceStorage->expects($this->once())
112 ->method('readMultiple')
113 ->will($this->returnValue($config_data));
114 $this->targetStorage->expects($this->once())
115 ->method('readMultiple')
116 ->will($this->returnValue($config_data));
117 $this->sourceStorage->expects($this->once())
118 ->method('getAllCollectionNames')
119 ->will($this->returnValue([]));
120 $this->targetStorage->expects($this->once())
121 ->method('getAllCollectionNames')
122 ->will($this->returnValue([]));
124 $this->storageComparer->createChangelist();
125 $this->assertEmpty($this->storageComparer->getChangelist('create'));
126 $this->assertEmpty($this->storageComparer->getChangelist('delete'));
127 $this->assertEmpty($this->storageComparer->getChangelist('update'));
131 * @covers ::createChangelist
133 public function testCreateChangelistCreate() {
134 $target_data = $source_data = $this->getConfigData();
135 unset($target_data['field.storage.node.body']);
136 unset($target_data['field.field.node.article.body']);
137 unset($target_data['views.view.test_view']);
139 $this->sourceStorage->expects($this->once())
141 ->will($this->returnValue(array_keys($source_data)));
142 $this->targetStorage->expects($this->once())
144 ->will($this->returnValue(array_keys($target_data)));
145 $this->sourceStorage->expects($this->once())
146 ->method('readMultiple')
147 ->will($this->returnValue($source_data));
148 $this->targetStorage->expects($this->once())
149 ->method('readMultiple')
150 ->will($this->returnValue($target_data));
151 $this->sourceStorage->expects($this->once())
152 ->method('getAllCollectionNames')
153 ->will($this->returnValue([]));
154 $this->targetStorage->expects($this->once())
155 ->method('getAllCollectionNames')
156 ->will($this->returnValue([]));
158 $this->storageComparer->createChangelist();
160 'field.storage.node.body',
161 'field.field.node.article.body',
162 'views.view.test_view',
164 $this->assertEquals($expected, $this->storageComparer->getChangelist('create'));
165 $this->assertEmpty($this->storageComparer->getChangelist('delete'));
166 $this->assertEmpty($this->storageComparer->getChangelist('update'));
170 * @covers ::createChangelist
172 public function testCreateChangelistDelete() {
173 $target_data = $source_data = $this->getConfigData();
174 unset($source_data['field.storage.node.body']);
175 unset($source_data['field.field.node.article.body']);
176 unset($source_data['views.view.test_view']);
178 $this->sourceStorage->expects($this->once())
180 ->will($this->returnValue(array_keys($source_data)));
181 $this->targetStorage->expects($this->once())
183 ->will($this->returnValue(array_keys($target_data)));
184 $this->sourceStorage->expects($this->once())
185 ->method('readMultiple')
186 ->will($this->returnValue($source_data));
187 $this->targetStorage->expects($this->once())
188 ->method('readMultiple')
189 ->will($this->returnValue($target_data));
190 $this->sourceStorage->expects($this->once())
191 ->method('getAllCollectionNames')
192 ->will($this->returnValue([]));
193 $this->targetStorage->expects($this->once())
194 ->method('getAllCollectionNames')
195 ->will($this->returnValue([]));
197 $this->storageComparer->createChangelist();
199 'views.view.test_view',
200 'field.field.node.article.body',
201 'field.storage.node.body',
203 $this->assertEquals($expected, $this->storageComparer->getChangelist('delete'));
204 $this->assertEmpty($this->storageComparer->getChangelist('create'));
205 $this->assertEmpty($this->storageComparer->getChangelist('update'));
209 * @covers ::createChangelist
211 public function testCreateChangelistUpdate() {
212 $target_data = $source_data = $this->getConfigData();
213 $source_data['system.site']['title'] = 'Drupal New!';
214 $source_data['field.field.node.article.body']['new_config_key'] = 'new data';
215 $source_data['field.storage.node.body']['new_config_key'] = 'new data';
217 $this->sourceStorage->expects($this->once())
219 ->will($this->returnValue(array_keys($source_data)));
220 $this->targetStorage->expects($this->once())
222 ->will($this->returnValue(array_keys($target_data)));
223 $this->sourceStorage->expects($this->once())
224 ->method('readMultiple')
225 ->will($this->returnValue($source_data));
226 $this->targetStorage->expects($this->once())
227 ->method('readMultiple')
228 ->will($this->returnValue($target_data));
229 $this->sourceStorage->expects($this->once())
230 ->method('getAllCollectionNames')
231 ->will($this->returnValue([]));
232 $this->targetStorage->expects($this->once())
233 ->method('getAllCollectionNames')
234 ->will($this->returnValue([]));
236 $this->storageComparer->createChangelist();
238 'field.storage.node.body',
239 'field.field.node.article.body',
242 $this->assertEquals($expected, $this->storageComparer->getChangelist('update'));
243 $this->assertEmpty($this->storageComparer->getChangelist('create'));
244 $this->assertEmpty($this->storageComparer->getChangelist('delete'));