2012-12-19 15:56:20 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\BehatExtension\Context;
|
|
|
|
|
|
|
|
use Behat\Behat\Context\ClosuredContextInterface,
|
|
|
|
Behat\Behat\Context\TranslatedContextInterface,
|
|
|
|
Behat\Behat\Context\BehatContext,
|
|
|
|
Behat\Behat\Context\Step,
|
|
|
|
Behat\Behat\Event\FeatureEvent,
|
|
|
|
Behat\Behat\Event\ScenarioEvent,
|
|
|
|
Behat\Behat\Exception\PendingException;
|
|
|
|
use Behat\Gherkin\Node\PyStringNode,
|
|
|
|
Behat\Gherkin\Node\TableNode;
|
|
|
|
use Symfony\Component\DomCrawler\Crawler;
|
|
|
|
|
|
|
|
// PHPUnit
|
|
|
|
require_once 'PHPUnit/Autoload.php';
|
|
|
|
require_once 'PHPUnit/Framework/Assert/Functions.php';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Context used to define steps related to email sending.
|
|
|
|
*/
|
|
|
|
class EmailContext extends BehatContext
|
|
|
|
{
|
|
|
|
protected $context;
|
|
|
|
|
|
|
|
protected $mailer;
|
|
|
|
|
2013-12-13 17:52:42 +01:00
|
|
|
/**
|
|
|
|
* Stored to simplify later assertions
|
|
|
|
*/
|
|
|
|
protected $lastMatchedEmail;
|
|
|
|
|
2012-12-19 15:56:20 +01:00
|
|
|
/**
|
|
|
|
* Initializes context.
|
|
|
|
* Every scenario gets it's own context object.
|
|
|
|
*
|
|
|
|
* @param array $parameters context parameters (set them up through behat.yml)
|
|
|
|
*/
|
|
|
|
public function __construct(array $parameters)
|
|
|
|
{
|
|
|
|
// Initialize your context here
|
|
|
|
$this->context = $parameters;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get Mink session from MinkContext
|
|
|
|
*/
|
|
|
|
public function getSession($name = null)
|
|
|
|
{
|
|
|
|
return $this->getMainContext()->getSession($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @BeforeScenario
|
|
|
|
*/
|
|
|
|
public function before(ScenarioEvent $event)
|
|
|
|
{
|
|
|
|
// Also set through the 'supportbehat' extension
|
|
|
|
// to ensure its available both in CLI execution and the tested browser session
|
|
|
|
$this->mailer = new \SilverStripe\BehatExtension\Utility\TestMailer();
|
|
|
|
\Email::set_mailer($this->mailer);
|
2013-06-19 12:24:00 +02:00
|
|
|
\Config::inst()->update("Email","send_all_emails_to", null);
|
2012-12-19 15:56:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-26 01:55:37 +01:00
|
|
|
* @Given /^there should (not |)be an email (to|from) "([^"]*)"$/
|
2012-12-19 15:56:20 +01:00
|
|
|
*/
|
2014-02-26 01:55:37 +01:00
|
|
|
public function thereIsAnEmailFromTo($negate, $direction, $email)
|
2012-12-19 15:56:20 +01:00
|
|
|
{
|
|
|
|
$to = ($direction == 'to') ? $email : null;
|
|
|
|
$from = ($direction == 'from') ? $email : null;
|
|
|
|
$match = $this->mailer->findEmail($to, $from);
|
2014-02-26 01:55:37 +01:00
|
|
|
if(trim($negate)) {
|
|
|
|
assertNull($match);
|
|
|
|
} else {
|
|
|
|
assertNotNull($match);
|
|
|
|
}
|
2013-12-13 17:52:42 +01:00
|
|
|
$this->lastMatchedEmail = $match;
|
2012-12-19 15:56:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-26 01:55:37 +01:00
|
|
|
* @Given /^there should (not |)be an email (to|from) "([^"]*)" titled "([^"]*)"$/
|
2012-12-19 15:56:20 +01:00
|
|
|
*/
|
2014-02-26 01:55:37 +01:00
|
|
|
public function thereIsAnEmailFromToTitled($negate, $direction, $email, $subject)
|
2012-12-19 15:56:20 +01:00
|
|
|
{
|
|
|
|
$to = ($direction == 'to') ? $email : null;
|
|
|
|
$from = ($direction == 'from') ? $email : null;
|
|
|
|
$match = $this->mailer->findEmail($to, $from, $subject);
|
2014-02-27 11:02:43 +01:00
|
|
|
$allMails = $this->mailer->findEmails($to, $from);
|
2014-03-01 06:26:21 +01:00
|
|
|
$allTitles = $allMails ? '"' . implode('","', array_map(function($email) {return $email->Subject;}, $allMails)) . '"' : null;
|
2014-02-26 01:55:37 +01:00
|
|
|
if(trim($negate)) {
|
|
|
|
assertNull($match);
|
|
|
|
} else {
|
2014-02-27 11:02:43 +01:00
|
|
|
$msg = sprintf(
|
|
|
|
'Could not find email %s "%s" titled "%s".',
|
|
|
|
$direction,
|
|
|
|
$email,
|
|
|
|
$subject
|
|
|
|
);
|
|
|
|
if($allTitles) {
|
|
|
|
$msg .= ' Existing emails: ' . $allTitles;
|
|
|
|
}
|
|
|
|
assertNotNull($match,$msg);
|
2014-02-26 01:55:37 +01:00
|
|
|
}
|
2013-12-13 17:52:42 +01:00
|
|
|
$this->lastMatchedEmail = $match;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-11-13 23:47:17 +01:00
|
|
|
* Example: Given the email should contain "Thank you for registering!".
|
2013-12-13 17:52:42 +01:00
|
|
|
* Assumes an email has been identified by a previous step,
|
|
|
|
* e.g. through 'Given there should be an email to "test@test.com"'.
|
|
|
|
*
|
2014-11-13 23:47:17 +01:00
|
|
|
* @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;
|
|
|
|
$emailContent = null;
|
|
|
|
if($email->Content) {
|
|
|
|
$emailContent = $email->Content;
|
|
|
|
} else {
|
|
|
|
$emailContent = $email->PlainContent;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(trim($negate)) {
|
|
|
|
assertNotContains($content, $emailContent);
|
|
|
|
} else {
|
|
|
|
assertContains($content, $emailContent);
|
|
|
|
}
|
|
|
|
}
|
2012-12-19 15:56:20 +01:00
|
|
|
|
2015-07-02 04:12:10 +02:00
|
|
|
/**
|
|
|
|
* Example: Given the email contains "Thank you for <strong>registering!<strong>".
|
|
|
|
* Then the email should contain plain text "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 plain text "([^"]*)"$/
|
|
|
|
*/
|
|
|
|
public function thereTheEmailContainsPlainText($content)
|
|
|
|
{
|
|
|
|
if(!$this->lastMatchedEmail) {
|
|
|
|
throw new \LogicException('No matched email found from previous step');
|
|
|
|
}
|
|
|
|
|
|
|
|
$email = $this->lastMatchedEmail;
|
|
|
|
$emailContent = ($email->Content) ? ($email->Content) : ($email->PlainContent);
|
|
|
|
$emailPlainText = strip_tags($emailContent);
|
|
|
|
$emailPlainText = preg_replace("/\h+/", " ", $emailPlainText);
|
|
|
|
|
|
|
|
assertContains($content, $emailPlainText);
|
|
|
|
}
|
|
|
|
|
2012-12-19 15:56:20 +01:00
|
|
|
/**
|
2013-10-22 00:04:15 +02:00
|
|
|
* @When /^I click on the "([^"]*)" link in the email (to|from) "([^"]*)"$/
|
2012-12-19 15:56:20 +01:00
|
|
|
*/
|
|
|
|
public function iGoToInTheEmailTo($linkSelector, $direction, $email)
|
|
|
|
{
|
|
|
|
$to = ($direction == 'to') ? $email : null;
|
|
|
|
$from = ($direction == 'from') ? $email : null;
|
|
|
|
$match = $this->mailer->findEmail($to, $from);
|
|
|
|
assertNotNull($match);
|
|
|
|
|
2014-03-01 06:26:21 +01:00
|
|
|
$crawler = new Crawler($match->Content);
|
2013-10-22 00:04:15 +02:00
|
|
|
$linkEl = $crawler->selectLink($linkSelector);
|
2012-12-19 15:56:20 +01:00
|
|
|
assertNotNull($linkEl);
|
|
|
|
$link = $linkEl->attr('href');
|
|
|
|
assertNotNull($link);
|
2014-05-01 00:28:20 +02:00
|
|
|
|
2012-12-19 15:56:20 +01:00
|
|
|
return new Step\When(sprintf('I go to "%s"', $link));
|
|
|
|
}
|
|
|
|
|
2014-05-01 00:28:20 +02:00
|
|
|
/**
|
|
|
|
* @When /^I click on the "([^"]*)" link in the email (to|from) "([^"]*)" titled "([^"]*)"$/
|
|
|
|
*/
|
|
|
|
public function iGoToInTheEmailToTitled($linkSelector, $direction, $email, $title)
|
|
|
|
{
|
|
|
|
$to = ($direction == 'to') ? $email : null;
|
|
|
|
$from = ($direction == 'from') ? $email : null;
|
|
|
|
$match = $this->mailer->findEmail($to, $from, $title);
|
|
|
|
assertNotNull($match);
|
|
|
|
|
|
|
|
$crawler = new Crawler($match->Content);
|
|
|
|
$linkEl = $crawler->selectLink($linkSelector);
|
|
|
|
assertNotNull($linkEl);
|
|
|
|
$link = $linkEl->attr('href');
|
|
|
|
assertNotNull($link);
|
|
|
|
return new Step\When(sprintf('I go to "%s"', $link));
|
|
|
|
}
|
|
|
|
|
2013-12-13 17:52:42 +01:00
|
|
|
/**
|
|
|
|
* Assumes an email has been identified by a previous step,
|
|
|
|
* e.g. through 'Given there should be an email to "test@test.com"'.
|
|
|
|
*
|
|
|
|
* @When /^I click on the "([^"]*)" link in the email"$/
|
|
|
|
*/
|
|
|
|
public function iGoToInTheEmail($linkSelector)
|
|
|
|
{
|
|
|
|
if(!$this->lastMatchedEmail) {
|
|
|
|
throw new \LogicException('No matched email found from previous step');
|
|
|
|
}
|
|
|
|
|
|
|
|
$match = $this->lastMatchedEmail;
|
2014-03-01 06:26:21 +01:00
|
|
|
$crawler = new Crawler($match->Content);
|
2013-12-13 17:52:42 +01:00
|
|
|
$linkEl = $crawler->selectLink($linkSelector);
|
|
|
|
assertNotNull($linkEl);
|
|
|
|
$link = $linkEl->attr('href');
|
|
|
|
assertNotNull($link);
|
|
|
|
|
|
|
|
return new Step\When(sprintf('I go to "%s"', $link));
|
|
|
|
}
|
|
|
|
|
2012-12-19 15:56:20 +01:00
|
|
|
/**
|
|
|
|
* @Given /^I clear all emails$/
|
|
|
|
*/
|
|
|
|
public function iClearAllEmails()
|
|
|
|
{
|
2013-12-13 17:52:42 +01:00
|
|
|
$this->lastMatchedEmail = null;
|
2012-12-19 15:56:20 +01:00
|
|
|
return $this->mailer->clearEmails();
|
|
|
|
}
|
2014-11-13 23:47:17 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
2015-07-02 04:12:10 +02:00
|
|
|
$emailContent = preg_replace("/\h+/", " ", $emailContent);
|
2014-11-13 23:47:17 +01:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-21 06:27:09 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @Then /^there should (not |)be an email titled "([^"]*)"$/
|
|
|
|
*/
|
|
|
|
public function thereIsAnEmailTitled($negate, $subject)
|
|
|
|
{
|
|
|
|
$match = $this->mailer->findEmail(null, null, $subject);
|
|
|
|
if(trim($negate)) {
|
|
|
|
assertNull($match);
|
|
|
|
} else {
|
|
|
|
$msg = sprintf(
|
|
|
|
'Could not find email titled "%s".',
|
|
|
|
$subject
|
|
|
|
);
|
|
|
|
assertNotNull($match,$msg);
|
|
|
|
}
|
|
|
|
$this->lastMatchedEmail = $match;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Then /^the email should (not |)be sent from "([^"]*)"$/
|
|
|
|
*/
|
|
|
|
public function theEmailSentFrom($negate, $from)
|
|
|
|
{
|
|
|
|
if(!$this->lastMatchedEmail) {
|
|
|
|
throw new \LogicException('No matched email found from previous step');
|
|
|
|
}
|
|
|
|
|
|
|
|
$match = $this->lastMatchedEmail;
|
|
|
|
if(trim($negate)) {
|
|
|
|
assertNotContains($from, $match->From);
|
|
|
|
} else {
|
|
|
|
assertContains($from, $match->From);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Then /^the email should (not |)be sent to "([^"]*)"$/
|
|
|
|
*/
|
|
|
|
public function theEmailSentTo($negate, $to)
|
|
|
|
{
|
|
|
|
if(!$this->lastMatchedEmail) {
|
|
|
|
throw new \LogicException('No matched email found from previous step');
|
|
|
|
}
|
|
|
|
|
|
|
|
$match = $this->lastMatchedEmail;
|
|
|
|
if(trim($negate)) {
|
|
|
|
assertNotContains($to, $match->To);
|
|
|
|
} else {
|
|
|
|
assertContains($to, $match->To);
|
|
|
|
}
|
|
|
|
}
|
2012-12-19 15:56:20 +01:00
|
|
|
}
|