diff --git a/core/modules/ckeditor5/tests/src/FunctionalJavascript/AdminUiTest.php b/core/modules/ckeditor5/tests/src/FunctionalJavascript/AdminUiTest.php index 1c003683d5..44c293a47e 100644 --- a/core/modules/ckeditor5/tests/src/FunctionalJavascript/AdminUiTest.php +++ b/core/modules/ckeditor5/tests/src/FunctionalJavascript/AdminUiTest.php @@ -34,9 +34,10 @@ public function testSettingsOnlyFireAjaxWithCkeditor5() { // Enable media embed to trigger an AJAX rebuild. $this->assertTrue($page->hasUncheckedField('filters[media_embed][status]')); - $this->assertNoAjaxRequestTriggered(); + $this->assertSame(0, $this->getAjaxResponseCount()); $page->checkField('filters[media_embed][status]'); - $this->assertSame(1, $assert_session->assertWaitOnAjaxRequest()); + $assert_session->assertWaitOnAjaxRequest(); + $this->assertSame(1, $this->getAjaxResponseCount()); // Perform the same steps as above with CKEditor, and confirm AJAX callbacks // are not triggered on settings changes. @@ -46,33 +47,25 @@ public function testSettingsOnlyFireAjaxWithCkeditor5() { // trigger an AJAX rebuild. $this->assertTrue($page->hasUncheckedField('filters[media_embed][status]')); $page->checkField('filters[media_embed][status]'); - $this->assertNoAjaxRequestTriggered(); + $this->assertSame(0, $this->getAjaxResponseCount()); // Confirm that AJAX updates happen when attempting to switch to CKEditor 5, // even if prevented from doing so by validation. $this->drupalGet('admin/config/content/formats/add'); - $this->assertFalse($assert_session->elementExists('css', '#edit-name-machine-name-suffix')->isVisible()); - $name_field = $page->findField('name'); - $name_field->setValue('trigger validator'); - // Trigger AJAX request to generate machine name by interacting with a non- - // textual field. - $name_field->blur(); - // Wait for the debounce, which only triggers the 'formUpdated` event up to - // once every 0.3 seconds. Don't wait in tests. - // @see Drupal.behaviors.formUpdated - $this->getSession()->executeScript('jQuery("input[name=name]").trigger("formUpdated");'); - $this->assertSame(1, $assert_session->assertWaitOnAjaxRequest()); - $this->assertTrue($assert_session->elementExists('css', '#edit-name-machine-name-suffix')->isVisible()); + $page->fillField('name', 'trigger validator'); + $assert_session->waitForText('Machine name'); + $page->checkField('roles[authenticated]'); // Enable a filter that is incompatible with CKEditor 5, so validation is // triggered when attempting to switch. $incompatible_filter_name = 'filters[filter_incompatible][status]'; $this->assertTrue($page->hasUncheckedField($incompatible_filter_name)); $page->checkField($incompatible_filter_name); - $this->assertNoAjaxRequestTriggered(1); + $this->assertSame(0, $this->getAjaxResponseCount()); $page->selectFieldOption('editor[editor]', 'ckeditor5'); - $this->assertSame(2, $assert_session->assertWaitOnAjaxRequest()); + $assert_session->assertWaitOnAjaxRequest(); + $this->assertSame(1, $this->getAjaxResponseCount()); $filter_warning = 'CKEditor 5 only works with HTML-based text formats. The "A TYPE_MARKUP_LANGUAGE filter incompatible with CKEditor 5" (filter_incompatible) filter implies this text format is not HTML anymore.'; @@ -85,62 +78,38 @@ public function testSettingsOnlyFireAjaxWithCkeditor5() { // been corrected. $this->assertTrue($page->hasCheckedField($incompatible_filter_name)); $page->uncheckField($incompatible_filter_name); - $this->assertSame(3, $assert_session->assertWaitOnAjaxRequest()); + $assert_session->assertWaitOnAjaxRequest(); + $this->assertSame(2, $this->getAjaxResponseCount()); $assert_session->pageTextNotContains($filter_warning); } /** - * Asserts that no (new) AJAX requests were triggered. + * Gets the Drupal AJAX response count observed on this page. * - * @param int $expected_cumulative_ajax_request_count - * The number of expected observed XHR requests since the page was loaded. + * @return int + * The number of completed XHR requests observed since the page was loaded. */ - protected function assertNoAjaxRequestTriggered(int $expected_cumulative_ajax_request_count = 0): void { - // In case of no requests triggered at all yet. - if ($expected_cumulative_ajax_request_count === 0) { - $result = $this->getSession()->evaluateScript(<<assertSame(0, $result); + protected function getAjaxResponseCount(): int { + // Half a second should suffice for any of the test's DOM interactions to + // have triggered an AJAX request, if any. + try { + $this->assertSession()->assertWaitOnAjaxRequest(500); } - else { - // In case of the non-first AJAX request, ensure that no AJAX requests are - // in progress. - try { - $this->assertSession()->assertWaitOnAjaxRequest(500); - } - catch (\RuntimeException $e) { - throw new \LogicException(sprintf('This call to %s claims there no AJAX request was triggered, but this is wrong: %s.', __METHOD__, $e->getMessage())); - } - catch (\LogicException $e) { - // This is the intent: ::assertWaitOnAjaxRequest() should detect an - // "incorrect" call, because this assertion is asserting *no* AJAX - // requests have been triggered. - assert(str_contains($e->getMessage(), 'Unnecessary')); - - $result = $this->getSession()->evaluateScript(<<assertSame($expected_cumulative_ajax_request_count, $result); - } + catch (\RuntimeException $e) { + throw new \LogicException('An AJAX request was still being processed, this suggests a assertWaitOnAjaxRequest() call is missing.'); } // Now that there definitely is no more AJAX request in progress, count the - // number of actual XHR requests, ensure they match. + // number of AJAX responses. $javascript = << entry.initiatorType === 'xmlhttprequest') + .filter(entry => entry.initiatorType === 'xmlhttprequest' && entry.name.indexOf('_wrapper_format=drupal_ajax') !== -1) .length })() JS; - $this->assertSame($expected_cumulative_ajax_request_count, $this->getSession()->evaluateScript($javascript)); + return $this->getSession()->evaluateScript($javascript); } /** @@ -150,6 +119,7 @@ public function testUnavailableFiltersHiddenWhenSwitching() { $page = $this->getSession()->getPage(); $assert_session = $this->assertSession(); $this->createNewTextFormat($page, $assert_session, 'unicorn'); + $assert_session->assertWaitOnAjaxRequest(); $assert_session->pageTextNotContains('Filter settings'); // Switching to CKEditor 5 should keep the filter settings hidden. @@ -167,6 +137,8 @@ public function testFilterCheckboxesToggleSettings() { $this->createNewTextFormat($page, $assert_session); + $assert_session->assertWaitOnAjaxRequest(); + $media_tab = $page->find('css', '[href^="#edit-filters-media-embed-settings"]'); $this->assertFalse($media_tab->isVisible(), 'Media filter settings should not be present because media filter is not enabled'); @@ -210,6 +182,7 @@ public function testMessagesDoNotAccumulate(): void { // validation error. $assert_session->waitForText('Source editing'); $page->find('css', '[href^="#edit-editor-settings-plugins-ckeditor5-sourceediting"]')->click(); + $assert_session->assertWaitOnAjaxRequest(); $assert_session->waitForText('Manually editable HTML tags'); $source_edit_tags_field = $assert_session->fieldExists('editor[settings][plugins][ckeditor5_sourceEditing][allowed_tags]'); $source_edit_tags_field->setValue(''); @@ -235,6 +208,7 @@ public function testPluginSettingsFormSection() { $assert_session = $this->assertSession(); $this->createNewTextFormat($page, $assert_session); + $assert_session->assertWaitOnAjaxRequest(); // The default toolbar only enables the configurable heading plugin and the // non-configurable bold and italic plugins. @@ -279,6 +253,7 @@ public function testLanguageConfigForm() { $assert_session = $this->assertSession(); $this->createNewTextFormat($page, $assert_session); + $assert_session->assertWaitOnAjaxRequest(); // The language plugin config form should not be present. $assert_session->elementNotExists('css', '[data-drupal-selector="edit-editor-settings-plugins-ckeditor5-language"]'); diff --git a/core/modules/ckeditor5/tests/src/FunctionalJavascript/CKEditor5AllowedTagsTest.php b/core/modules/ckeditor5/tests/src/FunctionalJavascript/CKEditor5AllowedTagsTest.php index 40c27474e6..db8ec0c474 100644 --- a/core/modules/ckeditor5/tests/src/FunctionalJavascript/CKEditor5AllowedTagsTest.php +++ b/core/modules/ckeditor5/tests/src/FunctionalJavascript/CKEditor5AllowedTagsTest.php @@ -63,10 +63,13 @@ public function testEnablingToVersion5Validation() { $filter_warning = 'CKEditor 5 only works with HTML-based text formats. The "A TYPE_MARKUP_LANGUAGE filter incompatible with CKEditor 5" (filter_incompatible) filter implies this text format is not HTML anymore.'; $this->createNewTextFormat($page, $assert_session, 'unicorn'); + $page->selectFieldOption('editor[editor]', 'unicorn'); + $assert_session->assertWaitOnAjaxRequest(); $page->checkField('filters[filter_html][status]'); $page->checkField($incompatible_filter_name); + $assert_session->assertWaitOnAjaxRequest(); $page->selectFieldOption('editor[editor]', 'ckeditor5'); - $this->assertSame(2, $assert_session->assertWaitOnAjaxRequest()); + $assert_session->assertWaitOnAjaxRequest(); $assert_session->pageTextContains($filter_warning); // Disable the incompatible filter. @@ -131,10 +134,12 @@ public function testSwitchToVersion5() { $assert_session = $this->assertSession(); $this->createNewTextFormat($page, $assert_session, 'unicorn'); + $assert_session->assertWaitOnAjaxRequest(); // Enable the HTML filter. $this->assertTrue($page->hasUncheckedField('filters[filter_html][status]')); $page->checkField('filters[filter_html][status]'); + $assert_session->assertWaitOnAjaxRequest(); // Confirm the allowed HTML tags are the defaults initially. $this->assertHtmlEsqueFieldValueEquals('filters[filter_html][settings][allowed_html]', $this->defaultElementsWhenUpdatingNotCkeditor5); @@ -145,6 +150,7 @@ public function testSwitchToVersion5() { // Return to the config form to confirm that switching text editors on // existing formats will properly switch allowed tags. $this->drupalGet('admin/config/content/formats/manage/unicorn'); + $assert_session->assertWaitOnAjaxRequest(); $this->assertHtmlEsqueFieldValueEquals('filters[filter_html][settings][allowed_html]', $this->defaultElementsWhenUpdatingNotCkeditor5); $page->selectFieldOption('editor[editor]', 'ckeditor5'); @@ -165,6 +171,7 @@ public function testImgAddedViaUploadPlugin() { $assert_session = $this->assertSession(); $this->createNewTextFormat($page, $assert_session); + $assert_session->assertWaitOnAjaxRequest(); $allowed_html_field = $assert_session->fieldExists('filters[filter_html][settings][allowed_html]'); $this->assertTrue($allowed_html_field->hasAttribute('readonly')); @@ -237,6 +244,7 @@ public function testAllowedTags() { $assert_session = $this->assertSession(); $this->createNewTextFormat($page, $assert_session); + $assert_session->assertWaitOnAjaxRequest(); // Confirm the "allowed tags" field is read only, and the value // matches the tags required by CKEditor. @@ -254,6 +262,7 @@ public function testAllowedTags() { $this->assertSame($this->allowedElements, FilterFormat::load('ckeditor5')->filters('filter_html')->getConfiguration()['settings']['allowed_html']); $page->find('css', '[data-drupal-selector="edit-formats-ckeditor5"]')->clickLink('Configure'); + $assert_session->assertWaitOnAjaxRequest(); // Add the block quote plugin to the CKEditor 5 toolbar. $this->assertNotEmpty($assert_session->waitForElement('css', '.ckeditor5-toolbar-item-blockQuote')); @@ -281,13 +290,15 @@ public function testAllowedTags() { $this->assertHtmlEsqueFieldValueEquals('filters[filter_html][settings][allowed_html]', $allowed_with_blockquote); // Add the source editing plugin to the CKEditor 5 toolbar. - $this->assertNotEmpty($assert_session->waitForElement('css', '.ckeditor5-toolbar-available .ckeditor5-toolbar-item-sourceEditing')); + $this->assertNotEmpty($assert_session->waitForElement('css', '.ckeditor5-toolbar-item-sourceEditing')); $this->triggerKeyUp('.ckeditor5-toolbar-item-sourceEditing', 'ArrowDown'); + $assert_session->assertWaitOnAjaxRequest(); // Updating Source Editing's editable tags should automatically update // filter_html to include those additional tags. $assert_session->waitForText('Source editing'); $page->find('css', '[href^="#edit-editor-settings-plugins-ckeditor5-sourceediting"]')->click(); + $assert_session->assertWaitOnAjaxRequest(); $assert_session->waitForText('Manually editable HTML tags'); $source_edit_tags_field = $assert_session->fieldExists('editor[settings][plugins][ckeditor5_sourceEditing][allowed_tags]'); $source_edit_tags_field->setValue('