Completed
Pull Request — master (#237)
by Fabian
02:30
created

MessageContext::getDrupalSelector()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 7
Ratio 100 %
Metric Value
dl 7
loc 7
rs 9.4286
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Drupal\DrupalExtension\Context;
4
5
use Behat\Behat\Context\TranslatableContext;
6
use Behat\Gherkin\Node\TableNode;
7
8
/**
9
 * Provides step-definitions for interacting with Drupal messages.
10
 */
11
class MessageContext extends RawDrupalContext implements TranslatableContext {
12
13
  /**
14
   * {@inheritDoc}
15
   */
16
  public static function getTranslationResources() {
17
    return glob(__DIR__ . '/../../../../i18n/*.xliff');
18
  }
19
20
  /**
21
   * Checks if the current page contains the given error message
22
   *
23
   * @param $message
24
   *   string The text to be checked
25
   *
26
   * @Then I should see the error message( containing) :message
27
   */
28
  public function assertErrorVisible($message) {
29
    $this->_assert(
30
      $message,
31
      'error_message_selector',
32
      "The page '%s' does not contain any error messages",
33
      "The page '%s' does not contain the error message '%s'"
34
    );
35
  }
36
37
  /**
38
   * Checks if the current page contains the given set of error messages
39
   *
40
   * @param $messages
41
   *   array An array of texts to be checked
42
   *
43
   * @Then I should see the following error message(s):
44
   */
45
  public function assertMultipleErrors(TableNode $messages) {
46
    foreach ($messages->getHash() as $key => $value) {
47
      $message = trim($value['error messages']);
48
      $this->assertErrorVisible($message);
49
    }
50
  }
51
52
  /**
53
   * Checks if the current page does not contain the given error message
54
   *
55
   * @param $message
56
   *   string The text to be checked
57
   *
58
   * @Given I should not see the error message( containing) :message
59
   */
60
  public function assertNotErrorVisible($message) {
61
    $this->_assertNot(
62
      $message,
63
      'error_message_selector',
64
      "The page '%s' contains the error message '%s'"
65
    );
66
  }
67
68
  /**
69
   * Checks if the current page does not contain the given set error messages
70
   *
71
   * @param $messages
72
   *   array An array of texts to be checked
73
   *
74
   * @Then I should not see the following error messages:
75
   */
76
  public function assertNotMultipleErrors(TableNode $messages) {
77
    foreach ($messages->getHash() as $key => $value) {
78
      $message = trim($value['error messages']);
79
      $this->assertNotErrorVisible($message);
80
    }
81
  }
82
83
  /**
84
   * Checks if the current page contains the given success message
85
   *
86
   * @param $message
87
   *   string The text to be checked
88
   *
89
   * @Then I should see the success message( containing) :message
90
   */
91
  public function assertSuccessMessage($message) {
92
    $this->_assert(
93
      $message,
94
      'success_message_selector',
95
      "The page '%s' does not contain any success messages",
96
      "The page '%s' does not contain the success message '%s'"
97
    );
98
  }
99
100
  /**
101
   * Checks if the current page contains the given set of success messages
102
   *
103
   * @param $message
104
   *   array An array of texts to be checked
105
   *
106
   * @Then I should see the following success messages:
107
   */
108
  public function assertMultipleSuccessMessage(TableNode $messages) {
109
    foreach ($messages->getHash() as $key => $value) {
110
      $message = trim($value['success messages']);
111
      $this->assertSuccessMessage($message);
112
    }
113
  }
114
115
  /**
116
   * Checks if the current page does not contain the given set of success message
117
   *
118
   * @param $message
119
   *   string The text to be checked
120
   *
121
   * @Given I should not see the success message( containing) :message
122
   */
123
  public function assertNotSuccessMessage($message) {
124
    $this->_assertNot(
125
      $message,
126
      'success_message_selector',
127
      "The page '%s' contains the success message '%s'"
128
    );
129
  }
130
131
  /**
132
   * Checks if the current page does not contain the given set of success messages
133
   *
134
   * @param $message
135
   *   array An array of texts to be checked
136
   *
137
   * @Then I should not see the following success messages:
138
   */
139
  public function assertNotMultipleSuccessMessage(TableNode $messages) {
140
    foreach ($messages->getHash() as $key => $value) {
141
      $message = trim($value['success messages']);
142
      $this->assertNotSuccessMessage($message);
143
    }
144
  }
145
146
  /**
147
   * Checks if the current page contains the given warning message
148
   *
149
   * @param $message
150
   *   string The text to be checked
151
   *
152
   * @Then I should see the warning message( containing) :message
153
   */
154
  public function assertWarningMessage($message) {
155
    $this->_assert(
156
      $message,
157
      'warning_message_selector',
158
      "The page '%s' does not contain any warning messages",
159
      "The page '%s' does not contain the warning message '%s'"
160
    );
161
  }
162
163
  /**
164
   * Checks if the current page contains the given set of warning messages
165
   *
166
   * @param $message
167
   *   array An array of texts to be checked
168
   *
169
   * @Then I should see the following warning messages:
170
   */
171
  public function assertMultipleWarningMessage(TableNode $messages) {
172
    foreach ($messages->getHash() as $key => $value) {
173
      $message = trim($value['warning messages']);
174
      $this->assertWarningMessage($message);
175
    }
176
  }
177
178
  /**
179
   * Checks if the current page does not contain the given set of warning message
180
   *
181
   * @param $message
182
   *   string The text to be checked
183
   *
184
   * @Given I should not see the warning message( containing) :message
185
   */
186
  public function assertNotWarningMessage($message) {
187
    $this->_assertNot(
188
      $message,
189
      'warning_message_selector',
190
      "The page '%s' contains the warning message '%s'"
191
    );
192
  }
193
194
  /**
195
   * Checks if the current page does not contain the given set of warning messages
196
   *
197
   * @param $message
198
   *   array An array of texts to be checked
199
   *
200
   * @Then I should not see the following warning messages:
201
   */
202
  public function assertNotMultipleWarningMessage(TableNode $messages) {
203
    foreach ($messages->getHash() as $key => $value) {
204
      $message = trim($value['warning messages']);
205
      $this->assertNotWarningMessage($message);
206
    }
207
  }
208
209
  /**
210
   * Checks if the current page contain the given message
211
   *
212
   * @param $message
213
   *   string The message to be checked
214
   *
215
   * @Then I should see the message( containing) :message
216
   */
217
  public function assertMessage($message) {
218
    $this->_assert(
219
      $message,
220
      'message_selector',
221
      "The page '%s' does not contain any messages",
222
      "The page '%s' does not contain the message '%s'"
223
    );
224
  }
225
226
  /**
227
   * Checks if the current page does not contain the given message
228
   *
229
   * @param $message
230
   *   string The message to be checked
231
   *
232
   * @Then I should not see the message( containing) :message
233
   */
234
  public function assertNotMessage($message) {
235
    $this->_assertNot(
236
      $message,
237
      'message_selector',
238
      "The page '%s' contains the message '%s'"
239
    );
240
  }
241
242
  /**
243
   * Internal callback to check for a specific message in a given context.
244
   *
245
   * @param $message
246
   *   string The message to be checked
247
   * @param $selectorId
248
   *   string CSS selector name
249
   * @param $exceptionMsgNone
250
   *   string The message being thrown when no message is contained, string
251
   *   should contain one '%s' as a placeholder for the current URL
252
   * @param $exceptionMsgMissing
253
   *   string The message being thrown when the message is not contained, string
254
   *   should contain two '%s' as placeholders for the current URL and the message.
255
   * @throws \Exception
256
   */
257
  private function _assert($message, $selectorId, $exceptionMsgNone, $exceptionMsgMissing) {
258
    $selector = $this->getDrupalSelector($selectorId);
259
    $selectorObjects = $this->getSession()->getPage()->findAll("css", $selector);
260
    if (empty($selectorObjects)) {
261
      throw new \Exception(sprintf($exceptionMsgNone, $this->getSession()->getCurrentUrl()));
262
    }
263
    foreach ($selectorObjects as $selectorObject) {
264
      if (strpos(trim($selectorObject->getText()), $message) !== FALSE) {
265
        return;
266
      }
267
    }
268
    throw new \Exception(sprintf($exceptionMsgMissing, $this->getSession()->getCurrentUrl(), $message));
269
  }
270
271
  /**
272
   * Internal callback to check if the current page does not contain the given message
273
   *
274
   * @param $message
275
   *   string The message to be checked
276
   * @param $selectorId
277
   *   string CSS selector name
278
   * @param $exceptionMsg
279
   *   string The message being thrown when the message is contained, string
280
   *   should contain two '%s' as placeholders for the current URL and the message.
281
   * @throws \Exception
282
   */
283
  private function _assertNot($message, $selectorId, $exceptionMsg) {
284
    $selector = $this->getDrupalSelector($selectorId);
285
    $selectorObjects = $this->getSession()->getPage()->findAll("css", $selector);
286
    if (!empty($selectorObjects)) {
287
      foreach ($selectorObjects as $selectorObject) {
288
        if (strpos(trim($selectorObject->getText()), $message) !== FALSE) {
289
          throw new \Exception(sprintf($exceptionMsg, $this->getSession()->getCurrentUrl(), $message));
290
        }
291
      }
292
    }
293
  }
294
295
}
296