You are here

class NegotiationMiddlewareTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Core/StackMiddleware/NegotiationMiddlewareTest.php \Drupal\Tests\Core\StackMiddleware\NegotiationMiddlewareTest

@coversDefaultClass \Drupal\Core\StackMiddleware\NegotiationMiddleware @group NegotiationMiddleware

Hierarchy

Expanded class hierarchy of NegotiationMiddlewareTest

File

core/tests/Drupal/Tests/Core/StackMiddleware/NegotiationMiddlewareTest.php, line 20
Contains \Drupal\Tests\Core\StackMiddleware\NegotiationMiddlewareTest.

Namespace

Drupal\Tests\Core\StackMiddleware
View source
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();
    $request->attributes
      ->set('ajax_iframe_upload', '1');
    $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.
    $request
      ->get('ajax_iframe_upload', false)
      ->shouldBeCalled();
    $request_mock = $request
      ->reveal();
    $request_mock->query = new ParameterBag([]);

    // 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();
    $request
      ->get('ajax_iframe_upload', false)
      ->shouldBeCalled();
    $request_mock = $request
      ->reveal();
    $request_mock->query = new ParameterBag([]);

    // Trigger handle.
    $this->contentNegotiation
      ->registerFormat('david', 'geeky/david');
    $this->contentNegotiation
      ->handle($request_mock);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
NegotiationMiddlewareTest::$app protected property
NegotiationMiddlewareTest::$contentNegotiation protected property
NegotiationMiddlewareTest::setUp protected function Overrides UnitTestCase::setUp
NegotiationMiddlewareTest::testAjaxIframeUpload public function Tests the getContentType() method with AJAX iframe upload.
NegotiationMiddlewareTest::testFormatViaQueryParameter public function Tests the specifying a format via query parameters gets used.
NegotiationMiddlewareTest::testHandle public function Test that handle() correctly hands off to sub application.
NegotiationMiddlewareTest::testSetFormat public function @covers ::registerFormat
NegotiationMiddlewareTest::testUnknowContentTypeButAjaxRequest public function Tests the getContentType() method when no priority format is found but it's an AJAX request.
NegotiationMiddlewareTest::testUnknowContentTypeReturnsHtmlByDefault public function Tests the getContentType() method when no priority format is found.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.