CommandLineOrUnsafeMethodTest.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace Drupal\Tests\Core\PageCache;
  3. use Drupal\Core\PageCache\RequestPolicyInterface;
  4. use Drupal\Tests\UnitTestCase;
  5. use Symfony\Component\HttpFoundation\Request;
  6. /**
  7. * @coversDefaultClass \Drupal\Core\PageCache\RequestPolicy\CommandLineOrUnsafeMethod
  8. * @group PageCache
  9. */
  10. class CommandLineOrUnsafeMethodTest extends UnitTestCase {
  11. /**
  12. * The request policy under test.
  13. *
  14. * @var \Drupal\Core\PageCache\RequestPolicy\CommandLineOrUnsafeMethod|\PHPUnit\Framework\MockObject\MockObject
  15. */
  16. protected $policy;
  17. protected function setUp() {
  18. // Note that it is necessary to partially mock the class under test in
  19. // order to disable the isCli-check.
  20. $this->policy = $this->getMockBuilder('Drupal\Core\PageCache\RequestPolicy\CommandLineOrUnsafeMethod')
  21. ->setMethods(['isCli'])
  22. ->getMock();
  23. }
  24. /**
  25. * Asserts that check() returns DENY for unsafe HTTP methods.
  26. *
  27. * @dataProvider providerTestHttpMethod
  28. * @covers ::check
  29. */
  30. public function testHttpMethod($expected_result, $method) {
  31. $this->policy->expects($this->once())
  32. ->method('isCli')
  33. ->will($this->returnValue(FALSE));
  34. $request = Request::create('/', $method);
  35. $actual_result = $this->policy->check($request);
  36. $this->assertSame($expected_result, $actual_result);
  37. }
  38. /**
  39. * Provides test data and expected results for the HTTP method test.
  40. *
  41. * @return array
  42. * Test data and expected results.
  43. */
  44. public function providerTestHttpMethod() {
  45. return [
  46. [NULL, 'GET'],
  47. [NULL, 'HEAD'],
  48. [RequestPolicyInterface::DENY, 'POST'],
  49. [RequestPolicyInterface::DENY, 'PUT'],
  50. [RequestPolicyInterface::DENY, 'DELETE'],
  51. [RequestPolicyInterface::DENY, 'OPTIONS'],
  52. [RequestPolicyInterface::DENY, 'TRACE'],
  53. [RequestPolicyInterface::DENY, 'CONNECT'],
  54. ];
  55. }
  56. /**
  57. * Asserts that check() returns DENY if running from the command line.
  58. *
  59. * @covers ::check
  60. */
  61. public function testIsCli() {
  62. $this->policy->expects($this->once())
  63. ->method('isCli')
  64. ->will($this->returnValue(TRUE));
  65. $request = Request::create('/', 'GET');
  66. $actual_result = $this->policy->check($request);
  67. $this->assertSame(RequestPolicyInterface::DENY, $actual_result);
  68. }
  69. }