Newer
Older

Adam G-H
committed
<?php
declare(strict_types = 1);

Adam G-H
committed
namespace Drupal\Tests\package_manager\Kernel;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Url;
use Drupal\package_manager\Event\CollectIgnoredPathsEvent;

Kunal Sachdev
committed
use Drupal\package_manager\Event\PreApplyEvent;

Ted Bowman
committed
use Drupal\package_manager\Event\PreCreateEvent;

Kunal Sachdev
committed
use Drupal\package_manager\Event\StatusCheckEvent;

Adam G-H
committed
use Drupal\package_manager\ValidationResult;

Adam G-H
committed
use PhpTuf\ComposerStager\Domain\Exception\PreconditionException;
use PhpTuf\ComposerStager\Domain\Service\Precondition\CodebaseContainsNoSymlinksInterface;
use PhpTuf\ComposerStager\Domain\Value\Path\PathInterface;
use PhpTuf\ComposerStager\Domain\Value\PathList\PathListInterface;
use PHPUnit\Framework\Assert;

Adam G-H
committed
use Prophecy\Argument;

Adam G-H
committed
/**
* @covers \Drupal\package_manager\Validator\SymlinkValidator
* @group package_manager

Yash Rode
committed
* @internal

Adam G-H
committed
*/
class SymlinkValidatorTest extends PackageManagerKernelTestBase {
/**

Adam G-H
committed
* The mocked precondition that checks for symlinks.
*
* @var \PhpTuf\ComposerStager\Domain\Service\Precondition\CodebaseContainsNoSymlinksInterface|\Prophecy\Prophecy\ObjectProphecy

Adam G-H
committed
*/

Adam G-H
committed
private $precondition;

Adam G-H
committed
/**

Adam G-H
committed
* {@inheritdoc}

Adam G-H
committed
*/

Adam G-H
committed
protected function setUp(): void {
$this->precondition = $this->prophesize(CodebaseContainsNoSymlinksInterface::class);
parent::setUp();

Adam G-H
committed
}
/**

Adam G-H
committed
* {@inheritdoc}

Adam G-H
committed
*/

Adam G-H
committed
public function register(ContainerBuilder $container) {
parent::register($container);

Adam G-H
committed

Adam G-H
committed
$container->getDefinition('package_manager.validator.symlink')
->setArgument('$precondition', $this->precondition->reveal());

Adam G-H
committed
}
/**

Adam G-H
committed
* Data provider for ::testSymlink().
*
* @return array[]
* The test cases.
*/

Adam G-H
committed
public function providerSymlink(): array {

Kunal Sachdev
committed
$test_cases = [];
foreach ([PreApplyEvent::class, PreCreateEvent::class, StatusCheckEvent::class] as $event) {
$test_cases["$event event with no symlinks"] = [
FALSE,
[],

Kunal Sachdev
committed
$event,
];
$test_cases["$event event with symlinks"] = [
TRUE,
[
ValidationResult::createError(['Symlinks were found.']),
],

Kunal Sachdev
committed
$event,
];
}
return $test_cases;
}
/**

Adam G-H
committed
* Tests that the validator invokes Composer Stager's symlink precondition.
*
* @param bool $symlinks_exist
* Whether or not the precondition will detect symlinks.
* @param \Drupal\package_manager\ValidationResult[] $expected_results
* The expected validation results.

Kunal Sachdev
committed
* @param string $event
* The event to test.
*

Adam G-H
committed
* @dataProvider providerSymlink
*/

Kunal Sachdev
committed
public function testSymlink(bool $symlinks_exist, array $expected_results, string $event): void {
$add_ignored_path = function (CollectIgnoredPathsEvent $event): void {
$event->add(['ignore/me']);
};

Kunal Sachdev
committed
$this->addEventTestListener($add_ignored_path, CollectIgnoredPathsEvent::class);
$arguments = [
Argument::type(PathInterface::class),
Argument::type(PathInterface::class),
Argument::type(PathListInterface::class),
];

Kunal Sachdev
committed
$listener = function () use ($arguments, $symlinks_exist): void {
$this->precondition->assertIsFulfilled(...$arguments)
->will(function (array $arguments) use ($symlinks_exist): void {
Assert::assertContains('ignore/me', $arguments[2]->getAll());
if ($symlinks_exist) {
throw new PreconditionException($this->reveal(), 'Symlinks were found.');
}
})
->shouldBeCalled();
};
$this->addEventTestListener($listener, $event);
if ($event === StatusCheckEvent::class) {
$this->assertStatusCheckResults($expected_results);
}
else {
$this->assertResults($expected_results, $event);
}
}

Adam G-H
committed

Kunal Sachdev
committed
/**
* Tests the Composer Stager's symlink precondition with richer help.
*
* @param bool $symlinks_exist
* Whether or not the precondition will detect symlinks.
* @param array $expected_results
* The expected validation results.
* @param string $event
* The event to test.
*
* @dataProvider providerSymlink
*/
public function testHelpLink(bool $symlinks_exist, array $expected_results, string $event): void {

Adam G-H
committed
$this->enableModules(['help']);
$url = Url::fromRoute('help.page', ['name' => 'package_manager'])
->setOption('fragment', 'package-manager-faq-symlinks-found')
->toString();
// Reformat the provided results so that they all have the link to the
// online documentation appended to them.
$map = function (string $message) use ($url): string {
return $message . ' See <a href="' . $url . '">the help page</a> for information on how to resolve the problem.';
};

Adam G-H
committed
foreach ($expected_results as $index => $result) {
$messages = array_map($map, $result->getMessages());

Adam G-H
committed
$expected_results[$index] = ValidationResult::createError($messages);
}

Kunal Sachdev
committed
$this->testSymlink($symlinks_exist, $expected_results, $event);