You are here

public function FormBuilderTest::testHandleRedirectWithResponse in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php \Drupal\Tests\Core\Form\FormBuilderTest::testHandleRedirectWithResponse()
  2. 9 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 178
Contains \Drupal\Tests\Core\Form\FormBuilderTest.

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());
}