From 49386b42dae9ccd3d446d1819141ddd56676e3f7 Mon Sep 17 00:00:00 2001
From: Dan Brown <ssddanbrown@googlemail.com>
Date: Sat, 15 Feb 2020 14:13:15 +0000
Subject: [PATCH] Updated email test send to show error on failure

- Added test to cover
- Closes #1874
---
 app/Http/Controllers/SettingController.php | 10 ++++++++--
 resources/lang/en/errors.php               |  3 +++
 tests/TestEmailTest.php                    | 19 +++++++++++++++++++
 3 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/app/Http/Controllers/SettingController.php b/app/Http/Controllers/SettingController.php
index 892b2d9cf..00dd60ac7 100644
--- a/app/Http/Controllers/SettingController.php
+++ b/app/Http/Controllers/SettingController.php
@@ -122,8 +122,14 @@ class SettingController extends Controller
     {
         $this->checkPermission('settings-manage');
 
-        user()->notify(new TestEmail());
-        $this->showSuccessNotification(trans('settings.maint_send_test_email_success', ['address' => user()->email]));
+        try {
+            user()->notify(new TestEmail());
+            $this->showSuccessNotification(trans('settings.maint_send_test_email_success', ['address' => user()->email]));
+        } catch (\Exception $exception) {
+            $errorMessage = trans('errors.maintenance_test_email_failure') . "\n" . $exception->getMessage();
+            $this->showErrorNotification($errorMessage);
+        }
+
 
         return redirect('/settings/maintenance#image-cleanup')->withInput();
     }
diff --git a/resources/lang/en/errors.php b/resources/lang/en/errors.php
index 4752d8b0c..38f1ce28a 100644
--- a/resources/lang/en/errors.php
+++ b/resources/lang/en/errors.php
@@ -96,4 +96,7 @@ return [
     'api_user_no_api_permission' => 'The owner of the used API token does not have permission to make API calls',
     'api_user_token_expired' => 'The authorization token used has expired',
 
+    // Settings & Maintenance
+    'maintenance_test_email_failure' => 'Error thrown when sending a test email:',
+
 ];
diff --git a/tests/TestEmailTest.php b/tests/TestEmailTest.php
index c06311d84..76ff322ff 100644
--- a/tests/TestEmailTest.php
+++ b/tests/TestEmailTest.php
@@ -1,6 +1,7 @@
 <?php namespace Tests;
 
 use BookStack\Notifications\TestEmail;
+use Illuminate\Contracts\Notifications\Dispatcher;
 use Illuminate\Support\Facades\Notification;
 
 class TestEmailTest extends TestCase
@@ -26,6 +27,24 @@ class TestEmailTest extends TestCase
         Notification::assertSentTo($admin, TestEmail::class);
     }
 
+    public function test_send_test_email_failure_displays_error_notification()
+    {
+        $mockDispatcher = $this->mock(Dispatcher::class);
+        $this->app[Dispatcher::class] = $mockDispatcher;
+
+        $exception = new \Exception('A random error occurred when testing an email');
+        $mockDispatcher->shouldReceive('send')->andThrow($exception);
+
+        $admin = $this->getAdmin();
+        $sendReq = $this->actingAs($admin)->post('/settings/maintenance/send-test-email');
+        $sendReq->assertRedirect('/settings/maintenance#image-cleanup');
+        $this->assertSessionHas('error');
+
+        $message = session()->get('error');
+        $this->assertStringContainsString('Error thrown when sending a test email:', $message);
+        $this->assertStringContainsString('A random error occurred when testing an email', $message);
+    }
+
     public function test_send_test_email_requires_settings_manage_permission()
     {
         Notification::fake();