You are here

public function FormAjaxSubscriberTest::testOnExceptionBrokenPostRequest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Core/Form/EventSubscriber/FormAjaxSubscriberTest.php \Drupal\Tests\Core\Form\EventSubscriber\FormAjaxSubscriberTest::testOnExceptionBrokenPostRequest()

@covers ::onException

File

core/tests/Drupal/Tests/Core/Form/EventSubscriber/FormAjaxSubscriberTest.php, line 150
Contains \Drupal\Tests\Core\Form\EventSubscriber\FormAjaxSubscriberTest.

Class

FormAjaxSubscriberTest
@coversDefaultClass \Drupal\Core\Form\EventSubscriber\FormAjaxSubscriber @group EventSubscriber

Namespace

Drupal\Tests\Core\Form\EventSubscriber

Code

public function testOnExceptionBrokenPostRequest() {
  $this->formAjaxResponseBuilder
    ->expects($this
    ->never())
    ->method('buildResponse');
  $this->subscriber = $this
    ->getMockBuilder('\\Drupal\\Core\\Form\\EventSubscriber\\FormAjaxSubscriber')
    ->setConstructorArgs([
    $this->formAjaxResponseBuilder,
    $this
      ->getStringTranslationStub(),
  ])
    ->setMethods([
    'drupalSetMessage',
    'formatSize',
  ])
    ->getMock();
  $this->subscriber
    ->expects($this
    ->once())
    ->method('drupalSetMessage')
    ->willReturn('asdf');
  $this->subscriber
    ->expects($this
    ->once())
    ->method('formatSize')
    ->with(32 * 1000000.0)
    ->willReturn('32M');
  $rendered_output = 'the rendered output';

  // CommandWithAttachedAssetsTrait::getRenderedContent() will call the
  // renderer service via the container.
  $renderer = $this
    ->getMock('Drupal\\Core\\Render\\RendererInterface');
  $renderer
    ->expects($this
    ->once())
    ->method('renderRoot')
    ->with()
    ->willReturnCallback(function (&$elements) use ($rendered_output) {
    $elements['#attached'] = [];
    return $rendered_output;
  });
  $container = new ContainerBuilder();
  $container
    ->set('renderer', $renderer);
  \Drupal::setContainer($container);
  $exception = new BrokenPostRequestException(32 * 1000000.0);
  $request = new Request([
    FormBuilderInterface::AJAX_FORM_REQUEST => TRUE,
  ]);
  $event = new GetResponseForExceptionEvent($this->httpKernel, $request, HttpKernelInterface::MASTER_REQUEST, $exception);
  $this->subscriber
    ->onException($event);
  $actual_response = $event
    ->getResponse();
  $this
    ->assertInstanceOf('\\Drupal\\Core\\Ajax\\AjaxResponse', $actual_response);
  $this
    ->assertSame(200, $actual_response->headers
    ->get('X-Status-Code'));
  $expected_commands[] = [
    'command' => 'insert',
    'method' => 'replaceWith',
    'selector' => NULL,
    'data' => $rendered_output,
    'settings' => NULL,
  ];
  $this
    ->assertSame($expected_commands, $actual_response
    ->getCommands());
}