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