|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bundle\SecurityBundle\Tests\DependencyInjection; |
| 13 | + |
| 14 | +use Symfony\Component\DependencyInjection\Reference; |
| 15 | + |
| 16 | +use Symfony\Component\DependencyInjection\Parameter; |
| 17 | +use Symfony\Bundle\SecurityBundle\SecurityBundle; |
| 18 | +use Symfony\Bundle\SecurityBundle\DependencyInjection\SecurityExtension; |
| 19 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 20 | + |
| 21 | +abstract class CompleteConfigurationTest extends \PHPUnit_Framework_TestCase |
| 22 | +{ |
| 23 | + abstract protected function loadFromFile(ContainerBuilder $container, $file); |
| 24 | + |
| 25 | + public function testRolesHierarchy() |
| 26 | + { |
| 27 | + $container = $this->getContainer('container1'); |
| 28 | + $this->assertEquals(array( |
| 29 | + 'ROLE_ADMIN' => array('ROLE_USER'), |
| 30 | + 'ROLE_SUPER_ADMIN' => array('ROLE_USER', 'ROLE_ADMIN', 'ROLE_ALLOWED_TO_SWITCH'), |
| 31 | + 'ROLE_REMOTE' => array('ROLE_USER', 'ROLE_ADMIN'), |
| 32 | + ), $container->getParameter('security.role_hierarchy.roles')); |
| 33 | + } |
| 34 | + |
| 35 | + public function testUserProviders() |
| 36 | + { |
| 37 | + $container = $this->getContainer('container1'); |
| 38 | + |
| 39 | + $providers = array_values(array_filter($container->getServiceIds(), function ($key) { return 0 === strpos($key, 'security.user.provider.concrete'); })); |
| 40 | + |
| 41 | + $expectedProviders = array( |
| 42 | + 'security.user.provider.concrete.default', |
| 43 | + 'security.user.provider.concrete.default_foo', |
| 44 | + 'security.user.provider.concrete.digest', |
| 45 | + 'security.user.provider.concrete.digest_foo', |
| 46 | + 'security.user.provider.concrete.basic', |
| 47 | + 'security.user.provider.concrete.basic_foo', |
| 48 | + 'security.user.provider.concrete.basic_bar', |
| 49 | + 'security.user.provider.concrete.service', |
| 50 | + 'security.user.provider.concrete.chain', |
| 51 | + ); |
| 52 | + |
| 53 | + $this->assertEquals(array(), array_diff($expectedProviders, $providers)); |
| 54 | + $this->assertEquals(array(), array_diff($providers, $expectedProviders)); |
| 55 | + |
| 56 | + // chain provider |
| 57 | + $this->assertEquals(array(array( |
| 58 | + new Reference('security.user.provider.concrete.service'), |
| 59 | + new Reference('security.user.provider.concrete.basic'), |
| 60 | + )), $container->getDefinition('security.user.provider.concrete.chain')->getArguments()); |
| 61 | + } |
| 62 | + |
| 63 | + public function testFirewalls() |
| 64 | + { |
| 65 | + $container = $this->getContainer('container1'); |
| 66 | + |
| 67 | + $arguments = $container->getDefinition('security.firewall.map')->getArguments(); |
| 68 | + $listeners = array(); |
| 69 | + foreach (array_keys($arguments[1]) as $contextId) { |
| 70 | + $contextDef = $container->getDefinition($contextId); |
| 71 | + $arguments = $contextDef->getArguments(); |
| 72 | + $listeners[] = array_map(function ($ref) { return (string) $ref; }, $arguments['index_0']); |
| 73 | + } |
| 74 | + |
| 75 | + $this->assertEquals(array( |
| 76 | + array(), |
| 77 | + array( |
| 78 | + 'security.channel_listener', |
| 79 | + 'security.logout_listener.secure', |
| 80 | + 'security.authentication.listener.x509.secure', |
| 81 | + 'security.authentication.listener.form.secure', |
| 82 | + 'security.authentication.listener.basic.secure', |
| 83 | + 'security.authentication.listener.digest.secure', |
| 84 | + 'security.authentication.listener.anonymous.secure', |
| 85 | + 'security.access_listener', |
| 86 | + 'security.authentication.switchuser_listener.secure', |
| 87 | + ), |
| 88 | + ), $listeners); |
| 89 | + } |
| 90 | + |
| 91 | + public function testAccess() |
| 92 | + { |
| 93 | + $container = $this->getContainer('container1'); |
| 94 | + |
| 95 | + $rules = array(); |
| 96 | + foreach ($container->getDefinition('security.access_map')->getMethodCalls() as $call) { |
| 97 | + if ($call[0] == 'add') { |
| 98 | + $rules[] = array((string) $call[1][0], $call[1][1], $call[1][2]); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + $matcherIds = array(); |
| 103 | + foreach ($rules as $rule) { |
| 104 | + list($matcherId, $roles, $channel) = $rule; |
| 105 | + $requestMatcher = $container->getDefinition($matcherId); |
| 106 | + |
| 107 | + $this->assertFalse(isset($matcherIds[$matcherId])); |
| 108 | + $matcherIds[$matcherId] = true; |
| 109 | + |
| 110 | + $i = count($matcherIds); |
| 111 | + if (1 === $i) { |
| 112 | + $this->assertEquals(array('ROLE_USER'), $roles); |
| 113 | + $this->assertEquals('https', $channel); |
| 114 | + $this->assertEquals( |
| 115 | + array('/blog/524', null, array('GET', 'POST')), |
| 116 | + $requestMatcher->getArguments() |
| 117 | + ); |
| 118 | + } elseif (2 === $i) { |
| 119 | + $this->assertEquals(array('IS_AUTHENTICATED_ANONYMOUSLY'), $roles); |
| 120 | + $this->assertNull($channel); |
| 121 | + $this->assertEquals( |
| 122 | + array('/blog/.*'), |
| 123 | + $requestMatcher->getArguments() |
| 124 | + ); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + public function testMerge() |
| 130 | + { |
| 131 | + $container = $this->getContainer('merge'); |
| 132 | + |
| 133 | + $this->assertEquals(array( |
| 134 | + 'FOO' => array('MOO'), |
| 135 | + 'ADMIN' => array('USER'), |
| 136 | + ), $container->getParameter('security.role_hierarchy.roles')); |
| 137 | + } |
| 138 | + |
| 139 | + public function testEncoders() |
| 140 | + { |
| 141 | + $container = $this->getContainer('container1'); |
| 142 | + |
| 143 | + $this->assertEquals(array(array( |
| 144 | + 'JMS\FooBundle\Entity\User1' => array( |
| 145 | + 'class' => new Parameter('security.encoder.plain.class'), |
| 146 | + 'arguments' => array(false), |
| 147 | + ), |
| 148 | + 'JMS\FooBundle\Entity\User2' => array( |
| 149 | + 'class' => new Parameter('security.encoder.digest.class'), |
| 150 | + 'arguments' => array('sha1', false, 5), |
| 151 | + ), |
| 152 | + 'JMS\FooBundle\Entity\User3' => array( |
| 153 | + 'class' => new Parameter('security.encoder.digest.class'), |
| 154 | + 'arguments' => array('md5', true, 5000), |
| 155 | + ), |
| 156 | + 'JMS\FooBundle\Entity\User4' => new Reference('security.encoder.foo'), |
| 157 | + 'JMS\FooBundle\Entity\User5' => array( |
| 158 | + 'class' => new Parameter('security.encoder.pbkdf2.class'), |
| 159 | + 'arguments' => array('sha1', false, 5, 30), |
| 160 | + ), |
| 161 | + 'JMS\FooBundle\Entity\User6' => array( |
| 162 | + 'class' => new Parameter('security.encoder.bcrypt.class'), |
| 163 | + 'arguments' => array(15), |
| 164 | + ), |
| 165 | + )), $container->getDefinition('security.encoder_factory.generic')->getArguments()); |
| 166 | + } |
| 167 | + |
| 168 | + public function testAcl() |
| 169 | + { |
| 170 | + $container = $this->getContainer('container1'); |
| 171 | + |
| 172 | + $this->assertTrue($container->hasDefinition('security.acl.dbal.provider')); |
| 173 | + $this->assertEquals('security.acl.dbal.provider', (string) $container->getAlias('security.acl.provider')); |
| 174 | + } |
| 175 | + |
| 176 | + public function testCustomAclProvider() |
| 177 | + { |
| 178 | + $container = $this->getContainer('custom_acl_provider'); |
| 179 | + |
| 180 | + $this->assertFalse($container->hasDefinition('security.acl.dbal.provider')); |
| 181 | + $this->assertEquals('foo', (string) $container->getAlias('security.acl.provider')); |
| 182 | + } |
| 183 | + |
| 184 | + protected function getContainer($file) |
| 185 | + { |
| 186 | + $container = new ContainerBuilder(); |
| 187 | + $security = new SecurityExtension(); |
| 188 | + $container->registerExtension($security); |
| 189 | + |
| 190 | + $bundle = new SecurityBundle(); |
| 191 | + $bundle->build($container); // Attach all default factories |
| 192 | + $this->loadFromFile($container, $file); |
| 193 | + |
| 194 | + $container->getCompilerPassConfig()->setOptimizationPasses(array()); |
| 195 | + $container->getCompilerPassConfig()->setRemovingPasses(array()); |
| 196 | + $container->compile(); |
| 197 | + |
| 198 | + return $container; |
| 199 | + } |
| 200 | +} |
0 commit comments