Updated to Drupal 8.5. Core Media not yet in use.
[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\JavascriptTestBase;
6
7 /**
8  * Base class contains common test functionality for the Off-canvas dialog.
9  */
10 abstract class OffCanvasTestBase extends JavascriptTestBase {
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   protected function waitForOffCanvasToOpen() {
65     $web_assert = $this->assertSession();
66     // Wait just slightly longer than the off-canvas dialog CSS animation.
67     // @see core/misc/dialog/off-canvas.motion.css
68     $this->getSession()->wait(800);
69     $web_assert->assertWaitOnAjaxRequest();
70     $this->assertElementVisibleAfterWait('css', '#drupal-off-canvas');
71   }
72
73   /**
74    * Waits for off-canvas dialog to close.
75    */
76   protected function waitForOffCanvasToClose() {
77     $this->waitForNoElement('#drupal-off-canvas');
78   }
79
80   /**
81    * Gets the off-canvas dialog element.
82    *
83    * @return \Behat\Mink\Element\NodeElement|null
84    */
85   protected function getOffCanvasDialog() {
86     $off_canvas_dialog = $this->getSession()->getPage()->find('css', '.ui-dialog[aria-describedby="drupal-off-canvas"]');
87     $this->assertEquals(FALSE, empty($off_canvas_dialog), 'The off-canvas dialog was found.');
88     return $off_canvas_dialog;
89   }
90
91   /**
92    * Waits for an element to be removed from the page.
93    *
94    * @param string $selector
95    *   CSS selector.
96    * @param int $timeout
97    *   (optional) Timeout in milliseconds, defaults to 10000.
98    *
99    * @todo Remove in https://www.drupal.org/node/2892440.
100    */
101   protected function waitForNoElement($selector, $timeout = 10000) {
102     $condition = "(typeof jQuery !== 'undefined' && jQuery('$selector').length === 0)";
103     $this->assertJsCondition($condition, $timeout);
104   }
105
106   /**
107    * Get themes to test.
108    *
109    * @return string[]
110    *   Theme names to test.
111    */
112   protected function getTestThemes() {
113     return ['bartik', 'stark', 'classy', 'stable', 'seven'];
114   }
115
116   /**
117    * Asserts the specified selector is visible after a wait.
118    *
119    * @param string $selector
120    *   The selector engine name. See ElementInterface::findAll() for the
121    *   supported selectors.
122    * @param string|array $locator
123    *   The selector locator.
124    * @param int $timeout
125    *   (Optional) Timeout in milliseconds, defaults to 10000.
126    */
127   protected function assertElementVisibleAfterWait($selector, $locator, $timeout = 10000) {
128     $this->assertNotEmpty($this->assertSession()->waitForElementVisible($selector, $locator, $timeout));
129   }
130
131 }