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

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
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@0 69 ];
Chris@0 70 }
Chris@0 71
Chris@0 72 /**
Chris@0 73 * Tests composer.json.
Chris@0 74 */
Chris@0 75 public function testComposerJson() {
Chris@0 76 foreach ($this->getPaths() as $path) {
Chris@0 77 $json = file_get_contents($path . '/composer.json');
Chris@0 78 $result = json_decode($json);
Chris@0 79 $this->assertNotNull($result, $this->getErrorMessages()[json_last_error()]);
Chris@0 80 }
Chris@0 81 }
Chris@0 82
Chris@0 83 /**
Chris@0 84 * Tests composer.lock content-hash.
Chris@0 85 */
Chris@0 86 public function testComposerLockHash() {
Chris@0 87 $content_hash = self::getContentHash(file_get_contents($this->root . '/composer.json'));
Chris@0 88 $lock = json_decode(file_get_contents($this->root . '/composer.lock'), TRUE);
Chris@0 89 $this->assertSame($content_hash, $lock['content-hash']);
Chris@0 90 }
Chris@0 91
Chris@0 92 /**
Chris@0 93 * Tests composer.json versions.
Chris@0 94 *
Chris@0 95 * @param string $path
Chris@0 96 * Path to a composer.json to test.
Chris@0 97 *
Chris@0 98 * @dataProvider providerTestComposerJson
Chris@0 99 */
Chris@0 100 public function testComposerTilde($path) {
Chris@0 101 $content = json_decode(file_get_contents($path), TRUE);
Chris@0 102 $composer_keys = array_intersect(['require', 'require-dev'], array_keys($content));
Chris@0 103 if (empty($composer_keys)) {
Chris@0 104 $this->markTestSkipped("$path has no keys to test");
Chris@0 105 }
Chris@0 106 foreach ($composer_keys as $composer_key) {
Chris@0 107 foreach ($content[$composer_key] as $dependency => $version) {
Chris@0 108 // We allow tildes if the dependency is a Symfony component.
Chris@0 109 // @see https://www.drupal.org/node/2887000
Chris@0 110 if (strpos($dependency, 'symfony/') === 0) {
Chris@0 111 continue;
Chris@0 112 }
Chris@0 113 $this->assertFalse(strpos($version, '~'), "Dependency $dependency in $path contains a tilde, use a caret.");
Chris@0 114 }
Chris@0 115 }
Chris@0 116 }
Chris@0 117
Chris@0 118 /**
Chris@0 119 * Data provider for all the composer.json provided by Drupal core.
Chris@0 120 *
Chris@0 121 * @return array
Chris@0 122 */
Chris@0 123 public function providerTestComposerJson() {
Chris@0 124 $root = realpath(__DIR__ . '/../../../../');
Chris@0 125 $tests = [[$root . '/composer.json']];
Chris@0 126 $directory = new \RecursiveDirectoryIterator($root . '/core');
Chris@0 127 $iterator = new \RecursiveIteratorIterator($directory);
Chris@0 128 /** @var \SplFileInfo $file */
Chris@0 129 foreach ($iterator as $file) {
Chris@0 130 if ($file->getFilename() === 'composer.json' && strpos($file->getPath(), 'core/modules/system/tests/fixtures/HtaccessTest') === FALSE) {
Chris@0 131 $tests[] = [$file->getRealPath()];
Chris@0 132 }
Chris@0 133 }
Chris@0 134 return $tests;
Chris@0 135 }
Chris@0 136
Chris@0 137 /**
Chris@0 138 * Tests core's composer.json replace section.
Chris@0 139 *
Chris@0 140 * Verify that all core modules are also listed in the 'replace' section of
Chris@0 141 * core's composer.json.
Chris@0 142 */
Chris@0 143 public function testAllModulesReplaced() {
Chris@0 144 // Assemble a path to core modules.
Chris@0 145 $module_path = $this->root . '/core/modules';
Chris@0 146
Chris@0 147 // Grab the 'replace' section of the core composer.json file.
Chris@0 148 $json = json_decode(file_get_contents($this->root . '/core/composer.json'));
Chris@0 149 $composer_replace_packages = (array) $json->replace;
Chris@0 150
Chris@0 151 // Get a list of all the files in the module path.
Chris@0 152 $folders = scandir($module_path);
Chris@0 153
Chris@0 154 // Make sure we only deal with directories that aren't . or ..
Chris@0 155 $module_names = [];
Chris@0 156 $discard = ['.', '..'];
Chris@0 157 foreach ($folders as $file_name) {
Chris@0 158 if ((!in_array($file_name, $discard)) && is_dir($module_path . '/' . $file_name)) {
Chris@0 159 $module_names[] = $file_name;
Chris@0 160 }
Chris@0 161 }
Chris@0 162
Chris@0 163 // Assert that each core module has a corresponding 'replace' in
Chris@0 164 // composer.json.
Chris@0 165 foreach ($module_names as $module_name) {
Chris@0 166 $this->assertArrayHasKey(
Chris@0 167 'drupal/' . $module_name,
Chris@0 168 $composer_replace_packages,
Chris@0 169 'Unable to find ' . $module_name . ' in replace list of composer.json'
Chris@0 170 );
Chris@0 171 }
Chris@0 172 }
Chris@0 173
Chris@0 174 // @codingStandardsIgnoreStart
Chris@0 175 /**
Chris@0 176 * The following method is copied from \Composer\Package\Locker.
Chris@0 177 *
Chris@0 178 * @see https://github.com/composer/composer
Chris@0 179 */
Chris@0 180 /**
Chris@0 181 * Returns the md5 hash of the sorted content of the composer file.
Chris@0 182 *
Chris@0 183 * @param string $composerFileContents The contents of the composer file.
Chris@0 184 *
Chris@0 185 * @return string
Chris@0 186 */
Chris@0 187 protected static function getContentHash($composerFileContents)
Chris@0 188 {
Chris@0 189 $content = json_decode($composerFileContents, true);
Chris@0 190
Chris@0 191 $relevantKeys = array(
Chris@0 192 'name',
Chris@0 193 'version',
Chris@0 194 'require',
Chris@0 195 'require-dev',
Chris@0 196 'conflict',
Chris@0 197 'replace',
Chris@0 198 'provide',
Chris@0 199 'minimum-stability',
Chris@0 200 'prefer-stable',
Chris@0 201 'repositories',
Chris@0 202 'extra',
Chris@0 203 );
Chris@0 204
Chris@0 205 $relevantContent = array();
Chris@0 206
Chris@0 207 foreach (array_intersect($relevantKeys, array_keys($content)) as $key) {
Chris@0 208 $relevantContent[$key] = $content[$key];
Chris@0 209 }
Chris@0 210 if (isset($content['config']['platform'])) {
Chris@0 211 $relevantContent['config']['platform'] = $content['config']['platform'];
Chris@0 212 }
Chris@0 213
Chris@0 214 ksort($relevantContent);
Chris@0 215
Chris@0 216 return md5(json_encode($relevantContent));
Chris@0 217 }
Chris@0 218 // @codingStandardsIgnoreEnd
Chris@0 219
Chris@0 220 }