3 namespace Drupal\Tests\Core\Routing;
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;
13 * @coversDefaultClass \Drupal\Core\Routing\RequestFormatRouteFilter
16 class RequestFormatRouteFilterTest extends UnitTestCase {
20 * @dataProvider filterProvider
22 public function testFilter(RouteCollection $collection, $request_format, array $expected_filtered_collection) {
23 $route_filter = new RequestFormatRouteFilter();
25 $request = new Request();
26 $request->setRequestFormat($request_format);
27 $collection = $route_filter->filter($collection, $request);
29 $this->assertCount(count($expected_filtered_collection), $collection);
30 $this->assertSame($expected_filtered_collection, array_keys($collection->all()));
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');
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);
45 $sole_route_match_single_format = new RouteCollection();
46 $sole_route_match_single_format->add('sole_route_single_format', $route_with_format);
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']],
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);
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);
78 public function testNoRouteFoundWhenNoRequestFormatAndSingleRouteWithMultipleFormats() {
79 $this->setExpectedException(NotAcceptableHttpException::class, 'No route found for the specified format html.');
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);
86 $request = Request::create('test', 'GET');
87 $route_filter = new RequestFormatRouteFilter();
88 $route_filter->filter($collection, $request);