You are here

protected function UpdatePostUpdateTest::setUp in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/system/tests/src/Functional/UpdateSystem/UpdatePostUpdateTest.php \Drupal\Tests\system\Functional\UpdateSystem\UpdatePostUpdateTest::setUp()
  2. 9 core/modules/system/tests/src/Functional/UpdateSystem/UpdatePostUpdateTest.php \Drupal\Tests\system\Functional\UpdateSystem\UpdatePostUpdateTest::setUp()

Overrides BrowserTestBase::setUp

File

core/modules/system/tests/src/Functional/UpdateSystem/UpdatePostUpdateTest.php, line 26

Class

UpdatePostUpdateTest
Tests hook_post_update().

Namespace

Drupal\Tests\system\Functional\UpdateSystem

Code

protected function setUp() : void {
  parent::setUp();
  $connection = Database::getConnection();

  // Set the schema version.
  \Drupal::service('update.update_hook_registry')
    ->setInstalledVersion('update_test_postupdate', 8000);

  // Update core.extension.
  $extensions = $connection
    ->select('config')
    ->fields('config', [
    'data',
  ])
    ->condition('collection', '')
    ->condition('name', 'core.extension')
    ->execute()
    ->fetchField();
  $extensions = unserialize($extensions);
  $extensions['module']['update_test_postupdate'] = 8000;
  $connection
    ->update('config')
    ->fields([
    'data' => serialize($extensions),
  ])
    ->condition('collection', '')
    ->condition('name', 'core.extension')
    ->execute();

  // Mimic the behavior of ModuleInstaller::install() for removed post
  // updates. Don't include the actual post updates because we want them to
  // run.
  $key_value = \Drupal::service('keyvalue');
  $existing_updates = $key_value
    ->get('post_update')
    ->get('existing_updates', []);
  $post_updates = [
    'update_test_postupdate_post_update_foo',
    'update_test_postupdate_post_update_bar',
    'update_test_postupdate_post_update_pub',
    'update_test_postupdate_post_update_baz',
  ];
  $key_value
    ->get('post_update')
    ->set('existing_updates', array_merge($existing_updates, $post_updates));
}