Drupal investigation

ComposerIntegrationTest.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace Drupal\Tests;
  3. /**
  4. * Tests Composer integration.
  5. *
  6. * @group Composer
  7. */
  8. class ComposerIntegrationTest extends UnitTestCase {
  9. /**
  10. * Gets human-readable JSON error messages.
  11. *
  12. * @return string[]
  13. * Keys are JSON_ERROR_* constants.
  14. */
  15. protected function getErrorMessages() {
  16. $messages = [
  17. 0 => 'No errors found',
  18. JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
  19. JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON',
  20. JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
  21. JSON_ERROR_SYNTAX => 'Syntax error',
  22. JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded',
  23. ];
  24. if (version_compare(phpversion(), '5.5.0', '>=')) {
  25. $messages[JSON_ERROR_RECURSION] = 'One or more recursive references in the value to be encoded';
  26. $messages[JSON_ERROR_INF_OR_NAN] = 'One or more NAN or INF values in the value to be encoded';
  27. $messages[JSON_ERROR_UNSUPPORTED_TYPE] = 'A value of a type that cannot be encoded was given';
  28. }
  29. return $messages;
  30. }
  31. /**
  32. * Gets the paths to the folders that contain the Composer integration.
  33. *
  34. * @return string[]
  35. * The paths.
  36. */
  37. protected function getPaths() {
  38. return [
  39. $this->root,
  40. $this->root . '/core',
  41. $this->root . '/core/lib/Drupal/Component/Annotation',
  42. $this->root . '/core/lib/Drupal/Component/Assertion',
  43. $this->root . '/core/lib/Drupal/Component/Bridge',
  44. $this->root . '/core/lib/Drupal/Component/Datetime',
  45. $this->root . '/core/lib/Drupal/Component/DependencyInjection',
  46. $this->root . '/core/lib/Drupal/Component/Diff',
  47. $this->root . '/core/lib/Drupal/Component/Discovery',
  48. $this->root . '/core/lib/Drupal/Component/EventDispatcher',
  49. $this->root . '/core/lib/Drupal/Component/FileCache',
  50. $this->root . '/core/lib/Drupal/Component/FileSystem',
  51. $this->root . '/core/lib/Drupal/Component/Gettext',
  52. $this->root . '/core/lib/Drupal/Component/Graph',
  53. $this->root . '/core/lib/Drupal/Component/HttpFoundation',
  54. $this->root . '/core/lib/Drupal/Component/PhpStorage',
  55. $this->root . '/core/lib/Drupal/Component/Plugin',
  56. $this->root . '/core/lib/Drupal/Component/ProxyBuilder',
  57. $this->root . '/core/lib/Drupal/Component/Render',
  58. $this->root . '/core/lib/Drupal/Component/Serialization',
  59. $this->root . '/core/lib/Drupal/Component/Transliteration',
  60. $this->root . '/core/lib/Drupal/Component/Utility',
  61. $this->root . '/core/lib/Drupal/Component/Uuid',
  62. ];
  63. }
  64. /**
  65. * Tests composer.json.
  66. */
  67. public function testComposerJson() {
  68. foreach ($this->getPaths() as $path) {
  69. $json = file_get_contents($path . '/composer.json');
  70. $result = json_decode($json);
  71. $this->assertNotNull($result, $this->getErrorMessages()[json_last_error()]);
  72. }
  73. }
  74. /**
  75. * Tests composer.lock content-hash.
  76. */
  77. public function testComposerLockHash() {
  78. $content_hash = self::getContentHash(file_get_contents($this->root . '/composer.json'));
  79. $lock = json_decode(file_get_contents($this->root . '/composer.lock'), TRUE);
  80. $this->assertSame($content_hash, $lock['content-hash']);
  81. }
  82. /**
  83. * Tests core's composer.json replace section.
  84. *
  85. * Verify that all core modules are also listed in the 'replace' section of
  86. * core's composer.json.
  87. */
  88. public function testAllModulesReplaced() {
  89. // Assemble a path to core modules.
  90. $module_path = $this->root . '/core/modules';
  91. // Grab the 'replace' section of the core composer.json file.
  92. $json = json_decode(file_get_contents($this->root . '/core/composer.json'));
  93. $composer_replace_packages = (array) $json->replace;
  94. // Get a list of all the files in the module path.
  95. $folders = scandir($module_path);
  96. // Make sure we only deal with directories that aren't . or ..
  97. $module_names = [];
  98. $discard = ['.', '..'];
  99. foreach ($folders as $file_name) {
  100. if ((!in_array($file_name, $discard)) && is_dir($module_path . '/' . $file_name)) {
  101. $module_names[] = $file_name;
  102. }
  103. }
  104. // Assert that each core module has a corresponding 'replace' in
  105. // composer.json.
  106. foreach ($module_names as $module_name) {
  107. $this->assertArrayHasKey(
  108. 'drupal/' . $module_name,
  109. $composer_replace_packages,
  110. 'Unable to find ' . $module_name . ' in replace list of composer.json'
  111. );
  112. }
  113. }
  114. // @codingStandardsIgnoreStart
  115. /**
  116. * The following method is copied from \Composer\Package\Locker.
  117. *
  118. * @see https://github.com/composer/composer
  119. */
  120. /**
  121. * Returns the md5 hash of the sorted content of the composer file.
  122. *
  123. * @param string $composerFileContents The contents of the composer file.
  124. *
  125. * @return string
  126. */
  127. protected static function getContentHash($composerFileContents)
  128. {
  129. $content = json_decode($composerFileContents, true);
  130. $relevantKeys = array(
  131. 'name',
  132. 'version',
  133. 'require',
  134. 'require-dev',
  135. 'conflict',
  136. 'replace',
  137. 'provide',
  138. 'minimum-stability',
  139. 'prefer-stable',
  140. 'repositories',
  141. 'extra',
  142. );
  143. $relevantContent = array();
  144. foreach (array_intersect($relevantKeys, array_keys($content)) as $key) {
  145. $relevantContent[$key] = $content[$key];
  146. }
  147. if (isset($content['config']['platform'])) {
  148. $relevantContent['config']['platform'] = $content['config']['platform'];
  149. }
  150. ksort($relevantContent);
  151. return md5(json_encode($relevantContent));
  152. }
  153. // @codingStandardsIgnoreEnd
  154. }