| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-30 00:30:26 +08:00
										 |  |  | namespace Tests\Settings; | 
					
						
							| 
									
										
										
										
											2019-10-24 03:25:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-12 02:26:28 +08:00
										 |  |  | use BookStack\Settings\TestEmailNotification; | 
					
						
							| 
									
										
										
										
											2020-02-15 22:13:15 +08:00
										 |  |  | use Illuminate\Contracts\Notifications\Dispatcher; | 
					
						
							| 
									
										
										
										
											2019-10-24 03:25:51 +08:00
										 |  |  | use Illuminate\Support\Facades\Notification; | 
					
						
							| 
									
										
										
										
											2022-08-30 00:30:26 +08:00
										 |  |  | use Tests\TestCase; | 
					
						
							| 
									
										
										
										
											2019-10-24 03:25:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class TestEmailTest extends TestCase | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     public function test_a_send_test_button_shows() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $pageView = $this->asAdmin()->get('/settings/maintenance'); | 
					
						
							|  |  |  |         $formCssSelector = 'form[action$="/settings/maintenance/send-test-email"]'; | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $this->withHtml($pageView)->assertElementExists($formCssSelector); | 
					
						
							|  |  |  |         $this->withHtml($pageView)->assertElementContains($formCssSelector . ' button', 'Send Test Email'); | 
					
						
							| 
									
										
										
										
											2019-10-24 03:25:51 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_send_test_email_endpoint_sends_email_and_redirects_user_and_shows_notification() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         Notification::fake(); | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |         $admin = $this->users->admin(); | 
					
						
							| 
									
										
										
										
											2019-10-24 03:25:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $sendReq = $this->actingAs($admin)->post('/settings/maintenance/send-test-email'); | 
					
						
							|  |  |  |         $sendReq->assertRedirect('/settings/maintenance#image-cleanup'); | 
					
						
							|  |  |  |         $this->assertSessionHas('success', 'Email sent to ' . $admin->email); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-12 02:26:28 +08:00
										 |  |  |         Notification::assertSentTo($admin, TestEmailNotification::class); | 
					
						
							| 
									
										
										
										
											2019-10-24 03:25:51 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-15 22:13:15 +08:00
										 |  |  |     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'); | 
					
						
							| 
									
										
										
										
											2021-12-19 00:41:42 +08:00
										 |  |  |         $mockDispatcher->shouldReceive('sendNow')->andThrow($exception); | 
					
						
							| 
									
										
										
										
											2020-02-15 22:13:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |         $admin = $this->users->admin(); | 
					
						
							| 
									
										
										
										
											2020-02-15 22:13:15 +08:00
										 |  |  |         $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); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-24 03:25:51 +08:00
										 |  |  |     public function test_send_test_email_requires_settings_manage_permission() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         Notification::fake(); | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |         $user = $this->users->viewer(); | 
					
						
							| 
									
										
										
										
											2019-10-24 03:25:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $sendReq = $this->actingAs($user)->post('/settings/maintenance/send-test-email'); | 
					
						
							|  |  |  |         Notification::assertNothingSent(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |         $this->permissions->grantUserRolePermissions($user, ['settings-manage']); | 
					
						
							| 
									
										
										
										
											2019-10-24 03:25:51 +08:00
										 |  |  |         $sendReq = $this->actingAs($user)->post('/settings/maintenance/send-test-email'); | 
					
						
							| 
									
										
										
										
											2023-09-12 02:26:28 +08:00
										 |  |  |         Notification::assertSentTo($user, TestEmailNotification::class); | 
					
						
							| 
									
										
										
										
											2019-10-24 03:25:51 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | } |