Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Update / UpdateRegistryTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Update;
4
5 use Drupal\Core\KeyValueStore\KeyValueStoreInterface;
6 use Drupal\Core\Site\Settings;
7 use Drupal\Core\Update\UpdateRegistry;
8 use Drupal\Tests\UnitTestCase;
9 use org\bovigo\vfs\vfsStream;
10
11 /**
12  * @coversDefaultClass \Drupal\Core\Update\UpdateRegistry
13  * @group Update
14  *
15  * Note we load code, so isolate the tests.
16  *
17  * @runTestsInSeparateProcesses
18  * @preserveGlobalState disabled
19  */
20 class UpdateRegistryTest extends UnitTestCase {
21
22   /**
23    * {@inheritdoc}
24    */
25   protected function setUp() {
26     parent::setUp();
27
28     $settings = [];
29     $settings['extension_discovery_scan_tests'] = TRUE;
30     new Settings($settings);
31   }
32
33   /**
34    * Sets up some modules with some update functions.
35    */
36   protected function setupBasicModules() {
37     $info_a = <<<'EOS'
38 type: module
39 name: Module A
40 core: 8.x
41 EOS;
42
43     $info_b = <<<'EOS'
44 type: module
45 name: Module B
46 core: 8.x
47 EOS;
48
49     $module_a = <<<'EOS'
50 <?php
51
52 /**
53  * Module A update B.
54  */
55 function module_a_post_update_b() {
56 }
57
58 /**
59  * Module A update A.
60  */
61 function module_a_post_update_a() {
62 }
63
64 EOS;
65     $module_b = <<<'EOS'
66 <?php
67
68 /**
69  * Module B update A.
70  */
71 function module_b_post_update_a() {
72 }
73
74 EOS;
75     vfsStream::setup('drupal');
76     vfsStream::create([
77       'sites' => [
78         'default' => [
79           'modules' => [
80             'module_a' => [
81               'module_a.post_update.php' => $module_a,
82               'module_a.info.yml' => $info_a,
83             ],
84             'module_b' => [
85               'module_b.post_update.php' => $module_b,
86               'module_b.info.yml' => $info_b,
87             ],
88           ],
89         ],
90       ],
91     ]);
92   }
93
94   /**
95    * @covers ::getPendingUpdateFunctions
96    */
97   public function testGetPendingUpdateFunctionsNoExistingUpdates() {
98     $this->setupBasicModules();
99
100     $key_value = $this->prophesize(KeyValueStoreInterface::class);
101     $key_value->get('existing_updates', [])->willReturn([]);
102     $key_value = $key_value->reveal();
103
104     $update_registry = new UpdateRegistry('vfs://drupal', 'sites/default', [
105       'module_a',
106       'module_b',
107     ], $key_value, FALSE);
108
109     $this->assertEquals([
110       'module_a_post_update_a',
111       'module_a_post_update_b',
112       'module_b_post_update_a',
113     ], $update_registry->getPendingUpdateFunctions());
114   }
115
116   /**
117    * @covers ::getPendingUpdateFunctions
118    */
119   public function testGetPendingUpdateFunctionsWithLoadedModulesButNotEnabled() {
120     $this->setupBasicModules();
121
122     $key_value = $this->prophesize(KeyValueStoreInterface::class);
123     $key_value->get('existing_updates', [])->willReturn([]);
124     $key_value = $key_value->reveal();
125
126     // Preload modules to ensure that ::getAvailableUpdateFunctions filters out
127     // not enabled modules.
128     include_once 'vfs://drupal/sites/default/modules/module_a/module_a.post_update.php';
129     include_once 'vfs://drupal/sites/default/modules/module_b/module_b.post_update.php';
130
131     $update_registry = new UpdateRegistry('vfs://drupal', 'sites/default', [
132       'module_a',
133     ], $key_value, FALSE);
134
135     $this->assertEquals([
136       'module_a_post_update_a',
137       'module_a_post_update_b',
138     ], $update_registry->getPendingUpdateFunctions());
139   }
140
141   /**
142    * @covers ::getPendingUpdateFunctions
143    */
144   public function testGetPendingUpdateFunctionsExistingUpdates() {
145     $this->setupBasicModules();
146
147     $key_value = $this->prophesize(KeyValueStoreInterface::class);
148     $key_value->get('existing_updates', [])->willReturn(['module_a_post_update_a']);
149     $key_value = $key_value->reveal();
150
151     $update_registry = new UpdateRegistry('vfs://drupal', 'sites/default', [
152       'module_a',
153       'module_b',
154     ], $key_value, FALSE);
155
156     $this->assertEquals(array_values([
157       'module_a_post_update_b',
158       'module_b_post_update_a',
159     ]), array_values($update_registry->getPendingUpdateFunctions()));
160
161   }
162
163   /**
164    * @covers ::getPendingUpdateInformation
165    */
166   public function testGetPendingUpdateInformation() {
167     $this->setupBasicModules();
168
169     $key_value = $this->prophesize(KeyValueStoreInterface::class);
170     $key_value->get('existing_updates', [])->willReturn([]);
171     $key_value = $key_value->reveal();
172
173     $update_registry = new UpdateRegistry('vfs://drupal', 'sites/default', [
174       'module_a',
175       'module_b',
176     ], $key_value, FALSE);
177
178     $expected = [];
179     $expected['module_a']['pending']['a'] = 'Module A update A.';
180     $expected['module_a']['pending']['b'] = 'Module A update B.';
181     $expected['module_a']['start'] = 'a';
182     $expected['module_b']['pending']['a'] = 'Module B update A.';
183     $expected['module_b']['start'] = 'a';
184
185     $this->assertEquals($expected, $update_registry->getPendingUpdateInformation());
186   }
187
188   /**
189    * @covers ::getPendingUpdateInformation
190    */
191   public function testGetPendingUpdateInformationWithExistingUpdates() {
192     $this->setupBasicModules();
193
194     $key_value = $this->prophesize(KeyValueStoreInterface::class);
195     $key_value->get('existing_updates', [])->willReturn(['module_a_post_update_a']);
196     $key_value = $key_value->reveal();
197
198     $update_registry = new UpdateRegistry('vfs://drupal', 'sites/default', [
199       'module_a',
200       'module_b',
201     ], $key_value, FALSE);
202
203     $expected = [];
204     $expected['module_a']['pending']['b'] = 'Module A update B.';
205     $expected['module_a']['start'] = 'b';
206     $expected['module_b']['pending']['a'] = 'Module B update A.';
207     $expected['module_b']['start'] = 'a';
208
209     $this->assertEquals($expected, $update_registry->getPendingUpdateInformation());
210   }
211
212   /**
213    * @covers ::getModuleUpdateFunctions
214    */
215   public function testGetModuleUpdateFunctions() {
216     $this->setupBasicModules();
217     $key_value = $this->prophesize(KeyValueStoreInterface::class)->reveal();
218
219     $update_registry = new UpdateRegistry('vfs://drupal', 'sites/default', [
220       'module_a',
221       'module_b',
222     ], $key_value, FALSE);
223
224     $this->assertEquals(['module_a_post_update_a', 'module_a_post_update_b'], array_values($update_registry->getModuleUpdateFunctions('module_a')));
225     $this->assertEquals(['module_b_post_update_a'], array_values($update_registry->getModuleUpdateFunctions('module_b')));
226   }
227
228   /**
229    * @covers ::registerInvokedUpdates
230    */
231   public function testRegisterInvokedUpdatesWithoutExistingUpdates() {
232     $this->setupBasicModules();
233     $key_value = $this->prophesize(KeyValueStoreInterface::class);
234     $key_value->get('existing_updates', [])
235       ->willReturn([])
236       ->shouldBeCalledTimes(1);
237     $key_value->set('existing_updates', ['module_a_post_update_a'])
238       ->willReturn(NULL)
239       ->shouldBeCalledTimes(1);
240     $key_value = $key_value->reveal();
241
242     $update_registry = new UpdateRegistry('vfs://drupal', 'sites/default', [
243       'module_a',
244       'module_b',
245     ], $key_value, FALSE);
246     $update_registry->registerInvokedUpdates(['module_a_post_update_a']);
247   }
248
249   /**
250    * @covers ::registerInvokedUpdates
251    */
252   public function testRegisterInvokedUpdatesWithMultiple() {
253     $this->setupBasicModules();
254     $key_value = $this->prophesize(KeyValueStoreInterface::class);
255     $key_value->get('existing_updates', [])
256       ->willReturn([])
257       ->shouldBeCalledTimes(1);
258     $key_value->set('existing_updates', ['module_a_post_update_a', 'module_a_post_update_b'])
259       ->willReturn(NULL)
260       ->shouldBeCalledTimes(1);
261     $key_value = $key_value->reveal();
262
263     $update_registry = new UpdateRegistry('vfs://drupal', 'sites/default', [
264       'module_a',
265       'module_b',
266     ], $key_value, FALSE);
267     $update_registry->registerInvokedUpdates(['module_a_post_update_a', 'module_a_post_update_b']);
268   }
269
270   /**
271    * @covers ::registerInvokedUpdates
272    */
273   public function testRegisterInvokedUpdatesWithExistingUpdates() {
274     $this->setupBasicModules();
275     $key_value = $this->prophesize(KeyValueStoreInterface::class);
276     $key_value->get('existing_updates', [])
277       ->willReturn(['module_a_post_update_b'])
278       ->shouldBeCalledTimes(1);
279     $key_value->set('existing_updates', ['module_a_post_update_b', 'module_a_post_update_a'])
280       ->willReturn(NULL)
281       ->shouldBeCalledTimes(1);
282     $key_value = $key_value->reveal();
283
284     $update_registry = new UpdateRegistry('vfs://drupal', 'sites/default', [
285       'module_a',
286       'module_b',
287     ], $key_value, FALSE);
288     $update_registry->registerInvokedUpdates(['module_a_post_update_a']);
289   }
290
291   /**
292    * @covers ::filterOutInvokedUpdatesByModule
293    */
294   public function testFilterOutInvokedUpdatesByModule() {
295     $this->setupBasicModules();
296     $key_value = $this->prophesize(KeyValueStoreInterface::class);
297     $key_value->get('existing_updates', [])
298       ->willReturn(['module_a_post_update_b', 'module_a_post_update_a', 'module_b_post_update_a'])
299       ->shouldBeCalledTimes(1);
300     $key_value->set('existing_updates', ['module_b_post_update_a'])
301       ->willReturn(NULL)
302       ->shouldBeCalledTimes(1);
303     $key_value = $key_value->reveal();
304
305     $update_registry = new UpdateRegistry('vfs://drupal', 'sites/default', [
306       'module_a',
307       'module_b',
308     ], $key_value, FALSE);
309
310     $update_registry->filterOutInvokedUpdatesByModule('module_a');
311   }
312
313 }