3 namespace Drupal\DrupalExtension\Context;
5 use Behat\Behat\Context\TranslatableContext;
6 use Behat\Gherkin\Node\TableNode;
9 * Provides step-definitions for interacting with Drupal messages.
11 class MessageContext extends RawDrupalContext implements TranslatableContext {
16 public static function getTranslationResources() {
17 return glob(__DIR__ . '/../../../../i18n/*.xliff');
21 * Checks if the current page contains the given error message
24 * string The text to be checked
26 * @Then I should see the error message( containing) :message
28 public function assertErrorVisible($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'"
38 * Checks if the current page contains the given set of error messages
41 * array An array of texts to be checked
43 * @Then I should see the following error message(s):
45 public function assertMultipleErrors(TableNode $messages) {
46 foreach ($messages->getHash() as $key => $value) {
47 $message = trim($value['error messages']);
48 $this->assertErrorVisible($message);
53 * Checks if the current page does not contain the given error message
56 * string The text to be checked
58 * @Given I should not see the error message( containing) :message
60 public function assertNotErrorVisible($message) {
63 'error_message_selector',
64 "The page '%s' contains the error message '%s'"
69 * Checks if the current page does not contain the given set error messages
72 * array An array of texts to be checked
74 * @Then I should not see the following error messages:
76 public function assertNotMultipleErrors(TableNode $messages) {
77 foreach ($messages->getHash() as $key => $value) {
78 $message = trim($value['error messages']);
79 $this->assertNotErrorVisible($message);
84 * Checks if the current page contains the given success message
87 * string The text to be checked
89 * @Then I should see the success message( containing) :message
91 public function assertSuccessMessage($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'"
101 * Checks if the current page contains the given set of success messages
104 * array An array of texts to be checked
106 * @Then I should see the following success messages:
108 public function assertMultipleSuccessMessage(TableNode $messages) {
109 foreach ($messages->getHash() as $key => $value) {
110 $message = trim($value['success messages']);
111 $this->assertSuccessMessage($message);
116 * Checks if the current page does not contain the given set of success message
119 * string The text to be checked
121 * @Given I should not see the success message( containing) :message
123 public function assertNotSuccessMessage($message) {
126 'success_message_selector',
127 "The page '%s' contains the success message '%s'"
132 * Checks if the current page does not contain the given set of success messages
135 * array An array of texts to be checked
137 * @Then I should not see the following success messages:
139 public function assertNotMultipleSuccessMessage(TableNode $messages) {
140 foreach ($messages->getHash() as $key => $value) {
141 $message = trim($value['success messages']);
142 $this->assertNotSuccessMessage($message);
147 * Checks if the current page contains the given warning message
150 * string The text to be checked
152 * @Then I should see the warning message( containing) :message
154 public function assertWarningMessage($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'"
164 * Checks if the current page contains the given set of warning messages
167 * array An array of texts to be checked
169 * @Then I should see the following warning messages:
171 public function assertMultipleWarningMessage(TableNode $messages) {
172 foreach ($messages->getHash() as $key => $value) {
173 $message = trim($value['warning messages']);
174 $this->assertWarningMessage($message);
179 * Checks if the current page does not contain the given set of warning message
182 * string The text to be checked
184 * @Given I should not see the warning message( containing) :message
186 public function assertNotWarningMessage($message) {
189 'warning_message_selector',
190 "The page '%s' contains the warning message '%s'"
195 * Checks if the current page does not contain the given set of warning messages
198 * array An array of texts to be checked
200 * @Then I should not see the following warning messages:
202 public function assertNotMultipleWarningMessage(TableNode $messages) {
203 foreach ($messages->getHash() as $key => $value) {
204 $message = trim($value['warning messages']);
205 $this->assertNotWarningMessage($message);
210 * Checks if the current page contain the given message
213 * string The message to be checked
215 * @Then I should see the message( containing) :message
217 public function assertMessage($message) {
221 "The page '%s' does not contain any messages",
222 "The page '%s' does not contain the message '%s'"
227 * Checks if the current page does not contain the given message
230 * string The message to be checked
232 * @Then I should not see the message( containing) :message
234 public function assertNotMessage($message) {
238 "The page '%s' contains the message '%s'"
243 * Internal callback to check for a specific message in a given context.
246 * string The message to be checked
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.
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()));
263 foreach ($selectorObjects as $selectorObject) {
264 if (strpos(trim($selectorObject->getText()), $message) !== FALSE) {
268 throw new \Exception(sprintf($exceptionMsgMissing, $this->getSession()->getCurrentUrl(), $message));
272 * Internal callback to check if the current page does not contain the given message
275 * string The message to be checked
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.
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));