function FormAjaxSubscriberTest::testOnExceptionBrokenPostRequest

Same name and namespace in other branches
  1. 8.9.x 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()
  3. 11.x 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 163

Class

FormAjaxSubscriberTest
@coversDefaultClass <a href="/api/drupal/core%21lib%21Drupal%21Core%21Form%21EventSubscriber%21FormAjaxSubscriber.php/class/FormAjaxSubscriber/9" 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() {
    $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,
    ])
        ->onlyMethods([
        '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 ExceptionEvent($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());
}

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