From 83f218e41038f26e83a27d1534aac61661882283 Mon Sep 17 00:00:00 2001 From: Pieter Frenssen Date: Mon, 15 Oct 2018 10:50:33 +0300 Subject: [PATCH] Warn users when the message table is not correctly formatted. --- .../Context/MessageContext.php | 71 +++++++++++++++---- 1 file changed, 59 insertions(+), 12 deletions(-) diff --git a/src/Drupal/DrupalExtension/Context/MessageContext.php b/src/Drupal/DrupalExtension/Context/MessageContext.php index 9b10c75a..1d41cff8 100644 --- a/src/Drupal/DrupalExtension/Context/MessageContext.php +++ b/src/Drupal/DrupalExtension/Context/MessageContext.php @@ -41,14 +41,17 @@ public function assertErrorVisible($message) /** * Checks if the current page contains the given set of error messages * - * @param $messages - * array An array of texts to be checked + * @param array $messages + * An array of texts to be checked. The first row should consist of the + * string "Error messages". * * @Then I should see the following error message(s): */ public function assertMultipleErrors(TableNode $messages) { + $this->assertValidMessageTable($messages, 'error messages'); foreach ($messages->getHash() as $key => $value) { + $value = array_change_key_case($value); $message = trim($value['error messages']); $this->assertErrorVisible($message); } @@ -74,14 +77,17 @@ public function assertNotErrorVisible($message) /** * Checks if the current page does not contain the given set error messages * - * @param $messages - * array An array of texts to be checked + * @param array $messages + * An array of texts to be checked. The first row should consist of the + * string "Error messages". * * @Then I should not see the following error messages: */ public function assertNotMultipleErrors(TableNode $messages) { + $this->assertValidMessageTable($messages, 'error messages'); foreach ($messages->getHash() as $key => $value) { + $value = array_change_key_case($value); $message = trim($value['error messages']); $this->assertNotErrorVisible($message); } @@ -108,14 +114,17 @@ public function assertSuccessMessage($message) /** * Checks if the current page contains the given set of success messages * - * @param $message - * array An array of texts to be checked + * @param array $message + * An array of texts to be checked. The first row should consist of the + * string "Success messages". * * @Then I should see the following success messages: */ public function assertMultipleSuccessMessage(TableNode $messages) { + $this->assertValidMessageTable($messages, 'success messages'); foreach ($messages->getHash() as $key => $value) { + $value = array_change_key_case($value); $message = trim($value['success messages']); $this->assertSuccessMessage($message); } @@ -141,14 +150,17 @@ public function assertNotSuccessMessage($message) /** * Checks if the current page does not contain the given set of success messages * - * @param $message - * array An array of texts to be checked + * @param array $message + * An array of texts to be checked. The first row should consist of the + * string "Success messages". * * @Then I should not see the following success messages: */ public function assertNotMultipleSuccessMessage(TableNode $messages) { + $this->assertValidMessageTable($messages, 'success messages'); foreach ($messages->getHash() as $key => $value) { + $value = array_change_key_case($value); $message = trim($value['success messages']); $this->assertNotSuccessMessage($message); } @@ -175,14 +187,17 @@ public function assertWarningMessage($message) /** * Checks if the current page contains the given set of warning messages * - * @param $message - * array An array of texts to be checked + * @param array $message + * An array of texts to be checked. The first row should consist of the + * string "Warning messages". * * @Then I should see the following warning messages: */ public function assertMultipleWarningMessage(TableNode $messages) { + $this->assertValidMessageTable($messages, 'warning messages'); foreach ($messages->getHash() as $key => $value) { + $value = array_change_key_case($value); $message = trim($value['warning messages']); $this->assertWarningMessage($message); } @@ -208,14 +223,17 @@ public function assertNotWarningMessage($message) /** * Checks if the current page does not contain the given set of warning messages * - * @param $message - * array An array of texts to be checked + * @param array $message + * An array of texts to be checked. The first row should consist of the + * string "Warning messages". * * @Then I should not see the following warning messages: */ public function assertNotMultipleWarningMessage(TableNode $messages) { + $this->assertValidMessageTable($messages, 'warning messages'); foreach ($messages->getHash() as $key => $value) { + $value = array_change_key_case($value); $message = trim($value['warning messages']); $this->assertNotWarningMessage($message); } @@ -256,6 +274,35 @@ public function assertNotMessage($message) ); } + /** + * Checks whether the given list of messages is valid. + * + * This checks whether the list has only one column and has the correct + * header. + * + * @param \Behat\Gherkin\Node\TableNode $messages + * The list of messages. + * @param string $expected_header + * The header that should be present in the list. + */ + protected function assertValidMessageTable(TableNode $messages, $expected_header) + { + // Check that the table only contains a single column. + $header_row = $messages->getRow(0); + + $column_count = count($header_row); + if ($column_count != 1) { + throw new \RuntimeException("The list of $expected_header should only contain 1 column. It has $column_count columns."); + } + + // Check that the correct header is used. + $actual_header = reset($header_row); + if (strtolower(trim($actual_header)) !== $expected_header) { + $capitalized_header = ucfirst($expected_header); + throw new \RuntimeException("The list of $expected_header should have the header '$capitalized_header'."); + } + } + /** * Internal callback to check for a specific message in a given context. *