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 {
21 public function testAppliesWithoutFormat() {
22 $route_filter = new RequestFormatRouteFilter();
23 $route = new Route('/test');
24 $this->assertFalse($route_filter->applies($route));
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));
39 * @dataProvider filterProvider
41 public function testFilter(RouteCollection $collection, $request_format, array $expected_filtered_collection) {
42 $route_filter = new RequestFormatRouteFilter();
44 $request = new Request();
45 $request->setRequestFormat($request_format);
46 $collection = $route_filter->filter($collection, $request);
48 $this->assertCount(count($expected_filtered_collection), $collection);
49 $this->assertSame($expected_filtered_collection, array_keys($collection->all()));
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');
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);
64 $sole_route_match_single_format = new RouteCollection();
65 $sole_route_match_single_format->add('sole_route_single_format', $route_with_format);
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']],
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);
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);
96 public function testNoRouteFoundWhenNoRequestFormatAndSingleRouteWithMultipleFormats() {
97 $this->setExpectedException(NotAcceptableHttpException::class, 'No route found for the specified format html.');
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);
104 $request = Request::create('test', 'GET');
105 $route_filter = new RequestFormatRouteFilter();
106 $route_filter->filter($collection, $request);