You are here

public function FapiExampleTest::doTestAjaxAddMore in Examples for Developers 8

Same name and namespace in other branches
  1. 3.x modules/form_api_example/tests/src/Functional/FapiExampleTest.php \Drupal\Tests\form_api_example\Functional\FapiExampleTest::doTestAjaxAddMore()

Test the Ajax Add More demo form.

1 call to FapiExampleTest::doTestAjaxAddMore()
FapiExampleTest::testFunctional in form_api_example/tests/src/Functional/FapiExampleTest.php
Aggregate all the tests.

File

form_api_example/tests/src/Functional/FapiExampleTest.php, line 277

Class

FapiExampleTest
Ensure that the form_api_example forms work properly.

Namespace

Drupal\Tests\form_api_example\Functional

Code

public function doTestAjaxAddMore() {

  // XPath for the remove button. We have to use contains() here because the
  // ID will have a hash value at the end.
  $button_xpath = '//input[contains(@id,"edit-names-fieldset-actions-remove-name")]';
  $ajax_addmore_url = Url::fromRoute('form_api_example.ajax_addmore');

  // Verify that anonymous can access the ajax_add_more page.
  $this
    ->drupalGet($ajax_addmore_url);
  $this
    ->assertResponse(200);

  // Verify that there is no remove button.
  $this
    ->assertEmpty($this
    ->xpath($button_xpath));
  $name_one = 'John';
  $name_two = 'Smith';

  // Enter the value in field-1.
  // and click on 'Add one more' button.
  $edit = [];
  $edit['names_fieldset[name][0]'] = $name_one;
  $this
    ->drupalPostForm($ajax_addmore_url, $edit, 'Add one more');

  // Verify field-2 gets added.
  // and value of field-1 should retained.
  $this
    ->assertFieldsByValue($this
    ->xpath('//input[@id = "edit-names-fieldset-name-0"]'), $name_one);
  $this
    ->assertNotEmpty($this
    ->xpath('//input[@id = "edit-names-fieldset-name-1"]'));

  // Verify that the remove button was added.
  $this
    ->assertNotEmpty($this
    ->xpath($button_xpath));

  // Enter the value in field-2
  // and click on 'Add one more' button.
  $edit['names_fieldset[name][1]'] = $name_two;
  $this
    ->drupalPostForm(NULL, $edit, 'Add one more');

  // Verify field-3 gets added.
  // and value of field-1 and field-2 are retained.
  $this
    ->assertFieldsByValue($this
    ->xpath('//input[@id = "edit-names-fieldset-name-0"]'), $name_one);
  $this
    ->assertFieldsByValue($this
    ->xpath('//input[@id = "edit-names-fieldset-name-1"]'), $name_two);
  $this
    ->assertNotEmpty($this
    ->xpath('//input[@id = "edit-names-fieldset-name-2"]'));

  // Click on "Remove one" button to test remove button works.
  // and value of field-1 and field-2 are retained.
  $this
    ->drupalPostForm(NULL, NULL, 'Remove one');
  $this
    ->assertFieldsByValue($this
    ->xpath('//input[@id = "edit-names-fieldset-name-0"]'), $name_one);
  $this
    ->assertFieldsByValue($this
    ->xpath('//input[@id = "edit-names-fieldset-name-1"]'), $name_two);
  $this
    ->assertEmpty($this
    ->xpath('//input[@id = "edit-names-fieldset-name-2"]'));

  // Submit the form and verify the results.
  $this
    ->drupalPostForm(NULL, NULL, 'Submit');
  $this
    ->assertText('These people are coming to the picnic: ' . $name_one . ', ' . $name_two);
}