123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379 |
- <?php
- namespace Drupal\Tests\Core\Update;
- use Drupal\Core\KeyValueStore\KeyValueStoreInterface;
- use Drupal\Core\Site\Settings;
- use Drupal\Core\Update\RemovedPostUpdateNameException;
- use Drupal\Core\Update\UpdateRegistry;
- use Drupal\Tests\UnitTestCase;
- use org\bovigo\vfs\vfsStream;
- /**
- * @coversDefaultClass \Drupal\Core\Update\UpdateRegistry
- * @group Update
- *
- * Note we load code, so isolate the tests.
- *
- * @runTestsInSeparateProcesses
- * @preserveGlobalState disabled
- */
- class UpdateRegistryTest extends UnitTestCase {
- /**
- * {@inheritdoc}
- */
- protected function setUp() {
- parent::setUp();
- $settings = [];
- $settings['extension_discovery_scan_tests'] = TRUE;
- new Settings($settings);
- }
- /**
- * Sets up some modules with some update functions.
- */
- protected function setupBasicModules() {
- $info_a = <<<'EOS'
- type: module
- name: Module A
- core_version_requirement: '*'
- EOS;
- $info_b = <<<'EOS'
- type: module
- name: Module B
- core_version_requirement: '*'
- EOS;
- $info_c = <<<'EOS'
- type: module
- name: Module C
- core_version_requirement: '*'
- EOS;
- $module_a = <<<'EOS'
- <?php
- /**
- * Module A update B.
- */
- function module_a_post_update_b() {
- }
- /**
- * Module A update A.
- */
- function module_a_post_update_a() {
- }
- EOS;
- $module_b = <<<'EOS'
- <?php
- /**
- * Module B update A.
- */
- function module_b_post_update_a() {
- }
- /**
- * Implements hook_removed_post_updates().
- */
- function module_b_removed_post_updates() {
- return [
- 'module_b_post_update_b' => '8.9.0',
- 'module_b_post_update_c' => '8.9.0',
- ];
- }
- EOS;
- $module_c = <<<'EOS'
- <?php
- /**
- * Module C update A.
- */
- function module_c_post_update_a() {
- }
- /**
- * Module C update B.
- */
- function module_c_post_update_b() {
- }
- /**
- * Implements hook_removed_post_updates().
- */
- function module_c_removed_post_updates() {
- return [
- 'module_c_post_update_b' => '8.9.0',
- 'module_c_post_update_c' => '8.9.0',
- ];
- }
- EOS;
- vfsStream::setup('drupal');
- vfsStream::create([
- 'sites' => [
- 'default' => [
- 'modules' => [
- 'module_a' => [
- 'module_a.post_update.php' => $module_a,
- 'module_a.info.yml' => $info_a,
- ],
- 'module_b' => [
- 'module_b.post_update.php' => $module_b,
- 'module_b.info.yml' => $info_b,
- ],
- 'module_c' => [
- 'module_c.post_update.php' => $module_c,
- 'module_c.info.yml' => $info_c,
- ],
- ],
- ],
- ],
- ]);
- }
- /**
- * @covers ::getPendingUpdateFunctions
- */
- public function testGetPendingUpdateFunctionsNoExistingUpdates() {
- $this->setupBasicModules();
- $key_value = $this->prophesize(KeyValueStoreInterface::class);
- $key_value->get('existing_updates', [])->willReturn([]);
- $key_value = $key_value->reveal();
- $update_registry = new UpdateRegistry('vfs://drupal', 'sites/default', [
- 'module_a',
- 'module_b',
- ], $key_value, FALSE);
- $this->assertEquals([
- 'module_a_post_update_a',
- 'module_a_post_update_b',
- 'module_b_post_update_a',
- ], $update_registry->getPendingUpdateFunctions());
- }
- /**
- * @covers ::getPendingUpdateFunctions
- */
- public function testGetPendingUpdateFunctionsWithLoadedModulesButNotEnabled() {
- $this->setupBasicModules();
- $key_value = $this->prophesize(KeyValueStoreInterface::class);
- $key_value->get('existing_updates', [])->willReturn([]);
- $key_value = $key_value->reveal();
- // Preload modules to ensure that ::getAvailableUpdateFunctions filters out
- // not enabled modules.
- include_once 'vfs://drupal/sites/default/modules/module_a/module_a.post_update.php';
- include_once 'vfs://drupal/sites/default/modules/module_b/module_b.post_update.php';
- $update_registry = new UpdateRegistry('vfs://drupal', 'sites/default', [
- 'module_a',
- ], $key_value, FALSE);
- $this->assertEquals([
- 'module_a_post_update_a',
- 'module_a_post_update_b',
- ], $update_registry->getPendingUpdateFunctions());
- }
- /**
- * @covers ::getPendingUpdateFunctions
- */
- public function testGetPendingUpdateFunctionsExistingUpdates() {
- $this->setupBasicModules();
- $key_value = $this->prophesize(KeyValueStoreInterface::class);
- $key_value->get('existing_updates', [])->willReturn(['module_a_post_update_a']);
- $key_value = $key_value->reveal();
- $update_registry = new UpdateRegistry('vfs://drupal', 'sites/default', [
- 'module_a',
- 'module_b',
- ], $key_value, FALSE);
- $this->assertEquals(array_values([
- 'module_a_post_update_b',
- 'module_b_post_update_a',
- ]), array_values($update_registry->getPendingUpdateFunctions()));
- }
- /**
- * @covers ::getPendingUpdateInformation
- */
- public function testGetPendingUpdateInformation() {
- $this->setupBasicModules();
- $key_value = $this->prophesize(KeyValueStoreInterface::class);
- $key_value->get('existing_updates', [])->willReturn([]);
- $key_value = $key_value->reveal();
- $update_registry = new UpdateRegistry('vfs://drupal', 'sites/default', [
- 'module_a',
- 'module_b',
- ], $key_value, FALSE);
- $expected = [];
- $expected['module_a']['pending']['a'] = 'Module A update A.';
- $expected['module_a']['pending']['b'] = 'Module A update B.';
- $expected['module_a']['start'] = 'a';
- $expected['module_b']['pending']['a'] = 'Module B update A.';
- $expected['module_b']['start'] = 'a';
- $this->assertEquals($expected, $update_registry->getPendingUpdateInformation());
- }
- /**
- * @covers ::getPendingUpdateInformation
- */
- public function testGetPendingUpdateInformationWithExistingUpdates() {
- $this->setupBasicModules();
- $key_value = $this->prophesize(KeyValueStoreInterface::class);
- $key_value->get('existing_updates', [])->willReturn(['module_a_post_update_a']);
- $key_value = $key_value->reveal();
- $update_registry = new UpdateRegistry('vfs://drupal', 'sites/default', [
- 'module_a',
- 'module_b',
- ], $key_value, FALSE);
- $expected = [];
- $expected['module_a']['pending']['b'] = 'Module A update B.';
- $expected['module_a']['start'] = 'b';
- $expected['module_b']['pending']['a'] = 'Module B update A.';
- $expected['module_b']['start'] = 'a';
- $this->assertEquals($expected, $update_registry->getPendingUpdateInformation());
- }
- /**
- * @covers ::getPendingUpdateInformation
- */
- public function testGetPendingUpdateInformationWithRemovedUpdates() {
- $this->setupBasicModules();
- $key_value = $this->prophesize(KeyValueStoreInterface::class);
- $key_value->get('existing_updates', [])->willReturn(['module_a_post_update_a']);
- $key_value = $key_value->reveal();
- $update_registry = new UpdateRegistry('vfs://drupal', 'sites/default', [
- 'module_c',
- ], $key_value, FALSE);
- $this->expectException(RemovedPostUpdateNameException::class);
- $update_registry->getPendingUpdateInformation();
- }
- /**
- * @covers ::getModuleUpdateFunctions
- */
- public function testGetModuleUpdateFunctions() {
- $this->setupBasicModules();
- $key_value = $this->prophesize(KeyValueStoreInterface::class)->reveal();
- $update_registry = new UpdateRegistry('vfs://drupal', 'sites/default', [
- 'module_a',
- 'module_b',
- ], $key_value, FALSE);
- $this->assertEquals(['module_a_post_update_a', 'module_a_post_update_b'], array_values($update_registry->getModuleUpdateFunctions('module_a')));
- $this->assertEquals(['module_b_post_update_a'], array_values($update_registry->getModuleUpdateFunctions('module_b')));
- }
- /**
- * @covers ::registerInvokedUpdates
- */
- public function testRegisterInvokedUpdatesWithoutExistingUpdates() {
- $this->setupBasicModules();
- $key_value = $this->prophesize(KeyValueStoreInterface::class);
- $key_value->get('existing_updates', [])
- ->willReturn([])
- ->shouldBeCalledTimes(1);
- $key_value->set('existing_updates', ['module_a_post_update_a'])
- ->willReturn(NULL)
- ->shouldBeCalledTimes(1);
- $key_value = $key_value->reveal();
- $update_registry = new UpdateRegistry('vfs://drupal', 'sites/default', [
- 'module_a',
- 'module_b',
- ], $key_value, FALSE);
- $update_registry->registerInvokedUpdates(['module_a_post_update_a']);
- }
- /**
- * @covers ::registerInvokedUpdates
- */
- public function testRegisterInvokedUpdatesWithMultiple() {
- $this->setupBasicModules();
- $key_value = $this->prophesize(KeyValueStoreInterface::class);
- $key_value->get('existing_updates', [])
- ->willReturn([])
- ->shouldBeCalledTimes(1);
- $key_value->set('existing_updates', ['module_a_post_update_a', 'module_a_post_update_b'])
- ->willReturn(NULL)
- ->shouldBeCalledTimes(1);
- $key_value = $key_value->reveal();
- $update_registry = new UpdateRegistry('vfs://drupal', 'sites/default', [
- 'module_a',
- 'module_b',
- ], $key_value, FALSE);
- $update_registry->registerInvokedUpdates(['module_a_post_update_a', 'module_a_post_update_b']);
- }
- /**
- * @covers ::registerInvokedUpdates
- */
- public function testRegisterInvokedUpdatesWithExistingUpdates() {
- $this->setupBasicModules();
- $key_value = $this->prophesize(KeyValueStoreInterface::class);
- $key_value->get('existing_updates', [])
- ->willReturn(['module_a_post_update_b'])
- ->shouldBeCalledTimes(1);
- $key_value->set('existing_updates', ['module_a_post_update_b', 'module_a_post_update_a'])
- ->willReturn(NULL)
- ->shouldBeCalledTimes(1);
- $key_value = $key_value->reveal();
- $update_registry = new UpdateRegistry('vfs://drupal', 'sites/default', [
- 'module_a',
- 'module_b',
- ], $key_value, FALSE);
- $update_registry->registerInvokedUpdates(['module_a_post_update_a']);
- }
- /**
- * @covers ::filterOutInvokedUpdatesByModule
- */
- public function testFilterOutInvokedUpdatesByModule() {
- $this->setupBasicModules();
- $key_value = $this->prophesize(KeyValueStoreInterface::class);
- $key_value->get('existing_updates', [])
- ->willReturn(['module_a_post_update_b', 'module_a_post_update_a', 'module_b_post_update_a'])
- ->shouldBeCalledTimes(1);
- $key_value->set('existing_updates', ['module_b_post_update_a'])
- ->willReturn(NULL)
- ->shouldBeCalledTimes(1);
- $key_value = $key_value->reveal();
- $update_registry = new UpdateRegistry('vfs://drupal', 'sites/default', [
- 'module_a',
- 'module_b',
- ], $key_value, FALSE);
- $update_registry->filterOutInvokedUpdatesByModule('module_a');
- }
- }
|