From 74b840a6ccc97f0fcdda44cddffe9243235f3263 Mon Sep 17 00:00:00 2001 From: Data-Kiss <39807751+Data-Kiss@users.noreply.github.com> Date: Wed, 2 Oct 2019 21:26:07 +0100 Subject: [PATCH 1/4] Add username to the dashboard of who logged an incident or any incident updates. --- resources/lang/en/cachet.php | 2 +- resources/lang/en/dashboard.php | 2 +- resources/views/dashboard/incidents/index.blade.php | 2 +- resources/views/dashboard/incidents/updates/index.blade.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/resources/lang/en/cachet.php b/resources/lang/en/cachet.php index b15c7ad7b577..cb24fc10e7c8 100644 --- a/resources/lang/en/cachet.php +++ b/resources/lang/en/cachet.php @@ -34,7 +34,7 @@ 'stickied' => 'Stickied Incidents', 'scheduled' => 'Maintenance', 'scheduled_at' => ', scheduled :timestamp', - 'posted' => 'Posted :timestamp', + 'posted' => 'Posted :timestamp by :username', 'posted_at' => 'Posted at :timestamp', 'status' => [ 1 => 'Investigating', diff --git a/resources/lang/en/dashboard.php b/resources/lang/en/dashboard.php index 3f70a3685e87..7dc9af669deb 100644 --- a/resources/lang/en/dashboard.php +++ b/resources/lang/en/dashboard.php @@ -35,7 +35,7 @@ 'failure' => 'Something went wrong updating the incident update', ], ], - 'reported_by' => 'Reported by :user', + 'reported_by' => 'Reported :timestamp by :user', 'add' => [ 'title' => 'Report an incident', 'success' => 'Incident added.', diff --git a/resources/views/dashboard/incidents/index.blade.php b/resources/views/dashboard/incidents/index.blade.php index 14af90f0d827..69103049676e 100644 --- a/resources/views/dashboard/incidents/index.blade.php +++ b/resources/views/dashboard/incidents/index.blade.php @@ -27,7 +27,7 @@

{{ Str::words($incident->message, 5) }}

@endif @if ($incident->user) -

— {{ trans('dashboard.incidents.reported_by', ['user' => $incident->user->username]) }}

+

— {{ trans('dashboard.incidents.reported_by', ['timestamp' => $incident->created_at_diff, 'user' => $incident->user->username]) }}

@endif
diff --git a/resources/views/dashboard/incidents/updates/index.blade.php b/resources/views/dashboard/incidents/updates/index.blade.php index bf48c9181a43..8220a7de50ba 100644 --- a/resources/views/dashboard/incidents/updates/index.blade.php +++ b/resources/views/dashboard/incidents/updates/index.blade.php @@ -26,7 +26,7 @@
{{ Str::words($update->message, 8) }} -

{{ trans('cachet.incidents.posted', ['timestamp' => $update->created_at_diff]) }}

+

{{ trans('cachet.incidents.posted', ['timestamp' => $update->created_at_diff, 'username' => $update->user->username]) }}

From 87d4e33632836aeb572e7cc8ac6058fb6bde7132 Mon Sep 17 00:00:00 2001 From: Data-Kiss <39807751+Data-Kiss@users.noreply.github.com> Date: Wed, 9 Oct 2019 19:04:58 +0100 Subject: [PATCH 2/4] WIP --- .../Incident/NewIncidentNotification.php | 14 ++- tests/Functional/Notifications/MailTest.php | 92 +++++++++++++++++++ 2 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 tests/Functional/Notifications/MailTest.php diff --git a/app/Notifications/Incident/NewIncidentNotification.php b/app/Notifications/Incident/NewIncidentNotification.php index c0f956b298b7..ab5277dcc6a5 100644 --- a/app/Notifications/Incident/NewIncidentNotification.php +++ b/app/Notifications/Incident/NewIncidentNotification.php @@ -72,7 +72,19 @@ public function toMail($notifiable) $content = trans('notifications.incident.new.mail.content', [ 'name' => $this->incident->name, ]); - + + dd(new MailMessage()) + ->subject(trans('notifications.incident.new.mail.subject')) + ->markdown('notifications.incident.new', [ + 'incident' => $this->incident, + 'content' => $content, + 'actionText' => trans('notifications.incident.new.mail.action'), + 'actionUrl' => cachet_route('incident', [$this->incident]), + 'unsubscribeText' => trans('cachet.subscriber.unsubscribe'), + 'unsubscribeUrl' => cachet_route('subscribe.unsubscribe', $notifiable->verify_code), + 'manageSubscriptionText' => trans('cachet.subscriber.manage_subscription'), + 'manageSubscriptionUrl' => cachet_route('subscribe.manage', $notifiable->verify_code), + ]); 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..e302180c636b --- /dev/null +++ b/tests/Functional/Notifications/MailTest.php @@ -0,0 +1,92 @@ +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); + } + + public function createSubscriber() + { + dispatch(new SubscribeSubscriberCommand( + $this->fakerFactory->safeEmail, + true + )); + } + + /** + * Send a notification to subscribers when a new incident + * is added. + */ + public function testNotificationSentForNewIncident() + { + Mail::fake(); + + $this->signIn(); + $this->createSubscriber(); + + $response = $this->post('dashboard/incidents/create', [ + 'name' => $this->fakerFactory->word, + 'status' => 1, + 'visible' => 1, + 'message' => $this->fakerFactory->paragraph, + 'notify' => 1 + ]); + + Mail::assertSent(NewIncidentNotification::class); + } +} \ No newline at end of file From 509e7d98da37aa9e23f6e948a44326f119b2e857 Mon Sep 17 00:00:00 2001 From: Data-Kiss <39807751+Data-Kiss@users.noreply.github.com> Date: Thu, 10 Oct 2019 20:04:28 +0100 Subject: [PATCH 3/4] WIP - Tests for notifications --- .../Incident/NewIncidentNotification.php | 14 +-- tests/Functional/Notifications/MailTest.php | 116 ++++++++++++++++-- 2 files changed, 108 insertions(+), 22 deletions(-) diff --git a/app/Notifications/Incident/NewIncidentNotification.php b/app/Notifications/Incident/NewIncidentNotification.php index ab5277dcc6a5..1b5c76372df1 100644 --- a/app/Notifications/Incident/NewIncidentNotification.php +++ b/app/Notifications/Incident/NewIncidentNotification.php @@ -72,19 +72,7 @@ public function toMail($notifiable) $content = trans('notifications.incident.new.mail.content', [ 'name' => $this->incident->name, ]); - - dd(new MailMessage()) - ->subject(trans('notifications.incident.new.mail.subject')) - ->markdown('notifications.incident.new', [ - 'incident' => $this->incident, - 'content' => $content, - 'actionText' => trans('notifications.incident.new.mail.action'), - 'actionUrl' => cachet_route('incident', [$this->incident]), - 'unsubscribeText' => trans('cachet.subscriber.unsubscribe'), - 'unsubscribeUrl' => cachet_route('subscribe.unsubscribe', $notifiable->verify_code), - 'manageSubscriptionText' => trans('cachet.subscriber.manage_subscription'), - 'manageSubscriptionUrl' => cachet_route('subscribe.manage', $notifiable->verify_code), - ]); + 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 index e302180c636b..3ab8d3a47954 100644 --- a/tests/Functional/Notifications/MailTest.php +++ b/tests/Functional/Notifications/MailTest.php @@ -15,7 +15,13 @@ use Illuminate\Foundation\Testing\DatabaseMigrations; use CachetHQ\Cachet\Settings\Repository as SettingsRepository; use CachetHQ\Cachet\Bus\Commands\Subscriber\SubscribeSubscriberCommand; -use Mail; +use CachetHQ\Cachet\Bus\Commands\Incident\CreateIncidentCommand; +use CachetHQ\Cachet\Notifications\Incident\NewIncidentNotification; +use CachetHQ\Cachet\Notifications\IncidentUpdate\IncidentUpdatedNotification; +use CachetHQ\Cachet\Models\Subscriber; +use CachetHQ\Cachet\Models\Incident; +use Illuminate\Support\Facades\Notification; +use Illuminate\Support\Str; class MailTest extends AbstractTestCase { @@ -31,6 +37,11 @@ class MailTest extends AbstractTestCase */ protected $appName; + /** + * @var array[]|null + */ + protected $incidents; + /** * @var string */ @@ -58,27 +69,65 @@ 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)], + ]; } - public function createSubscriber() + /** + * Create a new subscriber. + */ + public function createSubscriber($subscriberEmail) { dispatch(new SubscribeSubscriberCommand( - $this->fakerFactory->safeEmail, + $subscriberEmail, true )); + + return Subscriber::where('email', '=', $subscriberEmail)->firstOrFail(); } /** - * Send a notification to subscribers when a new incident + * @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 testNotificationSentForNewIncident() + public function testEmailNotificationSentForNewIncident() { - Mail::fake(); + Notification::fake(); $this->signIn(); - $this->createSubscriber(); + $subscriber = $this->createSubscriber($this->fakerFactory->safeEmail); + $response = $this->post('dashboard/incidents/create', [ 'name' => $this->fakerFactory->word, 'status' => 1, @@ -86,7 +135,56 @@ public function testNotificationSentForNewIncident() 'message' => $this->fakerFactory->paragraph, 'notify' => 1 ]); - - Mail::assertSent(NewIncidentNotification::class); + + 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 From f22e4c531ac85e33b67a8375fad3eeb407bfd551 Mon Sep 17 00:00:00 2001 From: Data-Kiss <39807751+Data-Kiss@users.noreply.github.com> Date: Thu, 10 Oct 2019 20:16:26 +0100 Subject: [PATCH 4/4] Fix some style inconsistencies. --- tests/Functional/Notifications/MailTest.php | 32 ++++++++++++--------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/tests/Functional/Notifications/MailTest.php b/tests/Functional/Notifications/MailTest.php index 3ab8d3a47954..364d37bc30b5 100644 --- a/tests/Functional/Notifications/MailTest.php +++ b/tests/Functional/Notifications/MailTest.php @@ -11,15 +11,15 @@ namespace CachetHQ\Tests\Cachet\Functional\Notifications; -use CachetHQ\Tests\Cachet\AbstractTestCase; -use Illuminate\Foundation\Testing\DatabaseMigrations; -use CachetHQ\Cachet\Settings\Repository as SettingsRepository; -use CachetHQ\Cachet\Bus\Commands\Subscriber\SubscribeSubscriberCommand; use CachetHQ\Cachet\Bus\Commands\Incident\CreateIncidentCommand; +use CachetHQ\Cachet\Bus\Commands\Subscriber\SubscribeSubscriberCommand; +use CachetHQ\Cachet\Models\Incident; +use CachetHQ\Cachet\Models\Subscriber; use CachetHQ\Cachet\Notifications\Incident\NewIncidentNotification; use CachetHQ\Cachet\Notifications\IncidentUpdate\IncidentUpdatedNotification; -use CachetHQ\Cachet\Models\Subscriber; -use CachetHQ\Cachet\Models\Incident; +use CachetHQ\Cachet\Settings\Repository as SettingsRepository; +use CachetHQ\Tests\Cachet\AbstractTestCase; +use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Support\Facades\Notification; use Illuminate\Support\Str; @@ -99,6 +99,7 @@ protected function createIncident(array $incident) $name = $incident['title']; $message = $incident['description']; + dispatch(new CreateIncidentCommand( $name, $this->fakerFactory->numberBetween(0, 3), @@ -127,13 +128,14 @@ public function testEmailNotificationSentForNewIncident() $this->signIn(); $subscriber = $this->createSubscriber($this->fakerFactory->safeEmail); - + + $response = $this->post('dashboard/incidents/create', [ - 'name' => $this->fakerFactory->word, - 'status' => 1, + 'name' => $this->fakerFactory->word, + 'status' => 1, 'visible' => 1, 'message' => $this->fakerFactory->paragraph, - 'notify' => 1 + 'notify' => 1 ]); Notification::assertSentTo( @@ -153,11 +155,11 @@ public function testEmailNotificationNotSentWhenNotifyNotCheckedForNewIncident() $subscriber = $this->createSubscriber($this->fakerFactory->safeEmail); $response = $this->post('dashboard/incidents/create', [ - 'name' => $this->fakerFactory->word, - 'status' => 1, + 'name' => $this->fakerFactory->word, + 'status' => 1, 'visible' => 1, 'message' => $this->fakerFactory->paragraph, - 'notify' => 0 + 'notify' => 0 ]); Notification::assertNotSentTo( @@ -173,13 +175,15 @@ 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, + 'status' => 1, 'message' => $this->fakerFactory->paragraph, ]);