Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Config / StorageComparerTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Config;
4
5 use Drupal\Component\Uuid\Php;
6 use Drupal\Core\Config\StorageComparer;
7 use Drupal\Tests\UnitTestCase;
8
9 /**
10  * @coversDefaultClass \Drupal\Core\Config\StorageComparer
11  * @group Config
12  */
13 class StorageComparerTest extends UnitTestCase {
14
15   /**
16    * @var \Drupal\Core\Config\StorageInterface|\PHPUnit_Framework_MockObject_MockObject
17    */
18   protected $sourceStorage;
19
20   /**
21    * @var \Drupal\Core\Config\StorageInterface|\PHPUnit_Framework_MockObject_MockObject
22    */
23   protected $targetStorage;
24
25   /**
26    * @var \Drupal\Core\Config\ConfigManager|\PHPUnit_Framework_MockObject_MockObject
27    */
28   protected $configManager;
29
30   /**
31    * The storage comparer to test.
32    *
33    * @var \Drupal\Core\Config\StorageComparer
34    */
35   protected $storageComparer;
36
37   /**
38    * An array of test configuration data keyed by configuration name.
39    *
40    * @var array
41    */
42   protected $configData;
43
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);
49   }
50
51   protected function getConfigData() {
52     $uuid = new Php();
53     // Mock data using minimal data to use ConfigDependencyManger.
54     $this->configData = [
55       // Simple config that controls configuration sync.
56       'system.site' => [
57         'title' => 'Drupal',
58         'uuid' => $uuid->generate(),
59       ],
60       // Config entity which requires another config entity.
61       'field.field.node.article.body' => [
62         'id' => 'node.article.body',
63         'uuid' => $uuid->generate(),
64         'dependencies' => [
65           'config' => [
66             'field.storage.node.body',
67           ],
68         ],
69       ],
70       // Config entity which is required by another config entity.
71       'field.storage.node.body' => [
72         'id' => 'node.body',
73         'uuid' => $uuid->generate(),
74         'dependencies' => [
75           'module' => [
76             'text',
77           ],
78         ],
79       ],
80       // Config entity not which has no dependencies on configuration.
81       'views.view.test_view' => [
82         'id' => 'test_view',
83         'uuid' => $uuid->generate(),
84         'dependencies' => [
85           'module' => [
86             'node',
87           ],
88         ],
89       ],
90       // Simple config.
91       'system.performance' => [
92         'stale_file_threshold' => 2592000,
93       ],
94
95     ];
96     return $this->configData;
97   }
98
99   /**
100    * @covers ::createChangelist
101    */
102   public function testCreateChangelistNoChange() {
103     $config_data = $this->getConfigData();
104     $config_files = array_keys($config_data);
105     $this->sourceStorage->expects($this->once())
106       ->method('listAll')
107       ->will($this->returnValue($config_files));
108     $this->targetStorage->expects($this->once())
109       ->method('listAll')
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([]));
123
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'));
128   }
129
130   /**
131    * @covers ::createChangelist
132    */
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']);
138
139     $this->sourceStorage->expects($this->once())
140       ->method('listAll')
141       ->will($this->returnValue(array_keys($source_data)));
142     $this->targetStorage->expects($this->once())
143       ->method('listAll')
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([]));
157
158     $this->storageComparer->createChangelist();
159     $expected = [
160       'field.storage.node.body',
161       'field.field.node.article.body',
162       'views.view.test_view',
163     ];
164     $this->assertEquals($expected, $this->storageComparer->getChangelist('create'));
165     $this->assertEmpty($this->storageComparer->getChangelist('delete'));
166     $this->assertEmpty($this->storageComparer->getChangelist('update'));
167   }
168
169   /**
170    * @covers ::createChangelist
171    */
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']);
177
178     $this->sourceStorage->expects($this->once())
179       ->method('listAll')
180       ->will($this->returnValue(array_keys($source_data)));
181     $this->targetStorage->expects($this->once())
182       ->method('listAll')
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([]));
196
197     $this->storageComparer->createChangelist();
198     $expected = [
199       'views.view.test_view',
200       'field.field.node.article.body',
201       'field.storage.node.body',
202     ];
203     $this->assertEquals($expected, $this->storageComparer->getChangelist('delete'));
204     $this->assertEmpty($this->storageComparer->getChangelist('create'));
205     $this->assertEmpty($this->storageComparer->getChangelist('update'));
206   }
207
208   /**
209    * @covers ::createChangelist
210    */
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';
216
217     $this->sourceStorage->expects($this->once())
218       ->method('listAll')
219       ->will($this->returnValue(array_keys($source_data)));
220     $this->targetStorage->expects($this->once())
221       ->method('listAll')
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([]));
235
236     $this->storageComparer->createChangelist();
237     $expected = [
238       'field.storage.node.body',
239       'field.field.node.article.body',
240       'system.site',
241     ];
242     $this->assertEquals($expected, $this->storageComparer->getChangelist('update'));
243     $this->assertEmpty($this->storageComparer->getChangelist('create'));
244     $this->assertEmpty($this->storageComparer->getChangelist('delete'));
245   }
246
247 }