diff --git a/app/Notifications/Incident/NewIncidentNotification.php b/app/Notifications/Incident/NewIncidentNotification.php index c0f956b298b7..1b5c76372df1 100644 --- a/app/Notifications/Incident/NewIncidentNotification.php +++ b/app/Notifications/Incident/NewIncidentNotification.php @@ -72,7 +72,7 @@ public function toMail($notifiable) $content = trans('notifications.incident.new.mail.content', [ 'name' => $this->incident->name, ]); - + return (new MailMessage()) ->subject(trans('notifications.incident.new.mail.subject')) ->markdown('notifications.incident.new', [ diff --git a/tests/Functional/Notifications/MailTest.php b/tests/Functional/Notifications/MailTest.php new file mode 100644 index 000000000000..364d37bc30b5 --- /dev/null +++ b/tests/Functional/Notifications/MailTest.php @@ -0,0 +1,194 @@ +fakerFactory = \Faker\Factory::create(); + $this->appName = 'MailTest'; + } + + /** + * Setup the application. + */ + public function setUp() + { + parent::setUp(); + $this->app->make(SettingsRepository::class)->set('app_name', $this->appName); + $this->app->config->set('setting.app_name', $this->appName); + $this->incidents = [ + ['title' => 'Foo '.Str::random(16), 'description' => 'Foo Bar Baz '.Str::random(32)], + ['title' => 'Foe '.Str::random(16), 'description' => 'Foe Baz Bar '.Str::random(32)], + ]; + } + + /** + * Create a new subscriber. + */ + public function createSubscriber($subscriberEmail) + { + dispatch(new SubscribeSubscriberCommand( + $subscriberEmail, + true + )); + + return Subscriber::where('email', '=', $subscriberEmail)->firstOrFail(); + } + + /** + * @param array $incident + * @param array $meta + * + * @return Incident + */ + protected function createIncident(array $incident) + { + $name = $incident['title']; + $message = $incident['description']; + + + dispatch(new CreateIncidentCommand( + $name, + $this->fakerFactory->numberBetween(0, 3), + $message, + true, + null, + null, + true, + true, + $this->fakerFactory->date('Y-m-d H:i'), + null, + [] + )); + + return Incident::where('name', '=', $name)->where('message', '=', $message)->firstOrFail(); + } + + /** + * Send an email notification to subscribers when a new incident + * is added. + */ + public function testEmailNotificationSentForNewIncident() + { + Notification::fake(); + + $this->signIn(); + + $subscriber = $this->createSubscriber($this->fakerFactory->safeEmail); + + + $response = $this->post('dashboard/incidents/create', [ + 'name' => $this->fakerFactory->word, + 'status' => 1, + 'visible' => 1, + 'message' => $this->fakerFactory->paragraph, + 'notify' => 1 + ]); + + Notification::assertSentTo( + [$subscriber], NewIncidentNotification::class + ); + } + + /** + * Do not send an email if notify not checked. + */ + public function testEmailNotificationNotSentWhenNotifyNotCheckedForNewIncident() + { + Notification::fake(); + + $this->signIn(); + + $subscriber = $this->createSubscriber($this->fakerFactory->safeEmail); + + $response = $this->post('dashboard/incidents/create', [ + 'name' => $this->fakerFactory->word, + 'status' => 1, + 'visible' => 1, + 'message' => $this->fakerFactory->paragraph, + 'notify' => 0 + ]); + + Notification::assertNotSentTo( + [$subscriber], NewIncidentNotification::class + ); + } + + /** + * Send an email notification to subscribers when an incident + * update is added. + */ + public function testEmailNotificationSentForIncidentUpdate() + { + Notification::fake(); + + + $this->signIn(); + + $incident = $this->createIncident($this->incidents[1]); + $subscriber = $this->createSubscriber($this->fakerFactory->safeEmail); + + + $response = $this->post('dashboard/incidents/'.$incident->id.'/updates/create', [ + 'status' => 1, + 'message' => $this->fakerFactory->paragraph, + ]); + + Notification::assertSentTo( + [$subscriber], IncidentUpdatedNotification::class + ); + } +} \ No newline at end of file