Security update for Core, with self-updated composer
[yaffs-website] / web / core / tests / Drupal / Tests / Core / EventSubscriber / OptionsRequestSubscriberTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\EventSubscriber;
4
5 use Drupal\Core\EventSubscriber\OptionsRequestSubscriber;
6 use Drupal\Tests\UnitTestCase;
7 use Symfony\Cmf\Component\Routing\RouteProviderInterface;
8 use Symfony\Component\HttpFoundation\Request;
9 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
10 use Symfony\Component\HttpKernel\HttpKernelInterface;
11 use Symfony\Component\Routing\Route;
12 use Symfony\Component\Routing\RouteCollection;
13
14 /**
15  * @coversDefaultClass \Drupal\Core\EventSubscriber\OptionsRequestSubscriber
16  * @group EventSubscriber
17  */
18 class OptionsRequestSubscriberTest extends UnitTestCase {
19
20   /**
21    * @covers ::onRequest
22    */
23   public function testWithNonOptionRequest() {
24     $kernel = $this->prophesize(HttpKernelInterface::class);
25     $request = Request::create('/example', 'GET');
26
27     $route_provider = $this->prophesize(RouteProviderInterface::class);
28     $route_provider->getRouteCollectionForRequest($request)->shouldNotBeCalled();
29
30     $subscriber = new OptionsRequestSubscriber($route_provider->reveal());
31     $event = new GetResponseEvent($kernel->reveal(), $request, HttpKernelInterface::MASTER_REQUEST);
32     $subscriber->onRequest($event);
33
34     $this->assertFalse($event->hasResponse());
35   }
36
37   /**
38    * @covers ::onRequest
39    */
40   public function testWithoutMatchingRoutes() {
41     $kernel = $this->prophesize(HttpKernelInterface::class);
42     $request = Request::create('/example', 'OPTIONS');
43
44     $route_provider = $this->prophesize(RouteProviderInterface::class);
45     $route_provider->getRouteCollectionForRequest($request)->willReturn(new RouteCollection())->shouldBeCalled();
46
47     $subscriber = new OptionsRequestSubscriber($route_provider->reveal());
48     $event = new GetResponseEvent($kernel->reveal(), $request, HttpKernelInterface::MASTER_REQUEST);
49     $subscriber->onRequest($event);
50
51     $this->assertFalse($event->hasResponse());
52   }
53
54   /**
55    * @covers ::onRequest
56    * @dataProvider providerTestOnRequestWithOptionsRequest
57    */
58   public function testWithOptionsRequest(RouteCollection $collection, $expected_header) {
59     $kernel = $this->prophesize(HttpKernelInterface::class);
60     $request = Request::create('/example', 'OPTIONS');
61
62     $route_provider = $this->prophesize(RouteProviderInterface::class);
63     $route_provider->getRouteCollectionForRequest($request)->willReturn($collection)->shouldBeCalled();
64
65     $subscriber = new OptionsRequestSubscriber($route_provider->reveal());
66     $event = new GetResponseEvent($kernel->reveal(), $request, HttpKernelInterface::MASTER_REQUEST);
67     $subscriber->onRequest($event);
68
69     $this->assertTrue($event->hasResponse());
70     $response = $event->getResponse();
71     $this->assertEquals(200, $response->getStatusCode());
72     $this->assertEquals($expected_header, $response->headers->get('Allow'));
73   }
74
75   public function providerTestOnRequestWithOptionsRequest() {
76     $data = [];
77
78     foreach (['GET', 'POST', 'PATCH', 'PUT', 'DELETE'] as $method) {
79       $collection = new RouteCollection();
80       $collection->add('example.1', new Route('/example', [], [], [], '', [], [$method]));
81       $data['one_route_' . $method] = [$collection, $method];
82     }
83
84     foreach (['GET', 'POST', 'PATCH', 'PUT', 'DELETE'] as $method_a) {
85       foreach (['GET', 'POST', 'PATCH', 'PUT', 'DELETE'] as $method_b) {
86         if ($method_a != $method_b) {
87           $collection = new RouteCollection();
88           $collection->add('example.1', new Route('/example', [], [], [], '', [], [$method_a, $method_b]));
89           $data['one_route_' . $method_a . '_' . $method_b] = [$collection, $method_a . ', ' . $method_b];
90         }
91       }
92     }
93
94     foreach (['GET', 'POST', 'PATCH', 'PUT', 'DELETE'] as $method_a) {
95       foreach (['GET', 'POST', 'PATCH', 'PUT', 'DELETE'] as $method_b) {
96         foreach (['GET', 'POST', 'PATCH', 'PUT', 'DELETE'] as $method_c) {
97           $collection = new RouteCollection();
98           $collection->add('example.1', new Route('/example', [], [], [], '', [], [$method_a]));
99           $collection->add('example.2', new Route('/example', [], [], [], '', [], [$method_a, $method_b]));
100           $collection->add('example.3', new Route('/example', [], [], [], '', [], [$method_b, $method_c]));
101           $methods = array_unique([$method_a, $method_b, $method_c]);
102           $data['multiple_routes_' . $method_a . '_' . $method_b . '_' . $method_c] = [$collection, implode(', ', $methods)];
103         }
104       }
105     }
106
107     return $data;
108   }
109
110 }