annotate core/modules/system/tests/src/FunctionalJavascript/OffCanvasTestBase.php @ 14:1fec387a4317

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