SessionCsrfProviderTest.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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\SessionCsrfProvider;
  12. class SessionCsrfProviderTest extends \PHPUnit_Framework_TestCase
  13. {
  14. protected $provider;
  15. protected function setUp()
  16. {
  17. $this->session = $this->getMock(
  18. 'Symfony\Component\HttpFoundation\Session',
  19. array(),
  20. array(),
  21. '',
  22. false // don't call constructor
  23. );
  24. $this->provider = new SessionCsrfProvider($this->session, 'SECRET');
  25. }
  26. public function testGenerateCsrfToken()
  27. {
  28. $this->session->expects($this->once())
  29. ->method('start');
  30. $this->session->expects($this->once())
  31. ->method('getId')
  32. ->will($this->returnValue('ABCDEF'));
  33. $token = $this->provider->generateCsrfToken('foo');
  34. $this->assertEquals(sha1('SECRET'.'foo'.'ABCDEF'), $token);
  35. }
  36. public function testIsCsrfTokenValidSucceeds()
  37. {
  38. $this->session->expects($this->once())
  39. ->method('start');
  40. $this->session->expects($this->once())
  41. ->method('getId')
  42. ->will($this->returnValue('ABCDEF'));
  43. $token = sha1('SECRET'.'foo'.'ABCDEF');
  44. $this->assertTrue($this->provider->isCsrfTokenValid('foo', $token));
  45. }
  46. public function testIsCsrfTokenValidFails()
  47. {
  48. $this->session->expects($this->once())
  49. ->method('start');
  50. $this->session->expects($this->once())
  51. ->method('getId')
  52. ->will($this->returnValue('ABCDEF'));
  53. $token = sha1('SECRET'.'bar'.'ABCDEF');
  54. $this->assertFalse($this->provider->isCsrfTokenValid('foo', $token));
  55. }
  56. }