NativeSessionTokenStorageTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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\Security\Csrf\TokenStorage\NativeSessionTokenStorage;
  13. /**
  14. * @author Bernhard Schussek <bschussek@gmail.com>
  15. *
  16. * @runTestsInSeparateProcesses
  17. * @preserveGlobalState disabled
  18. */
  19. class NativeSessionTokenStorageTest extends TestCase
  20. {
  21. const SESSION_NAMESPACE = 'foobar';
  22. /**
  23. * @var NativeSessionTokenStorage
  24. */
  25. private $storage;
  26. protected function setUp()
  27. {
  28. $_SESSION = array();
  29. $this->storage = new NativeSessionTokenStorage(self::SESSION_NAMESPACE);
  30. }
  31. public function testStoreTokenInClosedSession()
  32. {
  33. $this->storage->setToken('token_id', 'TOKEN');
  34. $this->assertSame(array(self::SESSION_NAMESPACE => array('token_id' => 'TOKEN')), $_SESSION);
  35. }
  36. /**
  37. * @requires PHP 5.4
  38. */
  39. public function testStoreTokenInClosedSessionWithExistingSessionId()
  40. {
  41. session_id('foobar');
  42. $this->assertSame(PHP_SESSION_NONE, session_status());
  43. $this->storage->setToken('token_id', 'TOKEN');
  44. $this->assertSame(PHP_SESSION_ACTIVE, session_status());
  45. $this->assertSame(array(self::SESSION_NAMESPACE => array('token_id' => 'TOKEN')), $_SESSION);
  46. }
  47. public function testStoreTokenInActiveSession()
  48. {
  49. session_start();
  50. $this->storage->setToken('token_id', 'TOKEN');
  51. $this->assertSame(array(self::SESSION_NAMESPACE => array('token_id' => 'TOKEN')), $_SESSION);
  52. }
  53. /**
  54. * @depends testStoreTokenInClosedSession
  55. */
  56. public function testCheckToken()
  57. {
  58. $this->assertFalse($this->storage->hasToken('token_id'));
  59. $this->storage->setToken('token_id', 'TOKEN');
  60. $this->assertTrue($this->storage->hasToken('token_id'));
  61. }
  62. /**
  63. * @depends testStoreTokenInClosedSession
  64. */
  65. public function testGetExistingToken()
  66. {
  67. $this->storage->setToken('token_id', 'TOKEN');
  68. $this->assertSame('TOKEN', $this->storage->getToken('token_id'));
  69. }
  70. /**
  71. * @expectedException \Symfony\Component\Security\Csrf\Exception\TokenNotFoundException
  72. */
  73. public function testGetNonExistingToken()
  74. {
  75. $this->storage->getToken('token_id');
  76. }
  77. /**
  78. * @depends testCheckToken
  79. */
  80. public function testRemoveNonExistingToken()
  81. {
  82. $this->assertNull($this->storage->removeToken('token_id'));
  83. $this->assertFalse($this->storage->hasToken('token_id'));
  84. }
  85. /**
  86. * @depends testCheckToken
  87. */
  88. public function testRemoveExistingToken()
  89. {
  90. $this->storage->setToken('token_id', 'TOKEN');
  91. $this->assertSame('TOKEN', $this->storage->removeToken('token_id'));
  92. $this->assertFalse($this->storage->hasToken('token_id'));
  93. }
  94. public function testClearRemovesAllTokensFromTheConfiguredNamespace()
  95. {
  96. $this->storage->setToken('foo', 'bar');
  97. $this->storage->clear();
  98. $this->assertFalse($this->storage->hasToken('foo'));
  99. $this->assertArrayNotHasKey(self::SESSION_NAMESPACE, $_SESSION);
  100. }
  101. public function testClearDoesNotRemoveSessionValuesFromOtherNamespaces()
  102. {
  103. $_SESSION['foo']['bar'] = 'baz';
  104. $this->storage->clear();
  105. $this->assertArrayHasKey('foo', $_SESSION);
  106. $this->assertArrayHasKey('bar', $_SESSION['foo']);
  107. $this->assertSame('baz', $_SESSION['foo']['bar']);
  108. }
  109. public function testClearDoesNotRemoveNonNamespacedSessionValues()
  110. {
  111. $_SESSION['foo'] = 'baz';
  112. $this->storage->clear();
  113. $this->assertArrayHasKey('foo', $_SESSION);
  114. $this->assertSame('baz', $_SESSION['foo']);
  115. }
  116. }