function FormBuilderTest::testRebuildForm

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

Tests the rebuildForm() method for a POST submission.

File

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

Class

FormBuilderTest
@coversDefaultClass \Drupal\Core\Form\FormBuilder[[api-linebreak]] @group Form

Namespace

Drupal\Tests\Core\Form

Code

public function testRebuildForm() : 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 = $this->formBuilder
    ->buildForm($form_arg, $form_state);
  $original_build_id = $form['#build_id'];
  $this->request
    ->setMethod('POST');
  $form_state->setRequestMethod('POST');
  // 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->assertTrue($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->assertTrue($form_state->isCached());
}

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