From cd289946a835ae73ab9944063226749a105e6ec2 Mon Sep 17 00:00:00 2001 From: Vlad Moyseenko Date: Mon, 4 Jul 2016 13:05:09 +0300 Subject: [PATCH 01/21] Run tests simply non cd'ing of running tests --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index c1bfdd6..ef6b9c9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -51,4 +51,4 @@ install: - drush en -y netsmtp script: - - cd modules/drupal-netsmtp/src/Tests/Integration && php -f runtests.php + - php -f modules/drupal-netsmtp/src/Tests/Integration/runtests.php From 649f49d8aa8a6d0d5be834fd54fb07963963df6f Mon Sep 17 00:00:00 2001 From: Vlad Moyseenko Date: Mon, 4 Jul 2016 13:11:30 +0300 Subject: [PATCH 02/21] First change directory, than run tests --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index ef6b9c9..c8f0525 100644 --- a/.travis.yml +++ b/.travis.yml @@ -51,4 +51,5 @@ install: - drush en -y netsmtp script: - - php -f modules/drupal-netsmtp/src/Tests/Integration/runtests.php + - cd modules/drupal-netsmtp/src/Tests/Integration + - php -f runtests.php From 13c1ff2cda125f26fce61b63806cf2c0fc2f7161 Mon Sep 17 00:00:00 2001 From: Vlad Moyseenko Date: Mon, 4 Jul 2016 13:41:29 +0300 Subject: [PATCH 03/21] Set manualy error code to trigger system error & try to load properly dependencies --- .travis.yml | 7 ++++--- src/Tests/Integration/CanSendEmailToMailtrapSmtp.php | 10 ++++++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index c8f0525..036e576 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,6 +28,8 @@ mysql: encoding: utf8 before_install: + - export MAILSYSTEM_VERSION=8.x-4.0 + - test ${$DRUPAL_VERSION} == "8" || export MAILSYSTEM_VERSION=8.x-4.x-dev - git config --global github.accesstoken $GITHUB_OAUTH_TOKEN - composer self-update @@ -45,11 +47,10 @@ install: - mv drupal-netsmtp drupal/modules - cd drupal - export DRUPAL_DIR=$(pwd) - # Install drupal default profile - drush --verbose site-install --db-url=mysql://root:@127.0.0.1/drupal --yes + - drush dl mailsystem-$MAILSYSTEM_VERSION -y - drush en -y netsmtp script: - - cd modules/drupal-netsmtp/src/Tests/Integration - - php -f runtests.php + - cd modules/drupal-netsmtp/src/Tests/Integration && php -f runtests.php diff --git a/src/Tests/Integration/CanSendEmailToMailtrapSmtp.php b/src/Tests/Integration/CanSendEmailToMailtrapSmtp.php index 6e37316..a01478f 100755 --- a/src/Tests/Integration/CanSendEmailToMailtrapSmtp.php +++ b/src/Tests/Integration/CanSendEmailToMailtrapSmtp.php @@ -87,11 +87,17 @@ public function testSendEmail() { $mail = reset($data); if (404 == $response->getStatusCode()) { - throw new \ErrorException(sprintf('Can\'t find a email with email subject: %s', $message_key)); + throw new \ErrorException( + sprintf('Can\'t find a email with email subject: %s', $message_key), + 1 + ); } if ($mail->subject != $message_key) { - throw new \ErrorException(sprintf('There is no email with email subject: %s', $message_key)); + throw new \ErrorException( + sprintf('There is no email with email subject: %s', $message_key), + 1 + ); } } From f5f98a521b86121110d1ca8fd78392b8172504ce Mon Sep 17 00:00:00 2001 From: Vlad Moyseenko Date: Mon, 4 Jul 2016 13:45:57 +0300 Subject: [PATCH 04/21] Add quotes --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 036e576..735c255 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,8 +28,8 @@ mysql: encoding: utf8 before_install: - - export MAILSYSTEM_VERSION=8.x-4.0 - - test ${$DRUPAL_VERSION} == "8" || export MAILSYSTEM_VERSION=8.x-4.x-dev + - export MAILSYSTEM_VERSION="8.x-4.0" + - test ${$DRUPAL_VERSION} == "8" || export MAILSYSTEM_VERSION="8.x-4.x-dev" - git config --global github.accesstoken $GITHUB_OAUTH_TOKEN - composer self-update From 0ebfe7671ef23e558be8b8af99c5e420cce3546e Mon Sep 17 00:00:00 2001 From: Vlad Moyseenko Date: Mon, 4 Jul 2016 14:14:29 +0300 Subject: [PATCH 05/21] remove typo --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 735c255..5be5b83 100644 --- a/.travis.yml +++ b/.travis.yml @@ -29,7 +29,7 @@ mysql: before_install: - export MAILSYSTEM_VERSION="8.x-4.0" - - test ${$DRUPAL_VERSION} == "8" || export MAILSYSTEM_VERSION="8.x-4.x-dev" + - test ${DRUPAL_VERSION} == "8" || export MAILSYSTEM_VERSION="8.x-4.x-dev" - git config --global github.accesstoken $GITHUB_OAUTH_TOKEN - composer self-update From d22c6576048f80039435b170b2773ce96922f322 Mon Sep 17 00:00:00 2001 From: Vlad Moyseenko Date: Mon, 4 Jul 2016 14:21:10 +0300 Subject: [PATCH 06/21] Set manualy error code to trigger system error on runtime function --- src/Tests/Integration/CanSendEmailToMailtrapSmtp.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Tests/Integration/CanSendEmailToMailtrapSmtp.php b/src/Tests/Integration/CanSendEmailToMailtrapSmtp.php index a01478f..ba44878 100755 --- a/src/Tests/Integration/CanSendEmailToMailtrapSmtp.php +++ b/src/Tests/Integration/CanSendEmailToMailtrapSmtp.php @@ -65,7 +65,10 @@ public function testSendEmail() { $message_key = \Drupal::state()->get('netsmtp.last_message_id'); } catch (\Exception $e) { - throw new \RuntimeException(sprintf('Can\'t send an email. Details: %s', $e->getMessage())); + throw new \RuntimeException( + sprintf('Can\'t send an email. Details: %s', $e->getMessage()), + 1 + ); } $inbox_url = implode('/', [ From ba0614e1bb5ccb7a5d6edacf89756c3db2ac72f5 Mon Sep 17 00:00:00 2001 From: Vlad Moyseenko Date: Mon, 4 Jul 2016 14:28:49 +0300 Subject: [PATCH 07/21] remove dump --- src/Tests/Integration/runtests.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Tests/Integration/runtests.php b/src/Tests/Integration/runtests.php index 840b6f9..449ddaa 100644 --- a/src/Tests/Integration/runtests.php +++ b/src/Tests/Integration/runtests.php @@ -8,7 +8,6 @@ use Symfony\Component\HttpFoundation\Request; $drupal_dir = getenv('DRUPAL_DIR'); -var_dump($drupal_dir); static $kernel; From 560939bfe21c2a041cee0f4618b8aaffb213330e Mon Sep 17 00:00:00 2001 From: Vlad Moyseenko Date: Mon, 4 Jul 2016 14:40:37 +0300 Subject: [PATCH 08/21] Try to fall --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 5be5b83..1926e80 100644 --- a/.travis.yml +++ b/.travis.yml @@ -53,4 +53,5 @@ install: - drush en -y netsmtp script: - - cd modules/drupal-netsmtp/src/Tests/Integration && php -f runtests.php + - cd modules/drupal-netsmtp/src/Tests/Integration + - php -f runtests.php From 93306aed66d6f2328d662814655ef308bb7e675c Mon Sep 17 00:00:00 2001 From: Vlad Moyseenko Date: Mon, 4 Jul 2016 14:48:33 +0300 Subject: [PATCH 09/21] Try to fail using exit() --- .travis.yml | 3 +-- .../CanSendEmailToMailtrapSmtp.php | 20 +++++++------------ 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1926e80..5be5b83 100644 --- a/.travis.yml +++ b/.travis.yml @@ -53,5 +53,4 @@ install: - drush en -y netsmtp script: - - cd modules/drupal-netsmtp/src/Tests/Integration - - php -f runtests.php + - cd modules/drupal-netsmtp/src/Tests/Integration && php -f runtests.php diff --git a/src/Tests/Integration/CanSendEmailToMailtrapSmtp.php b/src/Tests/Integration/CanSendEmailToMailtrapSmtp.php index ba44878..e713d1f 100755 --- a/src/Tests/Integration/CanSendEmailToMailtrapSmtp.php +++ b/src/Tests/Integration/CanSendEmailToMailtrapSmtp.php @@ -64,11 +64,9 @@ public function testSendEmail() { $result = $this->mailManager->mail('netsmtp', 'test_message'); $message_key = \Drupal::state()->get('netsmtp.last_message_id'); } - catch (\Exception $e) { - throw new \RuntimeException( - sprintf('Can\'t send an email. Details: %s', $e->getMessage()), - 1 - ); + catch (\RuntimeException $e) { + echo sprintf('Can\'t send an email. Details: %s', $e->getMessage()); + exit(1); } $inbox_url = implode('/', [ @@ -90,17 +88,13 @@ public function testSendEmail() { $mail = reset($data); if (404 == $response->getStatusCode()) { - throw new \ErrorException( - sprintf('Can\'t find a email with email subject: %s', $message_key), - 1 - ); + echo sprintf('Can\'t find a email with email subject: %s', $message_key); + exit(1); } if ($mail->subject != $message_key) { - throw new \ErrorException( - sprintf('There is no email with email subject: %s', $message_key), - 1 - ); + sprintf('There is no email with email subject: %s', $message_key); + exit(1); } } From 624e5a8eb1c566452b052daeb984b457f5ea9e28 Mon Sep 17 00:00:00 2001 From: Vlad Moyseenko Date: Mon, 4 Jul 2016 15:17:17 +0300 Subject: [PATCH 10/21] Try to apply workaround --- src/Tests/Integration/CanSendEmailToMailtrapSmtp.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Tests/Integration/CanSendEmailToMailtrapSmtp.php b/src/Tests/Integration/CanSendEmailToMailtrapSmtp.php index e713d1f..3e10fac 100755 --- a/src/Tests/Integration/CanSendEmailToMailtrapSmtp.php +++ b/src/Tests/Integration/CanSendEmailToMailtrapSmtp.php @@ -65,7 +65,7 @@ public function testSendEmail() { $message_key = \Drupal::state()->get('netsmtp.last_message_id'); } catch (\RuntimeException $e) { - echo sprintf('Can\'t send an email. Details: %s', $e->getMessage()); + file_put_contents('php://stderr', sprintf('Can\'t send an email. Details: %s', $e->getMessage()); exit(1); } From 48d1d6476bb12e33c6973359674a1b29546deb43 Mon Sep 17 00:00:00 2001 From: Vlad Moyseenko Date: Mon, 4 Jul 2016 15:22:23 +0300 Subject: [PATCH 11/21] Fix typo --- src/Tests/Integration/CanSendEmailToMailtrapSmtp.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Tests/Integration/CanSendEmailToMailtrapSmtp.php b/src/Tests/Integration/CanSendEmailToMailtrapSmtp.php index 3e10fac..0850267 100755 --- a/src/Tests/Integration/CanSendEmailToMailtrapSmtp.php +++ b/src/Tests/Integration/CanSendEmailToMailtrapSmtp.php @@ -65,7 +65,7 @@ public function testSendEmail() { $message_key = \Drupal::state()->get('netsmtp.last_message_id'); } catch (\RuntimeException $e) { - file_put_contents('php://stderr', sprintf('Can\'t send an email. Details: %s', $e->getMessage()); + file_put_contents('php://stderr', sprintf('Can\'t send an email. Details: %s', $e->getMessage())); exit(1); } From a2f27ffb18c28fcb5924fe77e45279ebe2980bb0 Mon Sep 17 00:00:00 2001 From: Vlad Moyseenko Date: Mon, 4 Jul 2016 15:32:53 +0300 Subject: [PATCH 12/21] Clear cache after enable --- .travis.yml | 3 ++- src/Tests/Integration/CanSendEmailToMailtrapSmtp.php | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5be5b83..99dd12f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -51,6 +51,7 @@ install: - drush --verbose site-install --db-url=mysql://root:@127.0.0.1/drupal --yes - drush dl mailsystem-$MAILSYSTEM_VERSION -y - drush en -y netsmtp + - drush cr script: - - cd modules/drupal-netsmtp/src/Tests/Integration && php -f runtests.php + - cd modules/drupal-netsmtp/src/Tests/Integration && php -f -e runtests.php diff --git a/src/Tests/Integration/CanSendEmailToMailtrapSmtp.php b/src/Tests/Integration/CanSendEmailToMailtrapSmtp.php index 0850267..4d14c48 100755 --- a/src/Tests/Integration/CanSendEmailToMailtrapSmtp.php +++ b/src/Tests/Integration/CanSendEmailToMailtrapSmtp.php @@ -64,7 +64,7 @@ public function testSendEmail() { $result = $this->mailManager->mail('netsmtp', 'test_message'); $message_key = \Drupal::state()->get('netsmtp.last_message_id'); } - catch (\RuntimeException $e) { + catch (\Exception $e) { file_put_contents('php://stderr', sprintf('Can\'t send an email. Details: %s', $e->getMessage())); exit(1); } From 56c594476d92496264da6879c98862c9a889957a Mon Sep 17 00:00:00 2001 From: Vlad Moyseenko Date: Mon, 4 Jul 2016 15:43:27 +0300 Subject: [PATCH 13/21] Fix param order --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 99dd12f..0813a76 100644 --- a/.travis.yml +++ b/.travis.yml @@ -54,4 +54,4 @@ install: - drush cr script: - - cd modules/drupal-netsmtp/src/Tests/Integration && php -f -e runtests.php + - cd modules/drupal-netsmtp/src/Tests/Integration && php -e -f runtests.php From ba69fea330005bccd7e97a7ae5374bc2a437890e Mon Sep 17 00:00:00 2001 From: Vlad Moyseenko Date: Wed, 6 Jul 2016 20:20:20 +0300 Subject: [PATCH 14/21] Use drush to run a test --- .gitattributes | 0 .travis.yml | 4 +- config/schema/netsmtp.schema.yml | 37 +++++++++- netsmtp.info.yml | 4 +- src/Plugin/Mail/NetSmtpMail.php | 33 ++++----- .../CanSendEmailToMailtrapSmtp.php | 67 ++++++++++++++----- src/Tests/Integration/runtests.php | 10 +-- 7 files changed, 113 insertions(+), 42 deletions(-) mode change 100644 => 100755 .gitattributes mode change 100644 => 100755 .travis.yml mode change 100644 => 100755 src/Tests/Integration/runtests.php diff --git a/.gitattributes b/.gitattributes old mode 100644 new mode 100755 diff --git a/.travis.yml b/.travis.yml old mode 100644 new mode 100755 index 0813a76..24668f0 --- a/.travis.yml +++ b/.travis.yml @@ -46,12 +46,12 @@ install: - drush dl drupal-$DRUPAL_VERSION --drupal-project-rename=drupal - mv drupal-netsmtp drupal/modules - cd drupal - - export DRUPAL_DIR=$(pwd) # Install drupal default profile - drush --verbose site-install --db-url=mysql://root:@127.0.0.1/drupal --yes - drush dl mailsystem-$MAILSYSTEM_VERSION -y - drush en -y netsmtp - drush cr + - export DRUPAL_DIR=$(drush dd) script: - - cd modules/drupal-netsmtp/src/Tests/Integration && php -e -f runtests.php + - cd modules/drupal-netsmtp/src/Tests/Integration && drush scr runtests.php diff --git a/config/schema/netsmtp.schema.yml b/config/schema/netsmtp.schema.yml index 9a4a328..f6902cc 100755 --- a/config/schema/netsmtp.schema.yml +++ b/config/schema/netsmtp.schema.yml @@ -24,7 +24,38 @@ netsmtp.settings: netsmtp_catch: type: sequence - label: 'Re-route emails' + label: 'Re-route emails' + sequence: + type: email + label: 'Re-route email' + providers: + type: sequence + label: List of modules + sequence: + type: sequence + label: List of keys for a given module sequence: - type: email - label: 'Re-route email' + type: mapping + label: Per module/key setting. + mapping: + hostname: + type: string + label: 'Hostname' + port: + type: string + label: 'port' + use_ssl: + type: string + label: 'ssl or tls' + description: 'true/false for "ssl" & tls to use "tls".' + username: + type: string + label: 'Username' + password: + type: string + label: 'Password' + localhost: + type: string + label: 'Localhost' + description: 'The value to give when sending EHLO or HELO.' + diff --git a/netsmtp.info.yml b/netsmtp.info.yml index 9ac9e16..b870ec4 100755 --- a/netsmtp.info.yml +++ b/netsmtp.info.yml @@ -1,7 +1,7 @@ -name: Net SMTP +name: "Net SMTP" type: module description: 'SMTP connector using Net_SMTP PEAR library.' -package: Mail +package: "Mail" core: 8.x configure: netsmtp.settings dependencies: diff --git a/src/Plugin/Mail/NetSmtpMail.php b/src/Plugin/Mail/NetSmtpMail.php index 77976a6..a8515f5 100755 --- a/src/Plugin/Mail/NetSmtpMail.php +++ b/src/Plugin/Mail/NetSmtpMail.php @@ -36,6 +36,12 @@ class NetSmtpMail implements MailInterface { * Default provider key. */ const PROVIDER_DEFAULT = 'default'; + + /** + * Provider config key. + */ + const PROVIDER_CONFIG_KEY = 'providers'; + /** * Default SSL port. */ @@ -53,17 +59,15 @@ class NetSmtpMail implements MailInterface { /** * Configuration handler. * - * @var \Drupal\Core\Config\ImmutableConfig + * @var \Drupal\Core\Config\ConfigFactoryInterface */ - private $config; + protected $config; /** * NetSmtpMail constructor. */ public function __construct() { - // Fuck hate PEAR. - $this->PEAR = new PEAR(); - $this->config = \Drupal::config('netsmtp.settings'); + $this->config = \Drupal::configFactory()->get('netsmtp.settings'); } /** @@ -82,7 +86,6 @@ protected function catchAddressesInto($string) { $ret = []; if (empty($string)) { - // Please bitch... Not my problem. return NULL; } @@ -154,15 +157,14 @@ protected function setError($e, $type = 'error') { protected function getInstance($module, $key) { $is_tls = FALSE; - // SMTP server configurations per module, key. $server_id_list = [ - $module . '.' . $key, - $module, - $key, - self::PROVIDER_DEFAULT, + self::PROVIDER_CONFIG_KEY . '.' . $module . '.' . $key, + self::PROVIDER_CONFIG_KEY . '.' . $module, + self::PROVIDER_CONFIG_KEY . '.' . $key, + self::PROVIDER_CONFIG_KEY . '.' . self::PROVIDER_DEFAULT ]; - foreach ($server_id_list as $provider) { + foreach($server_id_list as $provider) { $provider_config = $this->config->get($provider); if (!is_null($provider_config)) { break; @@ -170,7 +172,7 @@ protected function getInstance($module, $key) { } if (empty($provider_config) && isset($provider)) { - $this->setError(sprintf("Provider '%s' does not exists, fallback on default", $provider), 'warning'); + $this->setError(sprintf("Provider '%s' does not exists", $provider), 'warning'); return NULL; } @@ -181,9 +183,9 @@ protected function getInstance($module, $key) { $info = array_filter($provider_config) + [ 'port' => NULL, - 'username' => NULL, 'use_ssl' => FALSE, 'password' => '', + 'username' => NULL, 'localhost' => NULL, ]; @@ -213,7 +215,6 @@ protected function getInstance($module, $key) { return NULL; } } - // Finally! We did it I guess. return $smtp; } @@ -229,6 +230,8 @@ public function format(array $message) { * {@inheritdoc} */ public function mail(array $message) { + $this->PEAR = new PEAR(); + if (!$smtp = $this->getInstance($message['module'], $message['key'])) { return FALSE; } diff --git a/src/Tests/Integration/CanSendEmailToMailtrapSmtp.php b/src/Tests/Integration/CanSendEmailToMailtrapSmtp.php index 4d14c48..8238ca0 100755 --- a/src/Tests/Integration/CanSendEmailToMailtrapSmtp.php +++ b/src/Tests/Integration/CanSendEmailToMailtrapSmtp.php @@ -10,6 +10,8 @@ use GuzzleHttp\Client; define('NETSMTP_MAILTRAP_API_ENDPOINT', 'https://mailtrap.io/api/v1'); +define('NETSMTP_MAILTRAP_SMTP_HOSTNAME', 'mailtrap.io'); +define('NETSMTP_MAILTRAP_SMTP_PORT', 465); /** * Class CanSendEmailToMailtrapSmtp. @@ -28,13 +30,13 @@ class CanSendEmailToMailtrapSmtp { * * @var string */ - private $smtpToken; + private $apiToken; /** * Inbox ID. * * @var string */ - private $smtpInboxId; + private $apiInboxId; /** * Mail manager. * @@ -44,16 +46,46 @@ class CanSendEmailToMailtrapSmtp { /** * CanSendEmailToMailtrapSmtp constructor. - * - * @param DrupalKernel $kernel - * Drupal Kernel. */ - public function __construct(DrupalKernel $kernel) { + public function __construct() { // Prepare the initial properties. - $this->kernel = $kernel; - $this->mailManager = $this->kernel->getContainer()->get('plugin.manager.mail'); - $this->smtpInboxId = getenv('MAILTRAP_INBOX_ID'); - $this->smtpToken = getenv('MAILTRAP_TOKEN'); + $this->apiInboxId = getenv('MAILTRAP_INBOX_ID'); + $this->apiToken = getenv('MAILTRAP_TOKEN'); + $this->smtpUserName = getenv('MAILTRAP_SMTP_USERNAME'); + $this->smtpPassword = getenv('MAILTRAP_SMTP_PASSWORD'); + + // We need this before initMailManager(), + // to properly construct a netsmtp_mail plugin. + $this->createSmtpConfig( + $this->smtpUserName, + $this->smtpPassword + ); + $this->initMailManager(); + } + + /** + * Construct mailtrap smtp config object. + * + * This object later will be used by NetSmtpMail::__construct(). + */ + private function createSmtpConfig() { + $config = \Drupal::configFactory()->getEditable('netsmtp.settings'); + + $config + ->set('providers.netsmtp.test_message.hostname', NETSMTP_MAILTRAP_SMTP_HOSTNAME) + ->set('providers.netsmtp.test_message.port', NETSMTP_MAILTRAP_SMTP_PORT) + ->set('providers.netsmtp.test_message.use_ssl', FALSE) + ->set('providers.netsmtp.test_message.username', $this->smtpUserName) + ->set('providers.netsmtp.test_message.password', $this->smtpPassword); + + $config->save(); + } + + /** + * Get current mail manager. + */ + private function initMailManager() { + $this->mailManager = \Drupal::getContainer()->get('plugin.manager.mail'); } /** @@ -61,27 +93,29 @@ public function __construct(DrupalKernel $kernel) { */ public function testSendEmail() { try { - $result = $this->mailManager->mail('netsmtp', 'test_message'); - $message_key = \Drupal::state()->get('netsmtp.last_message_id'); + $this->mailManager->mail('netsmtp', 'test_message', 'netsmtp@example.com', []); } - catch (\Exception $e) { + catch (\RuntimeException $e) { + file_put_contents('php://stderr', $e->getTraceAsString()); file_put_contents('php://stderr', sprintf('Can\'t send an email. Details: %s', $e->getMessage())); exit(1); } + $message_key = \Drupal::state()->get('netsmtp.last_message_id'); + $inbox_url = implode('/', [ NETSMTP_MAILTRAP_API_ENDPOINT, 'inboxes', - $this->smtpInboxId, + $this->apiInboxId, ]); $client = new Client([ 'base_uri' => $inbox_url . '/', + 'headers' => ['Api-Token' => $this->apiToken], ]); $response = $client->request('GET', 'messages', [ 'query' => ['search' => $message_key], - 'headers' => ['Api-Token' => $this->smtpToken], ]); $data = \GuzzleHttp\json_decode($response->getBody()->getContents()); @@ -96,6 +130,9 @@ public function testSendEmail() { sprintf('There is no email with email subject: %s', $message_key); exit(1); } + var_dump('mail_id: ' . $mail->id); + $response = $client->request('GET', "messages/$mail->id/body.raw"); + var_dump($data = ($response->getBody()->getContents())); } } diff --git a/src/Tests/Integration/runtests.php b/src/Tests/Integration/runtests.php old mode 100644 new mode 100755 index 449ddaa..da2e9b9 --- a/src/Tests/Integration/runtests.php +++ b/src/Tests/Integration/runtests.php @@ -9,9 +9,9 @@ $drupal_dir = getenv('DRUPAL_DIR'); -static $kernel; +//static $kernel; -if (!isset($kernel) || !($kernel instanceof DrupalKernel)) { +/*if (!isset($kernel) || !($kernel instanceof DrupalKernel)) { require_once $drupal_dir . '/core/includes/database.inc'; require_once $drupal_dir . '/core/includes/schema.inc'; @@ -21,8 +21,8 @@ $kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod'); $kernel->boot(); $kernel->prepareLegacyRequest($request); -} +}*/ -require_once './CanSendEmailToMailtrapSmtp.php'; -$mailer = new \Drupal\netsmtp\Tests\Integration\CanSendEmailToMailtrapSmtp($kernel); +//require_once './CanSendEmailToMailtrapSmtp.php'; +$mailer = new \Drupal\netsmtp\Tests\Integration\CanSendEmailToMailtrapSmtp(); $mailer->testSendEmail(); From cb1993ac248c9b44a9a3d5d9f3e8d62c9c0f0ade Mon Sep 17 00:00:00 2001 From: Vlad Moyseenko Date: Wed, 6 Jul 2016 20:21:57 +0300 Subject: [PATCH 15/21] Change permissions on files --- .gitattributes | 0 .travis.yml | 0 src/Tests/Integration/runtests.php | 0 3 files changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 .gitattributes mode change 100755 => 100644 .travis.yml mode change 100755 => 100644 src/Tests/Integration/runtests.php diff --git a/.gitattributes b/.gitattributes old mode 100755 new mode 100644 diff --git a/.travis.yml b/.travis.yml old mode 100755 new mode 100644 diff --git a/src/Tests/Integration/runtests.php b/src/Tests/Integration/runtests.php old mode 100755 new mode 100644 From 58a6e1ad10eb7362a975d8c329d8e67078100ef5 Mon Sep 17 00:00:00 2001 From: Vlad Moyseenko Date: Wed, 6 Jul 2016 21:59:26 +0300 Subject: [PATCH 16/21] Set mailsystem settings inside test --- .../Integration/CanSendEmailToMailtrapSmtp.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Tests/Integration/CanSendEmailToMailtrapSmtp.php b/src/Tests/Integration/CanSendEmailToMailtrapSmtp.php index 8238ca0..a812f2b 100755 --- a/src/Tests/Integration/CanSendEmailToMailtrapSmtp.php +++ b/src/Tests/Integration/CanSendEmailToMailtrapSmtp.php @@ -69,16 +69,20 @@ public function __construct() { * This object later will be used by NetSmtpMail::__construct(). */ private function createSmtpConfig() { - $config = \Drupal::configFactory()->getEditable('netsmtp.settings'); - - $config + \Drupal::configFactory() + ->getEditable('mailsystem.settings') + ->set('defaults.sender', 'netsmtp_mail') + ->set('defaults.formatter','php_mail') + ->save(); + + \Drupal::configFactory() + ->getEditable('netsmtp.settings') ->set('providers.netsmtp.test_message.hostname', NETSMTP_MAILTRAP_SMTP_HOSTNAME) ->set('providers.netsmtp.test_message.port', NETSMTP_MAILTRAP_SMTP_PORT) ->set('providers.netsmtp.test_message.use_ssl', FALSE) ->set('providers.netsmtp.test_message.username', $this->smtpUserName) - ->set('providers.netsmtp.test_message.password', $this->smtpPassword); - - $config->save(); + ->set('providers.netsmtp.test_message.password', $this->smtpPassword) + ->save(); } /** From 4f79e0b14517a3822871839cc9030de42125cdbf Mon Sep 17 00:00:00 2001 From: Vlad Moyseenko Date: Wed, 6 Jul 2016 22:58:26 +0300 Subject: [PATCH 17/21] Add composer_manager & set pear/net_smtp version --- .travis.yml | 5 +++++ composer.json | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 24668f0..dff8405 100644 --- a/.travis.yml +++ b/.travis.yml @@ -50,6 +50,11 @@ install: - drush --verbose site-install --db-url=mysql://root:@127.0.0.1/drupal --yes - drush dl mailsystem-$MAILSYSTEM_VERSION -y - drush en -y netsmtp + + - drush dl composer_manager + - php modules/composer_manager/scripts/init.php + - composer drupal-update + - drush cr - export DRUPAL_DIR=$(drush dd) diff --git a/composer.json b/composer.json index 91ab373..56c803f 100644 --- a/composer.json +++ b/composer.json @@ -20,6 +20,6 @@ "source": "https://github.com/pear/Mail" }, "require": { - "pear/net_smtp": "*" + "pear/net_smtp": "^1.7" } } \ No newline at end of file From d2bf2a897c0cf71c19dfb48d93c814aa2076c745 Mon Sep 17 00:00:00 2001 From: Vlad Moyseenko Date: Wed, 6 Jul 2016 23:28:34 +0300 Subject: [PATCH 18/21] Avoid sendmail problems on site install --- .travis.yml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index dff8405..c61dbb5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -40,14 +40,15 @@ install: - composer global require --no-interaction "hirak/prestissimo:^0.3" - phpenv rehash - - mysql -e 'create database drupal;' - - cd .. - drush dl drupal-$DRUPAL_VERSION --drupal-project-rename=drupal - mv drupal-netsmtp drupal/modules - cd drupal # Install drupal default profile - - drush --verbose site-install --db-url=mysql://root:@127.0.0.1/drupal --yes + # create new site, stubbing sendmail path with true to prevent delivery errors and manually resolving drush path + - mysql -e 'create database drupal;' + - php -d sendmail_path=`which true` ~/.composer/vendor/bin/drush.php --yes core-quick-drupal --profile=testing --no-server --verbose --db-url=mysql://root:@127.0.0.1/drupal + - drush dl mailsystem-$MAILSYSTEM_VERSION -y - drush en -y netsmtp From df2430a00ec9d0bef7f1e1f42d6cf34d8ced6d49 Mon Sep 17 00:00:00 2001 From: Vlad Moyseenko Date: Thu, 7 Jul 2016 19:46:56 +0300 Subject: [PATCH 19/21] Ignore running non-static method as a static, because actually it is static if look inside. --- netsmtp.module | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/netsmtp.module b/netsmtp.module index 79aba2b..4a08eea 100755 --- a/netsmtp.module +++ b/netsmtp.module @@ -14,10 +14,10 @@ use Drupal\Core\Url; function netsmtp_mail($key, &$message) { switch ($key) { case 'test_message': - $uuid = \Drupal\Component\Uuid\Php::generate(); + $uuid = @\Drupal\Component\Uuid\Php::generate(); $message['subject'] = 'uniq-mailtrap-id:' . $uuid; \Drupal::state()->set('netsmtp.last_message_id', $message['subject']); - $message['body'][] = t('Testing Net Smtp mailer.'); + $message['body'][] = t('Testing NetSMTP mailer'); break; } } From 61f5ead91918d6b418fd926a76c4dd51b8055a42 Mon Sep 17 00:00:00 2001 From: Vlad Moyseenko Date: Thu, 7 Jul 2016 19:47:23 +0300 Subject: [PATCH 20/21] Remove old code for test runner --- src/Tests/Integration/runtests.php | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/src/Tests/Integration/runtests.php b/src/Tests/Integration/runtests.php index da2e9b9..c5aa0c5 100644 --- a/src/Tests/Integration/runtests.php +++ b/src/Tests/Integration/runtests.php @@ -4,25 +4,5 @@ * Test runner. */ -use Drupal\Core\DrupalKernel; -use Symfony\Component\HttpFoundation\Request; - -$drupal_dir = getenv('DRUPAL_DIR'); - -//static $kernel; - -/*if (!isset($kernel) || !($kernel instanceof DrupalKernel)) { - require_once $drupal_dir . '/core/includes/database.inc'; - require_once $drupal_dir . '/core/includes/schema.inc'; - - $autoloader = require_once $drupal_dir . '/autoload.php'; - $request = Request::createFromGlobals(); - - $kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod'); - $kernel->boot(); - $kernel->prepareLegacyRequest($request); -}*/ - -//require_once './CanSendEmailToMailtrapSmtp.php'; $mailer = new \Drupal\netsmtp\Tests\Integration\CanSendEmailToMailtrapSmtp(); $mailer->testSendEmail(); From 6ed401cd3ed17bc69c8a069aaeb8c52b6e78b34f Mon Sep 17 00:00:00 2001 From: Vlad Moyseenko Date: Thu, 7 Jul 2016 19:48:07 +0300 Subject: [PATCH 21/21] Actually we don't need DRUPAL_DIR anymore, because we're using drush to bootstrap a drupal. --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index c61dbb5..5c7108f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -57,7 +57,6 @@ install: - composer drupal-update - drush cr - - export DRUPAL_DIR=$(drush dd) script: - cd modules/drupal-netsmtp/src/Tests/Integration && drush scr runtests.php