diff --git a/core/tests/Drupal/Tests/ComposerIntegrationTest.php b/core/tests/Drupal/Tests/ComposerIntegrationTest.php index 80a18b65137..a64804cf8b8 100644 --- a/core/tests/Drupal/Tests/ComposerIntegrationTest.php +++ b/core/tests/Drupal/Tests/ComposerIntegrationTest.php @@ -74,19 +74,18 @@ class ComposerIntegrationTest extends UnitTestCase { public function testComposerJson() { foreach ($this->getPaths() as $path) { $json = file_get_contents($path . '/composer.json'); - $result = json_decode($json); $this->assertNotNull($result, $this->getErrorMessages()[json_last_error()]); } } /** - * Tests composer.lock hash. + * Tests composer.lock content-hash. */ public function testComposerLockHash() { - $json = file_get_contents($this->root . '/composer.json'); + $content_hash = self::getContentHash(file_get_contents($this->root . '/composer.json')); $lock = json_decode(file_get_contents($this->root . '/composer.lock'), TRUE); - $this->assertSame(md5($json), $lock['hash']); + $this->assertSame($content_hash, $lock['content-hash']); } /** @@ -126,4 +125,50 @@ class ComposerIntegrationTest extends UnitTestCase { } } + // @codingStandardsIgnoreStart + /** + * The following method is copied from \Composer\Package\Locker. + * + * @see https://github.com/composer/composer + */ + /** + * Returns the md5 hash of the sorted content of the composer file. + * + * @param string $composerFileContents The contents of the composer file. + * + * @return string + */ + protected static function getContentHash($composerFileContents) + { + $content = json_decode($composerFileContents, true); + + $relevantKeys = array( + 'name', + 'version', + 'require', + 'require-dev', + 'conflict', + 'replace', + 'provide', + 'minimum-stability', + 'prefer-stable', + 'repositories', + 'extra', + ); + + $relevantContent = array(); + + foreach (array_intersect($relevantKeys, array_keys($content)) as $key) { + $relevantContent[$key] = $content[$key]; + } + if (isset($content['config']['platform'])) { + $relevantContent['config']['platform'] = $content['config']['platform']; + } + + ksort($relevantContent); + + return md5(json_encode($relevantContent)); + } + // @codingStandardsIgnoreEnd + }