You are here

public function FormAjaxSubscriberTest::testOnExceptionBrokenPostRequest in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Form/EventSubscriber/FormAjaxSubscriberTest.php \Drupal\Tests\Core\Form\EventSubscriber\FormAjaxSubscriberTest::testOnExceptionBrokenPostRequest()
  2. 10 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 156

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->messenger
    ->expects($this
    ->once())
    ->method('addError');
  $this->subscriber = $this
    ->getMockBuilder('\\Drupal\\Core\\Form\\EventSubscriber\\FormAjaxSubscriber')
    ->setConstructorArgs([
    $this->formAjaxResponseBuilder,
    $this
      ->getStringTranslationStub(),
    $this->messenger,
  ])
    ->setMethods([
    'formatSize',
  ])
    ->getMock();
  $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
    ->createMock('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);
  $this
    ->assertTrue($event
    ->isAllowingCustomResponseCode());
  $actual_response = $event
    ->getResponse();
  $this
    ->assertInstanceOf('\\Drupal\\Core\\Ajax\\AjaxResponse', $actual_response);
  $this
    ->assertSame(200, $actual_response
    ->getStatusCode());
  $expected_commands[] = [
    'command' => 'insert',
    'method' => 'prepend',
    'selector' => NULL,
    'data' => $rendered_output,
    'settings' => NULL,
  ];
  $this
    ->assertSame($expected_commands, $actual_response
    ->getCommands());
}