View source
<?php
namespace Drupal\Tests\Composer\Plugin\Scaffold\Functional;
use Composer\Util\Filesystem;
use Drupal\Tests\Composer\Plugin\Scaffold\AssertUtilsTrait;
use Drupal\Tests\Composer\Plugin\Scaffold\ExecTrait;
use Drupal\Tests\Composer\Plugin\Scaffold\Fixtures;
use PHPUnit\Framework\TestCase;
class ComposerHookTest extends TestCase {
use ExecTrait;
use AssertUtilsTrait;
protected $fixturesDir;
protected $fileSystem;
protected $fixtures;
protected function setUp() : void {
$this->fileSystem = new Filesystem();
$this->fixtures = new Fixtures();
$this->fixtures
->createIsolatedComposerCacheDir();
$this->fixturesDir = $this->fixtures
->tmpDir($this
->getName());
$replacements = [
'SYMLINK' => 'false',
'PROJECT_ROOT' => $this->fixtures
->projectRoot(),
];
$this->fixtures
->cloneFixtureProjects($this->fixturesDir, $replacements);
}
protected function tearDown() : void {
$this->fixtures
->tearDown();
}
public function testComposerHooks() {
$topLevelProjectDir = 'composer-hooks-fixture';
$sut = $this->fixturesDir . '/' . $topLevelProjectDir;
$this
->mustExec("composer install --no-ansi", $sut);
$this
->assertScaffoldedFile($sut . '/sites/default/default.settings.php', FALSE, 'Test version of default.settings.php from drupal/core');
$stdout = $this
->mustExec("composer require --no-ansi --no-interaction fixtures/scaffold-override-fixture:dev-master", $sut);
$this
->assertScaffoldedFile($sut . '/sites/default/default.settings.php', FALSE, 'scaffolded from the scaffold-override-fixture');
$this
->assertStringContainsString('Package fixtures/scaffold-override-fixture has scaffold operations, and is already allowed in the root-level composer.json file.', $stdout);
@unlink($sut . '/sites/default/default.settings.php');
$this
->assertFileDoesNotExist($sut . '/sites/default/default.settings.php');
$this
->mustExec("composer update --no-ansi", $sut);
$this
->assertScaffoldedFile($sut . '/sites/default/default.settings.php', FALSE, 'scaffolded from the scaffold-override-fixture');
@unlink($sut . '/sites/default/default.settings.php');
$this
->assertFileDoesNotExist($sut . '/sites/default/default.settings.php');
$this
->mustExec("composer install --no-ansi", $sut);
$this
->assertScaffoldedFile($sut . '/sites/default/default.settings.php', FALSE, 'scaffolded from the scaffold-override-fixture');
@unlink($sut . '/sites/default/default.settings.php');
$this
->assertFileDoesNotExist($sut . '/sites/default/default.settings.php');
$this
->mustExec("composer drupal:scaffold --no-ansi", $sut);
$this
->assertScaffoldedFile($sut . '/sites/default/default.settings.php', FALSE, 'scaffolded from the scaffold-override-fixture');
$sut = $this->fixturesDir . '/create-project-test';
$filesystem = new Filesystem();
$filesystem
->remove($sut);
$stdout = $this
->mustExec("composer create-project --repository=packages.json fixtures/drupal-drupal {$sut}", $this->fixturesDir, [
'COMPOSER_MIRROR_PATH_REPOS' => 1,
]);
$this
->assertDirectoryExists($sut);
$this
->assertStringContainsString('Scaffolding files for fixtures/drupal-drupal', $stdout);
$this
->assertScaffoldedFile($sut . '/index.php', FALSE, 'Test version of index.php from drupal/core');
$topLevelProjectDir = 'composer-hooks-nothing-allowed-fixture';
$sut = $this->fixturesDir . '/' . $topLevelProjectDir;
$this
->mustExec("composer install --no-ansi", $sut);
$stdout = $this
->mustExec("composer require --no-ansi --no-interaction fixtures/scaffold-override-fixture:dev-master", $sut);
$this
->assertFileDoesNotExist($sut . '/sites/default/default.settings.php');
$this
->assertStringContainsString("Not scaffolding files for fixtures/scaffold-override-fixture, because it is not listed in the element 'extra.drupal-scaffold.allowed-packages' in the root-level composer.json file.", $stdout);
}
public function testScaffoldMessagesDoNotPrintTwice() {
$topLevelProjectDir = 'drupal-drupal';
$sut = $this->fixturesDir . '/' . $topLevelProjectDir;
$stdout = $this
->mustExec("composer install --no-ansi", $sut);
$this
->assertStringContainsString('- Copy [web-root]/index.php from assets/index.php', $stdout);
$this
->assertStringContainsString('- Copy [web-root]/update.php from assets/update.php', $stdout);
$stdout = $this
->mustExec("composer scaffold --no-ansi", $sut);
$this
->assertEquals('', $stdout);
unlink("{$sut}/index.php");
$stdout = $this
->mustExec("composer scaffold --no-ansi", $sut);
$this
->assertStringContainsString('- Copy [web-root]/index.php from assets/index.php', $stdout);
$this
->assertStringNotContainsString('- Copy [web-root]/update.php from assets/update.php', $stdout);
}
}