ConfigTest.php 10 KB

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