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