Security update for Core, with self-updated composer
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Routing / MethodFilterTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Routing;
4
5 use Drupal\Core\Routing\MethodFilter;
6 use Drupal\Tests\UnitTestCase;
7 use Symfony\Component\HttpFoundation\Request;
8 use Symfony\Component\Routing\Exception\MethodNotAllowedException;
9 use Symfony\Component\Routing\Route;
10 use Symfony\Component\Routing\RouteCollection;
11
12 /**
13  * @coversDefaultClass \Drupal\Core\Routing\MethodFilter
14  * @group Routing
15  */
16 class MethodFilterTest extends UnitTestCase {
17
18   /**
19    * @covers ::applies
20    * @dataProvider providerApplies
21    */
22   public function testApplies(array $route_methods, $expected_applies) {
23     $route = new Route('/test', [], [], [], '', [], $route_methods);
24     $method_filter = new MethodFilter();
25
26     $this->assertSame($expected_applies, $method_filter->applies($route));
27   }
28
29   /**
30    * Data provider for testApplies().
31    *
32    * @return array
33    */
34   public function providerApplies() {
35     return [
36       'only GET' => [['GET'], TRUE],
37       'only PATCH' => [['PATCH'], TRUE],
38       'only POST' => [['POST'], TRUE],
39       'only DELETE' => [['DELETE'], TRUE],
40       'only HEAD' => [['HEAD'], TRUE],
41       'all' => [['GET', 'PATCH', 'POST', 'DELETE', 'HEAD'], TRUE],
42       'none' => [[], FALSE],
43     ];
44   }
45
46   /**
47    * @covers ::filter
48    */
49   public function testWithAllowedMethod() {
50     $request = Request::create('/test', 'GET');
51     $collection = new RouteCollection();
52     $collection->add('test_route.get', new Route('/test', [], [], [], '', [], ['GET']));
53     $collection_before = clone $collection;
54
55     $method_filter = new MethodFilter();
56     $result_collection = $method_filter->filter($collection, $request);
57
58     $this->assertEquals($collection_before, $result_collection);
59   }
60
61   /**
62    * @covers ::filter
63    */
64   public function testWithAllowedMethodAndMultipleMatchingRoutes() {
65     $request = Request::create('/test', 'GET');
66     $collection = new RouteCollection();
67     $collection->add('test_route.get', new Route('/test', [], [], [], '', [], ['GET']));
68     $collection->add('test_route2.get', new Route('/test', [], [], [], '', [], ['GET']));
69     $collection->add('test_route3.get', new Route('/test', [], [], [], '', [], ['GET']));
70
71     $collection_before = clone $collection;
72
73     $method_filter = new MethodFilter();
74     $result_collection = $method_filter->filter($collection, $request);
75
76     $this->assertEquals($collection_before, $result_collection);
77   }
78
79   /**
80    * @covers ::filter
81    */
82   public function testMethodNotAllowedException() {
83     $request = Request::create('/test', 'PATCH');
84     $collection = new RouteCollection();
85     $collection->add('test_route.get', new Route('/test', [], [], [], '', [], ['GET']));
86
87     $this->setExpectedException(MethodNotAllowedException::class);
88
89     $method_filter = new MethodFilter();
90     $method_filter->filter($collection, $request);
91   }
92
93   /**
94    * @covers ::filter
95    */
96   public function testMethodNotAllowedExceptionWithMultipleRoutes() {
97     $request = Request::create('/test', 'PATCH');
98     $collection = new RouteCollection();
99     $collection->add('test_route.get', new Route('/test', [], [], [], '', [], ['GET']));
100     $collection->add('test_route2.get', new Route('/test', [], [], [], '', [], ['GET']));
101     $collection->add('test_route3.get', new Route('/test', [], [], [], '', [], ['GET']));
102
103     $this->setExpectedException(MethodNotAllowedException::class);
104
105     $method_filter = new MethodFilter();
106     $method_filter->filter($collection, $request);
107   }
108
109   /**
110    * @covers ::filter
111    */
112   public function testFilteredMethods() {
113     $request = Request::create('/test', 'PATCH');
114     $collection = new RouteCollection();
115     $collection->add('test_route.get', new Route('/test', [], [], [], '', [], ['GET']));
116     $collection->add('test_route2.get', new Route('/test', [], [], [], '', [], ['PATCH']));
117     $collection->add('test_route3.get', new Route('/test', [], [], [], '', [], ['POST']));
118
119     $expected_collection = new RouteCollection();
120     $expected_collection->add('test_route2.get', new Route('/test', [], [], [], '', [], ['PATCH']));
121
122     $method_filter = new MethodFilter();
123     $result_collection = $method_filter->filter($collection, $request);
124
125     $this->assertEquals($expected_collection, $result_collection);
126   }
127
128 }