View source
<?php
namespace Drupal\Tests\Composer\Plugin\Scaffold\Functional;
use Composer\Util\Filesystem;
use Drupal\Tests\Composer\Plugin\Scaffold\Fixtures;
use Drupal\Tests\Composer\Plugin\Scaffold\AssertUtilsTrait;
use Drupal\Tests\Composer\Plugin\Scaffold\ExecTrait;
use PHPUnit\Framework\TestCase;
class ManageGitIgnoreTest extends TestCase {
use ExecTrait;
use AssertUtilsTrait;
protected $projectRoot;
protected $fixturesDir;
protected $fileSystem;
protected $fixtures;
protected function setUp() : void {
$this->fileSystem = new Filesystem();
$this->fixtures = new Fixtures();
$this->fixtures
->createIsolatedComposerCacheDir();
$this->projectRoot = $this->fixtures
->projectRoot();
}
protected function tearDown() : void {
$this->fixtures
->tearDown();
}
protected function createSutWithGit($fixture_name) {
$this->fixturesDir = $this->fixtures
->tmpDir($this
->getName());
$sut = $this->fixturesDir . '/' . $fixture_name;
$replacements = [
'SYMLINK' => 'false',
'PROJECT_ROOT' => $this->projectRoot,
];
$this->fixtures
->cloneFixtureProjects($this->fixturesDir, $replacements);
$this
->mustExec('git init', $sut);
$this
->mustExec('git config user.email "test@example.com"', $sut);
$this
->mustExec('git config user.name "Test User"', $sut);
$this
->mustExec('git add .', $sut);
$this
->mustExec('git commit -m "Initial commit."', $sut);
$this->fixtures
->runComposer("install --no-ansi --no-scripts --no-plugins", $sut);
return $sut;
}
public function testManageGitIgnore() {
$sut = $this
->createSutWithGit('drupal-composer-drupal-project');
$this
->assertFileDoesNotExist($sut . '/docroot/autoload.php');
$this
->assertFileDoesNotExist($sut . '/docroot/index.php');
$this
->assertFileDoesNotExist($sut . '/docroot/sites/.gitignore');
$this->fixtures
->runScaffold($sut);
$this
->assertFileExists($sut . '/docroot/autoload.php');
$this
->assertFileExists($sut . '/docroot/index.php');
$expected = <<<EOT
/build
/.csslintrc
/.editorconfig
/.eslintignore
/.eslintrc.json
/.gitattributes
/.ht.router.php
/autoload.php
/index.php
/robots.txt
/update.php
/web.config
EOT;
$this
->assertScaffoldedFile($sut . '/docroot/.gitignore', FALSE, $expected);
$this
->assertScaffoldedFile($sut . '/docroot/sites/.gitignore', FALSE, 'example.settings.local.php');
$this
->assertScaffoldedFile($sut . '/docroot/sites/default/.gitignore', FALSE, 'default.services.yml');
$expected = <<<EOT
M docroot/.gitignore
?? docroot/sites/.gitignore
?? docroot/sites/default/.gitignore
EOT;
$stdout = $this
->mustExec('git status --porcelain', $sut);
$this
->assertEquals(trim($expected), trim($stdout));
}
public function testUnmanagedGitIgnoreWhenDisabled() {
$sut = $this
->createSutWithGit('drupal-drupal');
$this
->assertFileDoesNotExist($sut . '/docroot/autoload.php');
$this
->assertFileDoesNotExist($sut . '/docroot/index.php');
$this->fixtures
->runScaffold($sut);
$this
->assertFileExists($sut . '/autoload.php');
$this
->assertFileExists($sut . '/index.php');
$this
->assertFileDoesNotExist($sut . '/.gitignore');
$this
->assertFileDoesNotExist($sut . '/docroot/sites/default/.gitignore');
}
public function testAppendToEmptySettingsIsUnmanaged() {
$sut = $this
->createSutWithGit('drupal-drupal-append-settings');
$this
->assertFileDoesNotExist($sut . '/autoload.php');
$this
->assertFileDoesNotExist($sut . '/index.php');
$this
->assertFileDoesNotExist($sut . '/sites/.gitignore');
$this->fixtures
->runScaffold($sut);
$this
->assertFileExists($sut . '/autoload.php');
$this
->assertFileExists($sut . '/index.php');
$this
->assertScaffoldedFile($sut . '/sites/.gitignore', FALSE, 'example.sites.php');
$this
->assertScaffoldedFileDoesNotContain($sut . '/sites/.gitignore', 'settings.php');
}
public function testUnmanagedGitIgnoreWhenGitNotAvailable() {
$sut = $this
->createSutWithGit('drupal-composer-drupal-project');
$this
->assertFileDoesNotExist($sut . '/docroot/sites/default/.gitignore');
$this
->assertFileDoesNotExist($sut . '/docroot/index.php');
$this
->assertFileDoesNotExist($sut . '/docroot/sites/.gitignore');
$output = [];
exec('git --help', $output, $status);
$this
->assertEquals(0, $status);
$unavailableGitPath = $this->fixtures
->binFixtureDir('disable-git-bin');
chmod($unavailableGitPath . '/git', 0755);
$oldPath = getenv('PATH');
putenv('PATH=' . $unavailableGitPath . ':' . getenv('PATH'));
$output = [];
exec('git --help', $output, $status);
$this
->assertEquals(127, $status);
$output = [];
exec('composer drupal:scaffold', $output, $status);
putenv('PATH=' . $oldPath . ':' . getenv('PATH'));
$expected = <<<EOT
0
Scaffolding files for fixtures/drupal-assets-fixture:
- Copy [web-root]/.csslintrc from assets/.csslintrc
- Copy [web-root]/.editorconfig from assets/.editorconfig
- Copy [web-root]/.eslintignore from assets/.eslintignore
- Copy [web-root]/.eslintrc.json from assets/.eslintrc.json
- Copy [web-root]/.gitattributes from assets/.gitattributes
- Copy [web-root]/.ht.router.php from assets/.ht.router.php
- Skip [web-root]/.htaccess: overridden in fixtures/drupal-composer-drupal-project
- Copy [web-root]/sites/default/default.services.yml from assets/default.services.yml
- Skip [web-root]/sites/default/default.settings.php: overridden in fixtures/scaffold-override-fixture
- Copy [web-root]/sites/example.settings.local.php from assets/example.settings.local.php
- Copy [web-root]/sites/example.sites.php from assets/example.sites.php
- Copy [web-root]/index.php from assets/index.php
- Skip [web-root]/robots.txt: overridden in fixtures/drupal-composer-drupal-project
- Copy [web-root]/update.php from assets/update.php
- Copy [web-root]/web.config from assets/web.config
Scaffolding files for fixtures/scaffold-override-fixture:
- Copy [web-root]/sites/default/default.settings.php from assets/override-settings.php
Scaffolding files for fixtures/drupal-composer-drupal-project:
- Skip [web-root]/.htaccess: disabled
- Copy [web-root]/robots.txt from assets/robots-default.txt
EOT;
$this
->assertEquals($expected, $status . "\n\n" . implode("\n", $output));
$this
->assertFileExists($sut . '/docroot/index.php');
$this
->assertFileDoesNotExist($sut . '/docroot/sites/default/.gitignore');
}
}