Upgraded drupal core with security updates
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Routing / RequestFormatRouteFilterTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Routing;
4
5 use Drupal\Core\Routing\RequestFormatRouteFilter;
6 use Drupal\Tests\UnitTestCase;
7 use Symfony\Component\HttpFoundation\Request;
8 use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
9 use Symfony\Component\Routing\Route;
10 use Symfony\Component\Routing\RouteCollection;
11
12 /**
13  * @coversDefaultClass \Drupal\Core\Routing\RequestFormatRouteFilter
14  * @group Routing
15  */
16 class RequestFormatRouteFilterTest extends UnitTestCase {
17
18   /**
19    * @covers ::applies
20    */
21   public function testAppliesWithoutFormat() {
22     $route_filter = new RequestFormatRouteFilter();
23     $route = new Route('/test');
24     $this->assertFalse($route_filter->applies($route));
25   }
26
27   /**
28    * @covers ::applies
29    */
30   public function testAppliesWithFormat() {
31     $route_filter = new RequestFormatRouteFilter();
32     $route = new Route('/test');
33     $route->setRequirement('_format', 'json');
34     $this->assertTrue($route_filter->applies($route));
35   }
36
37   /**
38    * @covers ::filter
39    * @dataProvider filterProvider
40    */
41   public function testFilter(RouteCollection $collection, $request_format, array $expected_filtered_collection) {
42     $route_filter = new RequestFormatRouteFilter();
43
44     $request = new Request();
45     $request->setRequestFormat($request_format);
46     $collection = $route_filter->filter($collection, $request);
47
48     $this->assertCount(count($expected_filtered_collection), $collection);
49     $this->assertSame($expected_filtered_collection, array_keys($collection->all()));
50   }
51
52   public function filterProvider() {
53     $route_without_format = new Route('/test');
54     $route_with_format = $route = new Route('/test');
55     $route_with_format->setRequirement('_format', 'json');
56     $route_with_multiple_formats = $route = new Route('/test');
57     $route_with_multiple_formats->setRequirement('_format', 'json|xml');
58
59     $collection = new RouteCollection();
60     $collection->add('test_0', $route_without_format);
61     $collection->add('test_1', $route_with_format);
62     $collection->add('test_2', $route_with_multiple_formats);
63
64     $sole_route_match_single_format = new RouteCollection();
65     $sole_route_match_single_format->add('sole_route_single_format', $route_with_format);
66
67     return [
68       'xml requested' => [clone $collection, 'xml', ['test_2', 'test_0']],
69       'json requested' => [clone $collection, 'json', ['test_1', 'test_2', 'test_0']],
70       'html format requested' => [clone $collection, 'html', ['test_0']],
71       'no format requested, defaults to html' => [clone $collection, NULL, ['test_0']],
72       'no format requested, single route match with single format, defaults to that format' => [clone $sole_route_match_single_format, NULL, ['sole_route_single_format']],
73     ];
74   }
75
76   /**
77    * @covers ::filter
78    */
79   public function testNoRouteFound() {
80     $collection = new RouteCollection();
81     $route_with_format = $route = new Route('/test');
82     $route_with_format->setRequirement('_format', 'json');
83     $collection->add('test_0', $route_with_format);
84     $collection->add('test_1', clone $route_with_format);
85
86     $request = Request::create('test?_format=xml', 'GET');
87     $request->setRequestFormat('xml');
88     $route_filter = new RequestFormatRouteFilter();
89     $this->setExpectedException(NotAcceptableHttpException::class, 'No route found for the specified format xml.');
90     $route_filter->filter($collection, $request);
91   }
92
93   /**
94    * @covers ::filter
95    */
96   public function testNoRouteFoundWhenNoRequestFormatAndSingleRouteWithMultipleFormats() {
97     $this->setExpectedException(NotAcceptableHttpException::class, 'No route found for the specified format html.');
98
99     $collection = new RouteCollection();
100     $route_with_format = $route = new Route('/test');
101     $route_with_format->setRequirement('_format', 'json|xml');
102     $collection->add('sole_route_multiple_formats', $route_with_format);
103
104     $request = Request::create('test', 'GET');
105     $route_filter = new RequestFormatRouteFilter();
106     $route_filter->filter($collection, $request);
107   }
108
109 }