You are here

public function UpdateScriptTest::testOrphanedSchemaEntries in Drupal 9

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

Tests that orphan schemas are handled properly.

File

core/modules/system/tests/src/Functional/UpdateSystem/UpdateScriptTest.php, line 408

Class

UpdateScriptTest
Tests the update script access and functionality.

Namespace

Drupal\Tests\system\Functional\UpdateSystem

Code

public function testOrphanedSchemaEntries() {
  $this
    ->drupalLogin($this->updateUser);

  // Insert a bogus value into the system.schema key/value storage for a
  // nonexistent module. This replicates what would happen if you had a module
  // installed and then completely remove it from the filesystem and clear it
  // out of the core.extension config list without uninstalling it cleanly.
  \Drupal::service('update.update_hook_registry')
    ->setInstalledVersion('my_already_removed_module', 8000);

  // Visit update.php and make sure we can click through to the 'No pending
  // updates' page without errors.
  $this
    ->drupalGet($this->updateUrl, [
    'external' => TRUE,
  ]);
  $this
    ->updateRequirementsProblem();
  $this
    ->clickLink('Continue');

  // Make sure there are no pending updates (or uncaught exceptions).
  $this
    ->assertSession()
    ->elementTextContains('xpath', '//div[@aria-label="Status message"]', 'No pending updates.');

  // Verify that we warn the admin about this situation.
  $this
    ->assertSession()
    ->elementTextEquals('xpath', '//div[@aria-label="Warning message"]', 'Warning message Module my_already_removed_module has an entry in the system.schema key/value storage, but is missing from your site. More information about this error.');

  // Try again with another orphaned entry, this time for a test module that
  // does exist in the filesystem.
  \Drupal::service('update.update_hook_registry')
    ->deleteInstalledVersion('my_already_removed_module');
  \Drupal::service('update.update_hook_registry')
    ->setInstalledVersion('update_test_0', 8000);
  $this
    ->drupalGet($this->updateUrl, [
    'external' => TRUE,
  ]);
  $this
    ->updateRequirementsProblem();
  $this
    ->clickLink('Continue');

  // There should not be any pending updates.
  $this
    ->assertSession()
    ->elementTextContains('xpath', '//div[@aria-label="Status message"]', 'No pending updates.');

  // But verify that we warn the admin about this situation.
  $this
    ->assertSession()
    ->elementTextEquals('xpath', '//div[@aria-label="Warning message"]', 'Warning message Module update_test_0 has an entry in the system.schema key/value storage, but is not installed. More information about this error.');

  // Finally, try with both kinds of orphans and make sure we get both warnings.
  \Drupal::service('update.update_hook_registry')
    ->setInstalledVersion('my_already_removed_module', 8000);
  $this
    ->drupalGet($this->updateUrl, [
    'external' => TRUE,
  ]);
  $this
    ->updateRequirementsProblem();
  $this
    ->clickLink('Continue');

  // There still should not be any pending updates.
  $this
    ->assertSession()
    ->elementTextContains('xpath', '//div[@aria-label="Status message"]', 'No pending updates.');

  // Verify that we warn the admin about both orphaned entries.
  $this
    ->assertSession()
    ->elementTextContains('xpath', '//div[@aria-label="Warning message"]', 'Module update_test_0 has an entry in the system.schema key/value storage, but is not installed. More information about this error.');
  $this
    ->assertSession()
    ->elementTextNotContains('xpath', '//div[@aria-label="Warning message"]', 'Module update_test_0 has an entry in the system.schema key/value storage, but is missing from your site.');
  $this
    ->assertSession()
    ->elementTextContains('xpath', '//div[@aria-label="Warning message"]', 'Module my_already_removed_module has an entry in the system.schema key/value storage, but is missing from your site. More information about this error.');
  $this
    ->assertSession()
    ->elementTextNotContains('xpath', '//div[@aria-label="Warning message"]', 'Module my_already_removed_module has an entry in the system.schema key/value storage, but is not installed.');
}