Skip to content
Snippets Groups Projects
Commit 4f4a66c9 authored by Adam G-H's avatar Adam G-H
Browse files

Issue #3275282 by phenaproxima:...

Issue #3275282 by phenaproxima: PackageManagerKernelTestBase::createTestProject() should mirror a real directory
parent 52b3e054
No related branches found
No related tags found
1 merge request!271Issue #3275282: PackageManagerKernelTestBase::createTestProject() should mirror a real directory
This file should never be staged.
......@@ -12,6 +12,9 @@ use Drupal\package_manager\PathLocator;
use Drupal\package_manager\Stage;
use Drupal\Tests\package_manager\Traits\ValidationTestTrait;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use org\bovigo\vfs\vfsStreamFile;
use org\bovigo\vfs\visitor\vfsStreamAbstractVisitor;
/**
* Base class for kernel tests of Package Manager's functionality.
......@@ -140,92 +143,41 @@ abstract class PackageManagerKernelTestBase extends KernelTestBase {
* locator service will also be mocked so that it points to the test project.
*/
protected function createTestProject(): void {
$tree = [
'active' => [
'composer.json' => '{}',
'composer.lock' => '{}',
'.git' => [
'ignore.txt' => 'This file should never be staged.',
],
'.gitignore' => 'This file should be staged.',
'private' => [
'ignore.txt' => 'This file should never be staged.',
],
'modules' => [
'example' => [
'example.info.yml' => 'This file should be staged.',
'.git' => [
'ignore.txt' => 'This file should never be staged.',
],
],
],
'sites' => [
'default' => [
'services.yml' => <<<END
# This file should never be staged.
must_not_be: 'empty'
END,
'settings.local.php' => <<<END
<?php
/**
* @file
* This file should never be staged.
*/
END,
'settings.php' => <<<END
<?php
/**
* @file
* This file should never be staged.
*/
END,
'stage.txt' => 'This file should be staged.',
],
'example.com' => [
'files' => [
'ignore.txt' => 'This file should never be staged.',
],
'db.sqlite' => 'This file should never be staged.',
'db.sqlite-shm' => 'This file should never be staged.',
'db.sqlite-wal' => 'This file should never be staged.',
'services.yml' => <<<END
# This file should never be staged.
key: "value"
END,
'settings.local.php' => <<<END
<?php
// Create the active directory and copy its contents from a fixture.
$active_dir = vfsStream::newDirectory('active');
$this->vfsRoot->addChild($active_dir);
vfsStream::copyFromFileSystem(__DIR__ . '/../../fixtures/fake_site', $active_dir);
// Because we can't commit physical `.git` directories into the fixture, use
// a visitor to traverse the virtual file system and rename all `_git`
// directories to `.git`.
vfsStream::inspect(new class () extends vfsStreamAbstractVisitor {
/**
* {@inheritdoc}
*/
public function visitFile(vfsStreamFile $file) {}
/**
* {@inheritdoc}
*/
public function visitDirectory(vfsStreamDirectory $dir) {
if ($dir->getName() === '_git') {
$dir->rename('.git');
}
foreach ($dir->getChildren() as $child) {
$this->visit($child);
}
}
/**
* @file
* This file should never be staged.
*/
END,
'settings.php' => <<<END
<?php
});
/**
* @file
* This file should never be staged.
*/
END,
],
'simpletest' => [
'ignore.txt' => 'This file should never be staged.',
],
],
'vendor' => [
'.htaccess' => '# This file should never be staged.',
'web.config' => 'This file should never be staged.',
],
],
'stage' => [],
];
$root = vfsStream::create($tree, $this->vfsRoot)->url();
TestStage::$stagingRoot = "$root/stage";
// Create a staging root directory alongside the active directory.
$stage_dir = vfsStream::newDirectory('stage');
$this->vfsRoot->addChild($stage_dir);
TestStage::$stagingRoot = $stage_dir->url();
$path_locator = $this->mockPathLocator("$root/active");
$path_locator = $this->mockPathLocator($active_dir->url());
// Since the path locator now points to a virtual file system, we need to
// replace the disk space validator with a test-only version that bypasses
......
......@@ -10,8 +10,7 @@ use Drupal\package_manager\Exception\StageException;
use Drupal\package_manager\Exception\StageOwnershipException;
use Drupal\package_manager_test_validation\EventSubscriber\TestSubscriber;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Prophecy\Argument;
use Psr\Log\LoggerInterface;
use Psr\Log\Test\TestLogger;
/**
* Tests that ownership of the stage is enforced.
......@@ -272,15 +271,8 @@ class StageOwnershipTest extends PackageManagerKernelTestBase {
chmod($dir, 0400);
$this->assertDirectoryIsNotWritable($dir);
// Mock a logger to prove that a file system error was raised while trying
// to delete the stage directory.
$logger = $this->prophesize(LoggerInterface::class);
$logger->log(
RfcLogLevel::ERROR,
"Failed to unlink file '%path'.",
Argument::withEntry('%path', "$dir/composer.json")
)->shouldBeCalled();
$logger_channel->addLogger($logger->reveal());
$logger = new TestLogger();
$logger_channel->addLogger($logger);
// Listen to the post-destroy event so we can confirm that it was fired, and
// the stage was made available, despite the file system error.
......@@ -291,6 +283,17 @@ class StageOwnershipTest extends PackageManagerKernelTestBase {
});
$stage->destroy();
$this->assertTrue($stage_available);
// A file system error should have been logged while trying to delete the
// stage directory.
$predicate = function (array $record) use ($dir): bool {
return (
$record['message'] === "Failed to unlink file '%path'." &&
isset($record['context']['%path']) &&
str_contains($record['context']['%path'], $dir)
);
};
$this->assertTrue($logger->hasRecordThatPasses($predicate, RfcLogLevel::ERROR));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment