2e411b2b85458bb726a4be70dfd6adae21100245
[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 ::filter
20    */
21   public function testWithAllowedMethod() {
22     $request = Request::create('/test', 'GET');
23     $collection = new RouteCollection();
24     $collection->add('test_route.get', new Route('/test', [], [], [], '', [], ['GET']));
25     $collection_before = clone $collection;
26
27     $method_filter = new MethodFilter();
28     $result_collection = $method_filter->filter($collection, $request);
29
30     $this->assertEquals($collection_before, $result_collection);
31   }
32
33   /**
34    * @covers ::filter
35    */
36   public function testWithAllowedMethodAndMultipleMatchingRoutes() {
37     $request = Request::create('/test', 'GET');
38     $collection = new RouteCollection();
39     $collection->add('test_route.get', new Route('/test', [], [], [], '', [], ['GET']));
40     $collection->add('test_route2.get', new Route('/test', [], [], [], '', [], ['GET']));
41     $collection->add('test_route3.get', new Route('/test', [], [], [], '', [], ['GET']));
42
43     $collection_before = clone $collection;
44
45     $method_filter = new MethodFilter();
46     $result_collection = $method_filter->filter($collection, $request);
47
48     $this->assertEquals($collection_before, $result_collection);
49   }
50
51   /**
52    * @covers ::filter
53    */
54   public function testMethodNotAllowedException() {
55     $request = Request::create('/test', 'PATCH');
56     $collection = new RouteCollection();
57     $collection->add('test_route.get', new Route('/test', [], [], [], '', [], ['GET']));
58
59     $this->setExpectedException(MethodNotAllowedException::class);
60
61     $method_filter = new MethodFilter();
62     $method_filter->filter($collection, $request);
63   }
64
65   /**
66    * @covers ::filter
67    */
68   public function testMethodNotAllowedExceptionWithMultipleRoutes() {
69     $request = Request::create('/test', 'PATCH');
70     $collection = new RouteCollection();
71     $collection->add('test_route.get', new Route('/test', [], [], [], '', [], ['GET']));
72     $collection->add('test_route2.get', new Route('/test', [], [], [], '', [], ['GET']));
73     $collection->add('test_route3.get', new Route('/test', [], [], [], '', [], ['GET']));
74
75     $this->setExpectedException(MethodNotAllowedException::class);
76
77     $method_filter = new MethodFilter();
78     $method_filter->filter($collection, $request);
79   }
80
81   /**
82    * @covers ::filter
83    */
84   public function testFilteredMethods() {
85     $request = Request::create('/test', 'PATCH');
86     $collection = new RouteCollection();
87     $collection->add('test_route.get', new Route('/test', [], [], [], '', [], ['GET']));
88     $collection->add('test_route2.get', new Route('/test', [], [], [], '', [], ['PATCH']));
89     $collection->add('test_route3.get', new Route('/test', [], [], [], '', [], ['POST']));
90
91     $expected_collection = new RouteCollection();
92     $expected_collection->add('test_route2.get', new Route('/test', [], [], [], '', [], ['PATCH']));
93
94     $method_filter = new MethodFilter();
95     $result_collection = $method_filter->filter($collection, $request);
96
97     $this->assertEquals($expected_collection, $result_collection);
98   }
99
100   /**
101    * Ensures that the incoming and outgoing collections have the same order.
102    *
103    * @covers ::filter
104    */
105   public function testCollectionOrder() {
106     $request = Request::create('/test', 'GET');
107
108     $collection = new RouteCollection();
109     $collection->add('entity.taxonomy_term.canonical', new Route('/test'));
110     $collection->add('views.view.taxonomy_term_page', new Route('/test', [], [], [], '', [], ['GET', 'POST']));
111
112     $method_filter = new MethodFilter();
113     $result_collection = $method_filter->filter($collection, $request);
114
115     $this->assertEquals(['entity.taxonomy_term.canonical', 'views.view.taxonomy_term_page'], array_keys($result_collection->all()));
116   }
117
118 }