3 namespace Drupal\Tests\settings_tray\FunctionalJavascript;
5 use Drupal\block\Entity\Block;
6 use Drupal\Tests\contextual\FunctionalJavascript\ContextualLinkClickTrait;
7 use Drupal\Tests\system\FunctionalJavascript\OffCanvasTestBase;
10 * Base class for Settings Tray tests.
12 class SettingsTrayTestBase extends OffCanvasTestBase {
14 use ContextualLinkClickTrait;
19 public static $modules = [
21 // Add test module to override CSS pointer-events properties because they
22 // cause test failures.
23 'settings_tray_test_css',
26 const TOOLBAR_EDIT_LINK_SELECTOR = '#toolbar-bar div.contextual-toolbar-tab button';
28 const LABEL_INPUT_SELECTOR = 'input[data-drupal-selector="edit-settings-label"]';
31 * Open block form by clicking the element found with a css selector.
33 * @param string $block_selector
34 * A css selector selects the block or an element within it.
35 * @param string $contextual_link_container
36 * The element that contains the contextual links. If none provide the
37 * $block_selector will be used.
39 protected function openBlockForm($block_selector, $contextual_link_container = '') {
40 if (!$contextual_link_container) {
41 $contextual_link_container = $block_selector;
43 // Ensure that contextual link element is present because this is required
44 // to open the off-canvas dialog in edit mode.
45 $contextual_link = $this->assertSession()->waitForElement('css', "$contextual_link_container .contextual-links a");
46 $this->assertNotEmpty($contextual_link);
47 // When page first loads Edit Mode is not triggered until first contextual
49 $this->assertElementVisibleAfterWait('css', '.dialog-off-canvas-main-canvas.js-settings-tray-edit-mode');
50 // Ensure that all other Ajax activity is completed.
51 $this->assertSession()->assertWaitOnAjaxRequest();
52 $block = $this->getSession()->getPage()->find('css', $block_selector);
55 $this->waitForOffCanvasToOpen();
56 $this->assertOffCanvasBlockFormIsValid();
60 * Enables edit mode by pressing edit button in the toolbar.
62 protected function enableEditMode() {
63 $this->pressToolbarEditButton();
64 $this->assertEditModeEnabled();
68 * Disables edit mode by pressing edit button in the toolbar.
70 protected function disableEditMode() {
71 $this->assertSession()->assertWaitOnAjaxRequest();
72 $this->pressToolbarEditButton();
73 $this->assertEditModeDisabled();
77 * Press the toolbar Edit button provided by the contextual module.
79 protected function pressToolbarEditButton() {
80 $this->assertSession()->waitForElement('css', '[data-contextual-id] .contextual-links a');
81 $edit_button = $this->getSession()
83 ->find('css', static::TOOLBAR_EDIT_LINK_SELECTOR);
84 $this->getSession()->executeScript("jQuery('[data-quickedit-entity-id]').trigger('mouseleave')");
85 $edit_button->mouseOver();
86 $edit_button->press();
90 * Assert that edit mode has been properly disabled.
92 protected function assertEditModeDisabled() {
93 $web_assert = $this->assertSession();
94 $page = $this->getSession()->getPage();
95 $this->getSession()->executeScript("jQuery('[data-quickedit-entity-id]').trigger('mouseleave')");
96 $page->find('css', static::TOOLBAR_EDIT_LINK_SELECTOR)->mouseOver();
97 $this->assertTrue($page->waitFor(10, function ($page) {
98 return !$page->find('css', '.contextual .trigger:not(.visually-hidden)');
100 // Contextual triggers should be hidden.
101 $web_assert->elementExists('css', '.contextual .trigger.visually-hidden');
102 // No contextual triggers should be not hidden.
103 $web_assert->elementNotExists('css', '.contextual .trigger:not(.visually-hidden)');
104 // The toolbar edit button should read "Edit".
105 $web_assert->elementContains('css', static::TOOLBAR_EDIT_LINK_SELECTOR, 'Edit');
106 // The main canvas element should NOT have the "js-settings-tray-edit-mode"
108 $web_assert->elementNotExists('css', '.dialog-off-canvas-main-canvas.js-settings-tray-edit-mode');
112 * Assert that edit mode has been properly enabled.
114 protected function assertEditModeEnabled() {
115 $web_assert = $this->assertSession();
116 $page = $this->getSession()->getPage();
117 // Move the mouse over the toolbar button so that isn't over a contextual
118 // links area which cause the contextual link to be shown.
119 $page->find('css', static::TOOLBAR_EDIT_LINK_SELECTOR)->mouseOver();
120 $this->assertTrue($page->waitFor(10, function ($page) {
121 return !$page->find('css', '.contextual .trigger.visually-hidden');
123 // No contextual triggers should be hidden.
124 $web_assert->elementNotExists('css', '.contextual .trigger.visually-hidden');
125 // The toolbar edit button should read "Editing".
126 $web_assert->elementContains('css', static::TOOLBAR_EDIT_LINK_SELECTOR, 'Editing');
127 // The main canvas element should have the "js-settings-tray-edit-mode" class.
128 $web_assert->elementExists('css', '.dialog-off-canvas-main-canvas.js-settings-tray-edit-mode');
132 * Asserts that Off-Canvas block form is valid.
134 protected function assertOffCanvasBlockFormIsValid() {
135 $web_assert = $this->assertSession();
136 // Confirm that Block title display label has been changed.
137 $web_assert->elementTextContains('css', '.form-item-settings-label-display label', 'Display block title');
138 // Confirm Block title label is shown if checkbox is checked.
139 if ($this->getSession()->getPage()->find('css', 'input[name="settings[label_display]"]')->isChecked()) {
140 $this->assertEquals($this->isLabelInputVisible(), TRUE, 'Label is visible');
141 $web_assert->elementTextContains('css', '.form-item-settings-label label', 'Block title');
144 $this->assertEquals($this->isLabelInputVisible(), FALSE, 'Label is not visible');
147 // Check that common block form elements exist.
148 $web_assert->elementExists('css', static::LABEL_INPUT_SELECTOR);
149 $web_assert->elementExists('css', 'input[data-drupal-selector="edit-settings-label-display"]');
150 // Check that advanced block form elements do not exist.
151 $web_assert->elementNotExists('css', 'input[data-drupal-selector="edit-visibility-request-path-pages"]');
152 $web_assert->elementNotExists('css', 'select[data-drupal-selector="edit-region"]');
158 protected function getTestThemes() {
159 // Remove 'seven' theme. Settings Tray "Edit Mode" will not work with
160 // 'seven' because it removes all contextual links.
161 return array_filter(parent::getTestThemes(), function ($theme) {
162 return $theme !== 'seven';
167 * Gets the block CSS selector.
169 * @param \Drupal\block\Entity\Block $block
175 public function getBlockSelector(Block $block) {
176 return '#block-' . str_replace('_', '-', $block->id());
180 * Determines if the label input is visible.
183 * TRUE if the label is visible, FALSE if it is not.
185 protected function isLabelInputVisible() {
186 return $this->getSession()->getPage()->find('css', static::LABEL_INPUT_SELECTOR)->isVisible();