annotate core/tests/Drupal/Tests/ComposerIntegrationTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests;
Chris@0 4
Chris@0 5 /**
Chris@0 6 * Tests Composer integration.
Chris@0 7 *
Chris@0 8 * @group Composer
Chris@0 9 */
Chris@0 10 class ComposerIntegrationTest extends UnitTestCase {
Chris@0 11
Chris@0 12 /**
Chris@0 13 * Gets human-readable JSON error messages.
Chris@0 14 *
Chris@0 15 * @return string[]
Chris@0 16 * Keys are JSON_ERROR_* constants.
Chris@0 17 */
Chris@0 18 protected function getErrorMessages() {
Chris@0 19 $messages = [
Chris@0 20 0 => 'No errors found',
Chris@0 21 JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
Chris@0 22 JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON',
Chris@0 23 JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
Chris@0 24 JSON_ERROR_SYNTAX => 'Syntax error',
Chris@0 25 JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded',
Chris@0 26 ];
Chris@0 27
Chris@0 28 if (version_compare(phpversion(), '5.5.0', '>=')) {
Chris@0 29 $messages[JSON_ERROR_RECURSION] = 'One or more recursive references in the value to be encoded';
Chris@0 30 $messages[JSON_ERROR_INF_OR_NAN] = 'One or more NAN or INF values in the value to be encoded';
Chris@0 31 $messages[JSON_ERROR_UNSUPPORTED_TYPE] = 'A value of a type that cannot be encoded was given';
Chris@0 32 }
Chris@0 33
Chris@0 34 return $messages;
Chris@0 35 }
Chris@0 36
Chris@0 37 /**
Chris@0 38 * Gets the paths to the folders that contain the Composer integration.
Chris@0 39 *
Chris@0 40 * @return string[]
Chris@0 41 * The paths.
Chris@0 42 */
Chris@0 43 protected function getPaths() {
Chris@0 44 return [
Chris@0 45 $this->root,
Chris@0 46 $this->root . '/core',
Chris@0 47 $this->root . '/core/lib/Drupal/Component/Annotation',
Chris@0 48 $this->root . '/core/lib/Drupal/Component/Assertion',
Chris@0 49 $this->root . '/core/lib/Drupal/Component/Bridge',
Chris@0 50 $this->root . '/core/lib/Drupal/Component/ClassFinder',
Chris@0 51 $this->root . '/core/lib/Drupal/Component/Datetime',
Chris@0 52 $this->root . '/core/lib/Drupal/Component/DependencyInjection',
Chris@0 53 $this->root . '/core/lib/Drupal/Component/Diff',
Chris@0 54 $this->root . '/core/lib/Drupal/Component/Discovery',
Chris@0 55 $this->root . '/core/lib/Drupal/Component/EventDispatcher',
Chris@0 56 $this->root . '/core/lib/Drupal/Component/FileCache',
Chris@0 57 $this->root . '/core/lib/Drupal/Component/FileSystem',
Chris@0 58 $this->root . '/core/lib/Drupal/Component/Gettext',
Chris@0 59 $this->root . '/core/lib/Drupal/Component/Graph',
Chris@0 60 $this->root . '/core/lib/Drupal/Component/HttpFoundation',
Chris@0 61 $this->root . '/core/lib/Drupal/Component/PhpStorage',
Chris@0 62 $this->root . '/core/lib/Drupal/Component/Plugin',
Chris@0 63 $this->root . '/core/lib/Drupal/Component/ProxyBuilder',
Chris@0 64 $this->root . '/core/lib/Drupal/Component/Render',
Chris@0 65 $this->root . '/core/lib/Drupal/Component/Serialization',
Chris@0 66 $this->root . '/core/lib/Drupal/Component/Transliteration',
Chris@0 67 $this->root . '/core/lib/Drupal/Component/Utility',
Chris@0 68 $this->root . '/core/lib/Drupal/Component/Uuid',
Chris@18 69 $this->root . '/core/lib/Drupal/Component/Version',
Chris@0 70 ];
Chris@0 71 }
Chris@0 72
Chris@0 73 /**
Chris@0 74 * Tests composer.json.
Chris@0 75 */
Chris@0 76 public function testComposerJson() {
Chris@0 77 foreach ($this->getPaths() as $path) {
Chris@0 78 $json = file_get_contents($path . '/composer.json');
Chris@0 79 $result = json_decode($json);
Chris@0 80 $this->assertNotNull($result, $this->getErrorMessages()[json_last_error()]);
Chris@0 81 }
Chris@0 82 }
Chris@0 83
Chris@0 84 /**
Chris@0 85 * Tests composer.lock content-hash.
Chris@0 86 */
Chris@0 87 public function testComposerLockHash() {
Chris@0 88 $content_hash = self::getContentHash(file_get_contents($this->root . '/composer.json'));
Chris@0 89 $lock = json_decode(file_get_contents($this->root . '/composer.lock'), TRUE);
Chris@0 90 $this->assertSame($content_hash, $lock['content-hash']);
Chris@0 91 }
Chris@0 92
Chris@0 93 /**
Chris@0 94 * Tests composer.json versions.
Chris@0 95 *
Chris@0 96 * @param string $path
Chris@0 97 * Path to a composer.json to test.
Chris@0 98 *
Chris@0 99 * @dataProvider providerTestComposerJson
Chris@0 100 */
Chris@0 101 public function testComposerTilde($path) {
Chris@0 102 $content = json_decode(file_get_contents($path), TRUE);
Chris@0 103 $composer_keys = array_intersect(['require', 'require-dev'], array_keys($content));
Chris@0 104 if (empty($composer_keys)) {
Chris@0 105 $this->markTestSkipped("$path has no keys to test");
Chris@0 106 }
Chris@0 107 foreach ($composer_keys as $composer_key) {
Chris@0 108 foreach ($content[$composer_key] as $dependency => $version) {
Chris@0 109 // We allow tildes if the dependency is a Symfony component.
Chris@0 110 // @see https://www.drupal.org/node/2887000
Chris@0 111 if (strpos($dependency, 'symfony/') === 0) {
Chris@0 112 continue;
Chris@0 113 }
Chris@0 114 $this->assertFalse(strpos($version, '~'), "Dependency $dependency in $path contains a tilde, use a caret.");
Chris@0 115 }
Chris@0 116 }
Chris@0 117 }
Chris@0 118
Chris@0 119 /**
Chris@0 120 * Data provider for all the composer.json provided by Drupal core.
Chris@0 121 *
Chris@0 122 * @return array
Chris@0 123 */
Chris@0 124 public function providerTestComposerJson() {
Chris@0 125 $root = realpath(__DIR__ . '/../../../../');
Chris@0 126 $tests = [[$root . '/composer.json']];
Chris@0 127 $directory = new \RecursiveDirectoryIterator($root . '/core');
Chris@0 128 $iterator = new \RecursiveIteratorIterator($directory);
Chris@0 129 /** @var \SplFileInfo $file */
Chris@0 130 foreach ($iterator as $file) {
Chris@0 131 if ($file->getFilename() === 'composer.json' && strpos($file->getPath(), 'core/modules/system/tests/fixtures/HtaccessTest') === FALSE) {
Chris@0 132 $tests[] = [$file->getRealPath()];
Chris@0 133 }
Chris@0 134 }
Chris@0 135 return $tests;
Chris@0 136 }
Chris@0 137
Chris@0 138 /**
Chris@0 139 * Tests core's composer.json replace section.
Chris@0 140 *
Chris@0 141 * Verify that all core modules are also listed in the 'replace' section of
Chris@0 142 * core's composer.json.
Chris@0 143 */
Chris@0 144 public function testAllModulesReplaced() {
Chris@0 145 // Assemble a path to core modules.
Chris@0 146 $module_path = $this->root . '/core/modules';
Chris@0 147
Chris@0 148 // Grab the 'replace' section of the core composer.json file.
Chris@0 149 $json = json_decode(file_get_contents($this->root . '/core/composer.json'));
Chris@0 150 $composer_replace_packages = (array) $json->replace;
Chris@0 151
Chris@0 152 // Get a list of all the files in the module path.
Chris@0 153 $folders = scandir($module_path);
Chris@0 154
Chris@0 155 // Make sure we only deal with directories that aren't . or ..
Chris@0 156 $module_names = [];
Chris@0 157 $discard = ['.', '..'];
Chris@0 158 foreach ($folders as $file_name) {
Chris@0 159 if ((!in_array($file_name, $discard)) && is_dir($module_path . '/' . $file_name)) {
Chris@0 160 $module_names[] = $file_name;
Chris@0 161 }
Chris@0 162 }
Chris@0 163
Chris@0 164 // Assert that each core module has a corresponding 'replace' in
Chris@0 165 // composer.json.
Chris@0 166 foreach ($module_names as $module_name) {
Chris@0 167 $this->assertArrayHasKey(
Chris@0 168 'drupal/' . $module_name,
Chris@0 169 $composer_replace_packages,
Chris@0 170 'Unable to find ' . $module_name . ' in replace list of composer.json'
Chris@0 171 );
Chris@0 172 }
Chris@0 173 }
Chris@0 174
Chris@0 175 // @codingStandardsIgnoreStart
Chris@0 176 /**
Chris@0 177 * The following method is copied from \Composer\Package\Locker.
Chris@0 178 *
Chris@0 179 * @see https://github.com/composer/composer
Chris@0 180 */
Chris@0 181 /**
Chris@0 182 * Returns the md5 hash of the sorted content of the composer file.
Chris@0 183 *
Chris@0 184 * @param string $composerFileContents The contents of the composer file.
Chris@0 185 *
Chris@0 186 * @return string
Chris@0 187 */
Chris@0 188 protected static function getContentHash($composerFileContents)
Chris@0 189 {
Chris@0 190 $content = json_decode($composerFileContents, true);
Chris@0 191
Chris@0 192 $relevantKeys = array(
Chris@0 193 'name',
Chris@0 194 'version',
Chris@0 195 'require',
Chris@0 196 'require-dev',
Chris@0 197 'conflict',
Chris@0 198 'replace',
Chris@0 199 'provide',
Chris@0 200 'minimum-stability',
Chris@0 201 'prefer-stable',
Chris@0 202 'repositories',
Chris@0 203 'extra',
Chris@0 204 );
Chris@0 205
Chris@0 206 $relevantContent = array();
Chris@0 207
Chris@0 208 foreach (array_intersect($relevantKeys, array_keys($content)) as $key) {
Chris@0 209 $relevantContent[$key] = $content[$key];
Chris@0 210 }
Chris@0 211 if (isset($content['config']['platform'])) {
Chris@0 212 $relevantContent['config']['platform'] = $content['config']['platform'];
Chris@0 213 }
Chris@0 214
Chris@0 215 ksort($relevantContent);
Chris@0 216
Chris@0 217 return md5(json_encode($relevantContent));
Chris@0 218 }
Chris@0 219 // @codingStandardsIgnoreEnd
Chris@0 220
Chris@0 221 }