3 namespace Drupal\Tests\block\FunctionalJavascript;
5 use Behat\Mink\Element\NodeElement;
6 use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
9 * Tests the JavaScript functionality of the block add filter.
13 class BlockFilterTest extends JavascriptTestBase {
18 public static $modules = ['user', 'block'];
23 protected function setUp() {
26 $admin_user = $this->drupalCreateUser([
30 $this->drupalLogin($admin_user);
36 public function testBlockFilter() {
37 $this->drupalGet('admin/structure/block');
38 $assertSession = $this->assertSession();
39 $session = $this->getSession();
40 $page = $session->getPage();
42 // Find the block filter field on the add-block dialog.
43 $page->find('css', '#edit-blocks-region-header-title')->click();
44 $filter = $assertSession->waitForElement('css', '.block-filter-text');
46 // Get all block rows, for assertions later.
47 $block_rows = $page->findAll('css', '.block-add-table tbody tr');
49 // Test block filter reduces the number of visible rows.
50 $filter->setValue('ad');
51 $session->wait(10000, 'jQuery("#drupal-live-announce").html().indexOf("blocks are available") > -1');
52 $visible_rows = $this->filterVisibleElements($block_rows);
53 if (count($block_rows) > 0) {
54 $this->assertNotEquals(count($block_rows), count($visible_rows));
57 // Test Drupal.announce() message when multiple matches are expected.
58 $expected_message = count($visible_rows) . ' blocks are available in the modified list.';
59 $assertSession->elementTextContains('css', '#drupal-live-announce', $expected_message);
61 // Test Drupal.announce() message when only one match is expected.
62 $filter->setValue('Powered by');
63 $session->wait(10000, 'jQuery("#drupal-live-announce").html().indexOf("block is available") > -1');
64 $visible_rows = $this->filterVisibleElements($block_rows);
65 $this->assertEquals(1, count($visible_rows));
66 $expected_message = '1 block is available in the modified list.';
67 $assertSession->elementTextContains('css', '#drupal-live-announce', $expected_message);
69 // Test Drupal.announce() message when no matches are expected.
70 $filter->setValue('Pan-Galactic Gargle Blaster');
71 $session->wait(10000, 'jQuery("#drupal-live-announce").html().indexOf("0 blocks are available") > -1');
72 $visible_rows = $this->filterVisibleElements($block_rows);
73 $this->assertEquals(0, count($visible_rows));
74 $expected_message = '0 blocks are available in the modified list.';
75 $assertSession->elementTextContains('css', '#drupal-live-announce', $expected_message);
79 * Removes any non-visible elements from the passed array.
81 * @param \Behat\Mink\Element\NodeElement[] $elements
82 * An array of node elements.
84 * @return \Behat\Mink\Element\NodeElement[]
86 protected function filterVisibleElements(array $elements) {
87 $elements = array_filter($elements, function (NodeElement $element) {
88 return $element->isVisible();