e2c73bbe95debf6f35e855bbde0d4ce142a12c51
[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 GET requests.
46    */
47   public function testGetRequestFilter() {
48     $collection = $this->fixtures->sampleRouteCollection();
49     $collection->addCollection($this->fixtures->contentRouteCollection());
50
51     $request = Request::create('path/two', 'GET');
52     $routes = $this->matcher->filter($collection, $request);
53     $this->assertEquals(count($routes), 7, 'The correct number of routes was found.');
54   }
55
56   /**
57    * Tests that XML-restricted routes get filtered out on JSON requests.
58    */
59   public function testJsonRequest() {
60     $collection = $this->fixtures->sampleRouteCollection();
61     $collection->addCollection($this->fixtures->contentRouteCollection());
62
63     $request = Request::create('path/two', 'POST');
64     $request->headers->set('Content-type', 'application/json');
65     $routes = $this->matcher->filter($collection, $request);
66     $this->assertEquals(count($routes), 6, 'The correct number of routes was found.');
67     $this->assertNotNull($routes->get('route_f'), 'The json route was found.');
68     $this->assertNull($routes->get('route_g'), 'The xml route was not found.');
69     foreach ($routes as $name => $route) {
70       $this->assertEquals($name, 'route_f', 'The json route is the first one in the collection.');
71       break;
72     }
73   }
74
75   /**
76    * Tests route filtering on POST form submission requests.
77    */
78   public function testPostForm() {
79     $collection = $this->fixtures->sampleRouteCollection();
80     $collection->addCollection($this->fixtures->contentRouteCollection());
81
82     // Test that all XML and JSON restricted routes get filtered out on a POST
83     // form submission.
84     $request = Request::create('path/two', 'POST');
85     $request->headers->set('Content-type', 'application/www-form-urlencoded');
86     $routes = $this->matcher->filter($collection, $request);
87     $this->assertEquals(count($routes), 5, 'The correct number of routes was found.');
88     $this->assertNull($routes->get('route_f'), 'The json route was found.');
89     $this->assertNull($routes->get('route_g'), 'The xml route was not found.');
90   }
91
92   /**
93    * Confirms that the matcher throws an exception for no-route.
94    *
95    * @covers ::filter
96    */
97   public function testNoRouteFound() {
98     $matcher = new ContentTypeHeaderMatcher();
99
100     $routes = $this->fixtures->contentRouteCollection();
101     $request = Request::create('path/two', 'POST');
102     $request->headers->set('Content-type', 'application/hal+json');
103     $this->setExpectedException(UnsupportedMediaTypeHttpException::class, 'No route found that matches "Content-Type: application/hal+json"');
104     $matcher->filter($routes, $request);
105   }
106
107   /**
108    * Confirms that the matcher throws an exception for missing request header.
109    *
110    * @covers ::filter
111    */
112   public function testContentTypeRequestHeaderMissing() {
113     $matcher = new ContentTypeHeaderMatcher();
114
115     $routes = $this->fixtures->contentRouteCollection();
116     $request = Request::create('path/two', 'POST');
117     // Delete all request headers that Request::create() sets by default.
118     $request->headers = new ParameterBag();
119     $this->setExpectedException(UnsupportedMediaTypeHttpException::class, 'No "Content-Type" request header specified');
120     $matcher->filter($routes, $request);
121   }
122
123 }