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; ...@@ -12,6 +12,9 @@ use Drupal\package_manager\PathLocator;
use Drupal\package_manager\Stage; use Drupal\package_manager\Stage;
use Drupal\Tests\package_manager\Traits\ValidationTestTrait; use Drupal\Tests\package_manager\Traits\ValidationTestTrait;
use org\bovigo\vfs\vfsStream; 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. * Base class for kernel tests of Package Manager's functionality.
...@@ -140,92 +143,41 @@ abstract class PackageManagerKernelTestBase extends KernelTestBase { ...@@ -140,92 +143,41 @@ abstract class PackageManagerKernelTestBase extends KernelTestBase {
* locator service will also be mocked so that it points to the test project. * locator service will also be mocked so that it points to the test project.
*/ */
protected function createTestProject(): void { protected function createTestProject(): void {
$tree = [ // Create the active directory and copy its contents from a fixture.
'active' => [ $active_dir = vfsStream::newDirectory('active');
'composer.json' => '{}', $this->vfsRoot->addChild($active_dir);
'composer.lock' => '{}', vfsStream::copyFromFileSystem(__DIR__ . '/../../fixtures/fake_site', $active_dir);
'.git' => [
'ignore.txt' => 'This file should never be staged.', // Because we can't commit physical `.git` directories into the fixture, use
], // a visitor to traverse the virtual file system and rename all `_git`
'.gitignore' => 'This file should be staged.', // directories to `.git`.
'private' => [ vfsStream::inspect(new class () extends vfsStreamAbstractVisitor {
'ignore.txt' => 'This file should never be staged.',
], /**
'modules' => [ * {@inheritdoc}
'example' => [ */
'example.info.yml' => 'This file should be staged.', public function visitFile(vfsStreamFile $file) {}
'.git' => [
'ignore.txt' => 'This file should never be staged.', /**
], * {@inheritdoc}
], */
], public function visitDirectory(vfsStreamDirectory $dir) {
'sites' => [ if ($dir->getName() === '_git') {
'default' => [ $dir->rename('.git');
'services.yml' => <<<END }
# This file should never be staged. foreach ($dir->getChildren() as $child) {
must_not_be: 'empty' $this->visit($child);
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
/** });
* @file
* This file should never be staged.
*/
END,
'settings.php' => <<<END
<?php
/** // Create a staging root directory alongside the active directory.
* @file $stage_dir = vfsStream::newDirectory('stage');
* This file should never be staged. $this->vfsRoot->addChild($stage_dir);
*/ TestStage::$stagingRoot = $stage_dir->url();
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";
$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 // 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 // replace the disk space validator with a test-only version that bypasses
......
...@@ -10,8 +10,7 @@ use Drupal\package_manager\Exception\StageException; ...@@ -10,8 +10,7 @@ use Drupal\package_manager\Exception\StageException;
use Drupal\package_manager\Exception\StageOwnershipException; use Drupal\package_manager\Exception\StageOwnershipException;
use Drupal\package_manager_test_validation\EventSubscriber\TestSubscriber; use Drupal\package_manager_test_validation\EventSubscriber\TestSubscriber;
use Drupal\Tests\user\Traits\UserCreationTrait; use Drupal\Tests\user\Traits\UserCreationTrait;
use Prophecy\Argument; use Psr\Log\Test\TestLogger;
use Psr\Log\LoggerInterface;
/** /**
* Tests that ownership of the stage is enforced. * Tests that ownership of the stage is enforced.
...@@ -272,15 +271,8 @@ class StageOwnershipTest extends PackageManagerKernelTestBase { ...@@ -272,15 +271,8 @@ class StageOwnershipTest extends PackageManagerKernelTestBase {
chmod($dir, 0400); chmod($dir, 0400);
$this->assertDirectoryIsNotWritable($dir); $this->assertDirectoryIsNotWritable($dir);
// Mock a logger to prove that a file system error was raised while trying $logger = new TestLogger();
// to delete the stage directory. $logger_channel->addLogger($logger);
$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());
// Listen to the post-destroy event so we can confirm that it was fired, and // 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. // the stage was made available, despite the file system error.
...@@ -291,6 +283,17 @@ class StageOwnershipTest extends PackageManagerKernelTestBase { ...@@ -291,6 +283,17 @@ class StageOwnershipTest extends PackageManagerKernelTestBase {
}); });
$stage->destroy(); $stage->destroy();
$this->assertTrue($stage_available); $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