You are here

public function FormBuilderTest::testRebuildFormOnGetRequest 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::testRebuildFormOnGetRequest()
  2. 9 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 384
Contains \Drupal\Tests\Core\Form\FormBuilderTest.

Class

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

Namespace

Drupal\Tests\Core\Form

Code

public function testRebuildFormOnGetRequest() {
  $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_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());
}