LegacySessionCsrfProviderTest.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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\Component\Form\Tests\Extension\Csrf\CsrfProvider;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Form\Extension\Csrf\CsrfProvider\SessionCsrfProvider;
  13. /**
  14. * @group legacy
  15. */
  16. class LegacySessionCsrfProviderTest extends TestCase
  17. {
  18. protected $provider;
  19. protected $session;
  20. protected function setUp()
  21. {
  22. $this->session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session')->disableOriginalConstructor()->getMock();
  23. $this->provider = new SessionCsrfProvider($this->session, 'SECRET');
  24. }
  25. protected function tearDown()
  26. {
  27. $this->provider = null;
  28. $this->session = null;
  29. }
  30. public function testGenerateCsrfToken()
  31. {
  32. $this->session->expects($this->once())
  33. ->method('getId')
  34. ->will($this->returnValue('ABCDEF'));
  35. $token = $this->provider->generateCsrfToken('foo');
  36. $this->assertEquals(sha1('SECRET'.'foo'.'ABCDEF'), $token);
  37. }
  38. public function testIsCsrfTokenValidSucceeds()
  39. {
  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('getId')
  50. ->will($this->returnValue('ABCDEF'));
  51. $token = sha1('SECRET'.'bar'.'ABCDEF');
  52. $this->assertFalse($this->provider->isCsrfTokenValid('foo', $token));
  53. }
  54. }