View source
<?php
namespace Drupal\Tests\layout_builder_restrictions\Traits;
use Drupal\Tests\contextual\FunctionalJavascript\ContextualLinkClickTrait;
trait MoveBlockHelperTrait {
use ContextualLinkClickTrait;
protected function assertBlockTable(array $expected_block_labels) {
$page = $this
->getSession()
->getPage();
$this
->assertSession()
->assertWaitOnAjaxRequest();
$block_tds = $page
->findAll('css', '.layout-builder-components-table__block-label');
$this
->assertCount(count($block_tds), $expected_block_labels);
foreach ($block_tds as $block_td) {
$this
->assertSame(array_shift($expected_block_labels), trim($block_td
->getText()));
}
}
protected function waitForNoElement($selector, $timeout = 10000) {
$condition = "(typeof jQuery !== 'undefined' && jQuery('{$selector}').length === 0)";
$this
->assertJsCondition($condition, $timeout);
}
protected function moveBlockWithKeyboard($direction, $block_label, array $updated_blocks) {
$keys = [
'up' => 38,
'down' => 40,
];
$key = $keys[$direction];
$handle = $this
->findRowHandle($block_label);
$handle
->keyDown($key);
$handle
->keyUp($key);
$handle
->blur();
$this
->assertBlockTable($updated_blocks);
}
protected function findRowHandle($block_label) {
$assert_session = $this
->assertSession();
return $assert_session
->elementExists('css', "[data-drupal-selector=\"edit-components\"] td:contains(\"{$block_label}\") a.tabledrag-handle");
}
protected function assertRegionBlocksOrder($section_delta, $region, array $expected_block_selectors) {
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
$assert_session
->assertWaitOnAjaxRequest();
$this
->waitForNoElement('#drupal-off-canvas');
$region_selector = "[data-layout-delta=\"{$section_delta}\"] [data-region=\"{$region}\"]";
$blocks = $page
->findAll('css', "{$region_selector} [data-layout-block-uuid]");
$this
->assertCount(count($expected_block_selectors), $blocks);
foreach ($blocks as $block) {
$block_selector = array_shift($expected_block_selectors);
$assert_session
->elementsCount('css', "{$region_selector} {$block_selector}", 1);
$expected_block = $page
->find('css', "{$region_selector} {$block_selector}");
$this
->assertSame($expected_block
->getAttribute('data-layout-block-uuid'), $block
->getAttribute('data-layout-block-uuid'));
}
}
protected function openMoveForm($delta, $region, $field, array $initial_blocks) {
$assert_session = $this
->assertSession();
$body_field_locator = "[data-layout-delta=\"{$delta}\"] [data-region=\"{$region}\"] ." . $field;
$this
->clickContextualLink($body_field_locator, 'Move');
$assert_session
->assertWaitOnAjaxRequest();
$this
->assertNotEmpty($assert_session
->waitForElementVisible('named', [
'select',
'Region',
]));
$assert_session
->fieldValueEquals('Region', "{$delta}:{$region}");
$this
->assertBlockTable($initial_blocks);
}
}