diff --git a/README.md b/README.md index 776033d..26aef1b 100644 --- a/README.md +++ b/README.md @@ -655,13 +655,38 @@ It's based on the `vendor/bin/behat -di @cms` output. Given /^(?:(an|a|the) )"group" "(?[^"]+)" (?:(with|has)) permissions (?.*)$/ - Example: Given a "group" "Admin" with permissions "Access to 'Pages' section" and "Access to 'Files' section" + Given /^I assign (?:(an|a|the) )"(?[^"]+)" "(?[^"]+)" to (?:(an|a|the) )"(?[^"]+)" "(?[^"]+)"$/ - Example: I assign the "TaxonomyTerm" "For customers" to the "Page" "Page1" + Given /^the CMS settings have the following data$/ + - Example: Given the CMS settings has the following data + - Note: It only works with the SilverStripe CMS module installed + ### Environment - Given /^the current date is "([^"]*)"$/ - Given /^the current time is "([^"]*)"$/ + Given /^the current date is "([^"]*)"$/ + Given /^the current time is "([^"]*)"$/ + +### Email + + Given /^there should (not |)be an email (to|from) "([^"]*)"$/ + + Given /^there should (not |)be an email (to|from) "([^"]*)" titled "([^"]*)"$/ + + Given /^the email should (not |)contain "([^"]*)"$/ + - Example: Given the email should contain "Thank you for registering!" + + When /^I click on the "([^"]*)" link in the email (to|from) "([^"]*)"$/ + + When /^I click on the "([^"]*)" link in the email (to|from) "([^"]*)" titled "([^"]*)"$/ + + When /^I click on the "([^"]*)" link in the email"$/ + + Given /^I clear all emails$/ + + Then /^the email should (not |)contain the following data:$/ + Example: Then the email should contain the following data: ### Transformations diff --git a/src/SilverStripe/BehatExtension/Context/EmailContext.php b/src/SilverStripe/BehatExtension/Context/EmailContext.php index 9e3720f..b6b815d 100644 --- a/src/SilverStripe/BehatExtension/Context/EmailContext.php +++ b/src/SilverStripe/BehatExtension/Context/EmailContext.php @@ -107,25 +107,32 @@ class EmailContext extends BehatContext } /** - * Example: Given the email contains "Thank you for registering!". + * Example: Given the email should contain "Thank you for registering!". * Assumes an email has been identified by a previous step, * e.g. through 'Given there should be an email to "test@test.com"'. * - * @Given /^the email should contain "([^"]*)"$/ - */ - public function thereTheEmailContains($content) - { - if(!$this->lastMatchedEmail) { - throw new \LogicException('No matched email found from previous step'); - } + * @Given /^the email should (not |)contain "([^"]*)"$/ + */ + public function thereTheEmailContains($negate, $content) + { + if(!$this->lastMatchedEmail) { + throw new \LogicException('No matched email found from previous step'); + } - $email = $this->lastMatchedEmail; - if($email->Content) { - assertContains($content, $email->Content); - } else { - assertContains($content, $email->PlainContent); - } - } + $email = $this->lastMatchedEmail; + $emailContent = null; + if($email->Content) { + $emailContent = $email->Content; + } else { + $emailContent = $email->PlainContent; + } + + if(trim($negate)) { + assertNotContains($content, $emailContent); + } else { + assertContains($content, $emailContent); + } + } /** * @When /^I click on the "([^"]*)" link in the email (to|from) "([^"]*)"$/ @@ -194,4 +201,39 @@ class EmailContext extends BehatContext $this->lastMatchedEmail = null; return $this->mailer->clearEmails(); } + + /** + * Example: Then the email should contain the following data: + * | row1 | + * | row2 | + * Assumes an email has been identified by a previous step. + * @Then /^the email should (not |)contain the following data:$/ + */ + public function theEmailContainFollowingData($negate, TableNode $table) { + if(!$this->lastMatchedEmail) { + throw new \LogicException('No matched email found from previous step'); + } + + $email = $this->lastMatchedEmail; + $emailContent = null; + if($email->Content) { + $emailContent = $email->Content; + } else { + $emailContent = $email->PlainContent; + } + // Convert html content to plain text + $emailContent = strip_tags($emailContent); + $rows = $table->getRows(); + + // For "should not contain" + if(trim($negate)) { + foreach($rows as $row) { + assertNotContains($row[0], $emailContent); + } + } else { + foreach($rows as $row) { + assertContains($row[0], $emailContent); + } + } + } }