You are here

public function FormBuilderTest::testHandleFormStateResponse 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::testHandleFormStateResponse()
  2. 9 core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php \Drupal\Tests\Core\Form\FormBuilderTest::testHandleFormStateResponse()

Tests the handling of FormStateInterface::$response.

@dataProvider formStateResponseProvider

File

core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php, line 137
Contains \Drupal\Tests\Core\Form\FormBuilderTest.

Class

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

Namespace

Drupal\Tests\Core\Form

Code

public function testHandleFormStateResponse($class, $form_state_key) {
  $form_id = 'test_form_id';
  $expected_form = $form_id();
  $response = $this
    ->getMockBuilder($class)
    ->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, $form_state_key) {
    $form_state
      ->setFormState([
      $form_state_key => $response,
    ]);
  });
  $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());
}