NegotiationMiddlewareTest.php in Drupal 8
File
core/tests/Drupal/Tests/Core/StackMiddleware/NegotiationMiddlewareTest.php
View source
<?php
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;
class NegotiationMiddlewareTest extends UnitTestCase {
protected $app;
protected $contentNegotiation;
protected function setUp() {
parent::setUp();
$this->app = $this
->prophesize(HttpKernelInterface::class);
$this->contentNegotiation = new StubNegotiationMiddleware($this->app
->reveal());
}
public function testAjaxIframeUpload() {
$request = new Request();
$request->request
->set('ajax_iframe_upload', '1');
$this
->assertSame('iframeupload', $this->contentNegotiation
->getContentType($request));
}
public function testFormatViaQueryParameter() {
$request = new Request();
$request->query
->set('_format', 'bob');
$this
->assertSame('bob', $this->contentNegotiation
->getContentType($request));
}
public function testUnknowContentTypeReturnsNull() {
$request = new Request();
$this
->assertNull($this->contentNegotiation
->getContentType($request));
}
public function testUnknowContentTypeButAjaxRequest() {
$request = new Request();
$request->headers
->set('X-Requested-With', 'XMLHttpRequest');
$this
->assertNull($this->contentNegotiation
->getContentType($request));
}
public function testHandle() {
$request = $this
->prophesize(Request::class);
$request
->setFormat()
->shouldNotBeCalled();
$request
->setRequestFormat()
->shouldNotBeCalled();
$request_data = $this
->prophesize(ParameterBag::class);
$request_data
->get('ajax_iframe_upload', FALSE)
->shouldBeCalled();
$request_mock = $request
->reveal();
$request_mock->query = new ParameterBag([]);
$request_mock->request = $request_data
->reveal();
$this->app
->handle($request_mock, HttpKernelInterface::MASTER_REQUEST, TRUE)
->shouldBeCalled();
$this->contentNegotiation
->handle($request_mock);
$this->app
->handle($request_mock, HttpKernelInterface::SUB_REQUEST, FALSE)
->shouldBeCalled();
$this->contentNegotiation
->handle($request_mock, HttpKernelInterface::SUB_REQUEST, FALSE);
}
public function testSetFormat() {
$request = $this
->prophesize(Request::class);
$request
->setFormat('david', 'geeky/david')
->shouldBeCalled();
$request
->setRequestFormat()
->shouldNotBeCalled();
$request_data = $this
->prophesize(ParameterBag::class);
$request_data
->get('ajax_iframe_upload', FALSE)
->shouldBeCalled();
$request_mock = $request
->reveal();
$request_mock->query = new ParameterBag([]);
$request_mock->request = $request_data
->reveal();
$this->contentNegotiation
->registerFormat('david', 'geeky/david');
$this->contentNegotiation
->handle($request_mock);
}
}
class StubNegotiationMiddleware extends NegotiationMiddleware {
public function getContentType(Request $request) {
return parent::getContentType($request);
}
}