function SymlinkValidatorTest::testSymlinkPointingOutsideStageDirectory

Tests that relative symlinks cannot point outside the stage directory.

File

core/modules/package_manager/tests/src/Kernel/SymlinkValidatorTest.php, line 116

Class

SymlinkValidatorTest
Tests Symlink Validator.

Namespace

Drupal\Tests\package_manager\Kernel

Code

public function testSymlinkPointingOutsideStageDirectory() : void {
  // The same check should apply to symlinks in the stage directory that
  // point outside of it.
  $stage = $this->createStage();
  $stage->create();
  $stage->require([
    'ext-json:*',
  ]);
  $stage_dir = $stage->getSandboxDirectory();
  $parent_dir = dirname($stage_dir);
  touch($parent_dir . '/hello.txt');
  // Relative symlinks must be made from their actual directory to be
  // correctly evaluated.
  chdir($stage_dir);
  symlink('../hello.txt', 'fail.txt');
  $result = ValidationResult::createError([
    $this->t('The %which directory at %dir contains links that point outside the codebase, which is not supported. The first one is %file.', [
      '%which' => 'staging',
      '%dir' => $stage_dir,
      '%file' => $stage_dir . '/fail.txt',
    ]),
  ]);
  try {
    $stage->apply();
    $this->fail('Expected an exception, but none was thrown.');
  } catch (SandboxEventException $e) {
    $this->assertExpectedResultsFromException([
      $result,
    ], $e);
  }
}

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