function PackageManagerKernelTestBase::createTestProject

Creates a test project.

This will create a temporary uniques root directory and then creates two directories in it: 'active', which is the active directory containing a fake Drupal code base, and 'stage', which is the root directory used to stage changes. The path locator service will also be mocked so that it points to the test project.

Parameters

string|null $source_dir: (optional) The path of a directory which should be copied into the test project and used as the active directory.

File

core/modules/package_manager/tests/src/Kernel/PackageManagerKernelTestBase.php, line 271

Class

PackageManagerKernelTestBase
Base class for kernel tests of Package Manager's functionality.

Namespace

Drupal\Tests\package_manager\Kernel

Code

protected function createTestProject(?string $source_dir = NULL) : void {
    static $called;
    if (isset($called)) {
        throw new \LogicException('Only one test project should be created per kernel test method!');
    }
    else {
        $called = TRUE;
    }
    $this->testProjectRoot = DrupalFileSystem::getOsTemporaryDirectory() . DIRECTORY_SEPARATOR . 'package_manager_testing_root' . $this->databasePrefix;
    if (is_dir($this->testProjectRoot)) {
        $this->fileSystem
            ->remove($this->testProjectRoot);
    }
    $this->fileSystem
        ->mkdir($this->testProjectRoot);
    // Create the active directory and copy its contents from a fixture.
    $active_dir = $this->testProjectRoot . DIRECTORY_SEPARATOR . 'active';
    $this->assertTrue(mkdir($active_dir));
    static::copyFixtureFilesTo($source_dir ?? __DIR__ . '/../../fixtures/fake_site', $active_dir);
    // Removing 'vfs://root/' from site path set in
    // \Drupal\KernelTests\KernelTestBase::setUpFilesystem as we don't use vfs.
    $test_site_path = str_replace('vfs://root/', '', $this->siteDirectory);
    // Copy directory structure from vfs site directory to our site directory.
    $this->fileSystem
        ->mirror($this->siteDirectory, $active_dir . DIRECTORY_SEPARATOR . $test_site_path);
    // Override siteDirectory to point to root/active/... instead of root/... .
    $this->siteDirectory = $active_dir . DIRECTORY_SEPARATOR . $test_site_path;
    // Override KernelTestBase::setUpFilesystem's Settings object.
    $settings = Settings::getInstance() ? Settings::getAll() : [];
    $settings['file_public_path'] = $this->siteDirectory . '/files';
    $settings['config_sync_directory'] = $this->siteDirectory . '/files/config/sync';
    new Settings($settings);
    // Create a stage root directory alongside the active directory.
    $staging_root = $this->testProjectRoot . DIRECTORY_SEPARATOR . 'stage';
    $this->assertTrue(mkdir($staging_root));
    // Ensure the path locator points to the test project. We assume that is its
    // own web root and the vendor directory is at its top level.
    
    /** @var \Drupal\package_manager_bypass\MockPathLocator $path_locator */
    $path_locator = $this->container
        ->get(PathLocator::class);
    $path_locator->setPaths($active_dir, $active_dir . '/vendor', '', $staging_root);
    // This validator will persist through container rebuilds.
    // @see ::register()
    $validator = new TestDiskSpaceValidator($path_locator);
    // By default, the validator should report that the root, vendor, and
    // temporary directories have basically infinite free space.
    $validator->freeSpace = [
        $path_locator->getProjectRoot() => PHP_INT_MAX,
        $path_locator->getVendorDirectory() => PHP_INT_MAX,
        $validator->temporaryDirectory() => PHP_INT_MAX,
    ];
    $this->container
        ->set(DiskSpaceValidator::class, $validator);
}

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