function UnknownPathExcluderTest::testUnknownPath

Tests that the unknown files and directories are excluded.

@dataProvider providerTestUnknownPath

Parameters

bool $use_nested_webroot: Whether to create test project with a nested webroot.

string|null $unknown_dir: The path of unknown directory to test or NULL none should be tested.

string[] $unknown_files: The list of unknown files.

File

core/modules/package_manager/tests/src/Kernel/PathExcluder/UnknownPathExcluderTest.php, line 146

Class

UnknownPathExcluderTest
@covers \Drupal\package_manager\PathExcluder\UnknownPathExcluder @group package_manager @internal

Namespace

Drupal\Tests\package_manager\Kernel\PathExcluder

Code

public function testUnknownPath(bool $use_nested_webroot, ?string $unknown_dir, array $unknown_files) : void {
    $this->createTestProjectForTemplate($use_nested_webroot);
    $active_dir = $this->container
        ->get(PathLocator::class)
        ->getProjectRoot();
    if ($unknown_dir) {
        mkdir("{$active_dir}/{$unknown_dir}");
    }
    foreach ($unknown_files as $unknown_file) {
        file_put_contents("{$active_dir}/{$unknown_file}", "Unknown File");
    }
    $stage = $this->createStage();
    // Files are only excluded if the web root and project root are different.
    // If anything in the project root is excluded, those paths should be
    // logged.
    if ($use_nested_webroot) {
        $logger = new TestLogger();
        $this->container
            ->get('logger.factory')
            ->get('package_manager')
            ->addLogger($logger);
        $this->runStatusCheck($stage);
        $this->assertTrue($logger->hasRecordThatContains("The following paths in {$active_dir} aren't recognized as part of your Drupal site, so to be safe, Package Manager is excluding them from all stage operations. If these files are not needed for Composer to work properly in your site, no action is needed. Otherwise, you can disable this behavior by setting the <code>package_manager.settings:include_unknown_files_in_project_root</code> config setting to <code>TRUE</code>.", RfcLogLevel::INFO));
        foreach ($unknown_files as $unknown_file) {
            // If $unknown_file is in a subdirectory, only the subdirectory is going
            // to be logged as an excluded path. The excluder doesn't recurse into
            // subdirectories.
            if (str_contains($unknown_file, '/')) {
                $unknown_file = dirname($unknown_file);
            }
            $this->assertTrue($logger->hasRecordThatContains($unknown_file, RfcLogLevel::INFO));
        }
    }
    $stage->create();
    $stage->require([
        'ext-json:*',
    ]);
    $stage_dir = $stage->getStageDirectory();
    foreach ($unknown_files as $path) {
        $this->assertFileExists("{$active_dir}/{$path}");
        if ($use_nested_webroot) {
            // It will not exist in stage as it will be excluded because web and
            // project root are different.
            $this->assertFileDoesNotExist("{$stage_dir}/{$path}");
        }
        else {
            // If the project root and web root are the same, unknown files will not
            // be excluded, so this path should exist in the stage directory.
            $this->assertFileExists("{$stage_dir}/{$path}");
        }
    }
    $stage->apply();
    // The excluded files should still be in the active directory.
    foreach ($unknown_files as $path) {
        $this->assertFileExists("{$active_dir}/{$path}");
    }
}

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