annotate core/tests/Drupal/Tests/ComposerIntegrationTest.php @ 4:a9cd425dd02b

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