Skip to content
Snippets Groups Projects
Unverified Commit 3ba620b6 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2809535 by Lendude, martin107, dawehner, borisson_, alexpott: Convert...

Issue #2809535 by Lendude, martin107, dawehner, borisson_, alexpott: Convert AJAX part of \Drupal\system\Tests\Ajax\MultiFormTest to JavascriptTestBase
parent dc23efb6
Branches
Tags
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
<?php <?php
namespace Drupal\system\Tests\Ajax; namespace Drupal\FunctionalJavascriptTests\Ajax;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig; use Drupal\field\Entity\FieldStorageConfig;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
/** /**
* Tests that AJAX-enabled forms work when multiple instances of the same form * Tests that AJAX-enabled forms work when multiple instances of the same form
...@@ -12,15 +14,16 @@ ...@@ -12,15 +14,16 @@
* *
* @group Ajax * @group Ajax
*/ */
class MultiFormTest extends AjaxTestBase { class MultiFormTest extends WebDriverTestBase {
/** /**
* Modules to enable. * {@inheritdoc}
*
* @var array
*/ */
public static $modules = ['form_test']; public static $modules = ['node', 'form_test'];
/**
* {@inheritdoc}
*/
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
...@@ -68,31 +71,69 @@ public function testMultiForm() { ...@@ -68,31 +71,69 @@ public function testMultiForm() {
// each form. // each form.
$this->drupalGet('form-test/two-instances-of-same-form'); $this->drupalGet('form-test/two-instances-of-same-form');
$fields = $this->xpath($form_xpath . $field_xpath); // Wait for javascript on the page to prepare the form attributes.
$this->assertSession()->assertWaitOnAjaxRequest();
$session = $this->getSession();
$page = $session->getPage();
$fields = $page->findAll('xpath', $form_xpath . $field_xpath);
$this->assertEqual(count($fields), 2); $this->assertEqual(count($fields), 2);
foreach ($fields as $field) { foreach ($fields as $field) {
$this->assertEqual(count($field->xpath('.' . $field_items_xpath_suffix)), 1, 'Found the correct number of field items on the initial page.'); $this->assertEqual(count($field->find('xpath', '.' . $field_items_xpath_suffix)), 1, 'Found the correct number of field items on the initial page.');
$this->assertFieldsByValue($field->xpath('.' . $button_xpath_suffix), NULL, 'Found the "add more" button on the initial page.'); $this->assertFieldsByValue($field->find('xpath', '.' . $button_xpath_suffix), NULL, 'Found the "add more" button on the initial page.');
} }
$this->assertNoDuplicateIds(t('Initial page contains unique IDs'), 'Other'); $this->assertNoDuplicateIds();
// Submit the "add more" button of each form twice. After each corresponding // Submit the "add more" button of each form twice. After each corresponding
// page update, ensure the same as above. // page update, ensure the same as above.
for ($i = 0; $i < 2; $i++) { for ($i = 0; $i < 2; $i++) {
$forms = $this->xpath($form_xpath); $forms = $page->find('xpath', $form_xpath);
foreach ($forms as $offset => $form) { foreach ($forms as $offset => $form) {
$form_html_id = (string) $form['id']; $button = $form->findButton($button_value);
$this->drupalPostAjaxForm(NULL, [], [$button_name => $button_value], NULL, [], [], $form_html_id); $this->assertNotNull($button, 'Add Another Item button exists');
$form = $this->xpath($form_xpath)[$offset]; $button->press();
$field = $form->xpath('.' . $field_xpath);
// Wait for page update.
$this->assertEqual(count($field[0]->xpath('.' . $field_items_xpath_suffix)), $i + 2, 'Found the correct number of field items after an AJAX submission.'); $this->assertSession()->assertWaitOnAjaxRequest();
$this->assertFieldsByValue($field[0]->xpath('.' . $button_xpath_suffix), NULL, 'Found the "add more" button after an AJAX submission.');
$this->assertNoDuplicateIds(t('Updated page contains unique IDs'), 'Other'); // After AJAX request and response page will update.
$page_updated = $session->getPage();
$field = $page_updated->findAll('xpath', '.' . $field_xpath);
$this->assertEqual(count($field[0]->find('xpath', '.' . $field_items_xpath_suffix)), $i + 2, 'Found the correct number of field items after an AJAX submission.');
$this->assertFieldsByValue($field[0]->find('xpath', '.' . $button_xpath_suffix), NULL, 'Found the "add more" button after an AJAX submission.');
$this->assertNoDuplicateIds();
}
}
}
/**
* Asserts that each HTML ID is used for just a single element on the page.
*
* @param string $message
* (optional) A message to display with the assertion.
*/
protected function assertNoDuplicateIds($message = '') {
$args = ['@url' => $this->getUrl()];
if (!$elements = $this->xpath('//*[@id]')) {
$this->fail(new FormattableMarkup('The page @url contains no HTML IDs.', $args));
return;
}
$message = $message ?: new FormattableMarkup('The page @url does not contain duplicate HTML IDs', $args);
$seen_ids = [];
foreach ($elements as $element) {
$id = $element->getAttribute('id');
if (isset($seen_ids[$id])) {
$this->fail($message);
return;
} }
$seen_ids[$id] = TRUE;
} }
$this->assertTrue(TRUE, $message);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment