5 * Contains \Drupal\Tests\Core\StackMiddleware\NegotiationMiddlewareTest.
8 namespace Drupal\Tests\Core\StackMiddleware;
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;
17 * @coversDefaultClass \Drupal\Core\StackMiddleware\NegotiationMiddleware
18 * @group NegotiationMiddleware
20 class NegotiationMiddlewareTest extends UnitTestCase {
23 * @var \Symfony\Component\HttpKernel\HttpKernelInterface
28 * @var \Drupal\Tests\Core\StackMiddleware\StubNegotiationMiddleware
30 protected $contentNegotiation;
35 protected function setUp() {
38 $this->app = $this->prophesize(HttpKernelInterface::class);
39 $this->contentNegotiation = new StubNegotiationMiddleware($this->app->reveal());
43 * Tests the getContentType() method with AJAX iframe upload.
45 * @covers ::getContentType
47 public function testAjaxIframeUpload() {
48 $request = new Request();
49 $request->request->set('ajax_iframe_upload', '1');
51 $this->assertSame('iframeupload', $this->contentNegotiation->getContentType($request));
55 * Tests the specifying a format via query parameters gets used.
57 * @covers ::getContentType
59 public function testFormatViaQueryParameter() {
60 $request = new Request();
61 $request->query->set('_format', 'bob');
63 $this->assertSame('bob', $this->contentNegotiation->getContentType($request));
67 * Tests the getContentType() method when no priority format is found.
69 * @covers ::getContentType
71 public function testUnknowContentTypeReturnsHtmlByDefault() {
72 $request = new Request();
74 $this->assertSame('html', $this->contentNegotiation->getContentType($request));
78 * Tests the getContentType() method when no priority format is found but it's an AJAX request.
80 * @covers ::getContentType
82 public function testUnknowContentTypeButAjaxRequest() {
83 $request = new Request();
84 $request->headers->set('X-Requested-With', 'XMLHttpRequest');
86 $this->assertSame('html', $this->contentNegotiation->getContentType($request));
90 * Test that handle() correctly hands off to sub application.
94 public function testHandle() {
95 $request = $this->prophesize(Request::class);
97 // Default empty format list should not set any formats.
98 $request->setFormat()->shouldNotBeCalled();
100 // Request format will be set with default format.
101 $request->setRequestFormat('html')->shouldBeCalled();
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();
110 // Calling kernel app with default arguments.
111 $this->app->handle($request_mock, HttpKernelInterface::MASTER_REQUEST, TRUE)
113 $this->contentNegotiation->handle($request_mock);
114 // Calling kernel app with specified arguments.
115 $this->app->handle($request_mock, HttpKernelInterface::SUB_REQUEST, FALSE)
117 $this->contentNegotiation->handle($request_mock, HttpKernelInterface::SUB_REQUEST, FALSE);
121 * @covers ::registerFormat
123 public function testSetFormat() {
124 $request = $this->prophesize(Request::class);
126 // Default empty format list should not set any formats.
127 $request->setFormat('david', 'geeky/david')->shouldBeCalled();
129 // Some calls we don't care about.
130 $request->setRequestFormat('html')->shouldBeCalled();
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();
138 $this->contentNegotiation->registerFormat('david', 'geeky/david');
139 $this->contentNegotiation->handle($request_mock);
144 class StubNegotiationMiddleware extends NegotiationMiddleware {
145 public function getContentType(Request $request) { return parent::getContentType($request); }