View source
<?php
namespace Drupal\social\Behat;
use Behat\Behat\Context\Context;
use Behat\Gherkin\Node\TableNode;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpFoundation\File\File;
class EmailContext implements Context {
public function enableEmailSpool() {
$swiftmailer_config = \Drupal::configFactory()
->getEditable('swiftmailer.transport');
$swiftmailer_config
->set('transport', 'spool');
$swiftmailer_config
->set('spool_directory', $this
->getSpoolDir());
$swiftmailer_config
->save();
$this
->purgeSpool();
}
public function disableEmailSpool() {
$swiftmailer_config = \Drupal::configFactory()
->getEditable('swiftmailer.transport');
$swiftmailer_config
->set('transport', 'native');
$swiftmailer_config
->save();
$this
->purgeSpool();
}
public function getSpooledEmails() {
$finder = new Finder();
$spoolDir = $this
->getSpoolDir();
if (empty($spoolDir)) {
throw new \Exception('Could not retrieve the spool directory, or the directory does not exist.');
}
try {
$finder
->files()
->in($spoolDir);
return $finder;
} catch (InvalidArgumentException $exception) {
return NULL;
}
}
public function getEmailContent($file) {
return unserialize(file_get_contents($file));
}
protected function getSpoolDir() {
$path = '/var/www/html/profiles/contrib/social/tests/behat/features/swiftmailer-spool';
if (!file_exists($path)) {
mkdir($path, 0777, true);
}
return $path;
}
protected function purgeSpool() {
$filesystem = new Filesystem();
$finder = $this
->getSpooledEmails();
if ($finder) {
foreach ($finder as $file) {
$filesystem
->remove($file
->getRealPath());
}
}
}
protected function findSubjectAndBody($subject, $body) {
$finder = $this
->getSpooledEmails();
$found_email = FALSE;
if ($finder) {
foreach ($finder as $file) {
$email = $this
->getEmailContent($file);
$email_subject = $email
->getSubject();
$email_body = $email
->getBody();
$doc = new \DOMDocument();
$doc
->loadHTML($email_body);
$xpath = new \DOMXPath($doc);
$content = $xpath
->evaluate('string(//*[contains(@class,"postheader")])');
$content .= $xpath
->evaluate('string(//*[contains(@class,"main")])');
$content_found = 0;
foreach ($body as $string) {
if (strpos($content, $string)) {
$content_found++;
}
}
if ($email_subject == $subject && $content_found === count($body)) {
$found_email = TRUE;
}
}
}
else {
throw new \Exception('There are no email messages.');
}
return $found_email;
}
public function iRunTheDigestCron($frequency) {
$query = \Drupal::database()
->update('user_activity_digest');
$query
->fields([
'timestamp' => 1,
]);
$query
->condition('frequency', $frequency);
$query
->execute();
\Drupal::state()
->set('digest.' . $frequency . '.last_run', 1);
\Drupal::service('cron')
->run();
}
public function iShouldHaveAnEmailWithTitleAndBody($subject, $body) {
$found_email = $this
->findSubjectAndBody($subject, [
$body,
]);
if (!$found_email) {
throw new \Exception('There is no email with that subject and body.');
}
}
public function iShouldHaveAnEmailWithTitleAndBodyMulti($subject, TableNode $table) {
$body = [];
$hash = $table
->getHash();
foreach ($hash as $row) {
$body[] = $row['content'];
}
$found_email = $this
->findSubjectAndBody($subject, $body);
if (!$found_email) {
throw new \Exception('There is no email with that subject and body.');
}
}
public function iShouldNotHaveAnEmailWithTitleAndBody($subject, $body) {
$found_email = $this
->findSubjectAndBody($subject, [
$body,
]);
if ($found_email) {
throw new \Exception('There is an email with that subject and body.');
}
}
public function iShouldNotHaveAnEmailWithTitleAndBodyMulti($subject, TableNode $table) {
$body = [];
$hash = $table
->getHash();
foreach ($hash as $row) {
$body[] = $row['content'];
}
$found_email = $this
->findSubjectAndBody($subject, $body);
if ($found_email) {
throw new \Exception('There is an email with that subject and body.');
}
}
}