You are here

public function UpdateScriptTest::testRequirements 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::testRequirements()

Tests that requirements warnings and errors are correctly displayed.

File

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

Class

UpdateScriptTest
Tests the update script access and functionality.

Namespace

Drupal\Tests\system\Functional\UpdateSystem

Code

public function testRequirements() {
  $update_script_test_config = $this
    ->config('update_script_test.settings');
  $this
    ->drupalLogin($this->updateUser);

  // If there are no requirements warnings or errors, we expect to be able to
  // go through the update process uninterrupted.
  $this
    ->drupalGet($this->updateUrl, [
    'external' => TRUE,
  ]);
  $this
    ->updateRequirementsProblem();
  $this
    ->clickLink('Continue');
  $this
    ->assertSession()
    ->pageTextContains('No pending updates.');

  // Confirm that all caches were cleared.
  $this
    ->assertSession()
    ->pageTextContains('hook_cache_flush() invoked for update_script_test.module.');

  // 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.');

  // Now try again without pending updates to make sure that works too.
  $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
    ->assertSession()
    ->pageTextContains('No pending updates.');

  // Confirm that all caches were cleared.
  $this
    ->assertSession()
    ->pageTextContains('hook_cache_flush() invoked for update_script_test.module.');

  // If there is a requirements error, it should be displayed even after
  // clicking the link to proceed (since the problem that triggered the error
  // has not been fixed).
  $update_script_test_config
    ->set('requirement_type', REQUIREMENT_ERROR)
    ->save();
  $this
    ->drupalGet($this->updateUrl, [
    'external' => TRUE,
  ]);
  $this
    ->assertSession()
    ->pageTextContains('This is a requirements error provided by the update_script_test module.');
  $this
    ->clickLink('try again');
  $this
    ->assertSession()
    ->pageTextContains('This is a requirements error provided by the update_script_test module.');

  // Ensure that changes to a module's requirements that would cause errors
  // are displayed correctly.
  $update_script_test_config
    ->set('requirement_type', REQUIREMENT_OK)
    ->save();
  \Drupal::state()
    ->set('update_script_test.system_info_alter', [
    'dependencies' => [
      'a_module_that_does_not_exist',
    ],
  ]);
  $this
    ->drupalGet($this->updateUrl, [
    'external' => TRUE,
  ]);
  $this
    ->assertSession()
    ->responseContains('a_module_that_does_not_exist (Missing)');
  $this
    ->assertSession()
    ->responseContains('Update script test requires this module.');
  \Drupal::state()
    ->set('update_script_test.system_info_alter', [
    'dependencies' => [
      'node (<7.x-0.0-dev)',
    ],
  ]);
  $this
    ->drupalGet($this->updateUrl, [
    'external' => TRUE,
  ]);
  $this
    ->assertSession()
    ->assertEscaped('Node (Version <7.x-0.0-dev required)');
  $this
    ->assertSession()
    ->responseContains('Update script test requires this module and version. Currently using Node version ' . \Drupal::VERSION);

  // Test that issues with modules that themes depend on are properly
  // displayed.
  $this
    ->assertSession()
    ->responseNotContains('Test Module Required by Theme');
  $this
    ->drupalGet('admin/appearance');
  $this
    ->getSession()
    ->getPage()
    ->clickLink('Install Test Theme Depending on Modules theme');
  $this
    ->assertSession()
    ->addressEquals('admin/appearance');
  $this
    ->assertSession()
    ->pageTextContains('The Test Theme Depending on Modules theme has been installed');

  // Ensure that when a theme depends on a module and that module's
  // requirements change, errors are displayed in the same manner as modules
  // depending on other modules.
  \Drupal::state()
    ->set('test_theme_depending_on_modules.system_info_alter', [
    'dependencies' => [
      'test_module_required_by_theme (<7.x-0.0-dev)',
    ],
  ]);
  $this
    ->drupalGet($this->updateUrl, [
    'external' => TRUE,
  ]);
  $this
    ->assertSession()
    ->assertEscaped('Test Module Required by Theme (Version <7.x-0.0-dev required)');
  $this
    ->assertSession()
    ->responseContains('Test Theme Depending on Modules requires this module and version. Currently using Test Module Required by Theme version ' . \Drupal::VERSION);

  // Ensure that when a theme is updated to depend on an unavailable module,
  // errors are displayed in the same manner as modules depending on other
  // modules.
  \Drupal::state()
    ->set('test_theme_depending_on_modules.system_info_alter', [
    'dependencies' => [
      'a_module_theme_needs_that_does_not_exist',
    ],
  ]);
  $this
    ->drupalGet($this->updateUrl, [
    'external' => TRUE,
  ]);
  $this
    ->assertSession()
    ->responseContains('a_module_theme_needs_that_does_not_exist (Missing)');
  $this
    ->assertSession()
    ->responseContains('Test Theme Depending on Modules requires this module.');
}