Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / block / tests / src / FunctionalJavascript / BlockFilterTest.php
1 <?php
2
3 namespace Drupal\Tests\block\FunctionalJavascript;
4
5 use Behat\Mink\Element\NodeElement;
6 use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
7
8 /**
9  * Tests the JavaScript functionality of the block add filter.
10  *
11  * @group block
12  */
13 class BlockFilterTest extends JavascriptTestBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   public static $modules = ['user', 'block'];
19
20   /**
21    * {@inheritdoc}
22    */
23   protected function setUp() {
24     parent::setUp();
25
26     $admin_user = $this->drupalCreateUser([
27       'administer blocks',
28     ]);
29
30     $this->drupalLogin($admin_user);
31   }
32
33   /**
34    * Tests block filter.
35    */
36   public function testBlockFilter() {
37     $this->drupalGet('admin/structure/block');
38     $assertSession = $this->assertSession();
39     $session = $this->getSession();
40     $page = $session->getPage();
41
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');
45
46     // Get all block rows, for assertions later.
47     $block_rows = $page->findAll('css', '.block-add-table tbody tr');
48
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));
55     }
56
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);
60
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);
68
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);
76   }
77
78   /**
79    * Removes any non-visible elements from the passed array.
80    *
81    * @param \Behat\Mink\Element\NodeElement[] $elements
82    *   An array of node elements.
83    *
84    * @return \Behat\Mink\Element\NodeElement[]
85    */
86   protected function filterVisibleElements(array $elements) {
87     $elements = array_filter($elements, function (NodeElement $element) {
88       return $element->isVisible();
89     });
90     return $elements;
91   }
92
93 }