PageRenderTime 21ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/mohamed_hussein/prodt
PHP | 153 lines | 72 code | 15 blank | 66 comment | 1 complexity | 954d6c42134f489b7be1f68ed1e0fc78 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\AssertUtilsTrait;
  5. use Drupal\Tests\Composer\Plugin\Scaffold\ExecTrait;
  6. use Drupal\Tests\Composer\Plugin\Scaffold\Fixtures;
  7. use PHPUnit\Framework\TestCase;
  8. /**
  9. * Tests Composer Hooks that run scaffold operations.
  10. *
  11. * The purpose of this test file is to exercise all of the different Composer
  12. * commands that invoke scaffold operations, and ensure that files are
  13. * scaffolded when they should be.
  14. *
  15. * Note that this test file uses `exec` to run Composer for a pure functional
  16. * test. Other functional test files invoke Composer commands directly via the
  17. * Composer Application object, in order to get more accurate test coverage
  18. * information.
  19. *
  20. * @group Scaffold
  21. */
  22. class ComposerHookTest extends TestCase {
  23. use ExecTrait;
  24. use AssertUtilsTrait;
  25. /**
  26. * Directory to perform the tests in.
  27. *
  28. * @var string
  29. */
  30. protected $fixturesDir;
  31. /**
  32. * The Symfony FileSystem component.
  33. *
  34. * @var \Symfony\Component\Filesystem\Filesystem
  35. */
  36. protected $fileSystem;
  37. /**
  38. * The Fixtures object.
  39. *
  40. * @var \Drupal\Tests\Composer\Plugin\Scaffold\Fixtures
  41. */
  42. protected $fixtures;
  43. /**
  44. * {@inheritdoc}
  45. */
  46. protected function setUp(): void {
  47. $this->fileSystem = new Filesystem();
  48. $this->fixtures = new Fixtures();
  49. $this->fixtures->createIsolatedComposerCacheDir();
  50. $this->fixturesDir = $this->fixtures->tmpDir($this->getName());
  51. $replacements = ['SYMLINK' => 'false', 'PROJECT_ROOT' => $this->fixtures->projectRoot()];
  52. $this->fixtures->cloneFixtureProjects($this->fixturesDir, $replacements);
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. protected function tearDown(): void {
  58. // Remove any temporary directories et. al. that were created.
  59. $this->fixtures->tearDown();
  60. }
  61. /**
  62. * Tests to see if scaffold operation runs at the correct times.
  63. */
  64. public function testComposerHooks() {
  65. $topLevelProjectDir = 'composer-hooks-fixture';
  66. $sut = $this->fixturesDir . '/' . $topLevelProjectDir;
  67. // First test: run composer install. This is the same as composer update
  68. // since there is no lock file. Ensure that scaffold operation ran.
  69. $this->mustExec("composer install --no-ansi", $sut);
  70. $this->assertScaffoldedFile($sut . '/sites/default/default.settings.php', FALSE, 'Test version of default.settings.php from drupal/core');
  71. // Run composer required to add in the scaffold-override-fixture. This
  72. // project is "allowed" in our main fixture project, but not required.
  73. // We expect that requiring this library should re-scaffold, resulting
  74. // in a changed default.settings.php file.
  75. $stdout = $this->mustExec("composer require --no-ansi --no-interaction fixtures/drupal-assets-fixture:dev-master fixtures/scaffold-override-fixture:dev-master", $sut);
  76. $this->assertScaffoldedFile($sut . '/sites/default/default.settings.php', FALSE, 'scaffolded from the scaffold-override-fixture');
  77. // Make sure that the appropriate notice informing us that scaffolding
  78. // is allowed was printed.
  79. $this->assertStringContainsString('Package fixtures/scaffold-override-fixture has scaffold operations, and is already allowed in the root-level composer.json file.', $stdout);
  80. // Delete one scaffold file, just for test purposes, then run
  81. // 'composer update' and see if the scaffold file is replaced.
  82. @unlink($sut . '/sites/default/default.settings.php');
  83. $this->assertFileDoesNotExist($sut . '/sites/default/default.settings.php');
  84. $this->mustExec("composer update --no-ansi", $sut);
  85. $this->assertScaffoldedFile($sut . '/sites/default/default.settings.php', FALSE, 'scaffolded from the scaffold-override-fixture');
  86. // Delete the same test scaffold file again, then run
  87. // 'composer drupal:scaffold' and see if the scaffold file is
  88. // re-scaffolded.
  89. @unlink($sut . '/sites/default/default.settings.php');
  90. $this->assertFileDoesNotExist($sut . '/sites/default/default.settings.php');
  91. $this->mustExec("composer install --no-ansi", $sut);
  92. $this->assertScaffoldedFile($sut . '/sites/default/default.settings.php', FALSE, 'scaffolded from the scaffold-override-fixture');
  93. // Delete the same test scaffold file yet again, then run
  94. // 'composer install' and see if the scaffold file is re-scaffolded.
  95. @unlink($sut . '/sites/default/default.settings.php');
  96. $this->assertFileDoesNotExist($sut . '/sites/default/default.settings.php');
  97. $this->mustExec("composer drupal:scaffold --no-ansi", $sut);
  98. $this->assertScaffoldedFile($sut . '/sites/default/default.settings.php', FALSE, 'scaffolded from the scaffold-override-fixture');
  99. // Run 'composer create-project' to create a new test project called
  100. // 'create-project-test', which is a copy of 'fixtures/drupal-drupal'.
  101. $sut = $this->fixturesDir . '/create-project-test';
  102. $filesystem = new Filesystem();
  103. $filesystem->remove($sut);
  104. $stdout = $this->mustExec("composer create-project --repository=packages.json fixtures/drupal-drupal {$sut}", $this->fixturesDir, ['COMPOSER_MIRROR_PATH_REPOS' => 1]);
  105. $this->assertDirectoryExists($sut);
  106. $this->assertStringContainsString('Scaffolding files for fixtures/drupal-drupal', $stdout);
  107. $this->assertScaffoldedFile($sut . '/index.php', FALSE, 'Test version of index.php from drupal/core');
  108. $topLevelProjectDir = 'composer-hooks-nothing-allowed-fixture';
  109. $sut = $this->fixturesDir . '/' . $topLevelProjectDir;
  110. // Run composer install on an empty project.
  111. $this->mustExec("composer install --no-ansi", $sut);
  112. // Require a project that is not allowed to scaffold and confirm that we
  113. // get a warning, and it does not scaffold.
  114. $stdout = $this->mustExec("composer require --no-ansi --no-interaction fixtures/drupal-assets-fixture:dev-master fixtures/scaffold-override-fixture:dev-master", $sut);
  115. $this->assertFileDoesNotExist($sut . '/sites/default/default.settings.php');
  116. $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);
  117. }
  118. /**
  119. * Tests to see if scaffold messages are omitted when running scaffold twice.
  120. */
  121. public function testScaffoldMessagesDoNotPrintTwice() {
  122. $topLevelProjectDir = 'drupal-drupal';
  123. $sut = $this->fixturesDir . '/' . $topLevelProjectDir;
  124. // First test: run composer install. This is the same as composer update
  125. // since there is no lock file. Ensure that scaffold operation ran.
  126. $stdout = $this->mustExec("composer install --no-ansi", $sut);
  127. $this->assertStringContainsString('- Copy [web-root]/index.php from assets/index.php', $stdout);
  128. $this->assertStringContainsString('- Copy [web-root]/update.php from assets/update.php', $stdout);
  129. // Run scaffold operation again. It should not print anything.
  130. $stdout = $this->mustExec("composer scaffold --no-ansi", $sut);
  131. $this->assertEquals('', $stdout);
  132. // Delete a file and run it again. It should re-scaffold the removed file.
  133. unlink("$sut/index.php");
  134. $stdout = $this->mustExec("composer scaffold --no-ansi", $sut);
  135. $this->assertStringContainsString('- Copy [web-root]/index.php from assets/index.php', $stdout);
  136. $this->assertStringNotContainsString('- Copy [web-root]/update.php from assets/update.php', $stdout);
  137. }
  138. }