annotate core/modules/system/tests/src/FunctionalJavascript/OffCanvasTestBase.php @ 19:fa3358dc1485 tip

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