CompleteConfigurationTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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\SecurityBundle\Tests\DependencyInjection;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Bundle\SecurityBundle\DependencyInjection\SecurityExtension;
  13. use Symfony\Bundle\SecurityBundle\SecurityBundle;
  14. use Symfony\Component\DependencyInjection\ContainerBuilder;
  15. use Symfony\Component\DependencyInjection\Parameter;
  16. use Symfony\Component\DependencyInjection\Reference;
  17. abstract class CompleteConfigurationTest extends TestCase
  18. {
  19. private static $containerCache = array();
  20. abstract protected function getLoader(ContainerBuilder $container);
  21. abstract protected function getFileExtension();
  22. public function testRolesHierarchy()
  23. {
  24. $container = $this->getContainer('container1');
  25. $this->assertEquals(array(
  26. 'ROLE_ADMIN' => array('ROLE_USER'),
  27. 'ROLE_SUPER_ADMIN' => array('ROLE_USER', 'ROLE_ADMIN', 'ROLE_ALLOWED_TO_SWITCH'),
  28. 'ROLE_REMOTE' => array('ROLE_USER', 'ROLE_ADMIN'),
  29. ), $container->getParameter('security.role_hierarchy.roles'));
  30. }
  31. public function testUserProviders()
  32. {
  33. $container = $this->getContainer('container1');
  34. $providers = array_values(array_filter($container->getServiceIds(), function ($key) { return 0 === strpos($key, 'security.user.provider.concrete'); }));
  35. $expectedProviders = array(
  36. 'security.user.provider.concrete.default',
  37. 'security.user.provider.concrete.digest',
  38. 'security.user.provider.concrete.basic',
  39. 'security.user.provider.concrete.service',
  40. 'security.user.provider.concrete.chain',
  41. );
  42. $this->assertEquals(array(), array_diff($expectedProviders, $providers));
  43. $this->assertEquals(array(), array_diff($providers, $expectedProviders));
  44. // chain provider
  45. $this->assertEquals(array(array(
  46. new Reference('security.user.provider.concrete.service'),
  47. new Reference('security.user.provider.concrete.basic'),
  48. )), $container->getDefinition('security.user.provider.concrete.chain')->getArguments());
  49. }
  50. public function testFirewalls()
  51. {
  52. $container = $this->getContainer('container1');
  53. $arguments = $container->getDefinition('security.firewall.map')->getArguments();
  54. $listeners = array();
  55. foreach (array_keys($arguments[1]) as $contextId) {
  56. $contextDef = $container->getDefinition($contextId);
  57. $arguments = $contextDef->getArguments();
  58. $listeners[] = array_map('strval', $arguments['index_0']);
  59. }
  60. $this->assertEquals(array(
  61. array(),
  62. array(
  63. 'security.channel_listener',
  64. 'security.authentication.listener.x509.secure',
  65. 'security.authentication.listener.remote_user.secure',
  66. 'security.authentication.listener.form.secure',
  67. 'security.authentication.listener.basic.secure',
  68. 'security.authentication.listener.digest.secure',
  69. 'security.authentication.listener.rememberme.secure',
  70. 'security.authentication.listener.anonymous.secure',
  71. 'security.authentication.switchuser_listener.secure',
  72. 'security.access_listener',
  73. ),
  74. array(
  75. 'security.channel_listener',
  76. 'security.context_listener.0',
  77. 'security.authentication.listener.basic.host',
  78. 'security.authentication.listener.anonymous.host',
  79. 'security.access_listener',
  80. ),
  81. array(
  82. 'security.channel_listener',
  83. 'security.context_listener.1',
  84. 'security.authentication.listener.basic.with_user_checker',
  85. 'security.authentication.listener.anonymous.with_user_checker',
  86. 'security.access_listener',
  87. ),
  88. array(
  89. 'security.channel_listener',
  90. 'security.context_listener.2',
  91. 'security.authentication.listener.simple_form.simple_auth',
  92. 'security.authentication.listener.anonymous.simple_auth',
  93. 'security.access_listener',
  94. ),
  95. ), $listeners);
  96. }
  97. public function testFirewallRequestMatchers()
  98. {
  99. $container = $this->getContainer('container1');
  100. $arguments = $container->getDefinition('security.firewall.map')->getArguments();
  101. $matchers = array();
  102. foreach ($arguments[1] as $reference) {
  103. if ($reference instanceof Reference) {
  104. $definition = $container->getDefinition((string) $reference);
  105. $matchers[] = $definition->getArguments();
  106. }
  107. }
  108. $this->assertEquals(array(
  109. array(
  110. '/login',
  111. ),
  112. array(
  113. '/test',
  114. 'foo\\.example\\.org',
  115. array('GET', 'POST'),
  116. ),
  117. ), $matchers);
  118. }
  119. public function testAccess()
  120. {
  121. $container = $this->getContainer('container1');
  122. $rules = array();
  123. foreach ($container->getDefinition('security.access_map')->getMethodCalls() as $call) {
  124. if ('add' == $call[0]) {
  125. $rules[] = array((string) $call[1][0], $call[1][1], $call[1][2]);
  126. }
  127. }
  128. $matcherIds = array();
  129. foreach ($rules as $rule) {
  130. list($matcherId, $attributes, $channel) = $rule;
  131. $requestMatcher = $container->getDefinition($matcherId);
  132. $this->assertArrayNotHasKey($matcherId, $matcherIds);
  133. $matcherIds[$matcherId] = true;
  134. $i = \count($matcherIds);
  135. if (1 === $i) {
  136. $this->assertEquals(array('ROLE_USER'), $attributes);
  137. $this->assertEquals('https', $channel);
  138. $this->assertEquals(
  139. array('/blog/524', null, array('GET', 'POST')),
  140. $requestMatcher->getArguments()
  141. );
  142. } elseif (2 === $i) {
  143. $this->assertEquals(array('IS_AUTHENTICATED_ANONYMOUSLY'), $attributes);
  144. $this->assertNull($channel);
  145. $this->assertEquals(
  146. array('/blog/.*'),
  147. $requestMatcher->getArguments()
  148. );
  149. } elseif (3 === $i) {
  150. $this->assertEquals('IS_AUTHENTICATED_ANONYMOUSLY', $attributes[0]);
  151. $expression = $container->getDefinition((string) $attributes[1])->getArgument(0);
  152. $this->assertEquals("token.getUsername() matches '/^admin/'", $expression);
  153. }
  154. }
  155. }
  156. public function testMerge()
  157. {
  158. $container = $this->getContainer('merge');
  159. $this->assertEquals(array(
  160. 'FOO' => array('MOO'),
  161. 'ADMIN' => array('USER'),
  162. ), $container->getParameter('security.role_hierarchy.roles'));
  163. }
  164. public function testEncoders()
  165. {
  166. $container = $this->getContainer('container1');
  167. $this->assertEquals(array(array(
  168. 'JMS\FooBundle\Entity\User1' => array(
  169. 'class' => new Parameter('security.encoder.plain.class'),
  170. 'arguments' => array(false),
  171. ),
  172. 'JMS\FooBundle\Entity\User2' => array(
  173. 'class' => new Parameter('security.encoder.digest.class'),
  174. 'arguments' => array('sha1', false, 5),
  175. ),
  176. 'JMS\FooBundle\Entity\User3' => array(
  177. 'class' => new Parameter('security.encoder.digest.class'),
  178. 'arguments' => array('md5', true, 5000),
  179. ),
  180. 'JMS\FooBundle\Entity\User4' => new Reference('security.encoder.foo'),
  181. 'JMS\FooBundle\Entity\User5' => array(
  182. 'class' => new Parameter('security.encoder.pbkdf2.class'),
  183. 'arguments' => array('sha1', false, 5, 30),
  184. ),
  185. 'JMS\FooBundle\Entity\User6' => array(
  186. 'class' => new Parameter('security.encoder.bcrypt.class'),
  187. 'arguments' => array(15),
  188. ),
  189. )), $container->getDefinition('security.encoder_factory.generic')->getArguments());
  190. }
  191. public function testAcl()
  192. {
  193. $container = $this->getContainer('container1');
  194. $this->assertTrue($container->hasDefinition('security.acl.dbal.provider'));
  195. $this->assertEquals('security.acl.dbal.provider', (string) $container->getAlias('security.acl.provider'));
  196. }
  197. public function testCustomAclProvider()
  198. {
  199. $container = $this->getContainer('custom_acl_provider');
  200. $this->assertFalse($container->hasDefinition('security.acl.dbal.provider'));
  201. $this->assertEquals('foo', (string) $container->getAlias('security.acl.provider'));
  202. }
  203. public function testRememberMeThrowExceptionsDefault()
  204. {
  205. $container = $this->getContainer('container1');
  206. $this->assertTrue($container->getDefinition('security.authentication.listener.rememberme.secure')->getArgument(5));
  207. }
  208. public function testRememberMeThrowExceptions()
  209. {
  210. $container = $this->getContainer('remember_me_options');
  211. $service = $container->getDefinition('security.authentication.listener.rememberme.main');
  212. $this->assertEquals('security.authentication.rememberme.services.persistent.main', $service->getArgument(1));
  213. $this->assertFalse($service->getArgument(5));
  214. }
  215. public function testUserCheckerConfig()
  216. {
  217. $this->assertEquals('app.user_checker', $this->getContainer('container1')->getAlias('security.user_checker.with_user_checker'));
  218. }
  219. public function testUserCheckerConfigWithDefaultChecker()
  220. {
  221. $this->assertEquals('security.user_checker', $this->getContainer('container1')->getAlias('security.user_checker.host'));
  222. }
  223. public function testUserCheckerConfigWithNoCheckers()
  224. {
  225. $this->assertEquals('security.user_checker', $this->getContainer('container1')->getAlias('security.user_checker.secure'));
  226. }
  227. protected function getContainer($file)
  228. {
  229. $file .= '.'.$this->getFileExtension();
  230. if (isset(self::$containerCache[$file])) {
  231. return self::$containerCache[$file];
  232. }
  233. $container = new ContainerBuilder();
  234. $security = new SecurityExtension();
  235. $container->registerExtension($security);
  236. $bundle = new SecurityBundle();
  237. $bundle->build($container); // Attach all default factories
  238. $this->getLoader($container)->load($file);
  239. $container->getCompilerPassConfig()->setOptimizationPasses(array());
  240. $container->getCompilerPassConfig()->setRemovingPasses(array());
  241. $container->compile();
  242. return self::$containerCache[$file] = $container;
  243. }
  244. }