From 3a92fd8e128dd773f3eb23b37d29cb9c301e0173 Mon Sep 17 00:00:00 2001 From: Jonathan Shaw Date: Thu, 13 Jul 2017 16:33:38 +0100 Subject: [PATCH 1/4] Initial emails commit --- src/Drupal/Driver/BaseDriver.php | 35 ++++++++++++++++ src/Drupal/Driver/Cores/CoreInterface.php | 49 ++++++++++++++++++++++- src/Drupal/Driver/Cores/Drupal6.php | 39 ++++++++++++++++++ src/Drupal/Driver/Cores/Drupal7.php | 39 ++++++++++++++++++ src/Drupal/Driver/Cores/Drupal8.php | 48 ++++++++++++++++++++++ src/Drupal/Driver/DriverInterface.php | 47 ++++++++++++++++++++++ src/Drupal/Driver/DrupalDriver.php | 35 ++++++++++++++++ 7 files changed, 291 insertions(+), 1 deletion(-) diff --git a/src/Drupal/Driver/BaseDriver.php b/src/Drupal/Driver/BaseDriver.php index ef420667..068cd2ed 100644 --- a/src/Drupal/Driver/BaseDriver.php +++ b/src/Drupal/Driver/BaseDriver.php @@ -174,4 +174,39 @@ public function entityDelete($entity_type, $entity) { throw new UnsupportedDriverActionException($this->errorString('delete entities using the generic Entity API'), $this); } + /** + * {@inheritdoc} + */ + public function getMailBackend() { + throw new UnsupportedDriverActionException($this->errorString('work with mail'), $this); + } + + /** + * {@inheritdoc} + */ + public function setMailBackend($config) { + 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); + } + } diff --git a/src/Drupal/Driver/Cores/CoreInterface.php b/src/Drupal/Driver/Cores/CoreInterface.php index 303c5f90..dca080ff 100644 --- a/src/Drupal/Driver/Cores/CoreInterface.php +++ b/src/Drupal/Driver/Cores/CoreInterface.php @@ -236,4 +236,51 @@ public function entityCreate($entity_type, $entity); */ public function entityDelete($entity_type, $entity); -} + /** + * Get the mail backend. + * + * @return mixed $config + * The name or configuration array of the mail backend. + */ + public function getMailBackend(); + + /** + * Set the mail backend. + * + * @param mixed $config + * The name or configuration array of the desired mail backend. + */ + public function setMailBackend($config); + + /** + * 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); + +} \ No newline at end of file diff --git a/src/Drupal/Driver/Cores/Drupal6.php b/src/Drupal/Driver/Cores/Drupal6.php index 97026ea8..2a4bf403 100644 --- a/src/Drupal/Driver/Cores/Drupal6.php +++ b/src/Drupal/Driver/Cores/Drupal6.php @@ -513,4 +513,43 @@ 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 getMailBackend() { + // @todo: create a D6 version of this function + throw new \Exception('Mail testing is not yet implemented for Drupal 6.'); + } + + /** + * {@inheritdoc} + */ + public function setMailBackend($config) { + // @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.'); + } + } diff --git a/src/Drupal/Driver/Cores/Drupal7.php b/src/Drupal/Driver/Cores/Drupal7.php index bdbe1df4..fa2250a8 100644 --- a/src/Drupal/Driver/Cores/Drupal7.php +++ b/src/Drupal/Driver/Cores/Drupal7.php @@ -498,4 +498,43 @@ public function entityDelete($entity_type, $entity) { throw new \Exception('Deletion of entities via the generic Entity API is not yet implemented for Drupal 7.'); } + /** + * {@inheritdoc} + */ + public function getMailBackend() { + // @todo: create a D7 version of this function + throw new \Exception('Mail testing is not yet implemented for Drupal 7.'); + } + + /** + * {@inheritdoc} + */ + public function setMailBackend($config) { + // @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.'); + } + } diff --git a/src/Drupal/Driver/Cores/Drupal8.php b/src/Drupal/Driver/Cores/Drupal8.php index 108a44e4..781b41dc 100644 --- a/src/Drupal/Driver/Cores/Drupal8.php +++ b/src/Drupal/Driver/Cores/Drupal8.php @@ -470,4 +470,52 @@ public function entityDelete($entity_type, $entity) { } } + /** + * {@inheritdoc} + */ + public function getMailBackend() { + $mailConfig = \Drupal::configFactory()->get('system.mail'); + return $mailConfig->get('interface.default'); + } + + /** + * {@inheritdoc} + */ + public function setMailBackend($config) { + $mailConfig = \Drupal::configFactory()->getEditable('system.mail'); + $mailConfig->set('interface.default', $config)->save(); + } + + /** + * {@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; + } + } diff --git a/src/Drupal/Driver/DriverInterface.php b/src/Drupal/Driver/DriverInterface.php index 5bbafb5f..f4dcfc69 100644 --- a/src/Drupal/Driver/DriverInterface.php +++ b/src/Drupal/Driver/DriverInterface.php @@ -178,4 +178,51 @@ public function configGet($name, $key); */ public function configSet($name, $key, $value); + /** + * Get the mail backend. + * + * @return mixed $config + * The name or configuration array of the mail backend. + */ + public function getMailBackend(); + + /** + * Set the mail backend. + * + * @param mixed $config + * The name or configuration array of the desired mail backend. + */ + public function setMailBackend($config); + + /** + * 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); + } diff --git a/src/Drupal/Driver/DrupalDriver.php b/src/Drupal/Driver/DrupalDriver.php index e46bc5a8..159c8918 100644 --- a/src/Drupal/Driver/DrupalDriver.php +++ b/src/Drupal/Driver/DrupalDriver.php @@ -333,4 +333,39 @@ public function entityDelete($entity_type, $entity) { return $this->getCore()->entityDelete($entity_type, $entity); } + /** + * {@inheritdoc} + */ + public function getMailBackend() { + return $this->getCore()->getMailBackend(); + } + + /** + * {@inheritdoc} + */ + public function setMailBackend($config) { + return $this->getCore()->setMailBackend($config); + } + + /** + * {@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); + } + } From 0129bfe4edecdc17ae6608cd7e53fc2d7faf5acd Mon Sep 17 00:00:00 2001 From: Jonathan Shaw Date: Thu, 13 Jul 2017 19:26:37 +0100 Subject: [PATCH 2/4] Fix code sniffs --- src/Drupal/Driver/Cores/CoreInterface.php | 10 +++++----- src/Drupal/Driver/Cores/Drupal6.php | 5 +++-- src/Drupal/Driver/Cores/Drupal7.php | 5 +++-- src/Drupal/Driver/Cores/Drupal8.php | 2 +- src/Drupal/Driver/DriverInterface.php | 2 +- 5 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/Drupal/Driver/Cores/CoreInterface.php b/src/Drupal/Driver/Cores/CoreInterface.php index dca080ff..3e57f6a3 100644 --- a/src/Drupal/Driver/Cores/CoreInterface.php +++ b/src/Drupal/Driver/Cores/CoreInterface.php @@ -239,7 +239,7 @@ public function entityDelete($entity_type, $entity); /** * Get the mail backend. * - * @return mixed $config + * @return mixed * The name or configuration array of the mail backend. */ public function getMailBackend(); @@ -247,8 +247,8 @@ public function getMailBackend(); /** * Set the mail backend. * - * @param mixed $config - * The name or configuration array of the desired mail backend. + * @param mixed + * The name or configuration array of the mail backend. */ public function setMailBackend($config); @@ -257,7 +257,7 @@ public function setMailBackend($config); * * @return \stdClass[] * An array of collected emails, each formatted as a Drupal 8 - * \Drupal\Core\Mail\MailInterface::mail $message array. + * \Drupal\Core\Mail\MailInterface::mail $message array. */ public function getMail(); @@ -283,4 +283,4 @@ public function clearMail(); */ public function sendMail($body, $subject, $to, $langcode); -} \ No newline at end of file +} diff --git a/src/Drupal/Driver/Cores/Drupal6.php b/src/Drupal/Driver/Cores/Drupal6.php index 2a4bf403..6d5937ea 100644 --- a/src/Drupal/Driver/Cores/Drupal6.php +++ b/src/Drupal/Driver/Cores/Drupal6.php @@ -534,7 +534,8 @@ public function setMailBackend($config) { */ public function getMail() { // @todo: create a D6 version of this function - throw new \Exception('Mail testing is not yet implemented for Drupal 6.'); } + throw new \Exception('Mail testing is not yet implemented for Drupal 6.'); + } /** * {@inheritdoc} @@ -547,7 +548,7 @@ public function clearMail() { /** * {@inheritdoc} */ - public function sendMail($body, $subject ='', $to = '', $langcode = '') { + 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.'); } diff --git a/src/Drupal/Driver/Cores/Drupal7.php b/src/Drupal/Driver/Cores/Drupal7.php index fa2250a8..e5f1ef50 100644 --- a/src/Drupal/Driver/Cores/Drupal7.php +++ b/src/Drupal/Driver/Cores/Drupal7.php @@ -519,7 +519,8 @@ public function setMailBackend($config) { */ public function getMail() { // @todo: create a D7 version of this function - throw new \Exception('Mail testing is not yet implemented for Drupal 7.'); } + throw new \Exception('Mail testing is not yet implemented for Drupal 7.'); + } /** * {@inheritdoc} @@ -532,7 +533,7 @@ public function clearMail() { /** * {@inheritdoc} */ - public function sendMail($body, $subject ='', $to = '', $langcode = '') { + 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.'); } diff --git a/src/Drupal/Driver/Cores/Drupal8.php b/src/Drupal/Driver/Cores/Drupal8.php index 781b41dc..b3398b14 100644 --- a/src/Drupal/Driver/Cores/Drupal8.php +++ b/src/Drupal/Driver/Cores/Drupal8.php @@ -510,7 +510,7 @@ public function clearMail() { * {@inheritdoc} */ public function sendMail($body = '', $subject = '', $to = '', $langcode = '') { - //Send the mail, via the system module's hook_mail + // Send the mail, via the system module's hook_mail. $params['context']['message'] = $body; $params['context']['subject'] = $subject; $mailManager = \Drupal::service('plugin.manager.mail'); diff --git a/src/Drupal/Driver/DriverInterface.php b/src/Drupal/Driver/DriverInterface.php index f4dcfc69..6c4ab264 100644 --- a/src/Drupal/Driver/DriverInterface.php +++ b/src/Drupal/Driver/DriverInterface.php @@ -181,7 +181,7 @@ public function configSet($name, $key, $value); /** * Get the mail backend. * - * @return mixed $config + * @return mixed * The name or configuration array of the mail backend. */ public function getMailBackend(); From 9db54b63219b1f83efe839aa173d5718801d2dc4 Mon Sep 17 00:00:00 2001 From: jonathanjfshaw Date: Sun, 11 Feb 2018 12:32:28 +0000 Subject: [PATCH 3/4] Use config override --- src/Drupal/Driver/BaseDriver.php | 4 ++-- src/Drupal/Driver/Cores/CoreInterface.php | 14 ++++---------- src/Drupal/Driver/Cores/Drupal6.php | 4 ++-- src/Drupal/Driver/Cores/Drupal7.php | 4 ++-- src/Drupal/Driver/Cores/Drupal8.php | 14 ++++++++------ src/Drupal/Driver/DriverInterface.php | 14 ++++---------- src/Drupal/Driver/DrupalDriver.php | 8 ++++---- 7 files changed, 26 insertions(+), 36 deletions(-) diff --git a/src/Drupal/Driver/BaseDriver.php b/src/Drupal/Driver/BaseDriver.php index 068cd2ed..1b33ec52 100644 --- a/src/Drupal/Driver/BaseDriver.php +++ b/src/Drupal/Driver/BaseDriver.php @@ -177,14 +177,14 @@ public function entityDelete($entity_type, $entity) { /** * {@inheritdoc} */ - public function getMailBackend() { + public function startCollectingMail() { throw new UnsupportedDriverActionException($this->errorString('work with mail'), $this); } /** * {@inheritdoc} */ - public function setMailBackend($config) { + public function stopCollectingMail() { throw new UnsupportedDriverActionException($this->errorString('work with mail'), $this); } diff --git a/src/Drupal/Driver/Cores/CoreInterface.php b/src/Drupal/Driver/Cores/CoreInterface.php index 3e57f6a3..55ef5608 100644 --- a/src/Drupal/Driver/Cores/CoreInterface.php +++ b/src/Drupal/Driver/Cores/CoreInterface.php @@ -237,20 +237,14 @@ public function entityCreate($entity_type, $entity); public function entityDelete($entity_type, $entity); /** - * Get the mail backend. - * - * @return mixed - * The name or configuration array of the mail backend. + * Enable the test mail collector. */ - public function getMailBackend(); + public function startCollectingMail(); /** - * Set the mail backend. - * - * @param mixed - * The name or configuration array of the mail backend. + * Restore normal operation of outgoing mail. */ - public function setMailBackend($config); + public function stopCollectingMail(); /** * Get any mail collected by the test mail collector. diff --git a/src/Drupal/Driver/Cores/Drupal6.php b/src/Drupal/Driver/Cores/Drupal6.php index 6d5937ea..954b36cd 100644 --- a/src/Drupal/Driver/Cores/Drupal6.php +++ b/src/Drupal/Driver/Cores/Drupal6.php @@ -516,7 +516,7 @@ public function entityDelete($entity_type, $entity) { /** * {@inheritdoc} */ - public function getMailBackend() { + public function startCollectingMail() { // @todo: create a D6 version of this function throw new \Exception('Mail testing is not yet implemented for Drupal 6.'); } @@ -524,7 +524,7 @@ public function getMailBackend() { /** * {@inheritdoc} */ - public function setMailBackend($config) { + public function stopCollectingMail() { // @todo: create a D6 version of this function throw new \Exception('Mail testing is not yet implemented for Drupal 6.'); } diff --git a/src/Drupal/Driver/Cores/Drupal7.php b/src/Drupal/Driver/Cores/Drupal7.php index e5f1ef50..d4fa9e00 100644 --- a/src/Drupal/Driver/Cores/Drupal7.php +++ b/src/Drupal/Driver/Cores/Drupal7.php @@ -501,7 +501,7 @@ public function entityDelete($entity_type, $entity) { /** * {@inheritdoc} */ - public function getMailBackend() { + public function startCollectingMail() { // @todo: create a D7 version of this function throw new \Exception('Mail testing is not yet implemented for Drupal 7.'); } @@ -509,7 +509,7 @@ public function getMailBackend() { /** * {@inheritdoc} */ - public function setMailBackend($config) { + public function stopCollectingMail() { // @todo: create a D7 version of this function throw new \Exception('Mail testing is not yet implemented for Drupal 7.'); } diff --git a/src/Drupal/Driver/Cores/Drupal8.php b/src/Drupal/Driver/Cores/Drupal8.php index b3398b14..4f7087a7 100644 --- a/src/Drupal/Driver/Cores/Drupal8.php +++ b/src/Drupal/Driver/Cores/Drupal8.php @@ -473,17 +473,19 @@ public function entityDelete($entity_type, $entity) { /** * {@inheritdoc} */ - public function getMailBackend() { - $mailConfig = \Drupal::configFactory()->get('system.mail'); - return $mailConfig->get('interface.default'); + public function startCollectingMail() { + global $config; + // @todo Use a collector that supports html after D#2223967 lands. + $config['system.mail']['interface.default'] = 'test_mail_collector'; } /** * {@inheritdoc} */ - public function setMailBackend($config) { - $mailConfig = \Drupal::configFactory()->getEditable('system.mail'); - $mailConfig->set('interface.default', $config)->save(); + public function stopCollectingMail() { + global $config; + $original = \Drupal::config('system.mail')->getOriginal('interface.default', FALSE); + $config['system.mail']['interface.default'] = $original; } /** diff --git a/src/Drupal/Driver/DriverInterface.php b/src/Drupal/Driver/DriverInterface.php index 6c4ab264..d916a8df 100644 --- a/src/Drupal/Driver/DriverInterface.php +++ b/src/Drupal/Driver/DriverInterface.php @@ -179,20 +179,14 @@ public function configGet($name, $key); public function configSet($name, $key, $value); /** - * Get the mail backend. - * - * @return mixed - * The name or configuration array of the mail backend. + * Enable the test mail collector. */ - public function getMailBackend(); + public function startCollectingMail(); /** - * Set the mail backend. - * - * @param mixed $config - * The name or configuration array of the desired mail backend. + * Restore normal operation of outgoing mail. */ - public function setMailBackend($config); + public function stopCollectingMail(); /** * Get any mail collected by the test mail collector. diff --git a/src/Drupal/Driver/DrupalDriver.php b/src/Drupal/Driver/DrupalDriver.php index 159c8918..8ee20dab 100644 --- a/src/Drupal/Driver/DrupalDriver.php +++ b/src/Drupal/Driver/DrupalDriver.php @@ -336,15 +336,15 @@ public function entityDelete($entity_type, $entity) { /** * {@inheritdoc} */ - public function getMailBackend() { - return $this->getCore()->getMailBackend(); + public function startCollectingMail() { + return $this->getCore()->startCollectingMail(); } /** * {@inheritdoc} */ - public function setMailBackend($config) { - return $this->getCore()->setMailBackend($config); + public function stopCollectingMail() { + return $this->getCore()->stopCollectingMail(); } /** From 93f806fcc7012468ba7bf28d1e5cbd0d4b51c548 Mon Sep 17 00:00:00 2001 From: jonathanjfshaw Date: Mon, 12 Feb 2018 13:29:59 +0000 Subject: [PATCH 4/4] Use setModuleOverride not global config --- src/Drupal/Driver/Cores/Drupal8.php | 6 ++---- src/Drupal/Driver/DriverInterface.php | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Drupal/Driver/Cores/Drupal8.php b/src/Drupal/Driver/Cores/Drupal8.php index 4f7087a7..ca234794 100644 --- a/src/Drupal/Driver/Cores/Drupal8.php +++ b/src/Drupal/Driver/Cores/Drupal8.php @@ -474,18 +474,16 @@ public function entityDelete($entity_type, $entity) { * {@inheritdoc} */ public function startCollectingMail() { - global $config; // @todo Use a collector that supports html after D#2223967 lands. - $config['system.mail']['interface.default'] = 'test_mail_collector'; + \Drupal::config('system.mail')->setModuleOverride(['interface' => ['default' => 'test_mail_collector']]); } /** * {@inheritdoc} */ public function stopCollectingMail() { - global $config; $original = \Drupal::config('system.mail')->getOriginal('interface.default', FALSE); - $config['system.mail']['interface.default'] = $original; + \Drupal::config('system.mail')->setModuleOverride(['interface' => ['default' => $original]]); } /** diff --git a/src/Drupal/Driver/DriverInterface.php b/src/Drupal/Driver/DriverInterface.php index d916a8df..a791664f 100644 --- a/src/Drupal/Driver/DriverInterface.php +++ b/src/Drupal/Driver/DriverInterface.php @@ -193,7 +193,7 @@ public function stopCollectingMail(); * * @return \stdClass[] * An array of collected emails, each formatted as a Drupal 8 - * \Drupal\Core\Mail\MailInterface::mail $message array. + * \Drupal\Core\Mail\MailInterface::mail $message array. */ public function getMail();