function FapiExampleTest::doTestAjaxAddMore
Same name in other branches
- 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 modules/
form_api_example/ tests/ src/ Functional/ FapiExampleTest.php - Aggregate all the tests.
File
-
modules/
form_api_example/ tests/ src/ Functional/ FapiExampleTest.php, line 286
Class
- FapiExampleTest
- Ensure that the form_api_example forms work properly.
Namespace
Drupal\Tests\form_api_example\FunctionalCode
public function doTestAjaxAddMore() {
$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);
$assert_session = $this->assertSession();
$assert_session->statusCodeEquals(200);
// Verify that there is no remove button.
$assert_session->buttonNotExists('Remove one');
$name_one = 'John';
$name_two = 'Smith';
// Enter the value in field-1.
// and click on 'Add one more' button.
$assert_session->fieldExists('names_fieldset[name][0]')
->setValue($name_one);
$assert_session->buttonExists('Add one more')
->press();
// Verify field-2 gets added.
// and value of field-1 should be retained.
$assert_session->fieldValueEquals('names_fieldset[name][0]', $name_one);
$assert_session->fieldValueEquals('names_fieldset[name][1]', '');
$assert_session->buttonExists('Remove one');
// Enter the value in field-2
// and click on 'Add one more' button.
$assert_session->fieldExists('names_fieldset[name][1]')
->setValue($name_two);
$assert_session->buttonExists('Add one more')
->press();
// Verify field-3 gets added.
// and value of field-1 and field-2 are retained.
$assert_session->fieldValueEquals('names_fieldset[name][0]', $name_one);
$assert_session->fieldValueEquals('names_fieldset[name][1]', $name_two);
$assert_session->fieldValueEquals('names_fieldset[name][2]', '');
// Click on "Remove one" button to test remove button works.
// and value of field-1 and field-2 are retained.
$assert_session->buttonExists('Remove one')
->press();
$assert_session->fieldValueEquals('names_fieldset[name][0]', $name_one);
$assert_session->fieldValueEquals('names_fieldset[name][1]', $name_two);
// Submit the form and verify the results.
$assert_session->buttonExists('Submit')
->press();
$assert_session->pageTextContains('These people are coming to the picnic: ' . $name_one . ', ' . $name_two);
}