function FormBuilderTest::testRebuildFormOnGetRequest

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

Tests the rebuildForm() method for a GET submission.

File

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

Class

FormBuilderTest
@coversDefaultClass <a href="/api/drupal/core%21lib%21Drupal%21Core%21Form%21FormBuilder.php/class/FormBuilder/11.x" title="Provides form building and processing." class="local">\Drupal\Core\Form\FormBuilder</a> @group Form

Namespace

Drupal\Tests\Core\Form

Code

public function testRebuildFormOnGetRequest() : void {
    $form_id = 'test_form_id';
    $expected_form = $form_id();
    // The form will be built four times.
    $form_arg = $this->createMock('Drupal\\Core\\Form\\FormInterface');
    $form_arg->expects($this->exactly(2))
        ->method('getFormId')
        ->willReturn($form_id);
    $form_arg->expects($this->exactly(4))
        ->method('buildForm')
        ->willReturn($expected_form);
    // Do an initial build of the form and track the build ID.
    $form_state = new FormState();
    $form_state->setMethod('GET');
    $form = $this->formBuilder
        ->buildForm($form_arg, $form_state);
    $original_build_id = $form['#build_id'];
    // Rebuild the form, and assert that the build ID has not changed.
    $form_state->setRebuild();
    $input['form_id'] = $form_id;
    $form_state->setUserInput($input);
    $form_state->addRebuildInfo('copy', [
        '#build_id' => TRUE,
    ]);
    $this->formBuilder
        ->processForm($form_id, $form, $form_state);
    $this->assertSame($original_build_id, $form['#build_id']);
    $this->assertFalse($form_state->isCached());
    // Rebuild the form again, and assert that there is a new build ID.
    $form_state->setRebuildInfo([]);
    $form = $this->formBuilder
        ->buildForm($form_arg, $form_state);
    $this->assertNotSame($original_build_id, $form['#build_id']);
    $this->assertFalse($form_state->isCached());
}

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