Newer
Older
Alex Pott
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
/**
* @file
* Contains \Drupal\Tests\Core\StackMiddleware\NegotiationMiddlewareTest.
*/
namespace Drupal\Tests\Core\StackMiddleware;
use Drupal\Core\StackMiddleware\NegotiationMiddleware;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
/**
* @coversDefaultClass \Drupal\Core\StackMiddleware\NegotiationMiddleware
* @group NegotiationMiddleware
*/
class NegotiationMiddlewareTest extends UnitTestCase {
/**
* @var \Symfony\Component\HttpKernel\HttpKernelInterface
*/
protected $app;
/**
* @var \Drupal\Tests\Core\StackMiddleware\StubNegotiationMiddleware
*/
protected $contentNegotiation;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->app = $this->prophesize(HttpKernelInterface::class);
$this->contentNegotiation = new StubNegotiationMiddleware($this->app->reveal());
}
/**
* Tests the getContentType() method with AJAX iframe upload.
*
* @covers ::getContentType
*/
public function testAjaxIframeUpload() {
$request = new Request();
catch
committed
$request->request->set('ajax_iframe_upload', '1');
Alex Pott
committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
$this->assertSame('iframeupload', $this->contentNegotiation->getContentType($request));
}
/**
* Tests the specifying a format via query parameters gets used.
*
* @covers ::getContentType
*/
public function testFormatViaQueryParameter() {
$request = new Request();
$request->query->set('_format', 'bob');
$this->assertSame('bob', $this->contentNegotiation->getContentType($request));
}
/**
* Tests the getContentType() method when no priority format is found.
*
* @covers ::getContentType
*/
public function testUnknowContentTypeReturnsHtmlByDefault() {
$request = new Request();
$this->assertSame('html', $this->contentNegotiation->getContentType($request));
}
/**
* Tests the getContentType() method when no priority format is found but it's an AJAX request.
*
* @covers ::getContentType
*/
public function testUnknowContentTypeButAjaxRequest() {
$request = new Request();
$request->headers->set('X-Requested-With', 'XMLHttpRequest');
$this->assertSame('html', $this->contentNegotiation->getContentType($request));
}
/**
* Test that handle() correctly hands off to sub application.
*
* @covers ::handle
*/
public function testHandle() {
$request = $this->prophesize(Request::class);
// Default empty format list should not set any formats.
$request->setFormat()->shouldNotBeCalled();
// Request format will be set with default format.
$request->setRequestFormat('html')->shouldBeCalled();
// Some getContentType calls we don't really care about but have to mock.
catch
committed
$request_data = $this->prophesize(ParameterBag::class);
$request_data->get('ajax_iframe_upload', FALSE)->shouldBeCalled();
Alex Pott
committed
$request_mock = $request->reveal();
$request_mock->query = new ParameterBag([]);
catch
committed
$request_mock->request = $request_data->reveal();
Alex Pott
committed
// Calling kernel app with default arguments.
$this->app->handle($request_mock, HttpKernelInterface::MASTER_REQUEST, TRUE)
->shouldBeCalled();
$this->contentNegotiation->handle($request_mock);
// Calling kernel app with specified arguments.
$this->app->handle($request_mock, HttpKernelInterface::SUB_REQUEST, FALSE)
->shouldBeCalled();
$this->contentNegotiation->handle($request_mock, HttpKernelInterface::SUB_REQUEST, FALSE);
}
/**
* @covers ::registerFormat
*/
public function testSetFormat() {
$request = $this->prophesize(Request::class);
// Default empty format list should not set any formats.
$request->setFormat('david', 'geeky/david')->shouldBeCalled();
// Some calls we don't care about.
$request->setRequestFormat('html')->shouldBeCalled();
catch
committed
$request_data = $this->prophesize(ParameterBag::class);
$request_data->get('ajax_iframe_upload', FALSE)->shouldBeCalled();
Alex Pott
committed
$request_mock = $request->reveal();
$request_mock->query = new ParameterBag([]);
catch
committed
$request_mock->request = $request_data->reveal();
Alex Pott
committed
// Trigger handle.
$this->contentNegotiation->registerFormat('david', 'geeky/david');
$this->contentNegotiation->handle($request_mock);
}
}
class StubNegotiationMiddleware extends NegotiationMiddleware {
public function getContentType(Request $request) {
return parent::getContentType($request);
}