PageRenderTime 31ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/web/core/tests/Drupal/Tests/Composer/Plugin/Scaffold/Functional/ManageGitIgnoreTest.php

https://gitlab.com/mohamed_hussein/prodt
PHP | 256 lines | 142 code | 20 blank | 94 comment | 3 complexity | d4789ed329ec9b397d0a8add895ab501 MD5 | raw file
  1. <?php
  2. namespace Drupal\Tests\Composer\Plugin\Scaffold\Functional;
  3. use Composer\Util\Filesystem;
  4. use Drupal\Tests\Composer\Plugin\Scaffold\Fixtures;
  5. use Drupal\Tests\Composer\Plugin\Scaffold\AssertUtilsTrait;
  6. use Drupal\Tests\Composer\Plugin\Scaffold\ExecTrait;
  7. use PHPUnit\Framework\TestCase;
  8. /**
  9. * Tests to see whether .gitignore files are correctly managed.
  10. *
  11. * The purpose of this test file is to run a scaffold operation and
  12. * confirm that the files that were scaffolded are added to the
  13. * repository's .gitignore file.
  14. *
  15. * @group Scaffold
  16. */
  17. class ManageGitIgnoreTest extends TestCase {
  18. use ExecTrait;
  19. use AssertUtilsTrait;
  20. /**
  21. * The root of this project.
  22. *
  23. * Used to substitute this project's base directory into composer.json files
  24. * so Composer can find it.
  25. *
  26. * @var string
  27. */
  28. protected $projectRoot;
  29. /**
  30. * Directory to perform the tests in.
  31. *
  32. * @var string
  33. */
  34. protected $fixturesDir;
  35. /**
  36. * The Symfony FileSystem component.
  37. *
  38. * @var \Symfony\Component\Filesystem\Filesystem
  39. */
  40. protected $fileSystem;
  41. /**
  42. * The Fixtures object.
  43. *
  44. * @var \Drupal\Tests\Composer\Plugin\Scaffold\Fixtures
  45. */
  46. protected $fixtures;
  47. /**
  48. * {@inheritdoc}
  49. */
  50. protected function setUp(): void {
  51. $this->fileSystem = new Filesystem();
  52. $this->fixtures = new Fixtures();
  53. $this->fixtures->createIsolatedComposerCacheDir();
  54. $this->projectRoot = $this->fixtures->projectRoot();
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. protected function tearDown(): void {
  60. // Remove any temporary directories et. al. that were created.
  61. $this->fixtures->tearDown();
  62. }
  63. /**
  64. * Creates a system-under-test and initialize a git repository for it.
  65. *
  66. * @param string $fixture_name
  67. * The name of the fixture to use from
  68. * core/tests/Drupal/Tests/Component/Scaffold/fixtures.
  69. *
  70. * @return string
  71. * The path to the fixture directory.
  72. */
  73. protected function createSutWithGit($fixture_name) {
  74. $this->fixturesDir = $this->fixtures->tmpDir($this->getName());
  75. $sut = $this->fixturesDir . '/' . $fixture_name;
  76. $replacements = ['SYMLINK' => 'false', 'PROJECT_ROOT' => $this->projectRoot];
  77. $this->fixtures->cloneFixtureProjects($this->fixturesDir, $replacements);
  78. // .gitignore files will not be managed unless there is a git repository.
  79. $this->mustExec('git init', $sut);
  80. // Add some user info so git does not complain.
  81. $this->mustExec('git config user.email "test@example.com"', $sut);
  82. $this->mustExec('git config user.name "Test User"', $sut);
  83. $this->mustExec('git add .', $sut);
  84. $this->mustExec('git commit -m "Initial commit."', $sut);
  85. // Run composer install, but suppress scaffolding.
  86. $this->fixtures->runComposer("install --no-ansi --no-scripts --no-plugins", $sut);
  87. return $sut;
  88. }
  89. /**
  90. * Tests scaffold command correctly manages the .gitignore file.
  91. */
  92. public function testManageGitIgnore() {
  93. // Note that the drupal-composer-drupal-project fixture does not
  94. // have any configuration settings related to .gitignore management.
  95. $sut = $this->createSutWithGit('drupal-composer-drupal-project');
  96. $this->assertFileDoesNotExist($sut . '/docroot/autoload.php');
  97. $this->assertFileDoesNotExist($sut . '/docroot/index.php');
  98. $this->assertFileDoesNotExist($sut . '/docroot/sites/.gitignore');
  99. // Run the scaffold command.
  100. $this->fixtures->runScaffold($sut);
  101. $this->assertFileExists($sut . '/docroot/autoload.php');
  102. $this->assertFileExists($sut . '/docroot/index.php');
  103. $expected = <<<EOT
  104. /build
  105. /.csslintrc
  106. /.editorconfig
  107. /.eslintignore
  108. /.eslintrc.json
  109. /.gitattributes
  110. /.ht.router.php
  111. /autoload.php
  112. /index.php
  113. /robots.txt
  114. /update.php
  115. /web.config
  116. EOT;
  117. // At this point we should have a .gitignore file, because although we did
  118. // not explicitly ask for .gitignore tracking, the vendor directory is not
  119. // tracked, so the default in that instance is to manage .gitignore files.
  120. $this->assertScaffoldedFile($sut . '/docroot/.gitignore', FALSE, $expected);
  121. $this->assertScaffoldedFile($sut . '/docroot/sites/.gitignore', FALSE, 'example.settings.local.php');
  122. $this->assertScaffoldedFile($sut . '/docroot/sites/default/.gitignore', FALSE, 'default.services.yml');
  123. $expected = <<<EOT
  124. M docroot/.gitignore
  125. ?? docroot/sites/.gitignore
  126. ?? docroot/sites/default/.gitignore
  127. EOT;
  128. // Check to see whether there are any untracked files. We expect that
  129. // only the .gitignore files themselves should be untracked.
  130. $stdout = $this->mustExec('git status --porcelain', $sut);
  131. $this->assertEquals(trim($expected), trim($stdout));
  132. }
  133. /**
  134. * Tests scaffold command does not manage the .gitignore file when disabled.
  135. */
  136. public function testUnmanagedGitIgnoreWhenDisabled() {
  137. // Note that the drupal-drupal fixture has a configuration setting
  138. // `"gitignore": false,` which disables .gitignore file handling.
  139. $sut = $this->createSutWithGit('drupal-drupal');
  140. $this->assertFileDoesNotExist($sut . '/docroot/autoload.php');
  141. $this->assertFileDoesNotExist($sut . '/docroot/index.php');
  142. // Run the scaffold command.
  143. $this->fixtures->runScaffold($sut);
  144. $this->assertFileExists($sut . '/autoload.php');
  145. $this->assertFileExists($sut . '/index.php');
  146. $this->assertFileDoesNotExist($sut . '/.gitignore');
  147. $this->assertFileDoesNotExist($sut . '/docroot/sites/default/.gitignore');
  148. }
  149. /**
  150. * Tests appending to an unmanaged file, and confirm it is not .gitignored.
  151. *
  152. * If we append to an unmanaged (not scaffolded) file, and we are managing
  153. * .gitignore files, then we expect that the unmanaged file should not be
  154. * added to the .gitignore file, because unmanaged files should be committed.
  155. */
  156. public function testAppendToEmptySettingsIsUnmanaged() {
  157. $sut = $this->createSutWithGit('drupal-drupal-append-settings');
  158. $this->assertFileDoesNotExist($sut . '/autoload.php');
  159. $this->assertFileDoesNotExist($sut . '/index.php');
  160. $this->assertFileDoesNotExist($sut . '/sites/.gitignore');
  161. // Run the scaffold command.
  162. $this->fixtures->runScaffold($sut);
  163. $this->assertFileExists($sut . '/autoload.php');
  164. $this->assertFileExists($sut . '/index.php');
  165. $this->assertScaffoldedFile($sut . '/sites/.gitignore', FALSE, 'example.sites.php');
  166. $this->assertScaffoldedFileDoesNotContain($sut . '/sites/.gitignore', 'settings.php');
  167. }
  168. /**
  169. * Tests scaffold command disables .gitignore management when git not present.
  170. *
  171. * The scaffold operation should still succeed if there is no 'git'
  172. * executable.
  173. */
  174. public function testUnmanagedGitIgnoreWhenGitNotAvailable() {
  175. // Note that the drupal-composer-drupal-project fixture does not have any
  176. // configuration settings related to .gitignore management.
  177. $sut = $this->createSutWithGit('drupal-composer-drupal-project');
  178. $this->assertFileDoesNotExist($sut . '/docroot/sites/default/.gitignore');
  179. $this->assertFileDoesNotExist($sut . '/docroot/index.php');
  180. $this->assertFileDoesNotExist($sut . '/docroot/sites/.gitignore');
  181. // Confirm that 'git' is available (n.b. if it were not, createSutWithGit()
  182. // would fail).
  183. $output = [];
  184. exec('git --help', $output, $status);
  185. $this->assertEquals(0, $status);
  186. // Modify our $PATH so that it begins with a path that contains an
  187. // executable script named 'git' that always exits with 127, as if git were
  188. // not found. Note that we run our tests using process isolation, so we do
  189. // not need to restore the PATH when we are done.
  190. $unavailableGitPath = $sut . '/bin';
  191. mkdir($unavailableGitPath);
  192. $bash = <<<SH
  193. #!/bin/bash
  194. exit 127
  195. SH;
  196. file_put_contents($unavailableGitPath . '/git', $bash);
  197. chmod($unavailableGitPath . '/git', 0755);
  198. $oldPath = getenv('PATH');
  199. putenv('PATH=' . $unavailableGitPath . ':' . getenv('PATH'));
  200. // Confirm that 'git' is no longer available.
  201. $output = [];
  202. exec('git --help', $output, $status);
  203. $this->assertEquals(127, $status);
  204. // Run the scaffold command.
  205. $output = [];
  206. exec('composer drupal:scaffold', $output, $status);
  207. putenv('PATH=' . $oldPath . ':' . getenv('PATH'));
  208. $expected = <<<EOT
  209. 0
  210. Scaffolding files for fixtures/drupal-assets-fixture:
  211. - Copy [web-root]/.csslintrc from assets/.csslintrc
  212. - Copy [web-root]/.editorconfig from assets/.editorconfig
  213. - Copy [web-root]/.eslintignore from assets/.eslintignore
  214. - Copy [web-root]/.eslintrc.json from assets/.eslintrc.json
  215. - Copy [web-root]/.gitattributes from assets/.gitattributes
  216. - Copy [web-root]/.ht.router.php from assets/.ht.router.php
  217. - Skip [web-root]/.htaccess: overridden in fixtures/drupal-composer-drupal-project
  218. - Copy [web-root]/sites/default/default.services.yml from assets/default.services.yml
  219. - Skip [web-root]/sites/default/default.settings.php: overridden in fixtures/scaffold-override-fixture
  220. - Copy [web-root]/sites/example.settings.local.php from assets/example.settings.local.php
  221. - Copy [web-root]/sites/example.sites.php from assets/example.sites.php
  222. - Copy [web-root]/index.php from assets/index.php
  223. - Skip [web-root]/robots.txt: overridden in fixtures/drupal-composer-drupal-project
  224. - Copy [web-root]/update.php from assets/update.php
  225. - Copy [web-root]/web.config from assets/web.config
  226. Scaffolding files for fixtures/scaffold-override-fixture:
  227. - Copy [web-root]/sites/default/default.settings.php from assets/override-settings.php
  228. Scaffolding files for fixtures/drupal-composer-drupal-project:
  229. - Skip [web-root]/.htaccess: disabled
  230. - Copy [web-root]/robots.txt from assets/robots-default.txt
  231. EOT;
  232. $this->assertEquals($expected, $status . "\n\n" . implode("\n", $output));
  233. $this->assertFileExists($sut . '/docroot/index.php');
  234. $this->assertFileDoesNotExist($sut . '/docroot/sites/default/.gitignore');
  235. }
  236. }