]> BookStack Code Mirror - bookstack/blob - tests/Settings/TestEmailTest.php
Vectors: Added command to regenerate for all
[bookstack] / tests / Settings / TestEmailTest.php
1 <?php
2
3 namespace Tests\Settings;
4
5 use BookStack\Settings\TestEmailNotification;
6 use Illuminate\Contracts\Notifications\Dispatcher;
7 use Illuminate\Support\Facades\Notification;
8 use Tests\TestCase;
9
10 class TestEmailTest extends TestCase
11 {
12     public function test_a_send_test_button_shows()
13     {
14         $pageView = $this->asAdmin()->get('/settings/maintenance');
15         $formCssSelector = 'form[action$="/settings/maintenance/send-test-email"]';
16         $this->withHtml($pageView)->assertElementExists($formCssSelector);
17         $this->withHtml($pageView)->assertElementContains($formCssSelector . ' button', 'Send Test Email');
18     }
19
20     public function test_send_test_email_endpoint_sends_email_and_redirects_user_and_shows_notification()
21     {
22         Notification::fake();
23         $admin = $this->users->admin();
24
25         $sendReq = $this->actingAs($admin)->post('/settings/maintenance/send-test-email');
26         $sendReq->assertRedirect('/settings/maintenance#image-cleanup');
27         $this->assertSessionHas('success', 'Email sent to ' . $admin->email);
28
29         Notification::assertSentTo($admin, TestEmailNotification::class);
30     }
31
32     public function test_send_test_email_failure_displays_error_notification()
33     {
34         $mockDispatcher = $this->mock(Dispatcher::class);
35         $this->app[Dispatcher::class] = $mockDispatcher;
36
37         $exception = new \Exception('A random error occurred when testing an email');
38         $mockDispatcher->shouldReceive('sendNow')->andThrow($exception);
39
40         $admin = $this->users->admin();
41         $sendReq = $this->actingAs($admin)->post('/settings/maintenance/send-test-email');
42         $sendReq->assertRedirect('/settings/maintenance#image-cleanup');
43         $this->assertSessionHas('error');
44
45         $message = session()->get('error');
46         $this->assertStringContainsString('Error thrown when sending a test email:', $message);
47         $this->assertStringContainsString('A random error occurred when testing an email', $message);
48     }
49
50     public function test_send_test_email_requires_settings_manage_permission()
51     {
52         Notification::fake();
53         $user = $this->users->viewer();
54
55         $sendReq = $this->actingAs($user)->post('/settings/maintenance/send-test-email');
56         Notification::assertNothingSent();
57
58         $this->permissions->grantUserRolePermissions($user, ['settings-manage']);
59         $sendReq = $this->actingAs($user)->post('/settings/maintenance/send-test-email');
60         Notification::assertSentTo($user, TestEmailNotification::class);
61     }
62 }