7b5217d4c5ee342a3c32e3cdf6e4353d80c22163
[yaffs-website] / web / core / tests / Drupal / Tests / Core / StackMiddleware / NegotiationMiddlewareTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Tests\Core\StackMiddleware\NegotiationMiddlewareTest.
6  */
7
8 namespace Drupal\Tests\Core\StackMiddleware;
9
10 use Drupal\Core\StackMiddleware\NegotiationMiddleware;
11 use Drupal\Tests\UnitTestCase;
12 use Symfony\Component\HttpFoundation\ParameterBag;
13 use Symfony\Component\HttpFoundation\Request;
14 use Symfony\Component\HttpKernel\HttpKernelInterface;
15
16 /**
17  * @coversDefaultClass \Drupal\Core\StackMiddleware\NegotiationMiddleware
18  * @group NegotiationMiddleware
19  */
20 class NegotiationMiddlewareTest extends UnitTestCase {
21
22   /**
23    * @var \Symfony\Component\HttpKernel\HttpKernelInterface
24    */
25   protected $app;
26
27   /**
28    * @var \Drupal\Tests\Core\StackMiddleware\StubNegotiationMiddleware
29    */
30   protected $contentNegotiation;
31
32   /**
33    * {@inheritdoc}
34    */
35   protected function setUp() {
36     parent::setUp();
37
38     $this->app = $this->prophesize(HttpKernelInterface::class);
39     $this->contentNegotiation = new StubNegotiationMiddleware($this->app->reveal());
40   }
41
42   /**
43    * Tests the getContentType() method with AJAX iframe upload.
44    *
45    * @covers ::getContentType
46    */
47   public function testAjaxIframeUpload() {
48     $request = new Request();
49     $request->request->set('ajax_iframe_upload', '1');
50
51     $this->assertSame('iframeupload', $this->contentNegotiation->getContentType($request));
52   }
53
54   /**
55    * Tests the specifying a format via query parameters gets used.
56    *
57    * @covers ::getContentType
58    */
59   public function testFormatViaQueryParameter() {
60     $request = new Request();
61     $request->query->set('_format', 'bob');
62
63     $this->assertSame('bob', $this->contentNegotiation->getContentType($request));
64   }
65
66   /**
67    * Tests the getContentType() method when no priority format is found.
68    *
69    * @covers ::getContentType
70    */
71   public function testUnknowContentTypeReturnsNull() {
72     $request = new Request();
73
74     $this->assertNull($this->contentNegotiation->getContentType($request));
75   }
76
77   /**
78    * Tests the getContentType() method when no priority format is found but it's an AJAX request.
79    *
80    * @covers ::getContentType
81    */
82   public function testUnknowContentTypeButAjaxRequest() {
83     $request = new Request();
84     $request->headers->set('X-Requested-With', 'XMLHttpRequest');
85
86     $this->assertNull($this->contentNegotiation->getContentType($request));
87   }
88
89   /**
90    * Test that handle() correctly hands off to sub application.
91    *
92    * @covers ::handle
93    */
94   public function testHandle() {
95     $request = $this->prophesize(Request::class);
96
97     // Default empty format list should not set any formats.
98     $request->setFormat()->shouldNotBeCalled();
99
100     // Request format will be set with default format.
101     $request->setRequestFormat()->shouldNotBeCalled();
102
103     // Some getContentType calls we don't really care about but have to mock.
104     $request_data = $this->prophesize(ParameterBag::class);
105     $request_data->get('ajax_iframe_upload', FALSE)->shouldBeCalled();
106     $request_mock = $request->reveal();
107     $request_mock->query = new ParameterBag([]);
108     $request_mock->request = $request_data->reveal();
109
110     // Calling kernel app with default arguments.
111     $this->app->handle($request_mock, HttpKernelInterface::MASTER_REQUEST, TRUE)
112       ->shouldBeCalled();
113     $this->contentNegotiation->handle($request_mock);
114     // Calling kernel app with specified arguments.
115     $this->app->handle($request_mock, HttpKernelInterface::SUB_REQUEST, FALSE)
116       ->shouldBeCalled();
117     $this->contentNegotiation->handle($request_mock, HttpKernelInterface::SUB_REQUEST, FALSE);
118   }
119
120   /**
121    * @covers ::registerFormat
122    */
123   public function testSetFormat() {
124     $request = $this->prophesize(Request::class);
125
126     // Default empty format list should not set any formats.
127     $request->setFormat('david', 'geeky/david')->shouldBeCalled();
128
129     // Some calls we don't care about.
130     $request->setRequestFormat()->shouldNotBeCalled();
131     $request_data = $this->prophesize(ParameterBag::class);
132     $request_data->get('ajax_iframe_upload', FALSE)->shouldBeCalled();
133     $request_mock = $request->reveal();
134     $request_mock->query = new ParameterBag([]);
135     $request_mock->request = $request_data->reveal();
136
137     // Trigger handle.
138     $this->contentNegotiation->registerFormat('david', 'geeky/david');
139     $this->contentNegotiation->handle($request_mock);
140   }
141
142 }
143
144 class StubNegotiationMiddleware extends NegotiationMiddleware {
145
146   public function getContentType(Request $request) {
147     return parent::getContentType($request);
148   }
149
150 }