X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=vendor%2Fdrupal%2Fdrupal-extension%2Fsrc%2FDrupal%2FDrupalExtension%2FContext%2FMessageContext.php;fp=vendor%2Fdrupal%2Fdrupal-extension%2Fsrc%2FDrupal%2FDrupalExtension%2FContext%2FMessageContext.php;h=0d5e7ddacc070ddbe80dcf0861dbaf9d8be32dc2;hp=0000000000000000000000000000000000000000;hb=1270d9129ce8f27c9b28b10518e32132c58e0aca;hpb=c27c0f0cdaa3f354b1fe54a56ae7e854be6e3f68 diff --git a/vendor/drupal/drupal-extension/src/Drupal/DrupalExtension/Context/MessageContext.php b/vendor/drupal/drupal-extension/src/Drupal/DrupalExtension/Context/MessageContext.php new file mode 100644 index 000000000..0d5e7ddac --- /dev/null +++ b/vendor/drupal/drupal-extension/src/Drupal/DrupalExtension/Context/MessageContext.php @@ -0,0 +1,295 @@ +_assert( + $message, + 'error_message_selector', + "The page '%s' does not contain any error messages", + "The page '%s' does not contain the error message '%s'" + ); + } + + /** + * Checks if the current page contains the given set of error messages + * + * @param $messages + * array An array of texts to be checked + * + * @Then I should see the following error message(s): + */ + public function assertMultipleErrors(TableNode $messages) { + foreach ($messages->getHash() as $key => $value) { + $message = trim($value['error messages']); + $this->assertErrorVisible($message); + } + } + + /** + * Checks if the current page does not contain the given error message + * + * @param $message + * string The text to be checked + * + * @Given I should not see the error message( containing) :message + */ + public function assertNotErrorVisible($message) { + $this->_assertNot( + $message, + 'error_message_selector', + "The page '%s' contains the error message '%s'" + ); + } + + /** + * Checks if the current page does not contain the given set error messages + * + * @param $messages + * array An array of texts to be checked + * + * @Then I should not see the following error messages: + */ + public function assertNotMultipleErrors(TableNode $messages) { + foreach ($messages->getHash() as $key => $value) { + $message = trim($value['error messages']); + $this->assertNotErrorVisible($message); + } + } + + /** + * Checks if the current page contains the given success message + * + * @param $message + * string The text to be checked + * + * @Then I should see the success message( containing) :message + */ + public function assertSuccessMessage($message) { + $this->_assert( + $message, + 'success_message_selector', + "The page '%s' does not contain any success messages", + "The page '%s' does not contain the success message '%s'" + ); + } + + /** + * Checks if the current page contains the given set of success messages + * + * @param $message + * array An array of texts to be checked + * + * @Then I should see the following success messages: + */ + public function assertMultipleSuccessMessage(TableNode $messages) { + foreach ($messages->getHash() as $key => $value) { + $message = trim($value['success messages']); + $this->assertSuccessMessage($message); + } + } + + /** + * Checks if the current page does not contain the given set of success message + * + * @param $message + * string The text to be checked + * + * @Given I should not see the success message( containing) :message + */ + public function assertNotSuccessMessage($message) { + $this->_assertNot( + $message, + 'success_message_selector', + "The page '%s' contains the success message '%s'" + ); + } + + /** + * Checks if the current page does not contain the given set of success messages + * + * @param $message + * array An array of texts to be checked + * + * @Then I should not see the following success messages: + */ + public function assertNotMultipleSuccessMessage(TableNode $messages) { + foreach ($messages->getHash() as $key => $value) { + $message = trim($value['success messages']); + $this->assertNotSuccessMessage($message); + } + } + + /** + * Checks if the current page contains the given warning message + * + * @param $message + * string The text to be checked + * + * @Then I should see the warning message( containing) :message + */ + public function assertWarningMessage($message) { + $this->_assert( + $message, + 'warning_message_selector', + "The page '%s' does not contain any warning messages", + "The page '%s' does not contain the warning message '%s'" + ); + } + + /** + * Checks if the current page contains the given set of warning messages + * + * @param $message + * array An array of texts to be checked + * + * @Then I should see the following warning messages: + */ + public function assertMultipleWarningMessage(TableNode $messages) { + foreach ($messages->getHash() as $key => $value) { + $message = trim($value['warning messages']); + $this->assertWarningMessage($message); + } + } + + /** + * Checks if the current page does not contain the given set of warning message + * + * @param $message + * string The text to be checked + * + * @Given I should not see the warning message( containing) :message + */ + public function assertNotWarningMessage($message) { + $this->_assertNot( + $message, + 'warning_message_selector', + "The page '%s' contains the warning message '%s'" + ); + } + + /** + * Checks if the current page does not contain the given set of warning messages + * + * @param $message + * array An array of texts to be checked + * + * @Then I should not see the following warning messages: + */ + public function assertNotMultipleWarningMessage(TableNode $messages) { + foreach ($messages->getHash() as $key => $value) { + $message = trim($value['warning messages']); + $this->assertNotWarningMessage($message); + } + } + + /** + * Checks if the current page contain the given message + * + * @param $message + * string The message to be checked + * + * @Then I should see the message( containing) :message + */ + public function assertMessage($message) { + $this->_assert( + $message, + 'message_selector', + "The page '%s' does not contain any messages", + "The page '%s' does not contain the message '%s'" + ); + } + + /** + * Checks if the current page does not contain the given message + * + * @param $message + * string The message to be checked + * + * @Then I should not see the message( containing) :message + */ + public function assertNotMessage($message) { + $this->_assertNot( + $message, + 'message_selector', + "The page '%s' contains the message '%s'" + ); + } + + /** + * Internal callback to check for a specific message in a given context. + * + * @param $message + * string The message to be checked + * @param $selectorId + * string CSS selector name + * @param $exceptionMsgNone + * string The message being thrown when no message is contained, string + * should contain one '%s' as a placeholder for the current URL + * @param $exceptionMsgMissing + * string The message being thrown when the message is not contained, string + * should contain two '%s' as placeholders for the current URL and the message. + * @throws \Exception + */ + private function _assert($message, $selectorId, $exceptionMsgNone, $exceptionMsgMissing) { + $selector = $this->getDrupalSelector($selectorId); + $selectorObjects = $this->getSession()->getPage()->findAll("css", $selector); + if (empty($selectorObjects)) { + throw new \Exception(sprintf($exceptionMsgNone, $this->getSession()->getCurrentUrl())); + } + foreach ($selectorObjects as $selectorObject) { + if (strpos(trim($selectorObject->getText()), $message) !== FALSE) { + return; + } + } + throw new \Exception(sprintf($exceptionMsgMissing, $this->getSession()->getCurrentUrl(), $message)); + } + + /** + * Internal callback to check if the current page does not contain the given message + * + * @param $message + * string The message to be checked + * @param $selectorId + * string CSS selector name + * @param $exceptionMsg + * string The message being thrown when the message is contained, string + * should contain two '%s' as placeholders for the current URL and the message. + * @throws \Exception + */ + private function _assertNot($message, $selectorId, $exceptionMsg) { + $selector = $this->getDrupalSelector($selectorId); + $selectorObjects = $this->getSession()->getPage()->findAll("css", $selector); + if (!empty($selectorObjects)) { + foreach ($selectorObjects as $selectorObject) { + if (strpos(trim($selectorObject->getText()), $message) !== FALSE) { + throw new \Exception(sprintf($exceptionMsg, $this->getSession()->getCurrentUrl(), $message)); + } + } + } + } + +}