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