ComposerIntegrationTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. namespace Drupal\Tests;
  3. use Drupal\Tests\Composer\ComposerIntegrationTrait;
  4. /**
  5. * Tests Composer integration.
  6. *
  7. * @group Composer
  8. */
  9. class ComposerIntegrationTest extends UnitTestCase {
  10. use ComposerIntegrationTrait;
  11. /**
  12. * Tests composer.lock content-hash.
  13. */
  14. public function testComposerLockHash() {
  15. $content_hash = self::getContentHash(file_get_contents($this->root . '/composer.json'));
  16. $lock = json_decode(file_get_contents($this->root . '/composer.lock'), TRUE);
  17. $this->assertSame($content_hash, $lock['content-hash']);
  18. // @see \Composer\Repository\PathRepository::initialize()
  19. $core_lock_file_hash = '';
  20. $options = [];
  21. foreach ($lock['packages'] as $package) {
  22. if ($package['name'] === 'drupal/core') {
  23. $core_lock_file_hash = $package['dist']['reference'];
  24. $options = $package['transport-options'] ?? [];
  25. break;
  26. }
  27. }
  28. $core_content_hash = sha1(file_get_contents($this->root . '/core/composer.json') . serialize($options));
  29. $this->assertSame($core_content_hash, $core_lock_file_hash);
  30. }
  31. /**
  32. * Tests composer.json versions.
  33. *
  34. * @param string $path
  35. * Path to a composer.json to test.
  36. *
  37. * @dataProvider providerTestComposerJson
  38. */
  39. public function testComposerTilde($path) {
  40. $content = json_decode(file_get_contents($path), TRUE);
  41. $composer_keys = array_intersect(['require', 'require-dev'], array_keys($content));
  42. if (empty($composer_keys)) {
  43. $this->markTestSkipped("$path has no keys to test");
  44. }
  45. foreach ($composer_keys as $composer_key) {
  46. foreach ($content[$composer_key] as $dependency => $version) {
  47. // We allow tildes if the dependency is a Symfony component.
  48. // @see https://www.drupal.org/node/2887000
  49. if (strpos($dependency, 'symfony/') === 0) {
  50. continue;
  51. }
  52. $this->assertStringNotContainsString('~', $version, "Dependency $dependency in $path contains a tilde, use a caret.");
  53. }
  54. }
  55. }
  56. /**
  57. * Data provider for all the composer.json provided by Drupal core.
  58. *
  59. * @return array
  60. */
  61. public function providerTestComposerJson() {
  62. $data = [];
  63. $composer_json_finder = $this->getComposerJsonFinder(realpath(__DIR__ . '/../../../../'));
  64. foreach ($composer_json_finder->getIterator() as $composer_json) {
  65. $data[] = [$composer_json->getPathname()];
  66. }
  67. return $data;
  68. }
  69. /**
  70. * Tests core's composer.json replace section.
  71. *
  72. * Verify that all core modules are also listed in the 'replace' section of
  73. * core's composer.json.
  74. */
  75. public function testAllModulesReplaced() {
  76. // Assemble a path to core modules.
  77. $module_path = $this->root . '/core/modules';
  78. // Grab the 'replace' section of the core composer.json file.
  79. $json = json_decode(file_get_contents($this->root . '/core/composer.json'));
  80. $composer_replace_packages = (array) $json->replace;
  81. // Get a list of all the files in the module path.
  82. $folders = scandir($module_path);
  83. // Make sure we only deal with directories that aren't . or ..
  84. $module_names = [];
  85. $discard = ['.', '..'];
  86. foreach ($folders as $file_name) {
  87. if ((!in_array($file_name, $discard)) && is_dir($module_path . '/' . $file_name)) {
  88. $module_names[] = $file_name;
  89. }
  90. }
  91. // Assert that each core module has a corresponding 'replace' in
  92. // composer.json.
  93. foreach ($module_names as $module_name) {
  94. $this->assertArrayHasKey(
  95. 'drupal/' . $module_name,
  96. $composer_replace_packages,
  97. 'Unable to find ' . $module_name . ' in replace list of composer.json'
  98. );
  99. }
  100. }
  101. /**
  102. * Data provider for the scaffold files test for Drupal core.
  103. *
  104. * @return array
  105. */
  106. public function providerTestExpectedScaffoldFiles() {
  107. return [
  108. ['.editorconfig', 'assets/scaffold/files/editorconfig', '[project-root]'],
  109. ['.gitattributes', 'assets/scaffold/files/gitattributes', '[project-root]'],
  110. ['.csslintrc', 'assets/scaffold/files/csslintrc'],
  111. ['.eslintignore', 'assets/scaffold/files/eslintignore'],
  112. ['.eslintrc.json', 'assets/scaffold/files/eslintrc.json'],
  113. ['.ht.router.php', 'assets/scaffold/files/ht.router.php'],
  114. ['.htaccess', 'assets/scaffold/files/htaccess'],
  115. ['example.gitignore', 'assets/scaffold/files/example.gitignore'],
  116. ['index.php', 'assets/scaffold/files/index.php'],
  117. ['INSTALL.txt', 'assets/scaffold/files/drupal.INSTALL.txt'],
  118. ['README.txt', 'assets/scaffold/files/drupal.README.txt'],
  119. ['robots.txt', 'assets/scaffold/files/robots.txt'],
  120. ['update.php', 'assets/scaffold/files/update.php'],
  121. ['web.config', 'assets/scaffold/files/web.config'],
  122. ['sites/README.txt', 'assets/scaffold/files/sites.README.txt'],
  123. ['sites/development.services.yml', 'assets/scaffold/files/development.services.yml'],
  124. ['sites/example.settings.local.php', 'assets/scaffold/files/example.settings.local.php'],
  125. ['sites/example.sites.php', 'assets/scaffold/files/example.sites.php'],
  126. ['sites/default/default.services.yml', 'assets/scaffold/files/default.services.yml'],
  127. ['sites/default/default.settings.php', 'assets/scaffold/files/default.settings.php'],
  128. ['modules/README.txt', 'assets/scaffold/files/modules.README.txt'],
  129. ['profiles/README.txt', 'assets/scaffold/files/profiles.README.txt'],
  130. ['themes/README.txt', 'assets/scaffold/files/themes.README.txt'],
  131. ];
  132. }
  133. /**
  134. * Tests core's composer.json extra drupal-scaffold file-mappings section.
  135. *
  136. * Verify that every file listed in file-mappings exists in its destination
  137. * path (mapping key) and also at its source path (mapping value), and that
  138. * both of these files have exactly the same content.
  139. *
  140. * In Drupal 9, the files at the destination path will be removed. For the
  141. * remainder of the Drupal 8 development cycle, these files will remain in
  142. * order to maintain backwards compatibility with sites based on the template
  143. * project drupal-composer/drupal-project.
  144. *
  145. * See https://www.drupal.org/project/drupal/issues/3075954
  146. *
  147. * @param string $destRelPath
  148. * Path to scaffold file destination location
  149. * @param string $sourceRelPath
  150. * Path to scaffold file source location
  151. * @param string $expectedDestination
  152. * Named location to the destination path of the scaffold file
  153. *
  154. * @dataProvider providerTestExpectedScaffoldFiles
  155. */
  156. public function testExpectedScaffoldFiles($destRelPath, $sourceRelPath, $expectedDestination = '[web-root]') {
  157. // Grab the 'file-mapping' section of the core composer.json file.
  158. $json = json_decode(file_get_contents($this->root . '/core/composer.json'));
  159. $scaffold_file_mapping = (array) $json->extra->{'drupal-scaffold'}->{'file-mapping'};
  160. // Assert that the 'file-mapping' section has the expected entry.
  161. $this->assertArrayHasKey("$expectedDestination/$destRelPath", $scaffold_file_mapping);
  162. $this->assertEquals($sourceRelPath, $scaffold_file_mapping["$expectedDestination/$destRelPath"]);
  163. // Assert that the source file exists.
  164. $this->assertFileExists($this->root . '/core/' . $sourceRelPath);
  165. // Assert that the destination file exists and has the same contents as
  166. // the source file. Note that in Drupal 9, the destination file will be
  167. // removed.
  168. $this->assertFileExists($this->root . '/' . $destRelPath);
  169. $this->assertFileEquals($this->root . '/core/' . $sourceRelPath, $this->root . '/' . $destRelPath, 'Scaffold source and destination files must have the same contents.');
  170. }
  171. // @codingStandardsIgnoreStart
  172. /**
  173. * The following method is copied from \Composer\Package\Locker.
  174. *
  175. * @see https://github.com/composer/composer
  176. */
  177. /**
  178. * Returns the md5 hash of the sorted content of the composer file.
  179. *
  180. * @param string $composerFileContents The contents of the composer file.
  181. *
  182. * @return string
  183. */
  184. protected static function getContentHash($composerFileContents)
  185. {
  186. $content = json_decode($composerFileContents, true);
  187. $relevantKeys = array(
  188. 'name',
  189. 'version',
  190. 'require',
  191. 'require-dev',
  192. 'conflict',
  193. 'replace',
  194. 'provide',
  195. 'minimum-stability',
  196. 'prefer-stable',
  197. 'repositories',
  198. 'extra',
  199. );
  200. $relevantContent = array();
  201. foreach (array_intersect($relevantKeys, array_keys($content)) as $key) {
  202. $relevantContent[$key] = $content[$key];
  203. }
  204. if (isset($content['config']['platform'])) {
  205. $relevantContent['config']['platform'] = $content['config']['platform'];
  206. }
  207. ksort($relevantContent);
  208. return md5(json_encode($relevantContent));
  209. }
  210. // @codingStandardsIgnoreEnd
  211. }