Skip to content

Commit

Permalink
Merge pull request jhedstrom#171 from jhedstrom/reroll-email-support
Browse files Browse the repository at this point in the history
Reroll: jhedstrom#134 Support for testing emails sent from Drupal
  • Loading branch information
jhedstrom authored Mar 19, 2018
2 parents ef3bca0 + ee97847 commit 8ad2097
Show file tree
Hide file tree
Showing 7 changed files with 280 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/Drupal/Driver/BaseDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,39 @@ public function entityDelete($entity_type, \stdClass $entity) {
throw new UnsupportedDriverActionException($this->errorString('delete entities using the generic Entity API'), $this);
}

/**
* {@inheritdoc}
*/
public function startCollectingMail() {
throw new UnsupportedDriverActionException($this->errorString('work with mail'), $this);
}

/**
* {@inheritdoc}
*/
public function stopCollectingMail() {
throw new UnsupportedDriverActionException($this->errorString('work with mail'), $this);
}

/**
* {@inheritdoc}
*/
public function getMail() {
throw new UnsupportedDriverActionException($this->errorString('work with mail'), $this);
}

/**
* {@inheritdoc}
*/
public function clearMail() {
throw new UnsupportedDriverActionException($this->errorString('work with mail'), $this);
}

/**
* {@inheritdoc}
*/
public function sendMail($body, $subject, $to, $langcode) {
throw new UnsupportedDriverActionException($this->errorString('work with mail'), $this);
}

}
41 changes: 41 additions & 0 deletions src/Drupal/Driver/Cores/CoreInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,45 @@ public function entityCreate($entity_type, $entity);
*/
public function entityDelete($entity_type, $entity);

/**
* Enable the test mail collector.
*/
public function startCollectingMail();

/**
* Restore normal operation of outgoing mail.
*/
public function stopCollectingMail();

/**
* Get any mail collected by the test mail collector.
*
* @return \stdClass[]
* An array of collected emails, each formatted as a Drupal 8
* \Drupal\Core\Mail\MailInterface::mail $message array.
*/
public function getMail();

/**
* Empty the test mail collector store of any collected mail.
*/
public function clearMail();

/**
* Send a mail.
*
* @param string $body
* The body of the mail.
* @param string $subject
* The subject of the mail.
* @param string $to
* The recipient's email address, passing PHP email validation filter.
* @param string $langcode
* The language used in subject and body.
*
* @return bool
* Whether the email was sent successfully.
*/
public function sendMail($body, $subject, $to, $langcode);

}
40 changes: 40 additions & 0 deletions src/Drupal/Driver/Cores/Drupal6.php
Original file line number Diff line number Diff line change
Expand Up @@ -520,4 +520,44 @@ public function entityDelete($entity_type, $entity) {
throw new \Exception('Drupal 6 does not have a generic Entity API, so deletion of entities is not possible in this way.');
}

/**
* {@inheritdoc}
*/
public function startCollectingMail() {
// @todo: create a D6 version of this function
throw new \Exception('Mail testing is not yet implemented for Drupal 6.');
}

/**
* {@inheritdoc}
*/
public function stopCollectingMail() {
// @todo: create a D6 version of this function
throw new \Exception('Mail testing is not yet implemented for Drupal 6.');
}

/**
* {@inheritdoc}
*/
public function getMail() {
// @todo: create a D6 version of this function
throw new \Exception('Mail testing is not yet implemented for Drupal 6.');
}

/**
* {@inheritdoc}
*/
public function clearMail() {
// @todo: create a D6 version of this function
throw new \Exception('Mail testing is not yet implemented for Drupal 6.');
}

/**
* {@inheritdoc}
*/
public function sendMail($body, $subject = '', $to = '', $langcode = '') {
// @todo: create a D6 version of this function
throw new \Exception('Mail testing is not yet implemented for Drupal 6.');
}

}
40 changes: 40 additions & 0 deletions src/Drupal/Driver/Cores/Drupal7.php
Original file line number Diff line number Diff line change
Expand Up @@ -527,4 +527,44 @@ public function entityDelete($entity_type, $entity) {
entity_delete($entity_type, $id);
}

/**
* {@inheritdoc}
*/
public function startCollectingMail() {
// @todo: create a D7 version of this function
throw new \Exception('Mail testing is not yet implemented for Drupal 7.');
}

/**
* {@inheritdoc}
*/
public function stopCollectingMail() {
// @todo: create a D7 version of this function
throw new \Exception('Mail testing is not yet implemented for Drupal 7.');
}

/**
* {@inheritdoc}
*/
public function getMail() {
// @todo: create a D7 version of this function
throw new \Exception('Mail testing is not yet implemented for Drupal 7.');
}

/**
* {@inheritdoc}
*/
public function clearMail() {
// @todo: create a D7 version of this function
throw new \Exception('Mail testing is not yet implemented for Drupal 7.');
}

/**
* {@inheritdoc}
*/
public function sendMail($body, $subject = '', $to = '', $langcode = '') {
// @todo: create a D7 version of this function
throw new \Exception('Mail testing is not yet implemented for Drupal 7.');
}

}
48 changes: 48 additions & 0 deletions src/Drupal/Driver/Cores/Drupal8.php
Original file line number Diff line number Diff line change
Expand Up @@ -494,4 +494,52 @@ public function entityDelete($entity_type, $entity) {
}
}

/**
* {@inheritdoc}
*/
public function startCollectingMail() {
// @todo Use a collector that supports html after D#2223967 lands.
\Drupal::config('system.mail')->setModuleOverride(['interface' => ['default' => 'test_mail_collector']]);
}

/**
* {@inheritdoc}
*/
public function stopCollectingMail() {
$original = \Drupal::config('system.mail')->getOriginal('interface.default', FALSE);
\Drupal::config('system.mail')->setModuleOverride(['interface' => ['default' => $original]]);
}

/**
* {@inheritdoc}
*/
public function getMail() {
\Drupal::state()->resetCache();
$mail = \Drupal::state()->get('system.test_mail_collector') ?: [];
// Discard cancelled mail.
$mail = array_values(array_filter($mail, function ($mailItem) {
return ($mailItem['send'] == TRUE);
}));
return $mail;
}

/**
* {@inheritdoc}
*/
public function clearMail() {
\Drupal::state()->set('system.test_mail_collector', []);
}

/**
* {@inheritdoc}
*/
public function sendMail($body = '', $subject = '', $to = '', $langcode = '') {
// Send the mail, via the system module's hook_mail.
$params['context']['message'] = $body;
$params['context']['subject'] = $subject;
$mailManager = \Drupal::service('plugin.manager.mail');
$result = $mailManager->mail('system', '', $to, $langcode, $params, NULL, TRUE);
return $result;
}

}
41 changes: 41 additions & 0 deletions src/Drupal/Driver/DriverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,45 @@ public function createEntity($entity_type, \stdClass $entity);
*/
public function entityDelete($entity_type, \stdClass $entity);

/**
* Enable the test mail collector.
*/
public function startCollectingMail();

/**
* Restore normal operation of outgoing mail.
*/
public function stopCollectingMail();

/**
* Get any mail collected by the test mail collector.
*
* @return \stdClass[]
* An array of collected emails, each formatted as a Drupal 8
* \Drupal\Core\Mail\MailInterface::mail $message array.
*/
public function getMail();

/**
* Empty the test mail collector store of any collected mail.
*/
public function clearMail();

/**
* Send a mail.
*
* @param string $body
* The body of the mail.
* @param string $subject
* The subject of the mail.
* @param string $to
* The recipient's email address, passing PHP email validation filter.
* @param string $langcode
* The language used in subject and body.
*
* @return bool
* Whether the email was sent successfully.
*/
public function sendMail($body, $subject, $to, $langcode);

}
35 changes: 35 additions & 0 deletions src/Drupal/Driver/DrupalDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,39 @@ public function entityDelete($entity_type, \stdClass $entity) {
return $this->getCore()->entityDelete($entity_type, $entity);
}

/**
* {@inheritdoc}
*/
public function startCollectingMail() {
return $this->getCore()->startCollectingMail();
}

/**
* {@inheritdoc}
*/
public function stopCollectingMail() {
return $this->getCore()->stopCollectingMail();
}

/**
* {@inheritdoc}
*/
public function getMail() {
return $this->getCore()->getMail();
}

/**
* {@inheritdoc}
*/
public function clearMail() {
return $this->getCore()->clearMail();
}

/**
* {@inheritdoc}
*/
public function sendMail($body, $subject, $to, $langcode) {
return $this->getCore()->sendMail($body, $subject, $to, $langcode);
}

}

0 comments on commit 8ad2097

Please sign in to comment.