You are here

public function FormBuilderTest::testRebuildForm in Drupal 8

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

Class

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

Namespace

Drupal\Tests\Core\Form

Code

public function testRebuildForm() {
  $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')
    ->will($this
    ->returnValue($form_id));
  $form_arg
    ->expects($this
    ->exactly(4))
    ->method('buildForm')
    ->will($this
    ->returnValue($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());
}