function EquivalentUpdateTest::testUpdateHooks

Same name in other branches
  1. 11.x core/modules/system/tests/src/Functional/UpdateSystem/EquivalentUpdateTest.php \Drupal\Tests\system\Functional\UpdateSystem\EquivalentUpdateTest::testUpdateHooks()

Tests that update hooks are properly run.

File

core/modules/system/tests/src/Functional/UpdateSystem/EquivalentUpdateTest.php, line 59

Class

EquivalentUpdateTest
Tests that update hooks are properly run.

Namespace

Drupal\Tests\system\Functional\UpdateSystem

Code

public function testUpdateHooks() {
    
    /** @var \Drupal\Core\Update\UpdateHookRegistry $update_registry */
    $update_registry = \Drupal::service('update.update_hook_registry');
    $this->drupalLogin($this->user);
    // Verify that the 100000 schema is in place due to
    // equivalent_update_test_update_100000().
    $this->assertEquals(100000, $update_registry->getInstalledVersion('equivalent_update_test'));
    // Remove the update and implement hook_update_last_removed().
    \Drupal::state()->set('equivalent_update_test_last_removed', TRUE);
    $this->drupalGet($this->updateUrl, [
        'external' => TRUE,
    ]);
    $this->updateRequirementsProblem();
    $this->clickLink('Continue');
    $this->assertSession()
        ->pageTextContains('Schema version 100001.');
    // Run the update hooks.
    $this->clickLink('Apply pending updates');
    $this->checkForMetaRefresh();
    // Ensure schema has changed.
    $this->resetAll();
    
    /** @var \Drupal\Core\Update\UpdateHookRegistry $update_registry */
    $update_registry = \Drupal::service('update.update_hook_registry');
    $this->assertEquals(100001, $update_registry->getInstalledVersion('equivalent_update_test'));
    // Set the first update to run.
    \Drupal::state()->set('equivalent_update_test_update_100002', TRUE);
    $this->drupalGet($this->updateUrl, [
        'external' => TRUE,
    ]);
    $this->updateRequirementsProblem();
    $this->clickLink('Continue');
    $this->assertSession()
        ->pageTextContains('Schema version 100002.');
    // Run the update hooks.
    $this->clickLink('Apply pending updates');
    $this->checkForMetaRefresh();
    // Ensure schema has changed.
    $this->resetAll();
    
    /** @var \Drupal\Core\Update\UpdateHookRegistry $update_registry */
    $update_registry = \Drupal::service('update.update_hook_registry');
    $this->assertEquals(100002, $update_registry->getInstalledVersion('equivalent_update_test'));
    $this->assertSame(100002, $update_registry->getEquivalentUpdate('equivalent_update_test', 100101)->ran_update);
    \Drupal::state()->set('equivalent_update_test_update_100002', FALSE);
    \Drupal::state()->set('equivalent_update_test_update_100101', FALSE);
    $this->drupalGet($this->updateUrl, [
        'external' => TRUE,
    ]);
    $this->updateRequirementsProblem();
    $this->assertSession()
        ->pageTextContains('The version of the Equivalent Update test module that you are attempting to update to is missing update 100101 (which was marked as an equivalent by 100002). Update to at least Drupal Core 11.1.0.');
    \Drupal::state()->set('equivalent_update_test_update_100101', TRUE);
    $this->drupalGet($this->updateUrl, [
        'external' => TRUE,
    ]);
    $this->updateRequirementsProblem();
    $this->clickLink('Continue');
    $this->assertSession()
        ->pageTextContains('Schema version 100101.');
    // Run the update hooks.
    $this->clickLink('Apply pending updates');
    $this->checkForMetaRefresh();
    $this->assertSession()
        ->pageTextContains('Update 100101 for the equivalent_update_test module has been skipped because the equivalent change was already made in update 100002.');
    // Ensure that the schema has changed.
    $this->resetAll();
    
    /** @var \Drupal\Core\Update\UpdateHookRegistry $update_registry */
    $update_registry = \Drupal::service('update.update_hook_registry');
    $this->assertEquals(100101, $update_registry->getInstalledVersion('equivalent_update_test'));
    $this->assertNull($update_registry->getEquivalentUpdate('equivalent_update_test', 100101));
    \Drupal::state()->set('equivalent_update_test_update_100201', TRUE);
    $this->drupalGet($this->updateUrl, [
        'external' => TRUE,
    ]);
    $this->updateRequirementsProblem();
    $this->clickLink('Continue');
    $this->assertSession()
        ->pageTextContains('Schema version 100201.');
    // Run the update hooks.
    $this->clickLink('Apply pending updates');
    $this->checkForMetaRefresh();
    $this->assertSession()
        ->pageTextContains('Update 100201 for the equivalent_update_test module has been skipped because the equivalent change was already made in update 100201.');
    \Drupal::state()->set('equivalent_update_test_update_100301', TRUE);
    $this->drupalGet($this->updateUrl, [
        'external' => TRUE,
    ]);
    $this->updateRequirementsProblem();
    $this->clickLink('Continue');
    $this->assertSession()
        ->pageTextContains('Schema version 100301.');
    // Run the update hooks.
    $this->clickLink('Apply pending updates');
    $this->checkForMetaRefresh();
    $this->assertSession()
        ->pageTextContains('Update 100302 for the equivalent_update_test module has been skipped because the equivalent change was already made in update 100301.');
    \Drupal::state()->set('equivalent_update_test_update_100400', TRUE);
    $this->drupalGet($this->updateUrl, [
        'external' => TRUE,
    ]);
    $this->updateRequirementsProblem();
    $this->clickLink('Continue');
    $this->assertSession()
        ->pageTextContains('Schema version 100400.');
    // Run the update hooks.
    $this->clickLink('Apply pending updates');
    $this->checkForMetaRefresh();
    \Drupal::state()->set('equivalent_update_test_update_100400', FALSE);
    \Drupal::state()->set('equivalent_update_test_update_100401', TRUE);
    $this->drupalGet($this->updateUrl, [
        'external' => TRUE,
    ]);
    $this->updateRequirementsProblem();
    $this->assertSession()
        ->pageTextContains('The version of the Equivalent Update test module that you are attempting to update to is missing update 100402 (which was marked as an equivalent by 100400). Update to at least Drupal Core 11.2.0.');
    \Drupal::state()->set('equivalent_update_test_update_100400', TRUE);
    $this->drupalGet($this->updateUrl, [
        'external' => TRUE,
    ]);
    $this->updateRequirementsProblem();
    $this->clickLink('Continue');
    $this->assertSession()
        ->pageTextContains('Schema version 100401.');
    // Run the update hooks.
    $this->clickLink('Apply pending updates');
    $this->checkForMetaRefresh();
    \Drupal::state()->set('equivalent_update_test_update_100400', FALSE);
    \Drupal::state()->set('equivalent_update_test_update_100401', FALSE);
    $this->drupalGet($this->updateUrl, [
        'external' => TRUE,
    ]);
    $this->updateRequirementsProblem();
    $this->assertSession()
        ->pageTextContains('The version of the Equivalent Update test module that you are attempting to update to is missing update 100402 (which was marked as an equivalent by 100401). Update to at least Drupal Core 11.2.0.');
    \Drupal::state()->set('equivalent_update_test_update_100402', TRUE);
    $this->drupalGet($this->updateUrl, [
        'external' => TRUE,
    ]);
    $this->updateRequirementsProblem();
    $this->clickLink('Continue');
    $this->assertSession()
        ->pageTextContains('Schema version 100402.');
    // Run the update hooks.
    $this->clickLink('Apply pending updates');
    $this->checkForMetaRefresh();
    // Ensure that the schema has changed.
    $this->resetAll();
    
    /** @var \Drupal\Core\Update\UpdateHookRegistry $update_registry */
    $update_registry = \Drupal::service('update.update_hook_registry');
    $this->assertEquals(100402, $update_registry->getInstalledVersion('equivalent_update_test'));
    \Drupal::state()->set('equivalent_update_test_update_100501', TRUE);
    $this->drupalGet($this->updateUrl, [
        'external' => TRUE,
    ]);
    $this->updateRequirementsProblem();
    $this->clickLink('Continue');
    $this->assertSession()
        ->pageTextContains('Schema version 100501.');
    // Run the update hooks.
    $this->clickLink('Apply pending updates');
    $this->checkForMetaRefresh();
    $this->assertSession()
        ->pageTextContains('LogicException: Cannot mark the update 100302 as an equivalent since it is less than the current update 100501 for the equivalent_update_test module');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.