From 4c941e3e1a62a9a7d23a961dfefd3dc9876da416 Mon Sep 17 00:00:00 2001 From: Tomasz Smolarek <59400506+dyfero@users.noreply.github.com> Date: Fri, 7 Jul 2023 13:36:50 +0200 Subject: [PATCH] Add email templates for init deletion profile (#74) * Add email templates for init deletion profile * composer fix --------- Co-authored-by: Tomasz Smolarek --- composer.json | 2 +- .../AccountDeletionRequestedVariables.php | 57 +++++++++++++++++++ .../AuthTemplatesServiceProvider.php | 3 + tests/Api/AuthTest.php | 29 ++++++++++ 4 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 src/Auth/AccountDeletionRequestedVariables.php diff --git a/composer.json b/composer.json index 8ac0299..63fdc7c 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ "qferr/mjml-php": "^1.1" }, "require-dev": { - "escolalms/auth": "^0.1.54", + "escolalms/auth": "^0.2", "escolalms/courses": "^0.4", "escolalms/course-access": "^0", "escolalms/topic-types": "^0", diff --git a/src/Auth/AccountDeletionRequestedVariables.php b/src/Auth/AccountDeletionRequestedVariables.php new file mode 100644 index 0000000..5617853 --- /dev/null +++ b/src/Auth/AccountDeletionRequestedVariables.php @@ -0,0 +1,57 @@ +getUser(); + + try { + $url = $event->getReturnUrl(); + } catch (\Throwable $th) { + $url = null; + } + + if (!empty($url)) { + return $url . + '?id=' . $notifiable->getKey() . + '&token=' . $event->getToken(); + } + + return URL::temporarySignedRoute( + 'profile.delete.confirmation', + Carbon::now()->addMinutes(config('auth.verification.expire', 60)), + [ + 'userId' => $notifiable->getKey(), + 'token' => $event->getToken(), + ] + ); + } + + public static function defaultSectionsContent(): array + { + return [ + 'title' => Lang::get('Confirmation of account deletion'), + 'content' => self::wrapWithMjml( + '' + . '

' + . Lang::get('Please click the button below to delete account.') + . '

' + . '
' + . '' . Lang::get('Confirm account deletion') . '' + . '' + . '

' + . Lang::get('If you did not delete an account, no further action is required.') + . '

' + . '
' + ) + ]; + } +} diff --git a/src/Providers/AuthTemplatesServiceProvider.php b/src/Providers/AuthTemplatesServiceProvider.php index 3e7eec2..f46eb21 100644 --- a/src/Providers/AuthTemplatesServiceProvider.php +++ b/src/Providers/AuthTemplatesServiceProvider.php @@ -3,6 +3,7 @@ namespace EscolaLms\TemplatesEmail\Providers; use EscolaLms\Auth\Events\AccountConfirmed; +use EscolaLms\Auth\Events\AccountDeletionRequested; use EscolaLms\Auth\Events\AccountMustBeEnableByAdmin; use EscolaLms\Auth\Events\AccountRegistered; use EscolaLms\Auth\Events\ForgotPassword; @@ -11,6 +12,7 @@ use EscolaLms\Auth\Events\UserRemovedFromGroup; use EscolaLms\Templates\Facades\Template; use EscolaLms\TemplatesEmail\Auth\AccountConfirmedVariables; +use EscolaLms\TemplatesEmail\Auth\AccountDeletionRequestedVariables; use EscolaLms\TemplatesEmail\Auth\PasswordChangedVariables; use EscolaLms\Auth\Events\AccountBlocked; use EscolaLms\Auth\Events\AccountDeleted; @@ -38,5 +40,6 @@ public function boot() Template::register(AccountRegistered::class, EmailChannel::class, VerifyEmailVariables::class); Template::register(AccountDeleted::class, EmailChannel::class, AccountDeletedVariables::class); Template::register(AccountBlocked::class, EmailChannel::class, AccountBlockedVariables::class); + Template::register(AccountDeletionRequested::class, EmailChannel::class, AccountDeletionRequestedVariables::class); } } diff --git a/tests/Api/AuthTest.php b/tests/Api/AuthTest.php index 2cc6281..086b9b1 100644 --- a/tests/Api/AuthTest.php +++ b/tests/Api/AuthTest.php @@ -5,6 +5,7 @@ use EscolaLms\Auth\Database\Seeders\AuthPermissionSeeder; use EscolaLms\Auth\Enums\SettingStatusEnum; use EscolaLms\Auth\EscolaLmsAuthServiceProvider; +use EscolaLms\Auth\Events\AccountDeletionRequested; use EscolaLms\Auth\Events\AccountMustBeEnableByAdmin; use EscolaLms\Auth\Events\AccountRegistered; use EscolaLms\Auth\Events\ForgotPassword; @@ -149,4 +150,32 @@ public function testAccountMustBeEnableByAdmin(): void return true; }); } + + public function testInitProfileDeletion(): void + { + $this->seed(AuthPermissionSeeder::class); + + Mail::fake(); + Event::fake(); + Notification::fake(); + + $user = $this->makeStudent(); + + $this + ->actingAs($user, 'api') + ->postJson('/api/profile/delete/init', ['return_url' => 'https://escolalms.com/delete-account']) + ->assertOk(); + + Event::assertDispatched(AccountDeletionRequested::class); + Notification::assertNotSentTo($user, VerifyEmail::class); + + $listener = app(TemplateEventListener::class); + $listener->handle(new AccountDeletionRequested($user, 'https://escolalms.com/delete-account')); + + Mail::assertSent(EmailMailable::class, function (EmailMailable $mailable) use ($user) { + $this->assertEquals('Confirmation of account deletion', $mailable->subject); + $this->assertTrue($mailable->hasTo($user->email)); + return true; + }); + } }