summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/system/tests/src/FunctionalJavascript/OffCanvasTestBase.php
blob: 188e1bac91880c99c880f87114805e0520f146dd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php

declare(strict_types=1);

namespace Drupal\Tests\system\FunctionalJavascript;

use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\Tests\system\Traits\OffCanvasTestTrait;

/**
 * Base class contains common test functionality for the Off-canvas dialog.
 */
abstract class OffCanvasTestBase extends WebDriverTestBase {

  use OffCanvasTestTrait;

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'off_canvas_test',
  ];

  /**
   * {@inheritdoc}
   */
  protected function drupalGet($path, array $options = [], array $headers = []) {
    $return = parent::drupalGet($path, $options, $headers);
    $this->assertPageLoadComplete();
    return $return;
  }

  /**
   * Assert the page is completely loaded.
   *
   * Ajax requests may happen after page loads. Also for users who have access
   * to contextual links the contextual link placeholders will be filled after
   * the page is received.
   */
  protected function assertPageLoadComplete() {
    if ($this->loggedInUser && $this->loggedInUser->hasPermission('access contextual links')) {
      $this->assertAllContextualLinksLoaded();
    }
  }

  /**
   * Assert all contextual link areas have be loaded.
   *
   * Contextual link placeholders will be filled after
   * the page is received.
   *
   * @todo Move this function to https://www.drupal.org/node/2821724.
   */
  protected function assertAllContextualLinksLoaded() {
    $this->assertSession()->assertNoElementAfterWait('css', '[data-contextual-id]:empty');
  }

  /**
   * Enables a theme.
   *
   * @param string $theme
   *   The theme.
   */
  protected function enableTheme($theme) {
    // Enable the theme.
    \Drupal::service('theme_installer')->install([$theme]);
    $theme_config = \Drupal::configFactory()->getEditable('system.theme');
    $theme_config->set('default', $theme);
    $theme_config->save();
  }

  /**
   * Waits for off-canvas dialog to open.
   *
   * @param string $position
   *   The position of the dialog.
   *
   * @throws \Behat\Mink\Exception\ElementNotFoundException
   */
  protected function waitForOffCanvasToOpen($position = 'side') {
    $this->waitForOffCanvasArea();
    // Check that the canvas is positioned on the side.
    $this->assertSession()->elementExists('css', '.ui-dialog-position-' . $position);
  }

  /**
   * Waits for off-canvas dialog to close.
   */
  protected function waitForOffCanvasToClose() {
    $this->assertSession()->assertNoElementAfterWait('css', '#drupal-off-canvas');
  }

  /**
   * Gets the off-canvas dialog element.
   *
   * @return \Behat\Mink\Element\NodeElement|null
   *   The off-canvas dialog element.
   */
  protected function getOffCanvasDialog() {
    $off_canvas_dialog = $this->getSession()->getPage()->find('css', '.ui-dialog[aria-describedby="drupal-off-canvas"]');
    $this->assertEquals(FALSE, empty($off_canvas_dialog), 'The off-canvas dialog was found.');
    return $off_canvas_dialog;
  }

  /**
   * Get themes to test.
   *
   * @return string[]
   *   Theme names to test.
   */
  protected static function getTestThemes() {
    return ['claro', 'olivero', 'stable9', 'stark'];
  }

  /**
   * Asserts the specified selector is visible after a wait.
   *
   * @param string $selector
   *   The selector engine name. See ElementInterface::findAll() for the
   *   supported selectors.
   * @param string|array $locator
   *   The selector locator.
   * @param int $timeout
   *   (Optional) Timeout in milliseconds, defaults to 10000.
   */
  protected function assertElementVisibleAfterWait($selector, $locator, $timeout = 10000) {
    $this->assertSession()->assertWaitOnAjaxRequest();
    $this->assertNotEmpty($this->assertSession()->waitForElementVisible($selector, $locator, $timeout));
  }

  /**
   * Data provider that returns theme name as the sole argument.
   */
  public static function themeDataProvider() {
    $themes = static::getTestThemes();
    $data = [];
    foreach ($themes as $theme) {
      $data[$theme] = [
        $theme,
      ];
    }
    return $data;
  }

}