From e174df1405f4155a477ced68c698eb324e73c07c Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Tue, 16 Feb 2016 14:39:03 -0500
Subject: [PATCH] Only simulate emailing during tests.

---
 config/vufind/config.ini                      |  3 ++
 module/VuFind/src/VuFind/Mailer/Factory.php   | 33 ++++++++++++++-----
 .../src/VuFindTest/Mink/RecordActionsTest.php | 14 ++++++--
 3 files changed, 39 insertions(+), 11 deletions(-)

diff --git a/config/vufind/config.ini b/config/vufind/config.ini
index dd0eebe0be9..50906ba0f16 100644
--- a/config/vufind/config.ini
+++ b/config/vufind/config.ini
@@ -407,6 +407,9 @@ port            = 25
 ; connection. If no explicit protocol ('tls' or 'ssl') is configured, a protocol
 ; based on the configured port is chosen (587 -> tls, 487 -> ssl).
 ;secure         = tls
+; Uncomment this setting to disable outbound mail but simulate success; this
+; is useful for interface testing but should never be used in production!
+;testOnly = true
 ; If set to false, users can send anonymous emails; otherwise, they must log in first
 require_login   = true
 ; Should we put the logged-in user's address in the "from" field by default?
diff --git a/module/VuFind/src/VuFind/Mailer/Factory.php b/module/VuFind/src/VuFind/Mailer/Factory.php
index 46ca1d9c396..11617a5ed22 100644
--- a/module/VuFind/src/VuFind/Mailer/Factory.php
+++ b/module/VuFind/src/VuFind/Mailer/Factory.php
@@ -26,6 +26,7 @@
  * @link     http://vufind.org/wiki/vufind2:developer_manual Wiki
  */
 namespace VuFind\Mailer;
+use Zend\Mail\Transport\InMemory;
 use Zend\Mail\Transport\Smtp, Zend\Mail\Transport\SmtpOptions;
 use Zend\ServiceManager\ServiceLocatorInterface;
 
@@ -43,16 +44,18 @@ use Zend\ServiceManager\ServiceLocatorInterface;
 class Factory implements \Zend\ServiceManager\FactoryInterface
 {
     /**
-     * Create service
+     * Build the mail transport object.
      *
-     * @param ServiceLocatorInterface $sm Service manager
+     * @param \Zend\Config\Config $config Configuration
      *
-     * @return mixed
+     * @return InMemory|Smtp
      */
-    public function createService(ServiceLocatorInterface $sm)
+    protected function getTransport($config)
     {
-        // Load configurations:
-        $config = $sm->get('VuFind\Config')->get('config');
+        // In test mode? Return fake object:
+        if (isset($config->Mail->testOnly) && $config->Mail->testOnly) {
+            return new InMemory();
+        }
 
         // Create mail transport:
         $settings = [
@@ -76,10 +79,22 @@ class Factory implements \Zend\ServiceManager\FactoryInterface
                 }
             }
         }
-        $transport = new Smtp();
-        $transport->setOptions(new SmtpOptions($settings));
+        return new Smtp(new SmtpOptions($settings));
+    }
+
+    /**
+     * Create service
+     *
+     * @param ServiceLocatorInterface $sm Service manager
+     *
+     * @return mixed
+     */
+    public function createService(ServiceLocatorInterface $sm)
+    {
+        // Load configurations:
+        $config = $sm->get('VuFind\Config')->get('config');
 
         // Create service:
-        return new \VuFind\Mailer\Mailer($transport);
+        return new \VuFind\Mailer\Mailer($this->getTransport($config));
     }
 }
diff --git a/module/VuFind/tests/integration-tests/src/VuFindTest/Mink/RecordActionsTest.php b/module/VuFind/tests/integration-tests/src/VuFindTest/Mink/RecordActionsTest.php
index 10e5d92f32e..1c48074b786 100644
--- a/module/VuFind/tests/integration-tests/src/VuFindTest/Mink/RecordActionsTest.php
+++ b/module/VuFind/tests/integration-tests/src/VuFindTest/Mink/RecordActionsTest.php
@@ -242,7 +242,12 @@ class RecordActionsTest extends \VuFindTest\Unit\MinkTestCase
     {
         // Change the theme:
         $this->changeConfigs(
-            ['config' => ['Site' => ['theme' => 'bootstrap3']]]
+            [
+                'config' => [
+                    'Site' => ['theme' => 'bootstrap3'],
+                    'Mail' => ['testOnly' => 1],
+                ]
+            ]
         );
 
         // Go to a record view
@@ -295,7 +300,12 @@ class RecordActionsTest extends \VuFindTest\Unit\MinkTestCase
     {
         // Change the theme:
         $this->changeConfigs(
-            ['config' => ['Site' => ['theme' => 'bootstrap3']]]
+            [
+                'config' => [
+                    'Site' => ['theme' => 'bootstrap3'],
+                    'Mail' => ['testOnly' => 1],
+                ]
+            ]
         );
 
         // Go to a record view
-- 
GitLab