function FormBuilderTest::testUniqueHtmlId

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

Tests that HTML IDs are unique when rebuilding a form with errors.

File

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

Class

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

Namespace

Drupal\Tests\Core\Form

Code

public function testUniqueHtmlId() {
    $form_id = 'test_form_id';
    $expected_form = $form_id();
    $expected_form['test']['#required'] = TRUE;
    // Mock a form object that will be built two 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(2))
        ->method('buildForm')
        ->willReturn($expected_form);
    $form_state = new FormState();
    $form = $this->simulateFormSubmission($form_id, $form_arg, $form_state);
    $this->assertSame('test-form-id', $form['#id']);
    $form_state = new FormState();
    $form = $this->simulateFormSubmission($form_id, $form_arg, $form_state);
    $this->assertSame('test-form-id--2', $form['#id']);
}

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