setInstalledVersion example

foreach ($updates as $function => $update) {
      $dependency_map[$function] = !empty($update['reverse_paths']) ? array_keys($update['reverse_paths']) : [];
    }

    // Determine updates to be performed.     foreach ($updates as $function => $update) {
      if ($update['allowed']) {
        // Set the installed version of each module so updates will start at the         // correct place. (The updates are already sorted, so we can simply base         // this on the first one we come across in the above foreach loop.)         if (isset($start[$update['module']])) {
          \Drupal::service('update.update_hook_registry')->setInstalledVersion($update['module']$update['number'] - 1);
          unset($start[$update['module']]);
        }
        $batch_builder->addOperation('update_do_one', [$update['module']$update['number']$dependency_map[$function]]);
      }
    }

    $post_updates = $this->postUpdateRegistry->getPendingUpdateFunctions();

    if ($post_updates) {
      // Now we rebuild all caches and after that execute the hook_post_update()       // functions.

  protected static $modules = ['update_test_description'];

  /** * Tests the list of pending database updates. * * @see update_get_update_list() */
  public function testUpdateGetUpdateList() {
    require_once $this->root . '/core/includes/update.inc';
    \Drupal::service('update.update_hook_registry')->setInstalledVersion('update_test_description', 8000);
    \Drupal::moduleHandler()->loadInclude('update_test_description', 'install');

    $updates = update_get_update_list();
    $expected = [
      'pending' => [
        8001 => '8001 - Update test of slash in description and/or.',
        8002 => '8002 - Update test with multiline description, the quick brown fox jumped over the lazy dog.',
      ],
      'start' => 8001,
    ];
    $this->assertEquals($expected$updates['update_test_description']);
  }

  protected $defaultTheme = 'stark';

  /** * {@inheritdoc} */
  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')
      
->setSyncing(TRUE)
            ->setSourceStorage($source_storage);
        }
        \Drupal::service('config.installer')->installDefaultConfig('module', $module);

        // If the module has no current updates, but has some that were         // previously removed, set the version to the value of         // hook_update_last_removed().         if ($last_removed = $this->moduleHandler->invoke($module, 'update_last_removed')) {
          $version = max($version$last_removed);
        }
        $this->updateRegistry->setInstalledVersion($module$version);

        // Record the fact that it was installed.         $modules_installed[] = $module;

        // Drupal's stream wrappers needs to be re-registered in case a         // module-provided stream wrapper is used later in the same request. In         // particular, this happens when installing Drupal via Drush, as the         // 'translations' stream wrapper is provided by Interface Translation         // module and is later used to import translations.         \Drupal::service('stream_wrapper_manager')->register();

        
'administer software updates',
    ]);
  }

  /** * Tests that a module with a too old schema version can not be updated. */
  public function testLastRemovedVersion() {
    /** @var \Drupal\Core\Update\UpdateHookRegistry $update_registry */
    $update_registry = \Drupal::service('update.update_hook_registry');

    $update_registry->setInstalledVersion('update_test_last_removed', 8000);
    $update_registry->setInstalledVersion('user', 9300);

    // Access the update page with a schema version that is too old for system     // and the test module, only the generic core message should be shown.     $this->drupalLogin($this->updateUser);
    $this->drupalGet($this->updateUrl);
    $assert_session = $this->assertSession();
    $assert_session->pageTextContains('Requirements problem');
    $assert_session->pageTextContains('The version of Drupal you are trying to update from is too old');
    $assert_session->pageTextContains('Updating to Drupal 10 is only supported from Drupal version 9.4.0 or higher. If you are trying to update from an older version, first update to the latest version of Drupal 9');
    $assert_session->pageTextNotContains('Unsupported schema version: Update test with hook_update_last_removed() implementation');

    

  protected $defaultTheme = 'stark';

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();
    $connection = Database::getConnection();

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

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

  protected User $updateUser;

  /** * {@inheritdoc} */
  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')
      

  protected $defaultTheme = 'stark';

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();
    $connection = Database::getConnection();

    // Set the schema version.     \Drupal::service('update.update_hook_registry')->setInstalledVersion('update_test_failing', 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_failing'] = 8000;
    $connection->update('config')
      
$versions[$key] = UpdateHookRegistry::SCHEMA_UNINSTALLED;
      });
    $this->keyValueStore
      ->method('set')
      ->willReturnCallback(static function D$key$value) use (&$versions) {
        $versions[$key] = $value;
      });

    $update_registry = new UpdateHookRegistry([]$this->keyValueStore);

    $this->assertSame(3000, $update_registry->getInstalledVersion('module3'));
    $update_registry->setInstalledVersion('module3', 3001);
    $this->assertSame(3001, $update_registry->getInstalledVersion('module3'));
    $this->assertSame($versions$update_registry->getAllInstalledVersions());
    $update_registry->deleteInstalledVersion('module3');
    $this->assertSame(UpdateHookRegistry::SCHEMA_UNINSTALLED, $update_registry->getInstalledVersion('module3'));
  }

}
// If a module is fully installed no pending updates exists.     $this->assertSession()->pageTextNotContains('Out of date');

    // The setting config_sync_directory is not properly formed.     $this->assertSession()->pageTextContains("Your {$this->siteDirectory}/settings.php file must define the \$settings['config_sync_directory'] setting");

    /** @var \Drupal\Core\Update\UpdateHookRegistry $update_registry */
    $update_registry = \Drupal::service('update.update_hook_registry');

    // Set the schema version of update_test_postupdate to a lower version, so     // update_test_postupdate_update_8001() needs to be executed.     $update_registry->setInstalledVersion('update_test_postupdate', 8000);
    $this->drupalGet('admin/reports/status');
    $this->assertSession()->pageTextContains('Out of date');

    // Now cleanup the executed post update functions.     $update_registry->setInstalledVersion('update_test_postupdate', 8001);
    /** @var \Drupal\Core\Update\UpdateRegistry $post_update_registry */
    $post_update_registry = \Drupal::service('update.post_update_registry');
    $post_update_registry->filterOutInvokedUpdatesByExtension('update_test_postupdate');
    $this->drupalGet('admin/reports/status');
    $this->assertSession()->pageTextContains('Out of date');

    
public function testWith7x() {
    /** @var \Drupal\Core\Update\UpdateHookRegistry $update_registry */
    $update_registry = \Drupal::service('update.update_hook_registry');

    // Ensure that the minimum schema version is 8000, despite 7200 update     // hooks and a 7XXX hook_update_last_removed().     $this->assertEquals(8000, $update_registry->getInstalledVersion('update_test_with_7x'));

    // Try to manually set the schema version to 7110 and ensure that no     // updates are allowed.     $update_registry->setInstalledVersion('update_test_with_7x', 7110);

    // Click through update.php with 'administer software updates' permission.     $this->drupalLogin($this->updateUser);
    $this->drupalGet($this->updateUrl, ['external' => TRUE]);
    $this->updateRequirementsProblem();
    $this->clickLink('Continue');
    $this->assertSession()->pageTextContains('Some of the pending updates cannot be applied because their dependencies were not met.');
  }

}
// If there is a requirements warning, we expect it to be initially     // displayed, but clicking the link to proceed should allow us to go     // through the rest of the update process uninterrupted.
    // First, run this test with pending updates to make sure they can be run     // successfully.     $this->drupalLogin($this->updateUser);
    $update_script_test_config->set('requirement_type', REQUIREMENT_WARNING)->save();
    /** @var \Drupal\Core\Update\UpdateHookRegistry $update_registry */
    $update_registry = \Drupal::service('update.update_hook_registry');
    $update_registry->setInstalledVersion('update_script_test', $update_registry->getInstalledVersion('update_script_test') - 1);
    $this->drupalGet($this->updateUrl, ['external' => TRUE]);
    $this->assertSession()->pageTextContains('This is a requirements warning provided by the update_script_test module.');
    $this->clickLink('try again');
    $this->assertSession()->pageTextNotContains('This is a requirements warning provided by the update_script_test module.');
    $this->clickLink('Continue');
    $this->clickLink('Apply pending updates');
    $this->checkForMetaRefresh();
    $this->assertSession()->pageTextContains('The update_script_test_update_8001() update was executed successfully.');
    // Confirm that all caches were cleared.     $this->assertSession()->pageTextContains('hook_cache_flush() invoked for update_script_test.module.');

    
// No-op. Tests wishing to do test the selection page or the general     // update.php environment before running update.php can override this method     // and implement their required tests.   }

  /** * Installs the update_script_test module and makes an update available. */
  protected function ensureUpdatesToRun() {
    \Drupal::service('module_installer')->install(['update_script_test']);
    // Reset the schema so there is an update to run.     \Drupal::service('update.update_hook_registry')->setInstalledVersion('update_script_test', 8000);
  }

}

  public function testUpdateNewDependency() {
    // The new_dependency_test before the update is just an empty info.yml file.     // The code of the new_dependency_test module is after the update and     // contains the dependency on the new_dependency_test_with_service module.     $extension_config = $this->container->get('config.factory')->getEditable('core.extension');
    $extension_config
      ->set('module.new_dependency_test', 0)
      ->set('module', module_config_sort($extension_config->get('module')))
      ->save(TRUE);
    \Drupal::service('update.update_hook_registry')->setInstalledVersion('new_dependency_test', \Drupal::CORE_MINIMUM_SCHEMA_VERSION);

    // Rebuild the container and test that the service with the optional unmet     // dependency is still available while the ones that fail are not.     try {
      $this->rebuildContainer();
      $this->fail('The container has services with unmet dependencies and should have failed to rebuild.');
    }
    catch (ServiceNotFoundException $exception) {
      $this->assertStringContainsString('The service "new_dependency_test.dependent" has a dependency on a non-existent service "new_dependency_test_with_service.service".', $exception->getMessage());
    }

    
Home | Imprint | This part of the site doesn't use cookies.