function FormBuilderTest::testHandleRedirectWithResponse

Same name in other branches
  1. 8.9.x core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php \Drupal\Tests\Core\Form\FormBuilderTest::testHandleRedirectWithResponse()
  2. 10 core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php \Drupal\Tests\Core\Form\FormBuilderTest::testHandleRedirectWithResponse()
  3. 11.x core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php \Drupal\Tests\Core\Form\FormBuilderTest::testHandleRedirectWithResponse()

Tests the handling of a redirect when FormStateInterface::$response exists.

File

core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php, line 191

Class

FormBuilderTest
@coversDefaultClass \Drupal\Core\Form\FormBuilder @group Form

Namespace

Drupal\Tests\Core\Form

Code

public function testHandleRedirectWithResponse() {
    $form_id = 'test_form_id';
    $expected_form = $form_id();
    // Set up a response that will be used.
    $response = $this->getMockBuilder('Symfony\\Component\\HttpFoundation\\Response')
        ->disableOriginalConstructor()
        ->getMock();
    // Set up a redirect that will not be called.
    $redirect = $this->getMockBuilder('Symfony\\Component\\HttpFoundation\\RedirectResponse')
        ->disableOriginalConstructor()
        ->getMock();
    $form_arg = $this->getMockForm($form_id, $expected_form);
    $form_arg->expects($this->any())
        ->method('submitForm')
        ->willReturnCallback(function ($form, FormStateInterface $form_state) use ($response, $redirect) {
        // Set both the response and the redirect.
        $form_state->setResponse($response);
        $form_state->set('redirect', $redirect);
    });
    $form_state = new FormState();
    try {
        $input['form_id'] = $form_id;
        $form_state->setUserInput($input);
        $this->simulateFormSubmission($form_id, $form_arg, $form_state, FALSE);
        $this->fail('EnforcedResponseException was not thrown.');
    } catch (EnforcedResponseException $e) {
        $this->assertSame($response, $e->getResponse());
    }
    $this->assertSame($response, $form_state->getResponse());
}

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