function FormBuilderTest::testHandleFormStateResponse

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php \Drupal\Tests\Core\Form\FormBuilderTest::testHandleFormStateResponse()
  2. 10 core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php \Drupal\Tests\Core\Form\FormBuilderTest::testHandleFormStateResponse()
  3. 11.x 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

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')
        ->will($this->returnCallback(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());
}

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