4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Form\Tests\Extension\Csrf\CsrfProvider
;
14 use Symfony\Component\Form\Extension\Csrf\CsrfProvider\DefaultCsrfProvider
;
17 * @runTestsInSeparateProcesses
19 class DefaultCsrfProviderTest
extends \PHPUnit_Framework_TestCase
23 public static function setUpBeforeClass()
25 ini_set('session.save_handler', 'files');
26 ini_set('session.save_path', sys_get_temp_dir());
29 protected function setUp()
31 $this->provider
= new DefaultCsrfProvider('SECRET');
34 protected function tearDown()
36 $this->provider
= null;
39 public function testGenerateCsrfToken()
43 $token = $this->provider
->generateCsrfToken('foo');
45 $this->assertEquals(sha1('SECRET'.'foo'.session_id()), $token);
48 public function testGenerateCsrfTokenOnUnstartedSession()
52 if (!version_compare(PHP_VERSION
, '5.4', '>=')) {
53 $this->markTestSkipped('This test requires PHP >= 5.4');
56 $this->assertSame(PHP_SESSION_NONE
, session_status());
58 $token = $this->provider
->generateCsrfToken('foo');
60 $this->assertEquals(sha1('SECRET'.'foo'.session_id()), $token);
61 $this->assertSame(PHP_SESSION_ACTIVE
, session_status());
64 public function testIsCsrfTokenValidSucceeds()
68 $token = sha1('SECRET'.'foo'.session_id());
70 $this->assertTrue($this->provider
->isCsrfTokenValid('foo', $token));
73 public function testIsCsrfTokenValidFails()
77 $token = sha1('SECRET'.'bar'.session_id());
79 $this->assertFalse($this->provider
->isCsrfTokenValid('foo', $token));