Completed
Pull Request — 3.1 (#290)
by
unknown
09:17
created

MessageContext::assertNotErrorVisible()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
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
   * Returns a specific css selector.
244
   *
245
   * @param $name
246
   *   string CSS selector name
247
   */
248 View Code Duplication
  public function getDrupalSelector($name) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
249
    $text = $this->getDrupalParameter('selectors');
250
    if (!isset($text[$name])) {
251
      throw new \Exception(sprintf('No such selector configured: %s', $name));
252
    }
253
    return $text[$name];
254
  }
255
256
  /**
257
   * Internal callback to check for a specific message in a given context.
258
   *
259
   * @param $message
260
   *   string The message to be checked
261
   * @param $selectorId
262
   *   string CSS selector name
263
   * @param $exceptionMsgNone
264
   *   string The message being thrown when no message is contained, string
265
   *   should contain one '%s' as a placeholder for the current URL
266
   * @param $exceptionMsgMissing
267
   *   string The message being thrown when the message is not contained, string
268
   *   should contain two '%s' as placeholders for the current URL and the message.
269
   * @throws \Exception
270
   */
271
  private function _assert($message, $selectorId, $exceptionMsgNone, $exceptionMsgMissing) {
272
    $selector = $this->getDrupalSelector($selectorId);
273
    $selectorObjects = $this->getSession()->getPage()->findAll("css", $selector);
274
    if (empty($selectorObjects)) {
275
      throw new \Exception(sprintf($exceptionMsgNone, $this->getSession()->getCurrentUrl()));
276
    }
277
    foreach ($selectorObjects as $selectorObject) {
278
      if (strpos(trim($selectorObject->getText()), $message) !== FALSE) {
279
        return;
280
      }
281
    }
282
    throw new \Exception(sprintf($exceptionMsgMissing, $this->getSession()->getCurrentUrl(), $message));
283
  }
284
285
  /**
286
   * Internal callback to check if the current page does not contain the given message
287
   *
288
   * @param $message
289
   *   string The message to be checked
290
   * @param $selectorId
291
   *   string CSS selector name
292
   * @param $exceptionMsg
293
   *   string The message being thrown when the message is contained, string
294
   *   should contain two '%s' as placeholders for the current URL and the message.
295
   * @throws \Exception
296
   */
297
  private function _assertNot($message, $selectorId, $exceptionMsg) {
298
    $selector = $this->getDrupalSelector($selectorId);
299
    $selectorObjects = $this->getSession()->getPage()->findAll("css", $selector);
300
    if (!empty($selectorObjects)) {
301
      foreach ($selectorObjects as $selectorObject) {
302
        if (strpos(trim($selectorObject->getText()), $message) !== FALSE) {
303
          throw new \Exception(sprintf($exceptionMsg, $this->getSession()->getCurrentUrl(), $message));
304
        }
305
      }
306
    }
307
  }
308
309
}
310