Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Routing / AcceptHeaderMatcherTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Routing;
4
5 use Drupal\accept_header_routing_test\Routing\AcceptHeaderMatcher;
6 use Drupal\Tests\UnitTestCase;
7 use Symfony\Component\HttpFoundation\Request;
8 use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
9
10 /**
11  * Confirm that the mime types partial matcher is functioning properly.
12  *
13  * @group Routing
14  */
15 class AcceptHeaderMatcherTest extends UnitTestCase {
16
17   /**
18    * A collection of shared fixture data for tests.
19    *
20    * @var \Drupal\Tests\Core\Routing\RoutingFixtures
21    */
22   protected $fixtures;
23
24   /**
25    * The matcher object that is going to be tested.
26    *
27    * @var \Drupal\accept_header_routing_test\Routing\AcceptHeaderMatcher
28    */
29   protected $matcher;
30
31   /**
32    * {@inheritdoc}
33    */
34   protected function setUp() {
35     parent::setUp();
36
37     $this->fixtures = new RoutingFixtures();
38     $this->matcher = new AcceptHeaderMatcher();
39   }
40
41   /**
42    * Provides data for the Accept header filtering test.
43    *
44    * @see Drupal\Tests\Core\Routing\AcceptHeaderMatcherTest::testAcceptFiltering()
45    */
46   public function acceptFilterProvider() {
47     return [
48       // Check that JSON routes get filtered and prioritized correctly.
49       ['application/json, text/xml;q=0.9', 'json', 'route_c', 'route_e'],
50       // Tests a JSON request with alternative JSON MIME type Accept header.
51       ['application/x-json, text/xml;q=0.9', 'json', 'route_c', 'route_e'],
52       // Tests a standard HTML request.
53       ['text/html, text/xml;q=0.9', 'html', 'route_e', 'route_c'],
54     ];
55   }
56
57   /**
58    * Tests that requests using Accept headers get filtered correctly.
59    *
60    * @param string $accept_header
61    *   The HTTP Accept header value of the request.
62    * @param string $format
63    *   The request format.
64    * @param string $included_route
65    *   The route name that should survive the filter and be ranked first.
66    * @param string $excluded_route
67    *   The route name that should be filtered out during matching.
68    *
69    * @dataProvider acceptFilterProvider
70    */
71   public function testAcceptFiltering($accept_header, $format, $included_route, $excluded_route) {
72     $collection = $this->fixtures->sampleRouteCollection();
73
74     $request = Request::create('path/two', 'GET');
75     $request->headers->set('Accept', $accept_header);
76     $request->setRequestFormat($format);
77     $routes = $this->matcher->filter($collection, $request);
78     $this->assertEquals(count($routes), 4, 'The correct number of routes was found.');
79     $this->assertNotNull($routes->get($included_route), "Route $included_route was found when matching $accept_header.");
80     $this->assertNull($routes->get($excluded_route), "Route $excluded_route was not found when matching $accept_header.");
81     foreach ($routes as $name => $route) {
82       $this->assertEquals($name, $included_route, "Route $included_route is the first one in the collection when matching $accept_header.");
83       break;
84     }
85   }
86
87   /**
88    * Confirms that the AcceptHeaderMatcher throws an exception for no-route.
89    */
90   public function testNoRouteFound() {
91     // Remove the sample routes that would match any method.
92     $routes = $this->fixtures->sampleRouteCollection();
93     $routes->remove('route_a');
94     $routes->remove('route_b');
95     $routes->remove('route_c');
96     $routes->remove('route_d');
97
98     $request = Request::create('path/two', 'GET');
99     $request->headers->set('Accept', 'application/json, text/xml;q=0.9');
100     $request->setRequestFormat('json');
101     $this->setExpectedException(NotAcceptableHttpException::class, 'No route found for the specified formats application/json text/xml');
102     $this->matcher->filter($routes, $request);
103   }
104
105 }