function StageOwnershipTest::testClaim

Tests behavior of claiming a stage.

File

core/modules/package_manager/tests/src/Kernel/StageOwnershipTest.php, line 144

Class

StageOwnershipTest
Tests that ownership of the stage is enforced.

Namespace

Drupal\Tests\package_manager\Kernel

Code

public function testClaim() : void {
  // Log in as a user so that any stage instances created during the session
  // should be able to successfully call ::claim().
  $user_2 = $this->createUser([], NULL, FALSE, [
    'uid' => 2,
  ]);
  $this->setCurrentUser($user_2);
  $creator_stage = $this->createStage();
  // Ensure that exceptions thrown during ::create() will not lock the stage.
  $error = new \Exception('I am going to stop stage creation.');
  TestSubscriber::setException($error, PreCreateEvent::class);
  try {
    $creator_stage->create();
    $this->fail('Was able to create the stage despite throwing an exception in pre-create.');
  } catch (\RuntimeException $exception) {
    $this->assertSame($error->getMessage(), $exception->getMessage());
  }
  // The stage should be available, and throw if we try to claim it.
  $this->assertTrue($creator_stage->isAvailable());
  try {
    $creator_stage->claim('any-id-would-fail');
    $this->fail('Was able to claim a stage that has not been created.');
  } catch (SandboxException $exception) {
    $this->assertSame('Cannot claim the stage because no stage has been created.', $exception->getMessage());
  }
  TestSubscriber::setException(NULL, PreCreateEvent::class);
  // Even if we own the stage, we should not be able to claim it with an
  // incorrect ID.
  $stage_id = $creator_stage->create();
  try {
    $this->createStage()
      ->claim('not-correct-id');
    $this->fail('Was able to claim an owned stage with an incorrect ID.');
  } catch (SandboxOwnershipException $exception) {
    $this->assertSame('Cannot claim the stage because the current lock does not match the stored lock.', $exception->getMessage());
  }
  // A stage that is successfully claimed should be able to call any method
  // for its life cycle.
  $callbacks = [
    'require' => [
      [
        'vendor/lib:0.0.1',
      ],
    ],
    'apply' => [],
    'postApply' => [],
    'destroy' => [],
  ];
  foreach ($callbacks as $method => $arguments) {
    // Create a new stage instance for each method.
    $this->createStage()
      ->claim($stage_id)
      ->{$method}(...$arguments);
  }
  // The stage cannot be claimed after it's been destroyed.
  try {
    $this->createStage()
      ->claim($stage_id);
    $this->fail('Was able to claim an owned stage after it was destroyed.');
  } catch (SandboxException $exception) {
    $this->assertSame('This operation was already canceled.', $exception->getMessage());
  }
  // Create a new stage and then log in as a different user.
  $new_stage_id = $this->createStage()
    ->create();
  $user_3 = $this->createUser([], NULL, FALSE, [
    'uid' => 3,
  ]);
  $this->setCurrentUser($user_3);
  // Even if they use the correct stage ID, the current user cannot claim a
  // stage they didn't create.
  try {
    $this->createStage()
      ->claim($new_stage_id);
  } catch (SandboxOwnershipException $exception) {
    $this->assertSame('Cannot claim the stage because it is not owned by the current user or session.', $exception->getMessage());
  }
}

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