2017accd88aa786262ad63ec79041a2a562fa54a
[yaffs-website] / web / core / modules / settings_tray / tests / src / FunctionalJavascript / SettingsTrayTestBase.php
1 <?php
2
3 namespace Drupal\Tests\settings_tray\FunctionalJavascript;
4
5 use Drupal\block\Entity\Block;
6 use Drupal\Tests\contextual\FunctionalJavascript\ContextualLinkClickTrait;
7 use Drupal\Tests\system\FunctionalJavascript\OffCanvasTestBase;
8
9 /**
10  * Base class for Settings Tray tests.
11  */
12 class SettingsTrayTestBase extends OffCanvasTestBase {
13
14   use ContextualLinkClickTrait;
15
16   /**
17    * {@inheritdoc}
18    */
19   public static $modules = [
20     'settings_tray',
21     // Add test module to override CSS pointer-events properties because they
22     // cause test failures.
23     'settings_tray_test_css',
24   ];
25
26   const TOOLBAR_EDIT_LINK_SELECTOR = '#toolbar-bar div.contextual-toolbar-tab button';
27
28   const LABEL_INPUT_SELECTOR = 'input[data-drupal-selector="edit-settings-label"]';
29
30   /**
31    * Open block form by clicking the element found with a css selector.
32    *
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.
38    */
39   protected function openBlockForm($block_selector, $contextual_link_container = '') {
40     if (!$contextual_link_container) {
41       $contextual_link_container = $block_selector;
42     }
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
48     // link is added.
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);
53     $block->mouseOver();
54     $block->click();
55     $this->waitForOffCanvasToOpen();
56     $this->assertOffCanvasBlockFormIsValid();
57   }
58
59   /**
60    * Enables edit mode by pressing edit button in the toolbar.
61    */
62   protected function enableEditMode() {
63     $this->pressToolbarEditButton();
64     $this->assertEditModeEnabled();
65   }
66
67   /**
68    * Disables edit mode by pressing edit button in the toolbar.
69    */
70   protected function disableEditMode() {
71     $this->assertSession()->assertWaitOnAjaxRequest();
72     $this->pressToolbarEditButton();
73     $this->assertEditModeDisabled();
74   }
75
76   /**
77    * Press the toolbar Edit button provided by the contextual module.
78    */
79   protected function pressToolbarEditButton() {
80     $this->assertSession()->waitForElement('css', '[data-contextual-id] .contextual-links a');
81     $edit_button = $this->getSession()
82       ->getPage()
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();
87   }
88
89   /**
90    * Assert that edit mode has been properly disabled.
91    */
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)');
99     }));
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"
107     // class.
108     $web_assert->elementNotExists('css', '.dialog-off-canvas-main-canvas.js-settings-tray-edit-mode');
109   }
110
111   /**
112    * Assert that edit mode has been properly enabled.
113    */
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');
122     }));
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');
129   }
130
131   /**
132    * Asserts that Off-Canvas block form is valid.
133    */
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');
142     }
143     else {
144       $this->assertEquals($this->isLabelInputVisible(), FALSE, 'Label is not visible');
145     }
146
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"]');
153   }
154
155   /**
156    * {@inheritdoc}
157    */
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';
163     });
164   }
165
166   /**
167    * Gets the block CSS selector.
168    *
169    * @param \Drupal\block\Entity\Block $block
170    *   The block.
171    *
172    * @return string
173    *   The CSS selector.
174    */
175   public function getBlockSelector(Block $block) {
176     return '#block-' . str_replace('_', '-', $block->id());
177   }
178
179   /**
180    * Determines if the label input is visible.
181    *
182    * @return bool
183    *   TRUE if the label is visible, FALSE if it is not.
184    */
185   protected function isLabelInputVisible() {
186     return $this->getSession()->getPage()->find('css', static::LABEL_INPUT_SELECTOR)->isVisible();
187   }
188
189 }