2012-12-19 15:56:20 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\BehatExtension\Utility;
|
|
|
|
|
2022-10-25 02:41:57 +02:00
|
|
|
use InvalidArgumentException;
|
|
|
|
use SilverStripe\Control\Email\Email;
|
2022-10-20 01:24:41 +02:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
2017-01-11 22:02:01 +01:00
|
|
|
use SilverStripe\Dev\TestMailer as BaseTestMailer;
|
2017-04-26 06:24:20 +02:00
|
|
|
use SilverStripe\TestSession\TestSessionEnvironment;
|
2022-10-25 02:41:57 +02:00
|
|
|
use Symfony\Component\Mailer\Envelope;
|
2022-10-20 01:24:41 +02:00
|
|
|
use Symfony\Component\Mailer\Transport\TransportInterface;
|
2022-10-25 02:41:57 +02:00
|
|
|
use Symfony\Component\Mime\RawMessage;
|
2016-09-01 06:22:47 +02:00
|
|
|
|
2012-12-19 15:56:20 +01:00
|
|
|
/**
|
|
|
|
* Same principle as core TestMailer class,
|
2014-03-01 02:47:12 +01:00
|
|
|
* but saves emails in {@link TestSessionEnvironment}
|
2012-12-19 15:56:20 +01:00
|
|
|
* to share the state between PHP calls (CLI vs. browser).
|
|
|
|
*/
|
2017-01-11 22:02:01 +01:00
|
|
|
class TestMailer extends BaseTestMailer
|
2016-08-10 03:35:13 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var TestSessionEnvironment
|
|
|
|
*/
|
|
|
|
protected $testSessionEnvironment;
|
2014-03-01 02:47:12 +01:00
|
|
|
|
2022-10-20 01:24:41 +02:00
|
|
|
public function __construct(
|
|
|
|
TransportInterface $transport,
|
|
|
|
EventDispatcherInterface $dispatcher
|
|
|
|
) {
|
|
|
|
parent::__construct($transport, $dispatcher);
|
2017-04-26 06:24:20 +02:00
|
|
|
$this->testSessionEnvironment = TestSessionEnvironment::singleton();
|
2016-08-10 03:35:13 +02:00
|
|
|
}
|
2012-12-19 15:56:20 +01:00
|
|
|
|
2022-10-25 02:41:57 +02:00
|
|
|
public function send(RawMessage $message, Envelope $envelope = null): void
|
|
|
|
{
|
|
|
|
parent::send($message, $envelope);
|
|
|
|
/** @var Email $email */
|
|
|
|
$email = $message;
|
|
|
|
$data = $this->createData($email);
|
|
|
|
// save email to testsession state
|
|
|
|
$state = $this->testSessionEnvironment->getState();
|
|
|
|
if (!isset($state->emails)) {
|
|
|
|
$state->emails = array();
|
|
|
|
}
|
|
|
|
$state->emails[] = array_filter($data ?? []);
|
|
|
|
$this->testSessionEnvironment->applyState($state);
|
|
|
|
}
|
|
|
|
|
2016-08-10 03:35:13 +02:00
|
|
|
/**
|
|
|
|
* Clear the log of emails sent
|
|
|
|
*/
|
2022-10-19 22:55:48 +02:00
|
|
|
public function clearEmails(): void
|
2016-08-10 03:35:13 +02:00
|
|
|
{
|
|
|
|
$state = $this->testSessionEnvironment->getState();
|
|
|
|
if (isset($state->emails)) {
|
|
|
|
unset($state->emails);
|
|
|
|
}
|
|
|
|
$this->testSessionEnvironment->applyState($state);
|
|
|
|
}
|
2012-12-19 15:56:20 +01:00
|
|
|
|
2022-10-19 22:55:48 +02:00
|
|
|
public function findEmail(
|
|
|
|
string $to,
|
|
|
|
?string $from = null,
|
|
|
|
?string $subject = null,
|
|
|
|
?string $content = null
|
|
|
|
): ?array {
|
2016-08-10 03:35:13 +02:00
|
|
|
$matches = $this->findEmails($to, $from, $subject, $content);
|
2014-04-30 07:30:50 +02:00
|
|
|
//got the count of matches emails
|
2022-04-13 07:37:24 +02:00
|
|
|
$emailCount = count($matches ?? []);
|
2014-04-30 07:30:50 +02:00
|
|
|
//get the last(latest) one
|
2016-08-10 03:35:13 +02:00
|
|
|
return $matches ? $matches[$emailCount-1] : null;
|
|
|
|
}
|
2012-12-19 15:56:20 +01:00
|
|
|
|
2016-08-10 03:35:13 +02:00
|
|
|
/**
|
|
|
|
* Search for all emails.
|
|
|
|
* All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression.
|
|
|
|
*
|
|
|
|
* @param $to
|
|
|
|
* @param $from
|
|
|
|
* @param $subject
|
|
|
|
* @param $content
|
|
|
|
* @return array Contains the keys: 'type', 'to', 'from', 'subject', 'content', 'plainContent', 'attachedFiles',
|
|
|
|
* 'customHeaders', 'htmlContent', 'inlineImages'
|
|
|
|
*/
|
|
|
|
public function findEmails($to = null, $from = null, $subject = null, $content = null)
|
|
|
|
{
|
|
|
|
$matches = array();
|
|
|
|
$args = func_get_args();
|
|
|
|
$state = $this->testSessionEnvironment->getState();
|
|
|
|
$emails = isset($state->emails) ? $state->emails : array();
|
|
|
|
foreach ($emails as $email) {
|
|
|
|
$matched = true;
|
2014-02-27 11:02:43 +01:00
|
|
|
|
2016-08-10 03:35:13 +02:00
|
|
|
foreach (array('To', 'From', 'Subject', 'Content') as $i => $field) {
|
|
|
|
if (!isset($email->$field)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$value = (isset($args[$i])) ? $args[$i] : null;
|
|
|
|
if ($value) {
|
|
|
|
if ($value[0] == '/') {
|
2022-04-13 07:37:24 +02:00
|
|
|
$matched = preg_match($value ?? '', $email->$field ?? '');
|
2016-08-10 03:35:13 +02:00
|
|
|
} else {
|
|
|
|
$matched = ($value == $email->$field);
|
|
|
|
}
|
|
|
|
if (!$matched) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($matched) {
|
|
|
|
$matches[] = $email;
|
|
|
|
}
|
|
|
|
}
|
2012-12-19 15:56:20 +01:00
|
|
|
|
2016-08-10 03:35:13 +02:00
|
|
|
return $matches;
|
|
|
|
}
|
2012-12-19 15:56:20 +01:00
|
|
|
|
2016-08-10 03:35:13 +02:00
|
|
|
protected function saveEmail($data)
|
|
|
|
{
|
|
|
|
$state = $this->testSessionEnvironment->getState();
|
|
|
|
if (!isset($state->emails)) {
|
|
|
|
$state->emails = array();
|
|
|
|
}
|
2022-04-13 07:37:24 +02:00
|
|
|
$state->emails[] = array_filter($data ?? []);
|
2016-08-10 03:35:13 +02:00
|
|
|
$this->testSessionEnvironment->applyState($state);
|
|
|
|
}
|
2012-12-19 15:56:20 +01:00
|
|
|
}
|