MainConfigurationTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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\MainConfiguration;
  13. use Symfony\Component\Config\Definition\Processor;
  14. class MainConfigurationTest extends TestCase
  15. {
  16. /**
  17. * The minimal, required config needed to not have any required validation
  18. * issues.
  19. */
  20. protected static $minimalConfig = array(
  21. 'providers' => array(
  22. 'stub' => array(
  23. 'id' => 'foo',
  24. ),
  25. ),
  26. 'firewalls' => array(
  27. 'stub' => array(),
  28. ),
  29. );
  30. /**
  31. * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
  32. */
  33. public function testNoConfigForProvider()
  34. {
  35. $config = array(
  36. 'providers' => array(
  37. 'stub' => array(),
  38. ),
  39. );
  40. $processor = new Processor();
  41. $configuration = new MainConfiguration(array(), array());
  42. $processor->processConfiguration($configuration, array($config));
  43. }
  44. /**
  45. * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
  46. */
  47. public function testManyConfigForProvider()
  48. {
  49. $config = array(
  50. 'providers' => array(
  51. 'stub' => array(
  52. 'id' => 'foo',
  53. 'chain' => array(),
  54. ),
  55. ),
  56. );
  57. $processor = new Processor();
  58. $configuration = new MainConfiguration(array(), array());
  59. $processor->processConfiguration($configuration, array($config));
  60. }
  61. public function testCsrfAliases()
  62. {
  63. $config = array(
  64. 'firewalls' => array(
  65. 'stub' => array(
  66. 'logout' => array(
  67. 'csrf_token_generator' => 'a_token_generator',
  68. 'csrf_token_id' => 'a_token_id',
  69. ),
  70. ),
  71. ),
  72. );
  73. $config = array_merge(static::$minimalConfig, $config);
  74. $processor = new Processor();
  75. $configuration = new MainConfiguration(array(), array());
  76. $processedConfig = $processor->processConfiguration($configuration, array($config));
  77. $this->assertArrayHasKey('csrf_token_generator', $processedConfig['firewalls']['stub']['logout']);
  78. $this->assertEquals('a_token_generator', $processedConfig['firewalls']['stub']['logout']['csrf_token_generator']);
  79. $this->assertArrayHasKey('csrf_token_id', $processedConfig['firewalls']['stub']['logout']);
  80. $this->assertEquals('a_token_id', $processedConfig['firewalls']['stub']['logout']['csrf_token_id']);
  81. }
  82. /**
  83. * @group legacy
  84. */
  85. public function testLegacyCsrfAliases()
  86. {
  87. $config = array(
  88. 'firewalls' => array(
  89. 'stub' => array(
  90. 'logout' => array(
  91. 'csrf_provider' => 'a_token_generator',
  92. 'intention' => 'a_token_id',
  93. ),
  94. ),
  95. ),
  96. );
  97. $config = array_merge(static::$minimalConfig, $config);
  98. $processor = new Processor();
  99. $configuration = new MainConfiguration(array(), array());
  100. $processedConfig = $processor->processConfiguration($configuration, array($config));
  101. $this->assertArrayHasKey('csrf_token_generator', $processedConfig['firewalls']['stub']['logout']);
  102. $this->assertEquals('a_token_generator', $processedConfig['firewalls']['stub']['logout']['csrf_token_generator']);
  103. $this->assertArrayHasKey('csrf_token_id', $processedConfig['firewalls']['stub']['logout']);
  104. $this->assertEquals('a_token_id', $processedConfig['firewalls']['stub']['logout']['csrf_token_id']);
  105. }
  106. /**
  107. * @expectedException \InvalidArgumentException
  108. */
  109. public function testCsrfOriginalAndAliasValueCausesException()
  110. {
  111. $config = array(
  112. 'firewalls' => array(
  113. 'stub' => array(
  114. 'logout' => array(
  115. 'csrf_token_id' => 'a_token_id',
  116. 'intention' => 'old_name',
  117. ),
  118. ),
  119. ),
  120. );
  121. $config = array_merge(static::$minimalConfig, $config);
  122. $processor = new Processor();
  123. $configuration = new MainConfiguration(array(), array());
  124. $processor->processConfiguration($configuration, array($config));
  125. }
  126. public function testDefaultUserCheckers()
  127. {
  128. $processor = new Processor();
  129. $configuration = new MainConfiguration(array(), array());
  130. $processedConfig = $processor->processConfiguration($configuration, array(static::$minimalConfig));
  131. $this->assertEquals('security.user_checker', $processedConfig['firewalls']['stub']['user_checker']);
  132. }
  133. public function testUserCheckers()
  134. {
  135. $config = array(
  136. 'firewalls' => array(
  137. 'stub' => array(
  138. 'user_checker' => 'app.henk_checker',
  139. ),
  140. ),
  141. );
  142. $config = array_merge(static::$minimalConfig, $config);
  143. $processor = new Processor();
  144. $configuration = new MainConfiguration(array(), array());
  145. $processedConfig = $processor->processConfiguration($configuration, array($config));
  146. $this->assertEquals('app.henk_checker', $processedConfig['firewalls']['stub']['user_checker']);
  147. }
  148. }