2015-08-17 17:00:26 -07:00
< ? php
namespace Drupal\Tests ;
/**
* Tests Composer integration .
*
* @ group Composer
*/
class ComposerIntegrationTest extends UnitTestCase {
/**
* Gets human - readable JSON error messages .
*
* @ return string []
* Keys are JSON_ERROR_ * constants .
*/
protected function getErrorMessages () {
$messages = [
2015-09-04 13:20:09 -07:00
0 => 'No errors found' ,
2015-08-17 17:00:26 -07:00
JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded' ,
JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON' ,
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded' ,
JSON_ERROR_SYNTAX => 'Syntax error' ,
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded' ,
];
if ( version_compare ( phpversion (), '5.5.0' , '>=' )) {
$messages [ JSON_ERROR_RECURSION ] = 'One or more recursive references in the value to be encoded' ;
$messages [ JSON_ERROR_INF_OR_NAN ] = 'One or more NAN or INF values in the value to be encoded' ;
$messages [ JSON_ERROR_UNSUPPORTED_TYPE ] = 'A value of a type that cannot be encoded was given' ;
}
return $messages ;
}
/**
* Gets the paths to the folders that contain the Composer integration .
*
* @ return string []
* The paths .
*/
protected function getPaths () {
return [
$this -> root ,
$this -> root . '/core' ,
2016-04-20 09:56:34 -07:00
$this -> root . '/core/lib/Drupal/Component/Annotation' ,
$this -> root . '/core/lib/Drupal/Component/Assertion' ,
$this -> root . '/core/lib/Drupal/Component/Bridge' ,
$this -> root . '/core/lib/Drupal/Component/Datetime' ,
2016-07-07 09:44:38 -07:00
$this -> root . '/core/lib/Drupal/Component/DependencyInjection' ,
2016-04-20 09:56:34 -07:00
$this -> root . '/core/lib/Drupal/Component/Diff' ,
$this -> root . '/core/lib/Drupal/Component/Discovery' ,
$this -> root . '/core/lib/Drupal/Component/EventDispatcher' ,
$this -> root . '/core/lib/Drupal/Component/FileCache' ,
2016-06-02 15:56:09 -07:00
$this -> root . '/core/lib/Drupal/Component/FileSystem' ,
2015-08-27 12:03:05 -07:00
$this -> root . '/core/lib/Drupal/Component/Gettext' ,
2016-04-20 09:56:34 -07:00
$this -> root . '/core/lib/Drupal/Component/Graph' ,
$this -> root . '/core/lib/Drupal/Component/HttpFoundation' ,
$this -> root . '/core/lib/Drupal/Component/PhpStorage' ,
2015-08-17 17:00:26 -07:00
$this -> root . '/core/lib/Drupal/Component/Plugin' ,
$this -> root . '/core/lib/Drupal/Component/ProxyBuilder' ,
2016-04-20 09:56:34 -07:00
$this -> root . '/core/lib/Drupal/Component/Render' ,
$this -> root . '/core/lib/Drupal/Component/Serialization' ,
$this -> root . '/core/lib/Drupal/Component/Transliteration' ,
2015-08-17 17:00:26 -07:00
$this -> root . '/core/lib/Drupal/Component/Utility' ,
2016-08-03 13:22:33 -07:00
$this -> root . '/core/lib/Drupal/Component/Uuid' ,
2015-08-17 17:00:26 -07:00
];
}
/**
* Tests composer . json .
*/
public function testComposerJson () {
foreach ( $this -> getPaths () as $path ) {
$json = file_get_contents ( $path . '/composer.json' );
$result = json_decode ( $json );
2015-09-04 13:20:09 -07:00
$this -> assertNotNull ( $result , $this -> getErrorMessages ()[ json_last_error ()]);
}
}
2016-05-04 14:35:41 -07:00
/**
2017-02-02 16:28:38 -08:00
* Tests composer . lock content - hash .
2016-05-04 14:35:41 -07:00
*/
public function testComposerLockHash () {
2017-02-02 16:28:38 -08:00
$content_hash = self :: getContentHash ( file_get_contents ( $this -> root . '/composer.json' ));
2016-05-04 14:35:41 -07:00
$lock = json_decode ( file_get_contents ( $this -> root . '/composer.lock' ), TRUE );
2017-02-02 16:28:38 -08:00
$this -> assertSame ( $content_hash , $lock [ 'content-hash' ]);
2016-05-04 14:35:41 -07:00
}
2015-09-04 13:20:09 -07:00
/**
* Tests core ' s composer . json replace section .
*
* Verify that all core modules are also listed in the 'replace' section of
* core ' s composer . json .
*/
public function testAllModulesReplaced () {
2015-11-04 11:11:27 -08:00
// Assemble a path to core modules.
$module_path = $this -> root . '/core/modules' ;
// Grab the 'replace' section of the core composer.json file.
2015-09-04 13:20:09 -07:00
$json = json_decode ( file_get_contents ( $this -> root . '/core/composer.json' ));
2015-11-04 11:11:27 -08:00
$composer_replace_packages = ( array ) $json -> replace ;
2015-09-04 13:20:09 -07:00
2015-11-04 11:11:27 -08:00
// Get a list of all the files in the module path.
$folders = scandir ( $module_path );
2015-09-04 13:20:09 -07:00
2015-11-04 11:11:27 -08:00
// Make sure we only deal with directories that aren't . or ..
2015-09-04 13:20:09 -07:00
$module_names = [];
2015-11-04 11:11:27 -08:00
$discard = [ '.' , '..' ];
2015-09-04 13:20:09 -07:00
foreach ( $folders as $file_name ) {
2015-11-04 11:11:27 -08:00
if (( ! in_array ( $file_name , $discard )) && is_dir ( $module_path . '/' . $file_name )) {
2015-09-04 13:20:09 -07:00
$module_names [] = $file_name ;
2015-08-17 17:00:26 -07:00
}
}
2015-09-04 13:20:09 -07:00
2015-11-04 11:11:27 -08:00
// Assert that each core module has a corresponding 'replace' in
// composer.json.
2015-09-04 13:20:09 -07:00
foreach ( $module_names as $module_name ) {
2015-11-04 11:11:27 -08:00
$this -> assertArrayHasKey (
'drupal/' . $module_name ,
$composer_replace_packages ,
'Unable to find ' . $module_name . ' in replace list of composer.json'
);
2015-09-04 13:20:09 -07:00
}
2015-08-17 17:00:26 -07:00
}
2017-02-02 16:28:38 -08:00
// @codingStandardsIgnoreStart
/**
* The following method is copied from \Composer\Package\Locker .
*
* @ see https :// github . com / composer / composer
*/
/**
* Returns the md5 hash of the sorted content of the composer file .
*
* @ param string $composerFileContents The contents of the composer file .
*
* @ return string
*/
protected static function getContentHash ( $composerFileContents )
{
$content = json_decode ( $composerFileContents , true );
$relevantKeys = array (
'name' ,
'version' ,
'require' ,
'require-dev' ,
'conflict' ,
'replace' ,
'provide' ,
'minimum-stability' ,
'prefer-stable' ,
'repositories' ,
'extra' ,
);
$relevantContent = array ();
foreach ( array_intersect ( $relevantKeys , array_keys ( $content )) as $key ) {
$relevantContent [ $key ] = $content [ $key ];
}
if ( isset ( $content [ 'config' ][ 'platform' ])) {
$relevantContent [ 'config' ][ 'platform' ] = $content [ 'config' ][ 'platform' ];
}
ksort ( $relevantContent );
return md5 ( json_encode ( $relevantContent ));
}
// @codingStandardsIgnoreEnd
2015-08-17 17:00:26 -07:00
}