SessionTokenStorageTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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\Component\Security\Csrf\Tests\TokenStorage;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\Session\Session;
  13. use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
  14. use Symfony\Component\Security\Csrf\TokenStorage\SessionTokenStorage;
  15. /**
  16. * @author Bernhard Schussek <bschussek@gmail.com>
  17. */
  18. class SessionTokenStorageTest extends TestCase
  19. {
  20. const SESSION_NAMESPACE = 'foobar';
  21. /**
  22. * @var Session
  23. */
  24. private $session;
  25. /**
  26. * @var SessionTokenStorage
  27. */
  28. private $storage;
  29. protected function setUp()
  30. {
  31. $this->session = new Session(new MockArraySessionStorage());
  32. $this->storage = new SessionTokenStorage($this->session, self::SESSION_NAMESPACE);
  33. }
  34. public function testStoreTokenInNotStartedSessionStartsTheSession()
  35. {
  36. $this->storage->setToken('token_id', 'TOKEN');
  37. $this->assertTrue($this->session->isStarted());
  38. }
  39. public function testStoreTokenInActiveSession()
  40. {
  41. $this->session->start();
  42. $this->storage->setToken('token_id', 'TOKEN');
  43. $this->assertSame('TOKEN', $this->session->get(self::SESSION_NAMESPACE.'/token_id'));
  44. }
  45. public function testCheckTokenInClosedSession()
  46. {
  47. $this->session->set(self::SESSION_NAMESPACE.'/token_id', 'RESULT');
  48. $this->assertTrue($this->storage->hasToken('token_id'));
  49. $this->assertTrue($this->session->isStarted());
  50. }
  51. public function testCheckTokenInActiveSession()
  52. {
  53. $this->session->start();
  54. $this->session->set(self::SESSION_NAMESPACE.'/token_id', 'RESULT');
  55. $this->assertTrue($this->storage->hasToken('token_id'));
  56. }
  57. public function testGetExistingTokenFromClosedSession()
  58. {
  59. $this->session->set(self::SESSION_NAMESPACE.'/token_id', 'RESULT');
  60. $this->assertSame('RESULT', $this->storage->getToken('token_id'));
  61. $this->assertTrue($this->session->isStarted());
  62. }
  63. public function testGetExistingTokenFromActiveSession()
  64. {
  65. $this->session->start();
  66. $this->session->set(self::SESSION_NAMESPACE.'/token_id', 'RESULT');
  67. $this->assertSame('RESULT', $this->storage->getToken('token_id'));
  68. }
  69. /**
  70. * @expectedException \Symfony\Component\Security\Csrf\Exception\TokenNotFoundException
  71. */
  72. public function testGetNonExistingTokenFromClosedSession()
  73. {
  74. $this->storage->getToken('token_id');
  75. }
  76. /**
  77. * @expectedException \Symfony\Component\Security\Csrf\Exception\TokenNotFoundException
  78. */
  79. public function testGetNonExistingTokenFromActiveSession()
  80. {
  81. $this->session->start();
  82. $this->storage->getToken('token_id');
  83. }
  84. public function testRemoveNonExistingTokenFromClosedSession()
  85. {
  86. $this->assertNull($this->storage->removeToken('token_id'));
  87. }
  88. public function testRemoveNonExistingTokenFromActiveSession()
  89. {
  90. $this->session->start();
  91. $this->assertNull($this->storage->removeToken('token_id'));
  92. }
  93. public function testRemoveExistingTokenFromClosedSession()
  94. {
  95. $this->session->set(self::SESSION_NAMESPACE.'/token_id', 'TOKEN');
  96. $this->assertSame('TOKEN', $this->storage->removeToken('token_id'));
  97. }
  98. public function testRemoveExistingTokenFromActiveSession()
  99. {
  100. $this->session->start();
  101. $this->session->set(self::SESSION_NAMESPACE.'/token_id', 'TOKEN');
  102. $this->assertSame('TOKEN', $this->storage->removeToken('token_id'));
  103. }
  104. public function testClearRemovesAllTokensFromTheConfiguredNamespace()
  105. {
  106. $this->storage->setToken('foo', 'bar');
  107. $this->storage->clear();
  108. $this->assertFalse($this->storage->hasToken('foo'));
  109. $this->assertFalse($this->session->has(self::SESSION_NAMESPACE.'/foo'));
  110. }
  111. public function testClearDoesNotRemoveSessionValuesFromOtherNamespaces()
  112. {
  113. $this->session->set('foo/bar', 'baz');
  114. $this->storage->clear();
  115. $this->assertTrue($this->session->has('foo/bar'));
  116. $this->assertSame('baz', $this->session->get('foo/bar'));
  117. }
  118. public function testClearDoesNotRemoveNonNamespacedSessionValues()
  119. {
  120. $this->session->set('foo', 'baz');
  121. $this->storage->clear();
  122. $this->assertTrue($this->session->has('foo'));
  123. $this->assertSame('baz', $this->session->get('foo'));
  124. }
  125. }