SecurityExtensionTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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\Bundle\SecurityBundle\Tests\DependencyInjection\Fixtures\UserProvider\DummyProvider;
  15. use Symfony\Component\DependencyInjection\ContainerBuilder;
  16. class SecurityExtensionTest extends TestCase
  17. {
  18. /**
  19. * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
  20. * @expectedExceptionMessage The check_path "/some_area/login_check" for login method "form_login" is not matched by the firewall pattern "/secured_area/.*".
  21. */
  22. public function testInvalidCheckPath()
  23. {
  24. $container = $this->getRawContainer();
  25. $container->loadFromExtension('security', array(
  26. 'providers' => array(
  27. 'default' => array('id' => 'foo'),
  28. ),
  29. 'firewalls' => array(
  30. 'some_firewall' => array(
  31. 'pattern' => '/secured_area/.*',
  32. 'form_login' => array(
  33. 'check_path' => '/some_area/login_check',
  34. ),
  35. ),
  36. ),
  37. ));
  38. $container->compile();
  39. }
  40. /**
  41. * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
  42. * @expectedExceptionMessage No authentication listener registered for firewall "some_firewall"
  43. */
  44. public function testFirewallWithoutAuthenticationListener()
  45. {
  46. $container = $this->getRawContainer();
  47. $container->loadFromExtension('security', array(
  48. 'providers' => array(
  49. 'default' => array('id' => 'foo'),
  50. ),
  51. 'firewalls' => array(
  52. 'some_firewall' => array(
  53. 'pattern' => '/.*',
  54. ),
  55. ),
  56. ));
  57. $container->compile();
  58. }
  59. /**
  60. * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
  61. * @expectedExceptionMessage Unable to create definition for "security.user.provider.concrete.my_foo" user provider
  62. */
  63. public function testFirewallWithInvalidUserProvider()
  64. {
  65. $container = $this->getRawContainer();
  66. $extension = $container->getExtension('security');
  67. $extension->addUserProviderFactory(new DummyProvider());
  68. $container->loadFromExtension('security', array(
  69. 'providers' => array(
  70. 'my_foo' => array('foo' => array()),
  71. ),
  72. 'firewalls' => array(
  73. 'some_firewall' => array(
  74. 'pattern' => '/.*',
  75. 'http_basic' => array(),
  76. ),
  77. ),
  78. ));
  79. $container->compile();
  80. }
  81. public function testDisableRoleHierarchyVoter()
  82. {
  83. $container = $this->getRawContainer();
  84. $container->loadFromExtension('security', array(
  85. 'providers' => array(
  86. 'default' => array('id' => 'foo'),
  87. ),
  88. 'role_hierarchy' => null,
  89. 'firewalls' => array(
  90. 'some_firewall' => array(
  91. 'pattern' => '/.*',
  92. 'http_basic' => null,
  93. ),
  94. ),
  95. ));
  96. $container->compile();
  97. $this->assertFalse($container->hasDefinition('security.access.role_hierarchy_voter'));
  98. }
  99. public function testGuardHandlerIsPassedStatelessFirewalls()
  100. {
  101. $container = $this->getRawContainer();
  102. $container->loadFromExtension('security', array(
  103. 'providers' => array(
  104. 'default' => array('id' => 'foo'),
  105. ),
  106. 'firewalls' => array(
  107. 'some_firewall' => array(
  108. 'pattern' => '^/admin',
  109. 'http_basic' => null,
  110. ),
  111. 'stateless_firewall' => array(
  112. 'pattern' => '/.*',
  113. 'stateless' => true,
  114. 'http_basic' => null,
  115. ),
  116. ),
  117. ));
  118. $container->compile();
  119. $definition = $container->getDefinition('security.authentication.guard_handler');
  120. $this->assertSame(array('stateless_firewall'), $definition->getArgument(2));
  121. }
  122. protected function getRawContainer()
  123. {
  124. $container = new ContainerBuilder();
  125. $security = new SecurityExtension();
  126. $container->registerExtension($security);
  127. $bundle = new SecurityBundle();
  128. $bundle->build($container);
  129. $container->getCompilerPassConfig()->setOptimizationPasses(array());
  130. $container->getCompilerPassConfig()->setRemovingPasses(array());
  131. return $container;
  132. }
  133. protected function getContainer()
  134. {
  135. $container = $this->getRawContainer();
  136. $container->compile();
  137. return $container;
  138. }
  139. }