function FormAjaxSubscriberTest::testOnExceptionBrokenPostRequest

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. 8.9.x core/tests/Drupal/Tests/Core/Form/EventSubscriber/FormAjaxSubscriberTest.php \Drupal\Tests\Core\Form\EventSubscriber\FormAjaxSubscriberTest::testOnExceptionBrokenPostRequest()
  3. 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 165

Class

FormAjaxSubscriberTest
@coversDefaultClass <a href="/api/drupal/core%21lib%21Drupal%21Core%21Form%21EventSubscriber%21FormAjaxSubscriber.php/class/FormAjaxSubscriber/11.x" title="Wraps AJAX form submissions that are triggered via an exception." class="local">\Drupal\Core\Form\EventSubscriber\FormAjaxSubscriber</a> @group EventSubscriber

Namespace

Drupal\Tests\Core\Form\EventSubscriber

Code

public function testOnExceptionBrokenPostRequest() : void {
    $this->formAjaxResponseBuilder
        ->expects($this->never())
        ->method('buildResponse');
    $this->messenger
        ->expects($this->once())
        ->method('addError');
    $this->subscriber = new FormAjaxSubscriber($this->formAjaxResponseBuilder, $this->getStringTranslationStub(), $this->messenger);
    $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((int) (32 * 1000000.0));
    $request = new Request([
        FormBuilderInterface::AJAX_FORM_REQUEST => TRUE,
    ]);
    $event = new ExceptionEvent($this->httpKernel, $request, HttpKernelInterface::MAIN_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());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.