CsrfAccessCheckTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace Drupal\Tests\Core\Access;
  3. use Drupal\Core\Access\AccessResult;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\Routing\Route;
  6. use Drupal\Core\Access\CsrfAccessCheck;
  7. use Drupal\Tests\UnitTestCase;
  8. /**
  9. * @coversDefaultClass \Drupal\Core\Access\CsrfAccessCheck
  10. * @group Access
  11. */
  12. class CsrfAccessCheckTest extends UnitTestCase {
  13. /**
  14. * The mock CSRF token generator.
  15. *
  16. * @var \Drupal\Core\Access\CsrfTokenGenerator|\PHPUnit\Framework\MockObject\MockObject
  17. */
  18. protected $csrfToken;
  19. /**
  20. * The access checker.
  21. *
  22. * @var \Drupal\Core\Access\CsrfAccessCheck
  23. */
  24. protected $accessCheck;
  25. /**
  26. * The mock route match.
  27. *
  28. * @var \Drupal\Core\RouteMatch\RouteMatchInterface|\PHPUnit\Framework\MockObject\MockObject
  29. */
  30. protected $routeMatch;
  31. protected function setUp() {
  32. $this->csrfToken = $this->getMockBuilder('Drupal\Core\Access\CsrfTokenGenerator')
  33. ->disableOriginalConstructor()
  34. ->getMock();
  35. $this->routeMatch = $this->createMock('Drupal\Core\Routing\RouteMatchInterface');
  36. $this->accessCheck = new CsrfAccessCheck($this->csrfToken);
  37. }
  38. /**
  39. * Tests the access() method with a valid token.
  40. */
  41. public function testAccessTokenPass() {
  42. $this->csrfToken->expects($this->once())
  43. ->method('validate')
  44. ->with('test_query', 'test-path/42')
  45. ->will($this->returnValue(TRUE));
  46. $this->routeMatch->expects($this->once())
  47. ->method('getRawParameters')
  48. ->will($this->returnValue(['node' => 42]));
  49. $route = new Route('/test-path/{node}', [], ['_csrf_token' => 'TRUE']);
  50. $request = Request::create('/test-path/42?token=test_query');
  51. $this->assertEquals(AccessResult::allowed()->setCacheMaxAge(0), $this->accessCheck->access($route, $request, $this->routeMatch));
  52. }
  53. /**
  54. * @covers ::access
  55. */
  56. public function testCsrfTokenInvalid() {
  57. $this->csrfToken->expects($this->once())
  58. ->method('validate')
  59. ->with('test_query', 'test-path')
  60. ->will($this->returnValue(FALSE));
  61. $this->routeMatch->expects($this->once())
  62. ->method('getRawParameters')
  63. ->will($this->returnValue([]));
  64. $route = new Route('/test-path', [], ['_csrf_token' => 'TRUE']);
  65. $request = Request::create('/test-path?token=test_query');
  66. $this->assertEquals(AccessResult::forbidden("'csrf_token' URL query argument is invalid.")->setCacheMaxAge(0), $this->accessCheck->access($route, $request, $this->routeMatch));
  67. }
  68. /**
  69. * @covers ::access
  70. */
  71. public function testCsrfTokenMissing() {
  72. $this->csrfToken->expects($this->once())
  73. ->method('validate')
  74. ->with('', 'test-path')
  75. ->will($this->returnValue(FALSE));
  76. $this->routeMatch->expects($this->once())
  77. ->method('getRawParameters')
  78. ->will($this->returnValue([]));
  79. $route = new Route('/test-path', [], ['_csrf_token' => 'TRUE']);
  80. $request = Request::create('/test-path');
  81. $this->assertEquals(AccessResult::forbidden("'csrf_token' URL query argument is missing.")->setCacheMaxAge(0), $this->accessCheck->access($route, $request, $this->routeMatch));
  82. }
  83. }