ConfigTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Test;
  12. use Composer\Config;
  13. class ConfigTest extends \PHPUnit_Framework_TestCase
  14. {
  15. /**
  16. * @dataProvider dataAddPackagistRepository
  17. */
  18. public function testAddPackagistRepository($expected, $localConfig, $systemConfig = null)
  19. {
  20. $config = new Config(false);
  21. if ($systemConfig) {
  22. $config->merge(array('repositories' => $systemConfig));
  23. }
  24. $config->merge(array('repositories' => $localConfig));
  25. $this->assertEquals($expected, $config->getRepositories());
  26. }
  27. public function dataAddPackagistRepository()
  28. {
  29. $data = array();
  30. $data['local config inherits system defaults'] = array(
  31. array(
  32. 'packagist' => array('type' => 'composer', 'url' => 'https?://packagist.org', 'allow_ssl_downgrade' => true),
  33. ),
  34. array(),
  35. );
  36. $data['local config can disable system config by name'] = array(
  37. array(),
  38. array(
  39. array('packagist' => false),
  40. ),
  41. );
  42. $data['local config adds above defaults'] = array(
  43. array(
  44. 1 => array('type' => 'vcs', 'url' => 'git://github.com/composer/composer.git'),
  45. 0 => array('type' => 'pear', 'url' => 'http://pear.composer.org'),
  46. 'packagist' => array('type' => 'composer', 'url' => 'https?://packagist.org', 'allow_ssl_downgrade' => true),
  47. ),
  48. array(
  49. array('type' => 'vcs', 'url' => 'git://github.com/composer/composer.git'),
  50. array('type' => 'pear', 'url' => 'http://pear.composer.org'),
  51. ),
  52. );
  53. $data['system config adds above core defaults'] = array(
  54. array(
  55. 'example.com' => array('type' => 'composer', 'url' => 'http://example.com'),
  56. 'packagist' => array('type' => 'composer', 'url' => 'https?://packagist.org', 'allow_ssl_downgrade' => true),
  57. ),
  58. array(),
  59. array(
  60. 'example.com' => array('type' => 'composer', 'url' => 'http://example.com'),
  61. ),
  62. );
  63. $data['local config can disable repos by name and re-add them anonymously to bring them above system config'] = array(
  64. array(
  65. 0 => array('type' => 'composer', 'url' => 'http://packagist.org'),
  66. 'example.com' => array('type' => 'composer', 'url' => 'http://example.com'),
  67. ),
  68. array(
  69. array('packagist' => false),
  70. array('type' => 'composer', 'url' => 'http://packagist.org'),
  71. ),
  72. array(
  73. 'example.com' => array('type' => 'composer', 'url' => 'http://example.com'),
  74. ),
  75. );
  76. $data['local config can override by name to bring a repo above system config'] = array(
  77. array(
  78. 'packagist' => array('type' => 'composer', 'url' => 'http://packagistnew.org'),
  79. 'example.com' => array('type' => 'composer', 'url' => 'http://example.com'),
  80. ),
  81. array(
  82. 'packagist' => array('type' => 'composer', 'url' => 'http://packagistnew.org'),
  83. ),
  84. array(
  85. 'example.com' => array('type' => 'composer', 'url' => 'http://example.com'),
  86. ),
  87. );
  88. $data['incorrect local config does not cause ErrorException'] = array(
  89. array(
  90. 'packagist' => array('type' => 'composer', 'url' => 'https?://packagist.org', 'allow_ssl_downgrade' => true),
  91. 'type' => 'vcs',
  92. 'url' => 'http://example.com',
  93. ),
  94. array(
  95. 'type' => 'vcs',
  96. 'url' => 'http://example.com',
  97. ),
  98. );
  99. return $data;
  100. }
  101. public function testPreferredInstallAsString()
  102. {
  103. $config = new Config(false);
  104. $config->merge(array('config' => array('preferred-install' => 'source')));
  105. $config->merge(array('config' => array('preferred-install' => 'dist')));
  106. $this->assertEquals('dist', $config->get('preferred-install'));
  107. }
  108. public function testMergePreferredInstall()
  109. {
  110. $config = new Config(false);
  111. $config->merge(array('config' => array('preferred-install' => 'dist')));
  112. $config->merge(array('config' => array('preferred-install' => array('foo/*' => 'source'))));
  113. // This assertion needs to make sure full wildcard preferences are placed last
  114. // Handled by composer because we convert string preferences for BC, all other
  115. // care for ordering and collision prevention is up to the user
  116. $this->assertEquals(array('foo/*' => 'source', '*' => 'dist'), $config->get('preferred-install'));
  117. }
  118. public function testMergeGithubOauth()
  119. {
  120. $config = new Config(false);
  121. $config->merge(array('config' => array('github-oauth' => array('foo' => 'bar'))));
  122. $config->merge(array('config' => array('github-oauth' => array('bar' => 'baz'))));
  123. $this->assertEquals(array('foo' => 'bar', 'bar' => 'baz'), $config->get('github-oauth'));
  124. }
  125. public function testVarReplacement()
  126. {
  127. $config = new Config(false);
  128. $config->merge(array('config' => array('a' => 'b', 'c' => '{$a}')));
  129. $config->merge(array('config' => array('bin-dir' => '$HOME', 'cache-dir' => '~/foo/')));
  130. $home = rtrim(getenv('HOME') ?: getenv('USERPROFILE'), '\\/');
  131. $this->assertEquals('b', $config->get('c'));
  132. $this->assertEquals($home.'/', $config->get('bin-dir'));
  133. $this->assertEquals($home.'/foo', $config->get('cache-dir'));
  134. }
  135. public function testRealpathReplacement()
  136. {
  137. $config = new Config(false, '/foo/bar');
  138. $config->merge(array('config' => array(
  139. 'bin-dir' => '$HOME/foo',
  140. 'cache-dir' => '/baz/',
  141. 'vendor-dir' => 'vendor',
  142. )));
  143. $home = rtrim(getenv('HOME') ?: getenv('USERPROFILE'), '\\/');
  144. $this->assertEquals('/foo/bar/vendor', $config->get('vendor-dir'));
  145. $this->assertEquals($home.'/foo', $config->get('bin-dir'));
  146. $this->assertEquals('/baz', $config->get('cache-dir'));
  147. }
  148. public function testStreamWrapperDirs()
  149. {
  150. $config = new Config(false, '/foo/bar');
  151. $config->merge(array('config' => array(
  152. 'cache-dir' => 's3://baz/',
  153. )));
  154. $this->assertEquals('s3://baz', $config->get('cache-dir'));
  155. }
  156. public function testFetchingRelativePaths()
  157. {
  158. $config = new Config(false, '/foo/bar');
  159. $config->merge(array('config' => array(
  160. 'bin-dir' => '{$vendor-dir}/foo',
  161. 'vendor-dir' => 'vendor',
  162. )));
  163. $this->assertEquals('/foo/bar/vendor', $config->get('vendor-dir'));
  164. $this->assertEquals('/foo/bar/vendor/foo', $config->get('bin-dir'));
  165. $this->assertEquals('vendor', $config->get('vendor-dir', Config::RELATIVE_PATHS));
  166. $this->assertEquals('vendor/foo', $config->get('bin-dir', Config::RELATIVE_PATHS));
  167. }
  168. public function testOverrideGithubProtocols()
  169. {
  170. $config = new Config(false);
  171. $config->merge(array('config' => array('github-protocols' => array('https', 'ssh'))));
  172. $config->merge(array('config' => array('github-protocols' => array('https'))));
  173. $this->assertEquals(array('https'), $config->get('github-protocols'));
  174. }
  175. public function testGitDisabledByDefaultInGithubProtocols()
  176. {
  177. $config = new Config(false);
  178. $config->merge(array('config' => array('github-protocols' => array('https', 'git'))));
  179. $this->assertEquals(array('https'), $config->get('github-protocols'));
  180. $config->merge(array('config' => array('secure-http' => false)));
  181. $this->assertEquals(array('https', 'git'), $config->get('github-protocols'));
  182. }
  183. /**
  184. * @dataProvider allowedUrlProvider
  185. *
  186. * @param string $url
  187. */
  188. public function testAllowedUrlsPass($url)
  189. {
  190. $config = new Config(false);
  191. $config->prohibitUrlByConfig($url);
  192. }
  193. /**
  194. * @dataProvider prohibitedUrlProvider
  195. *
  196. * @param string $url
  197. */
  198. public function testProhibitedUrlsThrowException($url)
  199. {
  200. $this->setExpectedException(
  201. 'Composer\Downloader\TransportException',
  202. 'Your configuration does not allow connections to ' . $url
  203. );
  204. $config = new Config(false);
  205. $config->prohibitUrlByConfig($url);
  206. }
  207. /**
  208. * @return array List of test URLs that should pass strict security
  209. */
  210. public function allowedUrlProvider()
  211. {
  212. $urls = array(
  213. 'https://packagist.org',
  214. 'git@github.com:composer/composer.git',
  215. 'hg://user:pass@my.satis/satis',
  216. '\\myserver\myplace.git',
  217. 'file://myserver.localhost/mygit.git',
  218. 'file://example.org/mygit.git',
  219. );
  220. return array_combine($urls, array_map(function ($e) { return array($e); }, $urls));
  221. }
  222. /**
  223. * @return array List of test URLs that should not pass strict security
  224. */
  225. public function prohibitedUrlProvider()
  226. {
  227. $urls = array(
  228. 'http://packagist.org',
  229. 'http://10.1.0.1/satis',
  230. 'http://127.0.0.1/satis',
  231. 'svn://localhost/trunk',
  232. 'svn://will.not.resolve/trunk',
  233. 'svn://192.168.0.1/trunk',
  234. 'svn://1.2.3.4/trunk',
  235. 'git://5.6.7.8/git.git',
  236. );
  237. return array_combine($urls, array_map(function ($e) { return array($e); }, $urls));
  238. }
  239. /**
  240. * @group TLS
  241. */
  242. public function testDisableTlsCanBeOverridden()
  243. {
  244. $config = new Config;
  245. $config->merge(
  246. array('config' => array('disable-tls' => 'false'))
  247. );
  248. $this->assertFalse($config->get('disable-tls'));
  249. $config->merge(
  250. array('config' => array('disable-tls' => 'true'))
  251. );
  252. $this->assertTrue($config->get('disable-tls'));
  253. }
  254. }