b56849d86313823c10dc84a49484a6a0e26121c1
[yaffs-website] / web / core / tests / Drupal / Tests / ComposerIntegrationTest.php
1 <?php
2
3 namespace Drupal\Tests;
4
5 /**
6  * Tests Composer integration.
7  *
8  * @group Composer
9  */
10 class ComposerIntegrationTest extends UnitTestCase {
11
12   /**
13    * Gets human-readable JSON error messages.
14    *
15    * @return string[]
16    *   Keys are JSON_ERROR_* constants.
17    */
18   protected function getErrorMessages() {
19     $messages = [
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',
26     ];
27
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';
32     }
33
34     return $messages;
35   }
36
37   /**
38    * Gets the paths to the folders that contain the Composer integration.
39    *
40    * @return string[]
41    *   The paths.
42    */
43   protected function getPaths() {
44     return [
45       $this->root,
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',
69     ];
70   }
71
72   /**
73    * Tests composer.json.
74    */
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()]);
80     }
81   }
82
83   /**
84    * Tests composer.lock content-hash.
85    */
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']);
90   }
91
92   /**
93    * Tests composer.json versions.
94    *
95    * @param string $path
96    *   Path to a composer.json to test.
97    *
98    * @dataProvider providerTestComposerJson
99    */
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");
105     }
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) {
111           continue;
112         }
113         $this->assertFalse(strpos($version, '~'), "Dependency $dependency in $path contains a tilde, use a caret.");
114       }
115     }
116   }
117
118   /**
119    * Data provider for all the composer.json provided by Drupal core.
120    *
121    * @return array
122    */
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()];
132       }
133     }
134     return $tests;
135   }
136
137   /**
138    * Tests core's composer.json replace section.
139    *
140    * Verify that all core modules are also listed in the 'replace' section of
141    * core's composer.json.
142    */
143   public function testAllModulesReplaced() {
144     // Assemble a path to core modules.
145     $module_path = $this->root . '/core/modules';
146
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;
150
151     // Get a list of all the files in the module path.
152     $folders = scandir($module_path);
153
154     // Make sure we only deal with directories that aren't . or ..
155     $module_names = [];
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;
160       }
161     }
162
163     // Assert that each core module has a corresponding 'replace' in
164     // composer.json.
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'
170       );
171     }
172   }
173
174   // @codingStandardsIgnoreStart
175   /**
176    * The following method is copied from \Composer\Package\Locker.
177    *
178    * @see https://github.com/composer/composer
179    */
180   /**
181    * Returns the md5 hash of the sorted content of the composer file.
182    *
183    * @param string $composerFileContents The contents of the composer file.
184    *
185    * @return string
186    */
187   protected static function getContentHash($composerFileContents)
188   {
189     $content = json_decode($composerFileContents, true);
190
191     $relevantKeys = array(
192       'name',
193       'version',
194       'require',
195       'require-dev',
196       'conflict',
197       'replace',
198       'provide',
199       'minimum-stability',
200       'prefer-stable',
201       'repositories',
202       'extra',
203     );
204
205     $relevantContent = array();
206
207     foreach (array_intersect($relevantKeys, array_keys($content)) as $key) {
208       $relevantContent[$key] = $content[$key];
209     }
210     if (isset($content['config']['platform'])) {
211       $relevantContent['config']['platform'] = $content['config']['platform'];
212     }
213
214     ksort($relevantContent);
215
216     return md5(json_encode($relevantContent));
217   }
218   // @codingStandardsIgnoreEnd
219
220 }