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