BlockFilterTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace Drupal\Tests\block\FunctionalJavascript;
  3. use Behat\Mink\Element\NodeElement;
  4. use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
  5. /**
  6. * Tests the JavaScript functionality of the block add filter.
  7. *
  8. * @group block
  9. */
  10. class BlockFilterTest extends WebDriverTestBase {
  11. /**
  12. * {@inheritdoc}
  13. */
  14. public static $modules = ['user', 'block'];
  15. /**
  16. * {@inheritdoc}
  17. */
  18. protected function setUp() {
  19. parent::setUp();
  20. $admin_user = $this->drupalCreateUser([
  21. 'administer blocks',
  22. ]);
  23. $this->drupalLogin($admin_user);
  24. }
  25. /**
  26. * Tests block filter.
  27. */
  28. public function testBlockFilter() {
  29. $this->drupalGet('admin/structure/block');
  30. $assertSession = $this->assertSession();
  31. $session = $this->getSession();
  32. $page = $session->getPage();
  33. // Find the block filter field on the add-block dialog.
  34. $page->find('css', '#edit-blocks-region-header-title')->click();
  35. $filter = $assertSession->waitForElement('css', '.block-filter-text');
  36. // Get all block rows, for assertions later.
  37. $block_rows = $page->findAll('css', '.block-add-table tbody tr');
  38. // Test block filter reduces the number of visible rows.
  39. $filter->setValue('ad');
  40. $session->wait(10000, 'jQuery("#drupal-live-announce").html().indexOf("blocks are available") > -1');
  41. $visible_rows = $this->filterVisibleElements($block_rows);
  42. if (count($block_rows) > 0) {
  43. $this->assertNotEquals(count($block_rows), count($visible_rows));
  44. }
  45. // Test Drupal.announce() message when multiple matches are expected.
  46. $expected_message = count($visible_rows) . ' blocks are available in the modified list.';
  47. $assertSession->elementTextContains('css', '#drupal-live-announce', $expected_message);
  48. // Test Drupal.announce() message when only one match is expected.
  49. $filter->setValue('Powered by');
  50. $session->wait(10000, 'jQuery("#drupal-live-announce").html().indexOf("block is available") > -1');
  51. $visible_rows = $this->filterVisibleElements($block_rows);
  52. $this->assertEquals(1, count($visible_rows));
  53. $expected_message = '1 block is available in the modified list.';
  54. $assertSession->elementTextContains('css', '#drupal-live-announce', $expected_message);
  55. // Test Drupal.announce() message when no matches are expected.
  56. $filter->setValue('Pan-Galactic Gargle Blaster');
  57. $session->wait(10000, 'jQuery("#drupal-live-announce").html().indexOf("0 blocks are available") > -1');
  58. $visible_rows = $this->filterVisibleElements($block_rows);
  59. $this->assertEquals(0, count($visible_rows));
  60. $expected_message = '0 blocks are available in the modified list.';
  61. $assertSession->elementTextContains('css', '#drupal-live-announce', $expected_message);
  62. }
  63. /**
  64. * Removes any non-visible elements from the passed array.
  65. *
  66. * @param \Behat\Mink\Element\NodeElement[] $elements
  67. * An array of node elements.
  68. *
  69. * @return \Behat\Mink\Element\NodeElement[]
  70. */
  71. protected function filterVisibleElements(array $elements) {
  72. $elements = array_filter($elements, function (NodeElement $element) {
  73. return $element->isVisible();
  74. });
  75. return $elements;
  76. }
  77. }