More updates to stop using dev or alpha or beta versions.
[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 ::filter
20    * @dataProvider filterProvider
21    */
22   public function testFilter(RouteCollection $collection, $request_format, array $expected_filtered_collection) {
23     $route_filter = new RequestFormatRouteFilter();
24
25     $request = new Request();
26     $request->setRequestFormat($request_format);
27     $collection = $route_filter->filter($collection, $request);
28
29     $this->assertCount(count($expected_filtered_collection), $collection);
30     $this->assertSame($expected_filtered_collection, array_keys($collection->all()));
31   }
32
33   public function filterProvider() {
34     $route_without_format = new Route('/test');
35     $route_with_format = $route = new Route('/test');
36     $route_with_format->setRequirement('_format', 'json');
37     $route_with_multiple_formats = $route = new Route('/test');
38     $route_with_multiple_formats->setRequirement('_format', 'json|xml');
39
40     $collection = new RouteCollection();
41     $collection->add('test_0', $route_without_format);
42     $collection->add('test_1', $route_with_format);
43     $collection->add('test_2', $route_with_multiple_formats);
44
45     $sole_route_match_single_format = new RouteCollection();
46     $sole_route_match_single_format->add('sole_route_single_format', $route_with_format);
47
48     return [
49       'nothing requested' => [clone $collection, '', ['test_0']],
50       'xml requested' => [clone $collection, 'xml', ['test_2', 'test_0']],
51       'json requested' => [clone $collection, 'json', ['test_1', 'test_2', 'test_0']],
52       'html format requested' => [clone $collection, 'html', ['test_0']],
53       'no format requested, defaults to html' => [clone $collection, NULL, ['test_0']],
54       'no format requested, single route match with single format, defaults to that format' => [clone $sole_route_match_single_format, NULL, ['sole_route_single_format']],
55     ];
56   }
57
58   /**
59    * @covers ::filter
60    */
61   public function testNoRouteFound() {
62     $collection = new RouteCollection();
63     $route_with_format = $route = new Route('/test');
64     $route_with_format->setRequirement('_format', 'json');
65     $collection->add('test_0', $route_with_format);
66     $collection->add('test_1', clone $route_with_format);
67
68     $request = Request::create('test?_format=xml', 'GET');
69     $request->setRequestFormat('xml');
70     $route_filter = new RequestFormatRouteFilter();
71     $this->setExpectedException(NotAcceptableHttpException::class, 'No route found for the specified format xml.');
72     $route_filter->filter($collection, $request);
73   }
74
75   /**
76    * @covers ::filter
77    */
78   public function testNoRouteFoundWhenNoRequestFormatAndSingleRouteWithMultipleFormats() {
79     $this->setExpectedException(NotAcceptableHttpException::class, 'No route found for the specified format html.');
80
81     $collection = new RouteCollection();
82     $route_with_format = $route = new Route('/test');
83     $route_with_format->setRequirement('_format', 'json|xml');
84     $collection->add('sole_route_multiple_formats', $route_with_format);
85
86     $request = Request::create('test', 'GET');
87     $route_filter = new RequestFormatRouteFilter();
88     $route_filter->filter($collection, $request);
89   }
90
91 }