ConfigurationTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Bundle\FrameworkBundle\DependencyInjection\Configuration;
  13. use Symfony\Component\Config\Definition\Processor;
  14. class ConfigurationTest extends TestCase
  15. {
  16. public function testDefaultConfig()
  17. {
  18. $processor = new Processor();
  19. $config = $processor->processConfiguration(new Configuration(true), array(array('secret' => 's3cr3t')));
  20. $this->assertEquals(
  21. array_merge(array('secret' => 's3cr3t', 'trusted_hosts' => array()), self::getBundleDefaultConfig()),
  22. $config
  23. );
  24. }
  25. public function testDoNoDuplicateDefaultFormResources()
  26. {
  27. $input = array('templating' => array(
  28. 'form' => array('resources' => array('FrameworkBundle:Form')),
  29. 'engines' => array('php'),
  30. ));
  31. $processor = new Processor();
  32. $config = $processor->processConfiguration(new Configuration(true), array($input));
  33. $this->assertEquals(array('FrameworkBundle:Form'), $config['templating']['form']['resources']);
  34. }
  35. /**
  36. * @dataProvider getTestValidSessionName
  37. */
  38. public function testValidSessionName($sessionName)
  39. {
  40. $processor = new Processor();
  41. $config = $processor->processConfiguration(
  42. new Configuration(true),
  43. array(array('session' => array('name' => $sessionName)))
  44. );
  45. $this->assertEquals($sessionName, $config['session']['name']);
  46. }
  47. public function getTestValidSessionName()
  48. {
  49. return array(
  50. array(null),
  51. array('PHPSESSID'),
  52. array('a&b'),
  53. array(',_-!@#$%^*(){}:<>/?'),
  54. );
  55. }
  56. /**
  57. * @dataProvider getTestInvalidSessionName
  58. * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
  59. */
  60. public function testInvalidSessionName($sessionName)
  61. {
  62. $processor = new Processor();
  63. $processor->processConfiguration(
  64. new Configuration(true),
  65. array(array('session' => array('name' => $sessionName)))
  66. );
  67. }
  68. public function getTestInvalidSessionName()
  69. {
  70. return array(
  71. array('a.b'),
  72. array('a['),
  73. array('a[]'),
  74. array('a[b]'),
  75. array('a=b'),
  76. array('a+b'),
  77. );
  78. }
  79. /**
  80. * @dataProvider getTestValidTrustedProxiesData
  81. */
  82. public function testValidTrustedProxies($trustedProxies, $processedProxies)
  83. {
  84. $processor = new Processor();
  85. $configuration = new Configuration(true);
  86. $config = $processor->processConfiguration($configuration, array(array(
  87. 'secret' => 's3cr3t',
  88. 'trusted_proxies' => $trustedProxies,
  89. )));
  90. $this->assertEquals($processedProxies, $config['trusted_proxies']);
  91. }
  92. public function getTestValidTrustedProxiesData()
  93. {
  94. return array(
  95. array(array('127.0.0.1'), array('127.0.0.1')),
  96. array(array('::1'), array('::1')),
  97. array(array('127.0.0.1', '::1'), array('127.0.0.1', '::1')),
  98. array(null, array()),
  99. array(false, array()),
  100. array(array(), array()),
  101. array(array('10.0.0.0/8'), array('10.0.0.0/8')),
  102. array(array('::ffff:0:0/96'), array('::ffff:0:0/96')),
  103. array(array('0.0.0.0/0'), array('0.0.0.0/0')),
  104. );
  105. }
  106. /**
  107. * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
  108. */
  109. public function testInvalidTypeTrustedProxies()
  110. {
  111. $processor = new Processor();
  112. $configuration = new Configuration(true);
  113. $processor->processConfiguration($configuration, array(
  114. array(
  115. 'secret' => 's3cr3t',
  116. 'trusted_proxies' => 'Not an IP address',
  117. ),
  118. ));
  119. }
  120. /**
  121. * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
  122. */
  123. public function testInvalidValueTrustedProxies()
  124. {
  125. $processor = new Processor();
  126. $configuration = new Configuration(true);
  127. $processor->processConfiguration($configuration, array(
  128. array(
  129. 'secret' => 's3cr3t',
  130. 'trusted_proxies' => array('Not an IP address'),
  131. ),
  132. ));
  133. }
  134. /**
  135. * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
  136. * @expectedExceptionMessage You cannot use assets settings under "framework.templating" and "assets" configurations in the same project.
  137. * @group legacy
  138. */
  139. public function testLegacyInvalidValueAssets()
  140. {
  141. $processor = new Processor();
  142. $configuration = new Configuration(true);
  143. $processor->processConfiguration($configuration, array(
  144. array(
  145. 'templating' => array(
  146. 'engines' => null,
  147. 'assets_base_urls' => '//example.com',
  148. ),
  149. 'assets' => null,
  150. ),
  151. ));
  152. }
  153. protected static function getBundleDefaultConfig()
  154. {
  155. return array(
  156. 'http_method_override' => true,
  157. 'trusted_proxies' => array(),
  158. 'ide' => null,
  159. 'default_locale' => 'en',
  160. 'form' => array(
  161. 'enabled' => false,
  162. 'csrf_protection' => array(
  163. 'enabled' => null, // defaults to csrf_protection.enabled
  164. 'field_name' => null,
  165. ),
  166. ),
  167. 'csrf_protection' => array(
  168. 'enabled' => false,
  169. 'field_name' => '_token',
  170. ),
  171. 'esi' => array('enabled' => false),
  172. 'ssi' => array('enabled' => false),
  173. 'fragments' => array(
  174. 'enabled' => false,
  175. 'path' => '/_fragment',
  176. ),
  177. 'profiler' => array(
  178. 'enabled' => false,
  179. 'only_exceptions' => false,
  180. 'only_master_requests' => false,
  181. 'dsn' => 'file:%kernel.cache_dir%/profiler',
  182. 'username' => '',
  183. 'password' => '',
  184. 'lifetime' => 86400,
  185. 'collect' => true,
  186. ),
  187. 'translator' => array(
  188. 'enabled' => false,
  189. 'fallbacks' => array('en'),
  190. 'logging' => true,
  191. 'paths' => array(),
  192. ),
  193. 'validation' => array(
  194. 'enabled' => false,
  195. 'enable_annotations' => false,
  196. 'static_method' => array('loadValidatorMetadata'),
  197. 'translation_domain' => 'validators',
  198. 'strict_email' => false,
  199. ),
  200. 'annotations' => array(
  201. 'cache' => 'file',
  202. 'file_cache_dir' => '%kernel.cache_dir%/annotations',
  203. 'debug' => true,
  204. ),
  205. 'serializer' => array(
  206. 'enabled' => false,
  207. 'enable_annotations' => false,
  208. ),
  209. 'property_access' => array(
  210. 'magic_call' => false,
  211. 'throw_exception_on_invalid_index' => false,
  212. ),
  213. 'property_info' => array(
  214. 'enabled' => false,
  215. ),
  216. 'assets' => array(
  217. 'version' => null,
  218. 'version_format' => '%%s?%%s',
  219. 'base_path' => '',
  220. 'base_urls' => array(),
  221. 'packages' => array(),
  222. ),
  223. );
  224. }
  225. }