Completed
Push — Leksat-patch-1 ( 31c1c0...66f278 )
by Jonathan
01:23
created

MessageContext::getDrupalSelector()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 7
Ratio 100 %

Importance

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