3 use BookStack\Notifications\TestEmail;
4 use Illuminate\Contracts\Notifications\Dispatcher;
5 use Illuminate\Support\Facades\Notification;
7 class TestEmailTest extends TestCase
10 public function test_a_send_test_button_shows()
12 $pageView = $this->asAdmin()->get('/settings/maintenance');
13 $formCssSelector = 'form[action$="/settings/maintenance/send-test-email"]';
14 $pageView->assertElementExists($formCssSelector);
15 $pageView->assertElementContains($formCssSelector . ' button', 'Send Test Email');
18 public function test_send_test_email_endpoint_sends_email_and_redirects_user_and_shows_notification()
21 $admin = $this->getAdmin();
23 $sendReq = $this->actingAs($admin)->post('/settings/maintenance/send-test-email');
24 $sendReq->assertRedirect('/settings/maintenance#image-cleanup');
25 $this->assertSessionHas('success', 'Email sent to ' . $admin->email);
27 Notification::assertSentTo($admin, TestEmail::class);
30 public function test_send_test_email_failure_displays_error_notification()
32 $mockDispatcher = $this->mock(Dispatcher::class);
33 $this->app[Dispatcher::class] = $mockDispatcher;
35 $exception = new \Exception('A random error occurred when testing an email');
36 $mockDispatcher->shouldReceive('send')->andThrow($exception);
38 $admin = $this->getAdmin();
39 $sendReq = $this->actingAs($admin)->post('/settings/maintenance/send-test-email');
40 $sendReq->assertRedirect('/settings/maintenance#image-cleanup');
41 $this->assertSessionHas('error');
43 $message = session()->get('error');
44 $this->assertStringContainsString('Error thrown when sending a test email:', $message);
45 $this->assertStringContainsString('A random error occurred when testing an email', $message);
48 public function test_send_test_email_requires_settings_manage_permission()
51 $user = $this->getViewer();
53 $sendReq = $this->actingAs($user)->post('/settings/maintenance/send-test-email');
54 Notification::assertNothingSent();
56 $this->giveUserPermissions($user, ['settings-manage']);
57 $sendReq = $this->actingAs($user)->post('/settings/maintenance/send-test-email');
58 Notification::assertSentTo($user, TestEmail::class);