Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / outside_in / tests / src / FunctionalJavascript / OutsideInJavascriptTestBase.php
1 <?php
2
3 namespace Drupal\Tests\outside_in\FunctionalJavascript;
4
5 use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
6
7 /**
8  * Base class contains common test functionality for the Settings Tray module.
9  */
10 abstract class OutsideInJavascriptTestBase extends JavascriptTestBase {
11
12   /**
13    * {@inheritdoc}
14    */
15   protected function drupalGet($path, array $options = [], array $headers = []) {
16     $return = parent::drupalGet($path, $options, $headers);
17
18     // After the page loaded we need to additionally wait until the settings
19     // tray Ajax activity is done.
20     $this->assertSession()->assertWaitOnAjaxRequest();
21
22     return $return;
23   }
24
25   /**
26    * Enables a theme.
27    *
28    * @param string $theme
29    *   The theme.
30    */
31   public function enableTheme($theme) {
32     // Enable the theme.
33     \Drupal::service('theme_installer')->install([$theme]);
34     $theme_config = \Drupal::configFactory()->getEditable('system.theme');
35     $theme_config->set('default', $theme);
36     $theme_config->save();
37   }
38
39   /**
40    * Waits for off-canvas dialog to open.
41    */
42   protected function waitForOffCanvasToOpen() {
43     $web_assert = $this->assertSession();
44     $web_assert->assertWaitOnAjaxRequest();
45     $this->assertElementVisibleAfterWait('css', '#drupal-off-canvas');
46   }
47
48   /**
49    * Waits for off-canvas dialog to close.
50    */
51   protected function waitForOffCanvasToClose() {
52     $this->waitForNoElement('#drupal-off-canvas');
53   }
54
55   /**
56    * Gets the off-canvas dialog element.
57    *
58    * @return \Behat\Mink\Element\NodeElement|null
59    */
60   protected function getTray() {
61     $tray = $this->getSession()->getPage()->find('css', '.ui-dialog[aria-describedby="drupal-off-canvas"]');
62     $this->assertEquals(FALSE, empty($tray), 'The tray was found.');
63     return $tray;
64   }
65
66   /**
67    * Waits for an element to be removed from the page.
68    *
69    * @param string $selector
70    *   CSS selector.
71    * @param int $timeout
72    *   (optional) Timeout in milliseconds, defaults to 10000.
73    */
74   protected function waitForNoElement($selector, $timeout = 10000) {
75     $condition = "(jQuery('$selector').length == 0)";
76     $this->assertJsCondition($condition, $timeout);
77   }
78
79   /**
80    * Clicks a contextual link.
81    *
82    * @todo Remove this function when related trait added in
83    *   https://www.drupal.org/node/2821724.
84    *
85    * @param string $selector
86    *   The selector for the element that contains the contextual link.
87    * @param string $link_locator
88    *   The link id, title, or text.
89    * @param bool $force_visible
90    *   If true then the button will be forced to visible so it can be clicked.
91    */
92   protected function clickContextualLink($selector, $link_locator, $force_visible = TRUE) {
93     if ($force_visible) {
94       $this->toggleContextualTriggerVisibility($selector);
95     }
96
97     $element = $this->getSession()->getPage()->find('css', $selector);
98     $element->find('css', '.contextual button')->press();
99     $element->findLink($link_locator)->click();
100
101     if ($force_visible) {
102       $this->toggleContextualTriggerVisibility($selector);
103     }
104   }
105
106   /**
107    * Toggles the visibility of a contextual trigger.
108    *
109    * @todo Remove this function when related trait added in
110    *   https://www.drupal.org/node/2821724.
111    *
112    * @param string $selector
113    *   The selector for the element that contains the contextual link.
114    */
115   protected function toggleContextualTriggerVisibility($selector) {
116     // Hovering over the element itself with should be enough, but does not
117     // work. Manually remove the visually-hidden class.
118     $this->getSession()->executeScript("jQuery('{$selector} .contextual .trigger').toggleClass('visually-hidden');");
119   }
120
121   /**
122    * Waits for Toolbar to load.
123    */
124   protected function waitForToolbarToLoad() {
125     $web_assert = $this->assertSession();
126     // Waiting for Toolbar module.
127     // @todo Remove the hack after https://www.drupal.org/node/2542050.
128     $this->assertElementVisibleAfterWait('css', '.toolbar-fixed');
129     // Waiting for Toolbar animation.
130     $web_assert->assertWaitOnAjaxRequest();
131   }
132
133   /**
134    * Get themes to test.
135    *
136    * @return string[]
137    *   Theme names to test.
138    */
139   protected function getTestThemes() {
140     return ['bartik', 'stark', 'classy', 'stable'];
141   }
142
143   /**
144    * Asserts the specified selector is visible after a wait.
145    *
146    * @param string $selector
147    *   The selector engine name. See ElementInterface::findAll() for the
148    *   supported selectors.
149    * @param string|array $locator
150    *   The selector locator.
151    * @param int $timeout
152    *   (Optional) Timeout in milliseconds, defaults to 10000.
153    */
154   protected function assertElementVisibleAfterWait($selector, $locator, $timeout = 10000) {
155     $this->assertNotEmpty($this->assertSession()->waitForElementVisible($selector, $locator, $timeout));
156   }
157
158 }