Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Routing / ContentTypeHeaderMatcherTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Routing;
4
5 use Drupal\Core\Routing\ContentTypeHeaderMatcher;
6 use Drupal\Tests\UnitTestCase;
7 use Symfony\Component\HttpFoundation\ParameterBag;
8 use Symfony\Component\HttpFoundation\Request;
9 use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
10
11 /**
12  * Confirm that the content types partial matcher is functioning properly.
13  *
14  * @group Routing
15  *
16  * @coversDefaultClass \Drupal\Core\Routing\ContentTypeHeaderMatcher
17  */
18 class ContentTypeHeaderMatcherTest extends UnitTestCase {
19
20   /**
21    * A collection of shared fixture data for tests.
22    *
23    * @var RoutingFixtures
24    */
25   protected $fixtures;
26
27   /**
28    * The matcher object that is going to be tested.
29    *
30    * @var \Drupal\Core\Routing\ContentTypeHeaderMatcher
31    */
32   protected $matcher;
33
34   /**
35    * {@inheritdoc}
36    */
37   protected function setUp() {
38     parent::setUp();
39
40     $this->fixtures = new RoutingFixtures();
41     $this->matcher = new ContentTypeHeaderMatcher();
42   }
43
44   /**
45    * Tests that routes are not filtered on safe requests.
46    *
47    * @dataProvider providerTestSafeRequestFilter
48    */
49   public function testSafeRequestFilter($method) {
50     $collection = $this->fixtures->sampleRouteCollection();
51     $collection->addCollection($this->fixtures->contentRouteCollection());
52
53     $request = Request::create('path/two', $method);
54     $routes = $this->matcher->filter($collection, $request);
55     $this->assertEquals(count($routes), 7, 'The correct number of routes was found.');
56   }
57
58   public function providerTestSafeRequestFilter() {
59     return [
60       ['GET'],
61       ['HEAD'],
62       ['OPTIONS'],
63       ['TRACE'],
64     ];
65   }
66
67   /**
68    * Tests that XML-restricted routes get filtered out on JSON requests.
69    */
70   public function testJsonRequest() {
71     $collection = $this->fixtures->sampleRouteCollection();
72     $collection->addCollection($this->fixtures->contentRouteCollection());
73
74     $request = Request::create('path/two', 'POST');
75     $request->headers->set('Content-type', 'application/json');
76     $routes = $this->matcher->filter($collection, $request);
77     $this->assertEquals(count($routes), 6, 'The correct number of routes was found.');
78     $this->assertNotNull($routes->get('route_f'), 'The json route was found.');
79     $this->assertNull($routes->get('route_g'), 'The xml route was not found.');
80     foreach ($routes as $name => $route) {
81       $this->assertEquals($name, 'route_f', 'The json route is the first one in the collection.');
82       break;
83     }
84   }
85
86   /**
87    * Tests route filtering on POST form submission requests.
88    */
89   public function testPostForm() {
90     $collection = $this->fixtures->sampleRouteCollection();
91     $collection->addCollection($this->fixtures->contentRouteCollection());
92
93     // Test that all XML and JSON restricted routes get filtered out on a POST
94     // form submission.
95     $request = Request::create('path/two', 'POST');
96     $request->headers->set('Content-type', 'application/www-form-urlencoded');
97     $routes = $this->matcher->filter($collection, $request);
98     $this->assertEquals(count($routes), 5, 'The correct number of routes was found.');
99     $this->assertNull($routes->get('route_f'), 'The json route was found.');
100     $this->assertNull($routes->get('route_g'), 'The xml route was not found.');
101   }
102
103   /**
104    * Confirms that the matcher throws an exception for no-route.
105    *
106    * @covers ::filter
107    */
108   public function testNoRouteFound() {
109     $matcher = new ContentTypeHeaderMatcher();
110
111     $routes = $this->fixtures->contentRouteCollection();
112     $request = Request::create('path/two', 'POST');
113     $request->headers->set('Content-type', 'application/hal+json');
114     $this->setExpectedException(UnsupportedMediaTypeHttpException::class, 'No route found that matches "Content-Type: application/hal+json"');
115     $matcher->filter($routes, $request);
116   }
117
118   /**
119    * Confirms that the matcher throws an exception for missing request header.
120    *
121    * @covers ::filter
122    */
123   public function testContentTypeRequestHeaderMissing() {
124     $matcher = new ContentTypeHeaderMatcher();
125
126     $routes = $this->fixtures->contentRouteCollection();
127     $request = Request::create('path/two', 'POST');
128     // Delete all request headers that Request::create() sets by default.
129     $request->headers = new ParameterBag();
130     $this->setExpectedException(UnsupportedMediaTypeHttpException::class, 'No "Content-Type" request header specified');
131     $matcher->filter($routes, $request);
132   }
133
134 }