DefaultCsrfProviderTest.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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\Tests\Component\Form\CsrfProvider;
  11. use Symfony\Component\Form\CsrfProvider\DefaultCsrfProvider;
  12. class DefaultCsrfProviderTest extends \PHPUnit_Framework_TestCase
  13. {
  14. protected $provider;
  15. public static function setUpBeforeClass()
  16. {
  17. @session_start();
  18. }
  19. protected function setUp()
  20. {
  21. $this->provider = new DefaultCsrfProvider('SECRET');
  22. }
  23. public function testGenerateCsrfToken()
  24. {
  25. $token = $this->provider->generateCsrfToken('foo');
  26. $this->assertEquals(sha1('SECRET'.'foo'.session_id()), $token);
  27. }
  28. public function testIsCsrfTokenValidSucceeds()
  29. {
  30. $token = sha1('SECRET'.'foo'.session_id());
  31. $this->assertTrue($this->provider->isCsrfTokenValid('foo', $token));
  32. }
  33. public function testIsCsrfTokenValidFails()
  34. {
  35. $token = sha1('SECRET'.'bar'.session_id());
  36. $this->assertFalse($this->provider->isCsrfTokenValid('foo', $token));
  37. }
  38. }