You are here

public function BrowserTestBaseTest::testForm in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php \Drupal\FunctionalTests\BrowserTestBaseTest::testForm()
  2. 9 core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php \Drupal\FunctionalTests\BrowserTestBaseTest::testForm()

Tests basic form functionality.

File

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

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 submitForm().
  $this
    ->drupalGet('form-test/object-builder');

  // Submit the form using the button label.
  $this
    ->submitForm([
    'bananas' => 'red',
  ], 'Save');
  $value = $config_factory
    ->get('form_test.object')
    ->get('bananas');
  $this
    ->assertSame('red', $value);
  $this
    ->submitForm([], 'Save');
  $value = $config_factory
    ->get('form_test.object')
    ->get('bananas');
  $this
    ->assertSame('', $value);

  // Submit the form using the button id.
  $this
    ->submitForm([
    'bananas' => 'blue',
  ], 'edit-submit');
  $value = $config_factory
    ->get('form_test.object')
    ->get('bananas');
  $this
    ->assertSame('blue', $value);

  // Submit the form using the button name.
  $this
    ->submitForm([
    'bananas' => 'purple',
  ], 'op');
  $value = $config_factory
    ->get('form_test.object')
    ->get('bananas');
  $this
    ->assertSame('purple', $value);

  // Test submitForm() with no-html response.
  $this
    ->drupalGet('form_test/form-state-values-clean');
  $this
    ->submitForm([], 'Submit');
  $values = Json::decode($this
    ->getSession()
    ->getPage()
    ->getContent());
  $this
    ->assertSame(1000, $values['beer']);

  // Test submitForm() 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
    ->submitForm([], 'Save', 'node-page-form--2');
  $this
    ->assertSession()
    ->pageTextContains('Page form2 has been created.');
}