Updated Drupal to 8.6. This goes with the following updates because it's possible...
[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       ['DELETE'],
65     ];
66   }
67
68   /**
69    * Tests that XML-restricted routes get filtered out on JSON requests.
70    */
71   public function testJsonRequest() {
72     $collection = $this->fixtures->sampleRouteCollection();
73     $collection->addCollection($this->fixtures->contentRouteCollection());
74
75     $request = Request::create('path/two', 'POST');
76     $request->headers->set('Content-type', 'application/json');
77     $routes = $this->matcher->filter($collection, $request);
78     $this->assertEquals(count($routes), 6, 'The correct number of routes was found.');
79     $this->assertNotNull($routes->get('route_f'), 'The json route was found.');
80     $this->assertNull($routes->get('route_g'), 'The xml route was not found.');
81     foreach ($routes as $name => $route) {
82       $this->assertEquals($name, 'route_f', 'The json route is the first one in the collection.');
83       break;
84     }
85   }
86
87   /**
88    * Tests route filtering on POST form submission requests.
89    */
90   public function testPostForm() {
91     $collection = $this->fixtures->sampleRouteCollection();
92     $collection->addCollection($this->fixtures->contentRouteCollection());
93
94     // Test that all XML and JSON restricted routes get filtered out on a POST
95     // form submission.
96     $request = Request::create('path/two', 'POST');
97     $request->headers->set('Content-type', 'application/www-form-urlencoded');
98     $routes = $this->matcher->filter($collection, $request);
99     $this->assertEquals(count($routes), 5, 'The correct number of routes was found.');
100     $this->assertNull($routes->get('route_f'), 'The json route was found.');
101     $this->assertNull($routes->get('route_g'), 'The xml route was not found.');
102   }
103
104   /**
105    * Confirms that the matcher throws an exception for no-route.
106    *
107    * @covers ::filter
108    */
109   public function testNoRouteFound() {
110     $matcher = new ContentTypeHeaderMatcher();
111
112     $routes = $this->fixtures->contentRouteCollection();
113     $request = Request::create('path/two', 'POST');
114     $request->headers->set('Content-type', 'application/hal+json');
115     $this->setExpectedException(UnsupportedMediaTypeHttpException::class, 'No route found that matches "Content-Type: application/hal+json"');
116     $matcher->filter($routes, $request);
117   }
118
119   /**
120    * Confirms that the matcher throws an exception for missing request header.
121    *
122    * @covers ::filter
123    */
124   public function testContentTypeRequestHeaderMissing() {
125     $matcher = new ContentTypeHeaderMatcher();
126
127     $routes = $this->fixtures->contentRouteCollection();
128     $request = Request::create('path/two', 'POST');
129     // Delete all request headers that Request::create() sets by default.
130     $request->headers = new ParameterBag();
131     $this->setExpectedException(UnsupportedMediaTypeHttpException::class, 'No "Content-Type" request header specified');
132     $matcher->filter($routes, $request);
133   }
134
135 }