Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d463e25

Browse files
unkindfabpot
authored andcommitted
[SecurityBundle] Move format-dependent tests from SecurityExtensionTest
1 parent 2165d5d commit d463e25

File tree

10 files changed

+256
-214
lines changed

10 files changed

+256
-214
lines changed

src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\SecurityFactoryInterface;
1515
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\UserProvider\UserProviderFactoryInterface;
16+
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1617
use Symfony\Component\DependencyInjection\Definition;
1718
use Symfony\Component\DependencyInjection\DefinitionDecorator;
1819
use Symfony\Component\DependencyInjection\Alias;
@@ -413,7 +414,7 @@ private function createAuthenticationListeners($container, $id, $firewall, &$aut
413414
}
414415

415416
if (false === $hasListeners) {
416-
throw new \LogicException(sprintf('No authentication listener registered for firewall "%s".', $id));
417+
throw new InvalidConfigurationException(sprintf('No authentication listener registered for firewall "%s".', $id));
417418
}
418419

419420
return array($listeners, $defaultEntryPoint);
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
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(
164+
new Reference('security.secure_random'),
165+
15,
166+
)
167+
),
168+
)), $container->getDefinition('security.encoder_factory.generic')->getArguments());
169+
}
170+
171+
public function testAcl()
172+
{
173+
$container = $this->getContainer('container1');
174+
175+
$this->assertTrue($container->hasDefinition('security.acl.dbal.provider'));
176+
$this->assertEquals('security.acl.dbal.provider', (string) $container->getAlias('security.acl.provider'));
177+
}
178+
179+
public function testCustomAclProvider()
180+
{
181+
$container = $this->getContainer('custom_acl_provider');
182+
183+
$this->assertFalse($container->hasDefinition('security.acl.dbal.provider'));
184+
$this->assertEquals('foo', (string) $container->getAlias('security.acl.provider'));
185+
}
186+
187+
protected function getContainer($file)
188+
{
189+
$container = new ContainerBuilder();
190+
$security = new SecurityExtension();
191+
$container->registerExtension($security);
192+
193+
$bundle = new SecurityBundle();
194+
$bundle->build($container); // Attach all default factories
195+
$this->loadFromFile($container, $file);
196+
197+
$container->getCompilerPassConfig()->setOptimizationPasses(array());
198+
$container->getCompilerPassConfig()->setRemovingPasses(array());
199+
$container->compile();
200+
201+
return $container;
202+
}
203+
}

src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/Fixtures/php/invalid_check_path.php

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/Fixtures/xml/invalid_check_path.xml

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/Fixtures/yml/invalid_check_path.yml

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/ConfigurationTest.php renamed to src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/MainConfigurationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Symfony\Bundle\SecurityBundle\DependencyInjection\MainConfiguration;
1515
use Symfony\Component\Config\Definition\Processor;
1616

17-
class ConfigurationTest extends \PHPUnit_Framework_TestCase
17+
class MainConfigurationTest extends \PHPUnit_Framework_TestCase
1818
{
1919
/**
2020
* The minimal, required config needed to not have any required validation

src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/PhpSecurityExtensionTest.php renamed to src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/PhpCompleteConfigurationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
1616
use Symfony\Component\Config\FileLocator;
1717

18-
class PhpSecurityExtensionTest extends SecurityExtensionTest
18+
class PhpCompleteConfigurationTest extends CompleteConfigurationTest
1919
{
2020
protected function loadFromFile(ContainerBuilder $container, $file)
2121
{

0 commit comments

Comments
 (0)