function BrowserTestBaseTest::testForm

Same name in other branches
  1. 9 core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php \Drupal\FunctionalTests\BrowserTestBaseTest::testForm()
  2. 10 core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php \Drupal\FunctionalTests\BrowserTestBaseTest::testForm()
  3. 11.x core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php \Drupal\FunctionalTests\BrowserTestBaseTest::testForm()

Tests basic form functionality.

File

core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php, line 99

Class

BrowserTestBaseTest
Tests BrowserTestBase functionality.

Namespace

Drupal\FunctionalTests

Code

public function testForm() {
    // Ensure the proper response code for a _form route.
    $this->drupalGet('form-test/object-builder');
    $this->assertSession()
        ->statusCodeEquals(200);
    // Ensure the form and text field exist.
    $this->assertSession()
        ->elementExists('css', 'form#form-test-form-test-object');
    $this->assertSession()
        ->fieldExists('bananas');
    // Check that the hidden field exists and has a specific value.
    $this->assertSession()
        ->hiddenFieldExists('strawberry');
    $this->assertSession()
        ->hiddenFieldExists('red');
    $this->assertSession()
        ->hiddenFieldExists('redstrawberryhiddenfield');
    $this->assertSession()
        ->hiddenFieldValueNotEquals('strawberry', 'brown');
    $this->assertSession()
        ->hiddenFieldValueEquals('strawberry', 'red');
    // Check that a hidden field does not exist.
    $this->assertSession()
        ->hiddenFieldNotExists('bananas');
    $this->assertSession()
        ->hiddenFieldNotExists('pineapple');
    $edit = [
        'bananas' => 'green',
    ];
    $this->submitForm($edit, 'Save', 'form-test-form-test-object');
    $config_factory = $this->container
        ->get('config.factory');
    $value = $config_factory->get('form_test.object')
        ->get('bananas');
    $this->assertSame('green', $value);
    // Test drupalPostForm().
    $edit = [
        'bananas' => 'red',
    ];
    // Submit the form using the button label.
    $result = $this->drupalPostForm('form-test/object-builder', $edit, 'Save');
    $this->assertSame($this->getSession()
        ->getPage()
        ->getContent(), $result);
    $value = $config_factory->get('form_test.object')
        ->get('bananas');
    $this->assertSame('red', $value);
    $this->drupalPostForm('form-test/object-builder', NULL, 'Save');
    $value = $config_factory->get('form_test.object')
        ->get('bananas');
    $this->assertSame('', $value);
    // Submit the form using the button id.
    $edit = [
        'bananas' => 'blue',
    ];
    $result = $this->drupalPostForm('form-test/object-builder', $edit, 'edit-submit');
    $this->assertSame($this->getSession()
        ->getPage()
        ->getContent(), $result);
    $value = $config_factory->get('form_test.object')
        ->get('bananas');
    $this->assertSame('blue', $value);
    // Submit the form using the button name.
    $edit = [
        'bananas' => 'purple',
    ];
    $result = $this->drupalPostForm('form-test/object-builder', $edit, 'op');
    $this->assertSame($this->getSession()
        ->getPage()
        ->getContent(), $result);
    $value = $config_factory->get('form_test.object')
        ->get('bananas');
    $this->assertSame('purple', $value);
    // Test drupalPostForm() with no-html response.
    $values = Json::decode($this->drupalPostForm('form_test/form-state-values-clean', [], t('Submit')));
    $this->assertSame(1000, $values['beer']);
    // Test drupalPostForm() with form by HTML id.
    $this->drupalCreateContentType([
        'type' => 'page',
    ]);
    $this->drupalLogin($this->drupalCreateUser([
        'create page content',
    ]));
    $this->drupalGet('form-test/two-instances-of-same-form');
    $this->getSession()
        ->getPage()
        ->fillField('edit-title-0-value', 'form1');
    $this->getSession()
        ->getPage()
        ->fillField('edit-title-0-value--2', 'form2');
    $this->drupalPostForm(NULL, [], 'Save', [], 'node-page-form--2');
    $this->assertSession()
        ->pageTextContains('Page form2 has been created.');
}

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