123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\Security\Csrf\Tests\TokenStorage;
- use PHPUnit\Framework\TestCase;
- use Symfony\Component\HttpFoundation\Session\Session;
- use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
- use Symfony\Component\Security\Csrf\TokenStorage\SessionTokenStorage;
- /**
- * @author Bernhard Schussek <bschussek@gmail.com>
- */
- class SessionTokenStorageTest extends TestCase
- {
- const SESSION_NAMESPACE = 'foobar';
- /**
- * @var Session
- */
- private $session;
- /**
- * @var SessionTokenStorage
- */
- private $storage;
- protected function setUp()
- {
- $this->session = new Session(new MockArraySessionStorage());
- $this->storage = new SessionTokenStorage($this->session, self::SESSION_NAMESPACE);
- }
- public function testStoreTokenInNotStartedSessionStartsTheSession()
- {
- $this->storage->setToken('token_id', 'TOKEN');
- $this->assertTrue($this->session->isStarted());
- }
- public function testStoreTokenInActiveSession()
- {
- $this->session->start();
- $this->storage->setToken('token_id', 'TOKEN');
- $this->assertSame('TOKEN', $this->session->get(self::SESSION_NAMESPACE.'/token_id'));
- }
- public function testCheckTokenInClosedSession()
- {
- $this->session->set(self::SESSION_NAMESPACE.'/token_id', 'RESULT');
- $this->assertTrue($this->storage->hasToken('token_id'));
- $this->assertTrue($this->session->isStarted());
- }
- public function testCheckTokenInActiveSession()
- {
- $this->session->start();
- $this->session->set(self::SESSION_NAMESPACE.'/token_id', 'RESULT');
- $this->assertTrue($this->storage->hasToken('token_id'));
- }
- public function testGetExistingTokenFromClosedSession()
- {
- $this->session->set(self::SESSION_NAMESPACE.'/token_id', 'RESULT');
- $this->assertSame('RESULT', $this->storage->getToken('token_id'));
- $this->assertTrue($this->session->isStarted());
- }
- public function testGetExistingTokenFromActiveSession()
- {
- $this->session->start();
- $this->session->set(self::SESSION_NAMESPACE.'/token_id', 'RESULT');
- $this->assertSame('RESULT', $this->storage->getToken('token_id'));
- }
- /**
- * @expectedException \Symfony\Component\Security\Csrf\Exception\TokenNotFoundException
- */
- public function testGetNonExistingTokenFromClosedSession()
- {
- $this->storage->getToken('token_id');
- }
- /**
- * @expectedException \Symfony\Component\Security\Csrf\Exception\TokenNotFoundException
- */
- public function testGetNonExistingTokenFromActiveSession()
- {
- $this->session->start();
- $this->storage->getToken('token_id');
- }
- public function testRemoveNonExistingTokenFromClosedSession()
- {
- $this->assertNull($this->storage->removeToken('token_id'));
- }
- public function testRemoveNonExistingTokenFromActiveSession()
- {
- $this->session->start();
- $this->assertNull($this->storage->removeToken('token_id'));
- }
- public function testRemoveExistingTokenFromClosedSession()
- {
- $this->session->set(self::SESSION_NAMESPACE.'/token_id', 'TOKEN');
- $this->assertSame('TOKEN', $this->storage->removeToken('token_id'));
- }
- public function testRemoveExistingTokenFromActiveSession()
- {
- $this->session->start();
- $this->session->set(self::SESSION_NAMESPACE.'/token_id', 'TOKEN');
- $this->assertSame('TOKEN', $this->storage->removeToken('token_id'));
- }
- public function testClearRemovesAllTokensFromTheConfiguredNamespace()
- {
- $this->storage->setToken('foo', 'bar');
- $this->storage->clear();
- $this->assertFalse($this->storage->hasToken('foo'));
- $this->assertFalse($this->session->has(self::SESSION_NAMESPACE.'/foo'));
- }
- public function testClearDoesNotRemoveSessionValuesFromOtherNamespaces()
- {
- $this->session->set('foo/bar', 'baz');
- $this->storage->clear();
- $this->assertTrue($this->session->has('foo/bar'));
- $this->assertSame('baz', $this->session->get('foo/bar'));
- }
- public function testClearDoesNotRemoveNonNamespacedSessionValues()
- {
- $this->session->set('foo', 'baz');
- $this->storage->clear();
- $this->assertTrue($this->session->has('foo'));
- $this->assertSame('baz', $this->session->get('foo'));
- }
- }
|