From 5a5dcdab1776b9a65509b6c0388f3080d712cf74 Mon Sep 17 00:00:00 2001 From: Matt Inamdar Date: Fri, 6 Sep 2019 12:19:57 +0100 Subject: [PATCH] feature/organisation-sign-up-form-notifications (#252) * Added emails for new org sign up form * Updated naming * Added emails and tests for approve and reject * Fixed recursion bug * Linted code * Using latst version of grokzen/redis-cluster * Refactored * Extracted redis cluster discovery IP for only mac envs --- app/Emails/Email.php | 3 + .../NotifySubmitterEmail.php | 40 ++++++ .../NotifyGlobalAdminEmail.php | 40 ++++++ .../NotifySubmitterEmail.php | 40 ++++++ .../NotifySubmitterEmail.php | 40 ++++++ .../NotifyGlobalAdminEmail.php | 2 +- .../NotifySubmitterEmail.php | 2 +- .../Notifications/UpdateRequestApproved.php | 74 +++++++--- .../Notifications/UpdateRequestRejected.php | 56 ++++++-- app/Models/UpdateRequest.php | 63 ++------- app/Observers/UpdateRequestObserver.php | 93 +++++++++---- config/ck.php | 20 ++- develop | 19 ++- docker-compose.mac.yml | 10 ++ docker-compose.yml | 8 +- .../UpdateRequestApprovedTest.php | 101 ++++++++++++-- .../UpdateRequestRejectedTest.php | 101 ++++++++++++-- .../Observers/UpdateRequestObserverTest.php | 131 +++++++++++++++--- 18 files changed, 678 insertions(+), 165 deletions(-) create mode 100644 app/Emails/OrganisationSignUpFormApproved/NotifySubmitterEmail.php create mode 100644 app/Emails/OrganisationSignUpFormReceived/NotifyGlobalAdminEmail.php create mode 100644 app/Emails/OrganisationSignUpFormReceived/NotifySubmitterEmail.php create mode 100644 app/Emails/OrganisationSignUpFormRejected/NotifySubmitterEmail.php create mode 100644 docker-compose.mac.yml diff --git a/app/Emails/Email.php b/app/Emails/Email.php index 276f3788..793bc6e1 100644 --- a/app/Emails/Email.php +++ b/app/Emails/Email.php @@ -85,6 +85,9 @@ abstract protected function getReplyTo(): ?string; */ abstract public function getContent(): string; + /** + * Send the email. + */ public function send() { $this->handle(resolve(EmailSender::class)); diff --git a/app/Emails/OrganisationSignUpFormApproved/NotifySubmitterEmail.php b/app/Emails/OrganisationSignUpFormApproved/NotifySubmitterEmail.php new file mode 100644 index 00000000..86b0de9b --- /dev/null +++ b/app/Emails/OrganisationSignUpFormApproved/NotifySubmitterEmail.php @@ -0,0 +1,40 @@ +getModel(); if ($updateRequest->isExisting()) { - $this->notifySubmitter($updateRequest); + $this->notifySubmitterForExisting($updateRequest); + } + + if ($updateRequest->isNew()) { + $this->notifySubmitterForNew($updateRequest); } } /** * @param \App\Models\UpdateRequest $updateRequest + * @throws \Exception */ - protected function notifySubmitter(UpdateRequest $updateRequest) + protected function notifySubmitterForExisting(UpdateRequest $updateRequest) { - $resourceName = null; - $resourceType = null; - if ($updateRequest->updateable instanceof Location) { - $resourceName = $updateRequest->updateable->address_line_1; + $resourceName = 'N/A'; + $resourceType = 'N/A'; + if ($updateRequest->getUpdateable() instanceof Location) { + $resourceName = $updateRequest->getUpdateable()->address_line_1; $resourceType = 'location'; - } elseif ($updateRequest->updateable instanceof Service) { - $resourceName = $updateRequest->updateable->name; + } elseif ($updateRequest->getUpdateable() instanceof Service) { + $resourceName = $updateRequest->getUpdateable()->name; $resourceType = 'service'; - } elseif ($updateRequest->updateable instanceof ServiceLocation) { - $resourceName = $updateRequest->updateable->name ?? $updateRequest->updateable->location->address_line_1; + } elseif ($updateRequest->getUpdateable() instanceof ServiceLocation) { + $resourceName = $updateRequest->getUpdateable()->name ?? $updateRequest->getUpdateable()->location->address_line_1; $resourceType = 'service location'; - } elseif ($updateRequest->updateable instanceof Organisation) { - $resourceName = $updateRequest->updateable->name; + } elseif ($updateRequest->getUpdateable() instanceof Organisation) { + $resourceName = $updateRequest->getUpdateable()->name; $resourceType = 'organisation'; - } else { - $resourceName = 'N/A'; - $resourceType = 'N/A'; } - $updateRequest->user->sendEmail(new NotifySubmitterEmail($updateRequest->user->email, [ - 'SUBMITTER_NAME' => $updateRequest->user->first_name, - 'RESOURCE_NAME' => $resourceName, - 'RESOURCE_TYPE' => $resourceType, - 'REQUEST_DATE' => $updateRequest->created_at->format('j/n/Y'), - ])); + $updateRequest->user->sendEmail( + new \App\Emails\UpdateRequestApproved\NotifySubmitterEmail( + $updateRequest->user->email, + [ + 'SUBMITTER_NAME' => $updateRequest->user->first_name, + 'RESOURCE_NAME' => $resourceName, + 'RESOURCE_TYPE' => $resourceType, + 'REQUEST_DATE' => $updateRequest->created_at->format('j/n/Y'), + ] + ) + ); + } + + /** + * @param \App\Models\UpdateRequest $updateRequest + * @throws \Exception + */ + protected function notifySubmitterForNew(UpdateRequest $updateRequest) + { + if ($updateRequest->getUpdateable() instanceof OrganisationSignUpForm) { + Notification::sendEmail( + new \App\Emails\OrganisationSignUpFormApproved\NotifySubmitterEmail( + Arr::get($updateRequest->data, 'user.email'), + [ + 'SUBMITTER_NAME' => Arr::get($updateRequest->data, 'user.first_name'), + 'ORGANISATION_NAME' => Arr::get($updateRequest->data, 'organisation.name'), + 'REQUEST_DATE' => $updateRequest->created_at->format('j/n/Y'), + ] + ) + ); + } } } diff --git a/app/Listeners/Notifications/UpdateRequestRejected.php b/app/Listeners/Notifications/UpdateRequestRejected.php index 678ecf64..1793208e 100644 --- a/app/Listeners/Notifications/UpdateRequestRejected.php +++ b/app/Listeners/Notifications/UpdateRequestRejected.php @@ -6,10 +6,13 @@ use App\Events\EndpointHit; use App\Models\Audit; use App\Models\Location; +use App\Models\Notification; use App\Models\Organisation; use App\Models\Service; use App\Models\ServiceLocation; use App\Models\UpdateRequest; +use App\UpdateRequest\OrganisationSignUpForm; +use Illuminate\Support\Arr; class UpdateRequestRejected { @@ -17,6 +20,7 @@ class UpdateRequestRejected * Handle the event. * * @param EndpointHit $event + * @throws \Exception */ public function handle(EndpointHit $event) { @@ -29,32 +33,34 @@ public function handle(EndpointHit $event) $updateRequest = $event->getModel(); if ($updateRequest->isExisting()) { - $this->notifySubmitter($updateRequest); + $this->notifySubmitterForExisting($updateRequest); + } + + if ($updateRequest->isNew()) { + $this->notifySubmitterForNew($updateRequest); } } /** * @param \App\Models\UpdateRequest $updateRequest + * @throws \Exception */ - protected function notifySubmitter(UpdateRequest $updateRequest) + protected function notifySubmitterForExisting(UpdateRequest $updateRequest) { - $resourceName = null; - $resourceType = null; - if ($updateRequest->updateable instanceof Location) { - $resourceName = $updateRequest->updateable->address_line_1; + $resourceName = 'N/A'; + $resourceType = 'N/A'; + if ($updateRequest->getUpdateable() instanceof Location) { + $resourceName = $updateRequest->getUpdateable()->address_line_1; $resourceType = 'location'; - } elseif ($updateRequest->updateable instanceof Service) { - $resourceName = $updateRequest->updateable->name; + } elseif ($updateRequest->getUpdateable() instanceof Service) { + $resourceName = $updateRequest->getUpdateable()->name; $resourceType = 'service'; - } elseif ($updateRequest->updateable instanceof ServiceLocation) { - $resourceName = $updateRequest->updateable->name ?? $updateRequest->updateable->location->address_line_1; + } elseif ($updateRequest->getUpdateable() instanceof ServiceLocation) { + $resourceName = $updateRequest->getUpdateable()->name ?? $updateRequest->getUpdateable()->location->address_line_1; $resourceType = 'service location'; - } elseif ($updateRequest->updateable instanceof Organisation) { - $resourceName = $updateRequest->updateable->name; + } elseif ($updateRequest->getUpdateable() instanceof Organisation) { + $resourceName = $updateRequest->getUpdateable()->name; $resourceType = 'organisation'; - } else { - $resourceName = 'N/A'; - $resourceType = 'N/A'; } $updateRequest->user->sendEmail(new NotifySubmitterEmail($updateRequest->user->email, [ @@ -64,4 +70,24 @@ protected function notifySubmitter(UpdateRequest $updateRequest) 'REQUEST_DATE' => $updateRequest->created_at->format('j/n/Y'), ])); } + + /** + * @param \App\Models\UpdateRequest $updateRequest + * @throws \Exception + */ + protected function notifySubmitterForNew(UpdateRequest $updateRequest) + { + if ($updateRequest->getUpdateable() instanceof OrganisationSignUpForm) { + Notification::sendEmail( + new \App\Emails\OrganisationSignUpFormRejected\NotifySubmitterEmail( + Arr::get($updateRequest->data, 'user.email'), + [ + 'SUBMITTER_NAME' => Arr::get($updateRequest->data, 'user.first_name'), + 'ORGANISATION_NAME' => Arr::get($updateRequest->data, 'organisation.name'), + 'REQUEST_DATE' => $updateRequest->created_at->format('j/n/Y'), + ] + ) + ); + } + } } diff --git a/app/Models/UpdateRequest.php b/app/Models/UpdateRequest.php index 4c2a7834..76fe803d 100644 --- a/app/Models/UpdateRequest.php +++ b/app/Models/UpdateRequest.php @@ -6,7 +6,6 @@ use App\Models\Relationships\UpdateRequestRelationships; use App\Models\Scopes\UpdateRequestScopes; use App\UpdateRequest\AppliesUpdateRequests; -use Exception; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Date; @@ -58,80 +57,48 @@ public function isExisting(): bool } /** - * @throws \Exception * @return \Illuminate\Support\MessageBag */ public function getValidationErrors(): MessageBag { - if (!$this->updateable instanceof AppliesUpdateRequests) { - throw new Exception( - sprintf( - '[%s] must be an instance of %s', - get_class($this->updateable), - AppliesUpdateRequests::class - ) - ); - } - - return $this->updateable->validateUpdateRequest($this)->errors(); + return $this->getUpdateable()->validateUpdateRequest($this)->errors(); } /** - * @throws \Exception * @return bool */ public function validate(): bool { - $updateable = $this->isExisting() - ? $this->updateable - : $this->createUpdateableInstance($this->updateable_type); - - if (!$updateable instanceof AppliesUpdateRequests) { - throw new Exception( - sprintf( - '[%s] must be an instance of %s', - get_class($updateable), - AppliesUpdateRequests::class - ) - ); - } - - return $updateable->validateUpdateRequest($this)->fails() === false; + return $this->getUpdateable()->validateUpdateRequest($this)->fails() === false; } /** - * @throws \Exception * @return \App\Models\UpdateRequest */ public function apply(): self { - $updateable = $this->isExisting() - ? $this->updateable - : $this->createUpdateableInstance($this->updateable_type); - - if (!$updateable instanceof AppliesUpdateRequests) { - throw new Exception( - sprintf( - '[%s] must be an instance of %s', - get_class($updateable), - AppliesUpdateRequests::class - ) - ); - } - - $updateable->applyUpdateRequest($this); + $this->getUpdateable()->applyUpdateRequest($this); $this->update(['approved_at' => Date::now()]); return $this; } /** - * @param string $updateableType * @return \App\UpdateRequest\AppliesUpdateRequests */ - protected function createUpdateableInstance(string $updateableType): AppliesUpdateRequests + public function getUpdateable(): AppliesUpdateRequests + { + return $this->isExisting() + ? $this->updateable + : $this->createUpdateableInstance(); + } + + /** + * @return \App\UpdateRequest\AppliesUpdateRequests + */ + protected function createUpdateableInstance(): AppliesUpdateRequests { - $className = '\\App\\UpdateRequest\\' . Str::studly($updateableType); + $className = '\\App\\UpdateRequest\\' . Str::studly($this->updateable_type); return resolve($className); } diff --git a/app/Observers/UpdateRequestObserver.php b/app/Observers/UpdateRequestObserver.php index d6e78017..cf5e4f78 100644 --- a/app/Observers/UpdateRequestObserver.php +++ b/app/Observers/UpdateRequestObserver.php @@ -2,14 +2,13 @@ namespace App\Observers; -use App\Emails\UpdateRequestReceived\NotifyGlobalAdminEmail; -use App\Emails\UpdateRequestReceived\NotifySubmitterEmail; use App\Models\Location; use App\Models\Notification; use App\Models\Organisation; use App\Models\Service; use App\Models\ServiceLocation; use App\Models\UpdateRequest; +use App\UpdateRequest\OrganisationSignUpForm; use Illuminate\Support\Arr; use Illuminate\Support\Facades\DB; @@ -19,6 +18,7 @@ class UpdateRequestObserver * Handle to the update request "created" event. * * @param \App\Models\UpdateRequest $updateRequest + * @throws \Exception */ public function created(UpdateRequest $updateRequest) { @@ -27,6 +27,10 @@ public function created(UpdateRequest $updateRequest) $this->removeSameFieldsForPendingAndExisting($updateRequest); $this->deleteEmptyPendingForExisting($updateRequest); } + + if ($updateRequest->isNew()) { + $this->sendCreatedNotificationsForNew($updateRequest); + } } /** @@ -84,43 +88,80 @@ protected function deleteEmptyPendingForExisting(UpdateRequest $updateRequest) /** * @param \App\Models\UpdateRequest $updateRequest + * @throws \Exception */ protected function sendCreatedNotificationsForExisting(UpdateRequest $updateRequest) { - $resourceName = null; - $resourceType = null; - if ($updateRequest->updateable instanceof Location) { - $resourceName = $updateRequest->updateable->address_line_1; + $resourceName = 'N/A'; + $resourceType = 'N/A'; + if ($updateRequest->getUpdateable() instanceof Location) { + $resourceName = $updateRequest->getUpdateable()->address_line_1; $resourceType = 'location'; - } elseif ($updateRequest->updateable instanceof Service) { - $resourceName = $updateRequest->updateable->name; + } elseif ($updateRequest->getUpdateable() instanceof Service) { + $resourceName = $updateRequest->getUpdateable()->name; $resourceType = 'service'; - } elseif ($updateRequest->updateable instanceof ServiceLocation) { - $resourceName = $updateRequest->updateable->name ?? $updateRequest->updateable->location->address_line_1; + } elseif ($updateRequest->getUpdateable() instanceof ServiceLocation) { + $resourceName = $updateRequest->getUpdateable()->name ?? $updateRequest->getUpdateable()->location->address_line_1; $resourceType = 'service location'; - } elseif ($updateRequest->updateable instanceof Organisation) { - $resourceName = $updateRequest->updateable->name; + } elseif ($updateRequest->getUpdateable() instanceof Organisation) { + $resourceName = $updateRequest->getUpdateable()->name; $resourceType = 'organisation'; - } else { - $resourceName = 'N/A'; - $resourceType = 'N/A'; } // Send notification to the submitter. - $updateRequest->user->sendEmail(new NotifySubmitterEmail($updateRequest->user->email, [ - 'SUBMITTER_NAME' => $updateRequest->user->first_name, - 'RESOURCE_NAME' => $resourceName, - 'RESOURCE_TYPE' => $resourceType, - ])); + $updateRequest->user->sendEmail( + new \App\Emails\UpdateRequestReceived\NotifySubmitterEmail( + $updateRequest->user->email, + [ + 'SUBMITTER_NAME' => $updateRequest->user->first_name, + 'RESOURCE_NAME' => $resourceName, + 'RESOURCE_TYPE' => $resourceType, + ] + ) + ); // Send notification to the global admins. Notification::sendEmail( - new NotifyGlobalAdminEmail(config('ck.global_admin.email'), [ - 'RESOURCE_NAME' => $resourceName, - 'RESOURCE_TYPE' => $resourceType, - 'RESOURCE_ID' => $updateRequest->updateable_id, - 'REQUEST_URL' => backend_uri("/update-requests/{$updateRequest->id}"), - ]) + new \App\Emails\UpdateRequestReceived\NotifyGlobalAdminEmail( + config('ck.global_admin.email'), + [ + 'RESOURCE_NAME' => $resourceName, + 'RESOURCE_TYPE' => $resourceType, + 'RESOURCE_ID' => $updateRequest->updateable_id, + 'REQUEST_URL' => backend_uri("/update-requests/{$updateRequest->id}"), + ] + ) ); } + + /** + * @param \App\Models\UpdateRequest $updateRequest + * @throws \Exception + */ + protected function sendCreatedNotificationsForNew(UpdateRequest $updateRequest) + { + if ($updateRequest->getUpdateable() instanceof OrganisationSignUpForm) { + // Send notification to the submitter. + Notification::sendEmail( + new \App\Emails\OrganisationSignUpFormReceived\NotifySubmitterEmail( + Arr::get($updateRequest->data, 'user.email'), + [ + 'SUBMITTER_NAME' => Arr::get($updateRequest->data, 'user.first_name'), + 'ORGANISATION_NAME' => Arr::get($updateRequest->data, 'organisation.name'), + ] + ) + ); + + // Send notification to the global admins. + Notification::sendEmail( + new \App\Emails\OrganisationSignUpFormReceived\NotifyGlobalAdminEmail( + config('ck.global_admin.email'), + [ + 'ORGANISATION_NAME' => Arr::get($updateRequest->data, 'organisation.name'), + 'REQUEST_URL' => backend_uri("/update-requests/{$updateRequest->id}"), + ] + ) + ); + } + } } diff --git a/config/ck.php b/config/ck.php index 98a53f5a..cc797889 100644 --- a/config/ck.php +++ b/config/ck.php @@ -130,7 +130,7 @@ 'email' => '55af8b8f-1b36-4854-8cce-07d998fdd82a', ], ], - 'update_request_submitted' => [ + 'update_request_received' => [ 'notify_submitter' => [ 'email' => 'e6cd56cc-6259-4cc7-9568-7c48a4988abc', ], @@ -181,6 +181,24 @@ 'email' => 'fa6f064a-6b63-4dc0-a83d-602e9cbb5bfe', ], ], + 'organisation_sign_up_form_received' => [ + 'notify_submitter' => [ + 'email' => 'a6834de1-7144-4183-b3ce-edf481ef6953', + ], + 'notify_global_admin' => [ + 'email' => '0eb7828f-b28f-4c0c-acbe-62c7fbcc0be7', + ], + ], + 'organisation_sign_up_form_approved' => [ + 'notify_submitter' => [ + 'email' => '18815d90-b2b9-4205-9784-f0e05e801599', + ], + ], + 'organisation_sign_up_form_rejected' => [ + 'notify_submitter' => [ + 'email' => '6575fea8-5517-41fa-b78d-0c77cdc5d533', + ], + ], ], ]; diff --git a/develop b/develop index 5037d2ab..b0331f78 100755 --- a/develop +++ b/develop @@ -10,6 +10,13 @@ export DB_PASS=${DB_PASS:-secret} export ELASTICSEARCH_PORT=${ELASTICSEARCH_PORT:-9200} export CFN_TEMPLATE=${CFN_TEMPLATE:-cloudformation} +# Set the docker-compose files to use. +DOCKER_COMPOSE="docker-compose -f docker-compose.yml" + +if [[ $(uname) == "Darwin" ]]; then + DOCKER_COMPOSE="${DOCKER_COMPOSE} -f docker-compose.mac.yml" +fi + # Disable pseudo-TTY allocation for CI. The -T flag removes interaction. TTY="" @@ -25,33 +32,33 @@ if [[ $# -gt 0 ]]; then art|artisan ) shift 1 - docker-compose run --rm ${TTY} \ + ${DOCKER_COMPOSE} run --rm ${TTY} \ app \ php artisan "$@" ;; composer ) shift 1 - docker-compose run --rm ${TTY} \ + ${DOCKER_COMPOSE} run --rm ${TTY} \ app \ composer "$@" ;; npm ) shift 1 - docker-compose run --rm ${TTY} \ + ${DOCKER_COMPOSE} run --rm ${TTY} \ node \ npm "$@" ;; cfn|cloudformation ) - docker-compose run --rm troposphere > aws/${CFN_TEMPLATE}.json + ${DOCKER_COMPOSE} run --rm troposphere > aws/${CFN_TEMPLATE}.json cat aws/${CFN_TEMPLATE}.json ;; - * ) docker-compose "$@"; ;; + * ) ${DOCKER_COMPOSE} "$@"; ;; esac else - docker-compose ps + ${DOCKER_COMPOSE} ps fi diff --git a/docker-compose.mac.yml b/docker-compose.mac.yml new file mode 100644 index 00000000..37910fea --- /dev/null +++ b/docker-compose.mac.yml @@ -0,0 +1,10 @@ +version: "3" + +services: + redis: + environment: + REDIS_CLUSTER_IP: 0.0.0.0 + + redis-testing: + environment: + REDIS_CLUSTER_IP: 0.0.0.0 diff --git a/docker-compose.yml b/docker-compose.yml index 290ea147..ccebdbd8 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -64,18 +64,14 @@ services: - app-net redis: - image: grokzen/redis-cluster:5.0.2 - environment: - REDIS_CLUSTER_IP: 0.0.0.0 + image: grokzen/redis-cluster:5.0.5 volumes: - redis-data:/data networks: - app-net redis-testing: - image: grokzen/redis-cluster:5.0.2 - environment: - REDIS_CLUSTER_IP: 0.0.0.0 + image: grokzen/redis-cluster:5.0.5 networks: - app-net diff --git a/tests/Unit/Listeners/Notifications/UpdateRequestApprovedTest.php b/tests/Unit/Listeners/Notifications/UpdateRequestApprovedTest.php index 4a01b8d8..4a9d73b9 100644 --- a/tests/Unit/Listeners/Notifications/UpdateRequestApprovedTest.php +++ b/tests/Unit/Listeners/Notifications/UpdateRequestApprovedTest.php @@ -2,10 +2,11 @@ namespace Tests\Unit\Listeners\Notifications; -use App\Emails\UpdateRequestApproved\NotifySubmitterEmail; use App\Events\EndpointHit; use App\Listeners\Notifications\UpdateRequestApproved; use App\Models\Organisation; +use App\Models\Service; +use App\Models\UpdateRequest; use App\Models\User; use Illuminate\Http\Request; use Illuminate\Support\Facades\Queue; @@ -13,7 +14,7 @@ class UpdateRequestApprovedTest extends TestCase { - public function test_emails_sent_out() + public function test_emails_sent_out_for_existing() { Queue::fake(); @@ -37,13 +38,93 @@ public function test_emails_sent_out() $listener = new UpdateRequestApproved(); $listener->handle($event); - Queue::assertPushedOn('notifications', NotifySubmitterEmail::class); - Queue::assertPushed(NotifySubmitterEmail::class, function (NotifySubmitterEmail $email) { - $this->assertArrayHasKey('SUBMITTER_NAME', $email->values); - $this->assertArrayHasKey('RESOURCE_NAME', $email->values); - $this->assertArrayHasKey('RESOURCE_TYPE', $email->values); - $this->assertArrayHasKey('REQUEST_DATE', $email->values); - return true; - }); + Queue::assertPushedOn( + 'notifications', + \App\Emails\UpdateRequestApproved\NotifySubmitterEmail::class + ); + Queue::assertPushed( + \App\Emails\UpdateRequestApproved\NotifySubmitterEmail::class, + function (\App\Emails\UpdateRequestApproved\NotifySubmitterEmail $email) { + $this->assertArrayHasKey('SUBMITTER_NAME', $email->values); + $this->assertArrayHasKey('RESOURCE_NAME', $email->values); + $this->assertArrayHasKey('RESOURCE_TYPE', $email->values); + $this->assertArrayHasKey('REQUEST_DATE', $email->values); + return true; + } + ); + } + + public function test_emails_sent_out_for_new() + { + Queue::fake(); + + $updateRequest = UpdateRequest::create([ + 'updateable_type' => UpdateRequest::NEW_TYPE_ORGANISATION_SIGN_UP_FORM, + 'data' => [ + 'user' => [ + 'first_name' => $this->faker->firstName, + 'last_name' => $this->faker->lastName, + 'email' => $this->faker->safeEmail, + 'phone' => random_uk_phone(), + ], + 'organisation' => [ + 'slug' => 'test-org', + 'name' => 'Test Org', + 'description' => 'Test description', + 'url' => 'http://test-org.example.com', + 'email' => 'info@test-org.example.com', + 'phone' => '07700000000', + ], + 'service' => [ + 'slug' => 'test-service', + 'name' => 'Test Service', + 'type' => Service::TYPE_SERVICE, + 'intro' => 'This is a test intro', + 'description' => 'Lorem ipsum', + 'wait_time' => null, + 'is_free' => true, + 'fees_text' => null, + 'fees_url' => null, + 'testimonial' => null, + 'video_embed' => null, + 'url' => $this->faker->url, + 'contact_name' => $this->faker->name, + 'contact_phone' => random_uk_phone(), + 'contact_email' => $this->faker->safeEmail, + 'criteria' => [ + 'age_group' => '18+', + 'disability' => null, + 'employment' => null, + 'gender' => null, + 'housing' => null, + 'income' => null, + 'language' => null, + 'other' => null, + ], + 'useful_infos' => [], + 'offerings' => [], + 'social_medias' => [], + ], + ], + ]); + + $request = Request::create(''); + $event = EndpointHit::onUpdate($request, '', $updateRequest); + $listener = new UpdateRequestApproved(); + $listener->handle($event); + + Queue::assertPushedOn( + 'notifications', + \App\Emails\OrganisationSignUpFormApproved\NotifySubmitterEmail::class + ); + Queue::assertPushed( + \App\Emails\OrganisationSignUpFormApproved\NotifySubmitterEmail::class, + function (\App\Emails\OrganisationSignUpFormApproved\NotifySubmitterEmail $email) { + $this->assertArrayHasKey('SUBMITTER_NAME', $email->values); + $this->assertArrayHasKey('ORGANISATION_NAME', $email->values); + $this->assertArrayHasKey('REQUEST_DATE', $email->values); + return true; + } + ); } } diff --git a/tests/Unit/Listeners/Notifications/UpdateRequestRejectedTest.php b/tests/Unit/Listeners/Notifications/UpdateRequestRejectedTest.php index b4d0e084..ab56f07b 100644 --- a/tests/Unit/Listeners/Notifications/UpdateRequestRejectedTest.php +++ b/tests/Unit/Listeners/Notifications/UpdateRequestRejectedTest.php @@ -2,10 +2,11 @@ namespace Tests\Unit\Listeners\Notifications; -use App\Emails\UpdateRequestRejected\NotifySubmitterEmail; use App\Events\EndpointHit; use App\Listeners\Notifications\UpdateRequestRejected; use App\Models\Organisation; +use App\Models\Service; +use App\Models\UpdateRequest; use App\Models\User; use Illuminate\Http\Request; use Illuminate\Support\Facades\Queue; @@ -13,7 +14,7 @@ class UpdateRequestRejectedTest extends TestCase { - public function test_emails_sent_out() + public function test_emails_sent_out_for_existing() { Queue::fake(); @@ -37,13 +38,93 @@ public function test_emails_sent_out() $listener = new UpdateRequestRejected(); $listener->handle($event); - Queue::assertPushedOn('notifications', NotifySubmitterEmail::class); - Queue::assertPushed(NotifySubmitterEmail::class, function (NotifySubmitterEmail $email) { - $this->assertArrayHasKey('SUBMITTER_NAME', $email->values); - $this->assertArrayHasKey('RESOURCE_NAME', $email->values); - $this->assertArrayHasKey('RESOURCE_TYPE', $email->values); - $this->assertArrayHasKey('REQUEST_DATE', $email->values); - return true; - }); + Queue::assertPushedOn( + 'notifications', + \App\Emails\UpdateRequestRejected\NotifySubmitterEmail::class + ); + Queue::assertPushed( + \App\Emails\UpdateRequestRejected\NotifySubmitterEmail::class, + function (\App\Emails\UpdateRequestRejected\NotifySubmitterEmail $email) { + $this->assertArrayHasKey('SUBMITTER_NAME', $email->values); + $this->assertArrayHasKey('RESOURCE_NAME', $email->values); + $this->assertArrayHasKey('RESOURCE_TYPE', $email->values); + $this->assertArrayHasKey('REQUEST_DATE', $email->values); + return true; + } + ); + } + + public function test_emails_sent_out_for_new() + { + Queue::fake(); + + $updateRequest = UpdateRequest::create([ + 'updateable_type' => UpdateRequest::NEW_TYPE_ORGANISATION_SIGN_UP_FORM, + 'data' => [ + 'user' => [ + 'first_name' => $this->faker->firstName, + 'last_name' => $this->faker->lastName, + 'email' => $this->faker->safeEmail, + 'phone' => random_uk_phone(), + ], + 'organisation' => [ + 'slug' => 'test-org', + 'name' => 'Test Org', + 'description' => 'Test description', + 'url' => 'http://test-org.example.com', + 'email' => 'info@test-org.example.com', + 'phone' => '07700000000', + ], + 'service' => [ + 'slug' => 'test-service', + 'name' => 'Test Service', + 'type' => Service::TYPE_SERVICE, + 'intro' => 'This is a test intro', + 'description' => 'Lorem ipsum', + 'wait_time' => null, + 'is_free' => true, + 'fees_text' => null, + 'fees_url' => null, + 'testimonial' => null, + 'video_embed' => null, + 'url' => $this->faker->url, + 'contact_name' => $this->faker->name, + 'contact_phone' => random_uk_phone(), + 'contact_email' => $this->faker->safeEmail, + 'criteria' => [ + 'age_group' => '18+', + 'disability' => null, + 'employment' => null, + 'gender' => null, + 'housing' => null, + 'income' => null, + 'language' => null, + 'other' => null, + ], + 'useful_infos' => [], + 'offerings' => [], + 'social_medias' => [], + ], + ], + ]); + + $request = Request::create(''); + $event = EndpointHit::onDelete($request, '', $updateRequest); + $listener = new UpdateRequestRejected(); + $listener->handle($event); + + Queue::assertPushedOn( + 'notifications', + \App\Emails\OrganisationSignUpFormRejected\NotifySubmitterEmail::class + ); + Queue::assertPushed( + \App\Emails\OrganisationSignUpFormRejected\NotifySubmitterEmail::class, + function (\App\Emails\OrganisationSignUpFormRejected\NotifySubmitterEmail $email) { + $this->assertArrayHasKey('SUBMITTER_NAME', $email->values); + $this->assertArrayHasKey('ORGANISATION_NAME', $email->values); + $this->assertArrayHasKey('REQUEST_DATE', $email->values); + return true; + } + ); } } diff --git a/tests/Unit/Observers/UpdateRequestObserverTest.php b/tests/Unit/Observers/UpdateRequestObserverTest.php index 0579204a..324a91ac 100644 --- a/tests/Unit/Observers/UpdateRequestObserverTest.php +++ b/tests/Unit/Observers/UpdateRequestObserverTest.php @@ -2,16 +2,16 @@ namespace Tests\Unit\Observers; -use App\Emails\UpdateRequestReceived\NotifyGlobalAdminEmail; -use App\Emails\UpdateRequestReceived\NotifySubmitterEmail; use App\Models\Organisation; +use App\Models\Service; +use App\Models\UpdateRequest; use App\Models\User; use Illuminate\Support\Facades\Queue; use Tests\TestCase; class UpdateRequestObserverTest extends TestCase { - public function test_emails_sent() + public function test_emails_sent_for_existing_organisation() { Queue::fake(); @@ -29,21 +29,114 @@ public function test_emails_sent() ], ]); - Queue::assertPushedOn('notifications', NotifySubmitterEmail::class); - Queue::assertPushed(NotifySubmitterEmail::class, function (NotifySubmitterEmail $email) { - $this->assertArrayHasKey('SUBMITTER_NAME', $email->values); - $this->assertArrayHasKey('RESOURCE_NAME', $email->values); - $this->assertArrayHasKey('RESOURCE_TYPE', $email->values); - return true; - }); - - Queue::assertPushedOn('notifications', NotifyGlobalAdminEmail::class); - Queue::assertPushed(NotifyGlobalAdminEmail::class, function (NotifyGlobalAdminEmail $email) { - $this->assertArrayHasKey('RESOURCE_NAME', $email->values); - $this->assertArrayHasKey('RESOURCE_TYPE', $email->values); - $this->assertArrayHasKey('RESOURCE_ID', $email->values); - $this->assertArrayHasKey('REQUEST_URL', $email->values); - return true; - }); + Queue::assertPushedOn( + 'notifications', + \App\Emails\UpdateRequestReceived\NotifySubmitterEmail::class + ); + Queue::assertPushed( + \App\Emails\UpdateRequestReceived\NotifySubmitterEmail::class, + function (\App\Emails\UpdateRequestReceived\NotifySubmitterEmail $email) { + $this->assertArrayHasKey('SUBMITTER_NAME', $email->values); + $this->assertArrayHasKey('RESOURCE_NAME', $email->values); + $this->assertArrayHasKey('RESOURCE_TYPE', $email->values); + return true; + } + ); + + Queue::assertPushedOn( + 'notifications', + \App\Emails\UpdateRequestReceived\NotifyGlobalAdminEmail::class + ); + Queue::assertPushed( + \App\Emails\UpdateRequestReceived\NotifyGlobalAdminEmail::class, + function (\App\Emails\UpdateRequestReceived\NotifyGlobalAdminEmail $email) { + $this->assertArrayHasKey('RESOURCE_NAME', $email->values); + $this->assertArrayHasKey('RESOURCE_TYPE', $email->values); + $this->assertArrayHasKey('RESOURCE_ID', $email->values); + $this->assertArrayHasKey('REQUEST_URL', $email->values); + return true; + } + ); + } + + public function test_emails_sent_for_new_organisation() + { + Queue::fake(); + + UpdateRequest::create([ + 'updateable_type' => UpdateRequest::NEW_TYPE_ORGANISATION_SIGN_UP_FORM, + 'data' => [ + 'user' => [ + 'first_name' => $this->faker->firstName, + 'last_name' => $this->faker->lastName, + 'email' => $this->faker->safeEmail, + 'phone' => random_uk_phone(), + ], + 'organisation' => [ + 'slug' => 'test-org', + 'name' => 'Test Org', + 'description' => 'Test description', + 'url' => 'http://test-org.example.com', + 'email' => 'info@test-org.example.com', + 'phone' => '07700000000', + ], + 'service' => [ + 'slug' => 'test-service', + 'name' => 'Test Service', + 'type' => Service::TYPE_SERVICE, + 'intro' => 'This is a test intro', + 'description' => 'Lorem ipsum', + 'wait_time' => null, + 'is_free' => true, + 'fees_text' => null, + 'fees_url' => null, + 'testimonial' => null, + 'video_embed' => null, + 'url' => $this->faker->url, + 'contact_name' => $this->faker->name, + 'contact_phone' => random_uk_phone(), + 'contact_email' => $this->faker->safeEmail, + 'criteria' => [ + 'age_group' => '18+', + 'disability' => null, + 'employment' => null, + 'gender' => null, + 'housing' => null, + 'income' => null, + 'language' => null, + 'other' => null, + ], + 'useful_infos' => [], + 'offerings' => [], + 'social_medias' => [], + ], + ], + ]); + + Queue::assertPushedOn( + 'notifications', + \App\Emails\OrganisationSignUpFormReceived\NotifySubmitterEmail::class + ); + Queue::assertPushed( + \App\Emails\OrganisationSignUpFormReceived\NotifySubmitterEmail::class, + function (\App\Emails\OrganisationSignUpFormReceived\NotifySubmitterEmail $email) { + $this->assertArrayHasKey('SUBMITTER_NAME', $email->values); + $this->assertArrayHasKey('ORGANISATION_NAME', $email->values); + return true; + } + ); + + Queue::assertPushedOn( + 'notifications', + \App\Emails\OrganisationSignUpFormReceived\NotifyGlobalAdminEmail::class + ); + Queue::assertPushed( + \App\Emails\OrganisationSignUpFormReceived\NotifyGlobalAdminEmail::class, + function (\App\Emails\OrganisationSignUpFormReceived\NotifyGlobalAdminEmail $email) { + $this->assertArrayHasKey('ORGANISATION_NAME', $email->values); + $this->assertArrayHasKey('REQUEST_URL', $email->values); + return true; + } + ); } }