function SandboxManagerBaseTest::testFailureMarkerPreventsCreate

Tests that if a stage fails to apply, another stage cannot be created.

File

core/modules/package_manager/tests/src/Kernel/SandboxManagerBaseTest.php, line 176

Class

SandboxManagerBaseTest
@coversDefaultClass \Drupal\package_manager\SandboxManagerBase @group package_manager @group #slow @internal

Namespace

Drupal\Tests\package_manager\Kernel

Code

public function testFailureMarkerPreventsCreate() : void {
    $stage = $this->createStage();
    $stage->create();
    $stage->require([
        'ext-json:*',
    ]);
    // Make the committer throw an exception, which should cause the failure
    // marker to be present.
    $thrown_message = 'Thrown by the committer.';
    LoggingCommitter::setException(\Exception::class, $thrown_message);
    try {
        $stage->apply();
        $this->fail('Expected an exception.');
    } catch (ApplyFailedException $e) {
        $this->assertStringContainsString($thrown_message, $e->getMessage());
        $this->assertFalse($stage->isApplying());
    }
    $stage->destroy();
    // Even through the previous stage was destroyed, we cannot create a new one
    // because the failure marker is still there.
    $stage = $this->createStage();
    try {
        $stage->create();
        $this->fail('Expected an exception.');
    } catch (FailureMarkerExistsException $e) {
        $this->assertMatchesRegularExpression('/^Staged changes failed to apply, and the site is in an indeterminate state. It is strongly recommended to restore the code and database from a backup. Caused by Exception, with this message: ' . $thrown_message . "\nBacktrace:\n#0 .*/", $e->getMessage());
        $this->assertFalse($stage->isApplying());
    }
    // If the failure marker is cleared, we should be able to create the stage
    // without issue.
    $this->container
        ->get(FailureMarker::class)
        ->clear();
    $stage->create();
}

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