function StageOwnershipTest::assertOwnershipIsEnforced

Asserts that ownership is enforced across stage directories.

Parameters

\Drupal\Tests\package_manager\Kernel\TestSandboxManager $will_create: The stage that will be created, and owned by the current user or session.

\Drupal\Tests\package_manager\Kernel\TestSandboxManager $never_create: The stage that will not be created, but should still respect the ownership and status of the other stage.

2 calls to StageOwnershipTest::assertOwnershipIsEnforced()
StageOwnershipTest::testOwnershipEnforcedWhenLoggedIn in core/modules/package_manager/tests/src/Kernel/StageOwnershipTest.php
Tests only the owner of stage can perform operations.
StageOwnershipTest::testOwnershipEnforcedWhenLoggedOut in core/modules/package_manager/tests/src/Kernel/StageOwnershipTest.php
Tests only the owner of stage can perform operations, even if logged out.

File

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

Class

StageOwnershipTest
Tests that ownership of the stage is enforced.

Namespace

Drupal\Tests\package_manager\Kernel

Code

private function assertOwnershipIsEnforced(TestSandboxManager $will_create, TestSandboxManager $never_create) : void {
  // Before the stage directory is created, isAvailable() should return
  // TRUE.
  $this->assertTrue($will_create->isAvailable());
  $this->assertTrue($never_create->isAvailable());
  $stage_id = $will_create->create();
  // Both stage directories should be considered unavailable (i.e., cannot
  // be created until the existing one is destroyed first).
  $this->assertFalse($will_create->isAvailable());
  $this->assertFalse($never_create->isAvailable());
  // We should get an error if we try to create the stage directory again,
  // regardless of who owns it.
  foreach ([
    $will_create,
    $never_create,
  ] as $stage) {
    try {
      $stage->create();
      $this->fail("Able to create a stage that already exists.");
    } catch (SandboxException $exception) {
      $this->assertSame('Cannot create a new stage because one already exists.', $exception->getMessage());
    }
  }
  try {
    $never_create->claim($stage_id);
  } catch (SandboxOwnershipException $exception) {
    $this->assertSame('Cannot claim the stage because it is not owned by the current user or session.', $exception->getMessage());
  }
  // Only the stage's owner should be able to move it through its life cycle.
  $callbacks = [
    'require' => [
      [
        'vendor/lib:0.0.1',
      ],
    ],
    'apply' => [],
    'postApply' => [],
    'destroy' => [],
  ];
  foreach ($callbacks as $method => $arguments) {
    try {
      $never_create->{$method}(...$arguments);
      $this->fail("Able to call '{$method}' on a stage that was never created.");
    } catch (\LogicException $exception) {
      $this->assertSame('Stage must be claimed before performing any operations on it.', $exception->getMessage());
    }
    // The call should succeed on the created stage.
    $will_create->{$method}(...$arguments);
  }
}

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