Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / modules / system / tests / src / FunctionalJavascript / OffCanvasTestBase.php
1 <?php
2
3 namespace Drupal\Tests\system\FunctionalJavascript;
4
5 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
6
7 /**
8  * Base class contains common test functionality for the Off-canvas dialog.
9  */
10 abstract class OffCanvasTestBase extends WebDriverTestBase {
11
12   /**
13    * {@inheritdoc}
14    */
15   protected function drupalGet($path, array $options = [], array $headers = []) {
16     $return = parent::drupalGet($path, $options, $headers);
17     $this->assertPageLoadComplete();
18     return $return;
19   }
20
21   /**
22    * Assert the page is completely loaded.
23    *
24    * Ajax requests may happen after page loads. Also for users who have access
25    * to contextual links the contextual link placeholders will be filled after
26    * the page is received.
27    */
28   protected function assertPageLoadComplete() {
29     $this->assertSession()->assertWaitOnAjaxRequest();
30     if ($this->loggedInUser && $this->loggedInUser->hasPermission('access contextual links')) {
31       $this->assertAllContextualLinksLoaded();
32     }
33   }
34
35   /**
36    * Assert all contextual link areas have be loaded.
37    *
38    * Contextual link placeholders will be filled after
39    * the page is received.
40    *
41    * @todo Move this function to https://www.drupal.org/node/2821724.
42    */
43   protected function assertAllContextualLinksLoaded() {
44     $this->waitForNoElement('[data-contextual-id]:empty');
45   }
46
47   /**
48    * Enables a theme.
49    *
50    * @param string $theme
51    *   The theme.
52    */
53   protected function enableTheme($theme) {
54     // Enable the theme.
55     \Drupal::service('theme_installer')->install([$theme]);
56     $theme_config = \Drupal::configFactory()->getEditable('system.theme');
57     $theme_config->set('default', $theme);
58     $theme_config->save();
59   }
60
61   /**
62    * Waits for off-canvas dialog to open.
63    *
64    * @param string $position
65    *   The position of the dialog.
66    *
67    * @throws \Behat\Mink\Exception\ElementNotFoundException
68    */
69   protected function waitForOffCanvasToOpen($position = 'side') {
70     $web_assert = $this->assertSession();
71     // Wait just slightly longer than the off-canvas dialog CSS animation.
72     // @see core/misc/dialog/off-canvas.motion.css
73     $this->getSession()->wait(800);
74     $web_assert->assertWaitOnAjaxRequest();
75     $this->assertElementVisibleAfterWait('css', '#drupal-off-canvas');
76     // Check that the canvas is positioned on the side.
77     $web_assert->elementExists('css', '.ui-dialog-position-' . $position);
78   }
79
80   /**
81    * Waits for off-canvas dialog to close.
82    */
83   protected function waitForOffCanvasToClose() {
84     $this->waitForNoElement('#drupal-off-canvas');
85   }
86
87   /**
88    * Gets the off-canvas dialog element.
89    *
90    * @return \Behat\Mink\Element\NodeElement|null
91    */
92   protected function getOffCanvasDialog() {
93     $off_canvas_dialog = $this->getSession()->getPage()->find('css', '.ui-dialog[aria-describedby="drupal-off-canvas"]');
94     $this->assertEquals(FALSE, empty($off_canvas_dialog), 'The off-canvas dialog was found.');
95     return $off_canvas_dialog;
96   }
97
98   /**
99    * Waits for an element to be removed from the page.
100    *
101    * @param string $selector
102    *   CSS selector.
103    * @param int $timeout
104    *   (optional) Timeout in milliseconds, defaults to 10000.
105    *
106    * @todo Remove in https://www.drupal.org/node/2892440.
107    */
108   protected function waitForNoElement($selector, $timeout = 10000) {
109
110     $start = microtime(TRUE);
111     $end = $start + ($timeout / 1000);
112     $page = $this->getSession()->getPage();
113
114     do {
115       $result = $page->find('css', $selector);
116
117       if (empty($result)) {
118         return;
119       }
120
121       usleep(100000);
122     } while (microtime(TRUE) < $end);
123
124     $this->assertEmpty($result, 'Element was not on the page after wait.');
125   }
126
127   /**
128    * Get themes to test.
129    *
130    * @return string[]
131    *   Theme names to test.
132    */
133   protected function getTestThemes() {
134     return ['bartik', 'stark', 'classy', 'stable', 'seven'];
135   }
136
137   /**
138    * Asserts the specified selector is visible after a wait.
139    *
140    * @param string $selector
141    *   The selector engine name. See ElementInterface::findAll() for the
142    *   supported selectors.
143    * @param string|array $locator
144    *   The selector locator.
145    * @param int $timeout
146    *   (Optional) Timeout in milliseconds, defaults to 10000.
147    */
148   protected function assertElementVisibleAfterWait($selector, $locator, $timeout = 10000) {
149     $this->assertSession()->assertWaitOnAjaxRequest();
150     $this->assertNotEmpty($this->assertSession()->waitForElementVisible($selector, $locator, $timeout));
151   }
152
153   /**
154    * Dataprovider that returns theme name as the sole argument.
155    */
156   public function themeDataProvider() {
157     $themes = $this->getTestThemes();
158     $data = [];
159     foreach ($themes as $theme) {
160       $data[$theme] = [
161         $theme,
162       ];
163     }
164     return $data;
165   }
166
167 }