View source
<?php
namespace Drupal\Tests\settings_tray\FunctionalJavascript;
use Drupal\block\Entity\Block;
use Drupal\Tests\contextual\FunctionalJavascript\ContextualLinkClickTrait;
use Drupal\Tests\system\FunctionalJavascript\OffCanvasTestBase;
class SettingsTrayTestBase extends OffCanvasTestBase {
use ContextualLinkClickTrait;
protected static $modules = [
'settings_tray',
'settings_tray_test_css',
];
const TOOLBAR_EDIT_LINK_SELECTOR = '#toolbar-bar div.contextual-toolbar-tab button';
const LABEL_INPUT_SELECTOR = 'input[data-drupal-selector="edit-settings-label"]';
protected function openBlockForm($block_selector, $contextual_link_container = '') {
if (!$contextual_link_container) {
$contextual_link_container = $block_selector;
}
$contextual_link = $this
->assertSession()
->waitForElement('css', "{$contextual_link_container} .contextual-links a");
$this
->assertNotEmpty($contextual_link);
$this
->assertElementVisibleAfterWait('css', '.dialog-off-canvas-main-canvas.js-settings-tray-edit-mode');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$block = $this
->getSession()
->getPage()
->find('css', $block_selector);
$block
->mouseOver();
$block
->click();
$this
->waitForOffCanvasToOpen();
$this
->assertOffCanvasBlockFormIsValid();
}
protected function enableEditMode() {
$this
->pressToolbarEditButton();
$this
->assertEditModeEnabled();
}
protected function disableEditMode() {
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->pressToolbarEditButton();
$this
->assertEditModeDisabled();
}
protected function pressToolbarEditButton() {
$this
->assertSession()
->waitForElement('css', '[data-contextual-id] .contextual-links a');
$edit_button = $this
->getSession()
->getPage()
->find('css', static::TOOLBAR_EDIT_LINK_SELECTOR);
$this
->getSession()
->executeScript("jQuery('[data-quickedit-entity-id]').trigger('mouseleave')");
$edit_button
->mouseOver();
$edit_button
->press();
}
protected function assertEditModeDisabled() {
$web_assert = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$this
->getSession()
->executeScript("jQuery('[data-quickedit-entity-id]').trigger('mouseleave')");
$page
->find('css', static::TOOLBAR_EDIT_LINK_SELECTOR)
->mouseOver();
$this
->assertTrue($page
->waitFor(10, function ($page) {
return !$page
->find('css', '.contextual .trigger:not(.visually-hidden)');
}));
$web_assert
->elementExists('css', '.contextual .trigger.visually-hidden');
$web_assert
->elementNotExists('css', '.contextual .trigger:not(.visually-hidden)');
$web_assert
->elementContains('css', static::TOOLBAR_EDIT_LINK_SELECTOR, 'Edit');
$web_assert
->elementNotExists('css', '.dialog-off-canvas-main-canvas.js-settings-tray-edit-mode');
}
protected function assertEditModeEnabled() {
$web_assert = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$page
->find('css', static::TOOLBAR_EDIT_LINK_SELECTOR)
->mouseOver();
$this
->assertTrue($page
->waitFor(10, function ($page) {
return !$page
->find('css', '.contextual .trigger.visually-hidden');
}));
$web_assert
->elementNotExists('css', '.contextual .trigger.visually-hidden');
$web_assert
->elementContains('css', static::TOOLBAR_EDIT_LINK_SELECTOR, 'Editing');
$web_assert
->elementExists('css', '.dialog-off-canvas-main-canvas.js-settings-tray-edit-mode');
}
protected function assertOffCanvasBlockFormIsValid() {
$web_assert = $this
->assertSession();
$web_assert
->elementTextContains('css', '.form-item-settings-label-display label', 'Display block title');
if ($this
->getSession()
->getPage()
->find('css', 'input[name="settings[label_display]"]')
->isChecked()) {
$this
->assertTrue($this
->isLabelInputVisible(), 'Label is visible');
$web_assert
->elementTextContains('css', '.form-item-settings-label label', 'Block title');
}
else {
$this
->assertFalse($this
->isLabelInputVisible(), 'Label is not visible');
}
$web_assert
->elementExists('css', static::LABEL_INPUT_SELECTOR);
$web_assert
->elementExists('css', 'input[data-drupal-selector="edit-settings-label-display"]');
$web_assert
->elementNotExists('css', 'input[data-drupal-selector="edit-visibility-request-path-pages"]');
$web_assert
->elementNotExists('css', 'select[data-drupal-selector="edit-region"]');
}
protected function getTestThemes() {
return array_filter(parent::getTestThemes(), function ($theme) {
return $theme !== 'claro';
});
}
public function getBlockSelector(Block $block) {
return '#block-' . str_replace('_', '-', $block
->id());
}
protected function isLabelInputVisible() {
return $this
->getSession()
->getPage()
->find('css', static::LABEL_INPUT_SELECTOR)
->isVisible();
}
}