View source
<?php
namespace Drupal\Tests\layout_builder\FunctionalJavascript;
use Behat\Mink\Element\NodeElement;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay;
class BlockFilterTest extends WebDriverTestBase {
protected static $modules = [
'block',
'node',
'datetime',
'layout_builder',
'user',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$this
->drupalLogin($this
->drupalCreateUser([
'configure any layout',
]));
$this
->createContentType([
'type' => 'bundle_with_section_field',
]);
LayoutBuilderEntityViewDisplay::load('node.bundle_with_section_field.default')
->enableLayoutBuilder()
->setOverridable()
->save();
$this
->createNode([
'type' => 'bundle_with_section_field',
]);
}
public function testBlockFilter() {
$assert_session = $this
->assertSession();
$session = $this
->getSession();
$page = $session
->getPage();
$this
->drupalGet('node/1/layout');
$assert_session
->linkExists('Add block');
$this
->clickLink('Add block');
$assert_session
->assertWaitOnAjaxRequest();
$blocks = $page
->findAll('css', '.js-layout-builder-block-link');
$categories = $page
->findAll('css', '.js-layout-builder-category');
$filter = $assert_session
->elementExists('css', '.js-layout-builder-filter');
$init_message = 'init message';
$session
->evaluateScript("Drupal.announce('{$init_message}')");
$filter
->setValue('a');
$this
->assertAnnounceContains($init_message);
$visible_rows = $this
->filterVisibleElements($blocks);
$this
->assertSameSize($blocks, $visible_rows);
$contentFieldsCategory = $page
->find('named', [
'content',
'Content fields',
]);
$promoteToFrontPageLink = $page
->find('named', [
'content',
'Promoted to front page',
]);
$this
->assertTrue($promoteToFrontPageLink
->isVisible());
$contentFieldsCategory
->click();
$this
->assertFalse($promoteToFrontPageLink
->isVisible());
$filter
->setValue('ad');
$fewer_blocks_message = ' blocks are available in the modified list';
$this
->assertAnnounceContains($fewer_blocks_message);
$visible_rows = $this
->filterVisibleElements($blocks);
$this
->assertGreaterThan(0, count($blocks));
$this
->assertLessThan(count($blocks), count($visible_rows));
$visible_categories = $this
->filterVisibleElements($categories);
$this
->assertGreaterThan(0, count($visible_categories));
$this
->assertLessThan(count($categories), count($visible_categories));
$expected_message = count($visible_rows) . $fewer_blocks_message;
$this
->assertAnnounceContains($expected_message);
$filter
->setValue('Powered by');
$this
->assertAnnounceContains(' block is available');
$visible_rows = $this
->filterVisibleElements($blocks);
$this
->assertCount(1, $visible_rows);
$visible_categories = $this
->filterVisibleElements($categories);
$this
->assertCount(1, $visible_categories);
$this
->assertAnnounceContains('1 block is available in the modified list.');
$filter
->setValue('Pan-Galactic Gargle Blaster');
$visible_rows = $this
->filterVisibleElements($blocks);
$this
->assertCount(0, $visible_rows);
$visible_categories = $this
->filterVisibleElements($categories);
$this
->assertCount(0, $visible_categories);
$announce_element = $page
->find('css', '#drupal-live-announce');
$page
->waitFor(2, function () use ($announce_element) {
return strpos($announce_element
->getText(), '0 blocks are available') === 0;
});
$filter
->setValue('');
$this
->assertAnnounceContains('All available blocks are listed.');
$this
->assertFalse($promoteToFrontPageLink
->isVisible());
}
protected function filterVisibleElements(array $elements) {
return array_filter($elements, function (NodeElement $element) {
return $element
->isVisible();
});
}
protected function assertAnnounceContains(string $expected_message) : void {
$assert_session = $this
->assertSession();
$this
->assertNotEmpty($assert_session
->waitForElement('css', "#drupal-live-announce:contains('{$expected_message}')"));
}
}