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\SessionCsrfProvider
;
16 class SessionCsrfProviderTest
extends \PHPUnit_Framework_TestCase
21 protected function setUp()
23 if (!class_exists('Symfony\Component\HttpFoundation\Session\Session')) {
24 $this->markTestSkipped('The "HttpFoundation" component is not available');
27 $this->session
= $this->getMock(
28 'Symfony\Component\HttpFoundation\Session\Session',
32 false // don't call constructor
34 $this->provider
= new SessionCsrfProvider($this->session
, 'SECRET');
37 protected function tearDown()
39 $this->provider
= null;
40 $this->session
= null;
43 public function testGenerateCsrfToken()
45 $this->session
->expects($this->once())
47 ->will($this->returnValue('ABCDEF'));
49 $token = $this->provider
->generateCsrfToken('foo');
51 $this->assertEquals(sha1('SECRET'.'foo'.'ABCDEF'), $token);
54 public function testIsCsrfTokenValidSucceeds()
56 $this->session
->expects($this->once())
58 ->will($this->returnValue('ABCDEF'));
60 $token = sha1('SECRET'.'foo'.'ABCDEF');
62 $this->assertTrue($this->provider
->isCsrfTokenValid('foo', $token));
65 public function testIsCsrfTokenValidFails()
67 $this->session
->expects($this->once())
69 ->will($this->returnValue('ABCDEF'));
71 $token = sha1('SECRET'.'bar'.'ABCDEF');
73 $this->assertFalse($this->provider
->isCsrfTokenValid('foo', $token));