3 namespace Drupal\Tests;
6 * Tests Composer integration.
10 class ComposerIntegrationTest extends UnitTestCase {
13 * Gets human-readable JSON error messages.
16 * Keys are JSON_ERROR_* constants.
18 protected function getErrorMessages() {
20 0 => 'No errors found',
21 JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
22 JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON',
23 JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
24 JSON_ERROR_SYNTAX => 'Syntax error',
25 JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded',
28 if (version_compare(phpversion(), '5.5.0', '>=')) {
29 $messages[JSON_ERROR_RECURSION] = 'One or more recursive references in the value to be encoded';
30 $messages[JSON_ERROR_INF_OR_NAN] = 'One or more NAN or INF values in the value to be encoded';
31 $messages[JSON_ERROR_UNSUPPORTED_TYPE] = 'A value of a type that cannot be encoded was given';
38 * Gets the paths to the folders that contain the Composer integration.
43 protected function getPaths() {
46 $this->root . '/core',
47 $this->root . '/core/lib/Drupal/Component/Annotation',
48 $this->root . '/core/lib/Drupal/Component/Assertion',
49 $this->root . '/core/lib/Drupal/Component/Bridge',
50 $this->root . '/core/lib/Drupal/Component/ClassFinder',
51 $this->root . '/core/lib/Drupal/Component/Datetime',
52 $this->root . '/core/lib/Drupal/Component/DependencyInjection',
53 $this->root . '/core/lib/Drupal/Component/Diff',
54 $this->root . '/core/lib/Drupal/Component/Discovery',
55 $this->root . '/core/lib/Drupal/Component/EventDispatcher',
56 $this->root . '/core/lib/Drupal/Component/FileCache',
57 $this->root . '/core/lib/Drupal/Component/FileSystem',
58 $this->root . '/core/lib/Drupal/Component/Gettext',
59 $this->root . '/core/lib/Drupal/Component/Graph',
60 $this->root . '/core/lib/Drupal/Component/HttpFoundation',
61 $this->root . '/core/lib/Drupal/Component/PhpStorage',
62 $this->root . '/core/lib/Drupal/Component/Plugin',
63 $this->root . '/core/lib/Drupal/Component/ProxyBuilder',
64 $this->root . '/core/lib/Drupal/Component/Render',
65 $this->root . '/core/lib/Drupal/Component/Serialization',
66 $this->root . '/core/lib/Drupal/Component/Transliteration',
67 $this->root . '/core/lib/Drupal/Component/Utility',
68 $this->root . '/core/lib/Drupal/Component/Uuid',
73 * Tests composer.json.
75 public function testComposerJson() {
76 foreach ($this->getPaths() as $path) {
77 $json = file_get_contents($path . '/composer.json');
78 $result = json_decode($json);
79 $this->assertNotNull($result, $this->getErrorMessages()[json_last_error()]);
84 * Tests composer.lock content-hash.
86 public function testComposerLockHash() {
87 $content_hash = self::getContentHash(file_get_contents($this->root . '/composer.json'));
88 $lock = json_decode(file_get_contents($this->root . '/composer.lock'), TRUE);
89 $this->assertSame($content_hash, $lock['content-hash']);
93 * Tests composer.json versions.
96 * Path to a composer.json to test.
98 * @dataProvider providerTestComposerJson
100 public function testComposerTilde($path) {
101 $content = json_decode(file_get_contents($path), TRUE);
102 $composer_keys = array_intersect(['require', 'require-dev'], array_keys($content));
103 if (empty($composer_keys)) {
104 $this->markTestSkipped("$path has no keys to test");
106 foreach ($composer_keys as $composer_key) {
107 foreach ($content[$composer_key] as $dependency => $version) {
108 // We allow tildes if the dependency is a Symfony component.
109 // @see https://www.drupal.org/node/2887000
110 if (strpos($dependency, 'symfony/') === 0) {
113 $this->assertFalse(strpos($version, '~'), "Dependency $dependency in $path contains a tilde, use a caret.");
119 * Data provider for all the composer.json provided by Drupal core.
123 public function providerTestComposerJson() {
124 $root = realpath(__DIR__ . '/../../../../');
125 $tests = [[$root . '/composer.json']];
126 $directory = new \RecursiveDirectoryIterator($root . '/core');
127 $iterator = new \RecursiveIteratorIterator($directory);
128 /** @var \SplFileInfo $file */
129 foreach ($iterator as $file) {
130 if ($file->getFilename() === 'composer.json' && strpos($file->getPath(), 'core/modules/system/tests/fixtures/HtaccessTest') === FALSE) {
131 $tests[] = [$file->getRealPath()];
138 * Tests core's composer.json replace section.
140 * Verify that all core modules are also listed in the 'replace' section of
141 * core's composer.json.
143 public function testAllModulesReplaced() {
144 // Assemble a path to core modules.
145 $module_path = $this->root . '/core/modules';
147 // Grab the 'replace' section of the core composer.json file.
148 $json = json_decode(file_get_contents($this->root . '/core/composer.json'));
149 $composer_replace_packages = (array) $json->replace;
151 // Get a list of all the files in the module path.
152 $folders = scandir($module_path);
154 // Make sure we only deal with directories that aren't . or ..
156 $discard = ['.', '..'];
157 foreach ($folders as $file_name) {
158 if ((!in_array($file_name, $discard)) && is_dir($module_path . '/' . $file_name)) {
159 $module_names[] = $file_name;
163 // Assert that each core module has a corresponding 'replace' in
165 foreach ($module_names as $module_name) {
166 $this->assertArrayHasKey(
167 'drupal/' . $module_name,
168 $composer_replace_packages,
169 'Unable to find ' . $module_name . ' in replace list of composer.json'
174 // @codingStandardsIgnoreStart
176 * The following method is copied from \Composer\Package\Locker.
178 * @see https://github.com/composer/composer
181 * Returns the md5 hash of the sorted content of the composer file.
183 * @param string $composerFileContents The contents of the composer file.
187 protected static function getContentHash($composerFileContents)
189 $content = json_decode($composerFileContents, true);
191 $relevantKeys = array(
205 $relevantContent = array();
207 foreach (array_intersect($relevantKeys, array_keys($content)) as $key) {
208 $relevantContent[$key] = $content[$key];
210 if (isset($content['config']['platform'])) {
211 $relevantContent['config']['platform'] = $content['config']['platform'];
214 ksort($relevantContent);
216 return md5(json_encode($relevantContent));
218 // @codingStandardsIgnoreEnd