diff --git a/core/modules/system/js/system.modules.es6.js b/core/modules/system/js/system.modules.es6.js index 98be18f989..1b0c8167e4 100644 --- a/core/modules/system/js/system.modules.es6.js +++ b/core/modules/system/js/system.modules.es6.js @@ -67,9 +67,11 @@ $details.attr('open', true).each(hidePackageDetails); Drupal.announce( - Drupal.t('!modules modules are available in the modified list.', { - '!modules': $rowsAndDetails.find('tbody tr:visible').length, - }), + Drupal.formatPlural( + $rowsAndDetails.find('tbody tr:visible').length, + '1 module is available in the modified list.', + '@count modules are available in the modified list.', + ), ); } else if (searching) { searching = false; diff --git a/core/modules/system/js/system.modules.js b/core/modules/system/js/system.modules.js index 9e2f7d403d..b60cf29cb4 100644 --- a/core/modules/system/js/system.modules.js +++ b/core/modules/system/js/system.modules.js @@ -58,9 +58,7 @@ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } $rows.each(showModuleRow); $details.not('[open]').attr('data-drupal-system-state', 'forced-open'); $details.attr('open', true).each(hidePackageDetails); - Drupal.announce(Drupal.t('!modules modules are available in the modified list.', { - '!modules': $rowsAndDetails.find('tbody tr:visible').length - })); + Drupal.announce(Drupal.formatPlural($rowsAndDetails.find('tbody tr:visible').length, '1 module is available in the modified list.', '@count modules are available in the modified list.')); } else if (searching) { searching = false; $rowsAndDetails.show(); diff --git a/core/modules/system/tests/src/FunctionalJavascript/ModuleFilterTest.php b/core/modules/system/tests/src/FunctionalJavascript/ModuleFilterTest.php new file mode 100644 index 0000000000..1bffcd38f0 --- /dev/null +++ b/core/modules/system/tests/src/FunctionalJavascript/ModuleFilterTest.php @@ -0,0 +1,96 @@ +drupalCreateUser([ + 'administer modules', + ]); + $this->drupalLogin($admin_user); + } + + /** + * Tests that filter results announcement has correct pluralization. + */ + public function testModuleFilter() { + + // Find the module filter field. + $this->drupalGet('admin/modules'); + $assertSession = $this->assertSession(); + $session = $this->getSession(); + $page = $session->getPage(); + + $filter = $page->findField('edit-text'); + + // Get all module rows, for assertions later. + $module_rows = $page->findAll('css', '.package-listing tbody tr td.module'); + + // Test module filter reduces the number of visible rows. + $filter->setValue('test'); + $session->wait(1000, 'jQuery("#module-node:visible").length == 0'); + $visible_rows = $this->filterVisibleElements($module_rows); + self::assertNotEquals(count($module_rows), count($visible_rows)); + + // Test Drupal.announce() message when multiple matches are expected. + $expected_message = count($visible_rows) . ' modules are available in the modified list.'; + $assertSession->elementTextContains('css', '#drupal-live-announce', $expected_message); + + // Test Drupal.announce() message only one match is expected. + // Using a very specific module name, we expect only one row. + $filter->setValue('System dependency test'); + $session->wait(1000, 'jQuery("#module-node:visible").length == 0'); + $visible_rows = $this->filterVisibleElements($module_rows); + self::assertEquals(1, count($visible_rows)); + $expected_message = '1 module is available in the modified list.'; + $assertSession->elementTextContains('css', '#drupal-live-announce', $expected_message); + + // Test Drupal.announce() message when no matches are expected. + $filter->setValue('Pan-Galactic Gargle Blaster'); + $session->wait(1000, 'jQuery("#module-node:visible").length == 0'); + $visible_rows = $this->filterVisibleElements($module_rows); + self::assertEquals(0, count($visible_rows)); + + $expected_message = '0 modules are available in the modified list.'; + $assertSession->elementTextContains('css', '#drupal-live-announce', $expected_message); + } + + /** + * Removes any non-visible elements from the passed array. + * + * @param \Behat\Mink\Element\NodeElement[] $elements + * An array of node elements. + * + * @return \Behat\Mink\Element\NodeElement[] + */ + protected function filterVisibleElements($elements) { + $elements = array_filter($elements, function ($element) { + return $element->isVisible(); + }); + return $elements; + } + +}