From 0bd8033cb0fc1b8de73d3224993aae6ea99a428b Mon Sep 17 00:00:00 2001 From: jeffreyguo Date: Fri, 19 Aug 2016 09:20:05 +1200 Subject: [PATCH] Add new step to click the http link address in email --- README.md | 3 ++ .../BehatExtension/Context/EmailContext.php | 31 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/README.md b/README.md index 2d7c4e9..a98ee58 100644 --- a/README.md +++ b/README.md @@ -722,6 +722,9 @@ It's based on the `vendor/bin/behat -di @cms` output. Then /^the email should (not |)be sent to "([^"]*)"$/ + When /^I click on the http link "([^"]*)" in the email$/ + - Example: When I click on the http link "http://localhost/changepassword" in the email + ### Transformations Behat [transformations](http://docs.behat.org/guides/2.definitions.html#step-argument-transformations) diff --git a/src/SilverStripe/BehatExtension/Context/EmailContext.php b/src/SilverStripe/BehatExtension/Context/EmailContext.php index f3c2341..9fb2f1c 100644 --- a/src/SilverStripe/BehatExtension/Context/EmailContext.php +++ b/src/SilverStripe/BehatExtension/Context/EmailContext.php @@ -313,4 +313,35 @@ class EmailContext extends BehatContext assertContains($to, $match->To); } } + + /** + * The link text is the link address itself which contains special characters + * e.g. http://localhost/Security/changepassword?m=199&title=reset + * Example: When I click on the http link "changepassword" in the email + * @When /^I click on the http link "([^"]*)" in the email$/ + */ + public function iClickOnHttpLinkInEmail($httpText) + { + if (!$this->lastMatchedEmail) { + throw new \LogicException('No matched email found from previous step'); + } + + $email = $this->lastMatchedEmail; + $html = $email->Content; + $dom = new \DOMDocument(); + $dom->loadHTML($html); + + $tags = $dom->getElementsByTagName('a'); + $href = null; + foreach ($tags as $tag) { + $linkText = $tag->nodeValue; + if (strpos($linkText, $httpText) !== false) { + $href = $linkText; + break; + } + } + assertNotNull($href); + + return new Step\When(sprintf('I go to "%s"', $href)); + } }