From 2f4a332e10baab91c1ffc5568b161eb1b511e3a7 Mon Sep 17 00:00:00 2001 From: Maciej Rymarz <59456825+mako321@users.noreply.github.com> Date: Fri, 14 Jan 2022 12:10:26 +0100 Subject: [PATCH] Add template for new imported user (#15) * Add template for new imported user * Add csv-users to require-dev * Change csv-users test --- composer.json | 1 + src/CsvUsers/ImportedNewUserVariables.php | 29 +++++++++ ...EscolaLmsTemplatesEmailServiceProvider.php | 4 ++ .../CsvUsersTemplatesServiceProvider.php | 17 ++++++ tests/Services/CsvUsersTest.php | 60 +++++++++++++++++++ tests/TestCase.php | 4 ++ 6 files changed, 115 insertions(+) create mode 100644 src/CsvUsers/ImportedNewUserVariables.php create mode 100644 src/Providers/CsvUsersTemplatesServiceProvider.php create mode 100644 tests/Services/CsvUsersTest.php diff --git a/composer.json b/composer.json index 5543779..1080391 100644 --- a/composer.json +++ b/composer.json @@ -14,6 +14,7 @@ "escolalms/auth": "^0.1.54", "escolalms/courses": "^0.3.8", "escolalms/settings": "^0.1.2", + "escolalms/csv-users": "^0.1.0", "orchestra/testbench": "^5.0|^6.0", "phpunit/phpunit": "^9.0" }, diff --git a/src/CsvUsers/ImportedNewUserVariables.php b/src/CsvUsers/ImportedNewUserVariables.php new file mode 100644 index 0000000..5b03123 --- /dev/null +++ b/src/CsvUsers/ImportedNewUserVariables.php @@ -0,0 +1,29 @@ + Lang::get('User Import Notification'), + 'content' => self::wrapWithMjml( + '' + . '

' + . Lang::get('You are receiving this email because you have been added to the system.') + . '

' + . '
' + . '' . Lang::get('Set Password') . '' + . '' + . '

' + . Lang::get('This link will expire in :count minutes.', ['count' => self::VAR_ACTION_LINK_EXPIRATION]) + . '

' + . '
' + ) + ]; + } +} diff --git a/src/EscolaLmsTemplatesEmailServiceProvider.php b/src/EscolaLmsTemplatesEmailServiceProvider.php index e6b6028..8d032b8 100644 --- a/src/EscolaLmsTemplatesEmailServiceProvider.php +++ b/src/EscolaLmsTemplatesEmailServiceProvider.php @@ -7,6 +7,7 @@ use EscolaLms\TemplatesEmail\Providers\AuthTemplatesEventServiceProvider; use EscolaLms\TemplatesEmail\Providers\AuthTemplatesServiceProvider; use EscolaLms\TemplatesEmail\Providers\CourseTemplatesServiceProvider; +use EscolaLms\TemplatesEmail\Providers\CsvUsersTemplatesServiceProvider; use EscolaLms\TemplatesEmail\Rules\MjmlRule; use EscolaLms\TemplatesEmail\Services\Contracts\MjmlServiceContract; use EscolaLms\TemplatesEmail\Services\MjmlService; @@ -37,6 +38,9 @@ public function register() if (class_exists(\EscolaLms\Courses\EscolaLmsCourseServiceProvider::class)) { $this->app->register(CourseTemplatesServiceProvider::class); } + if (class_exists(\EscolaLms\CsvUsers\EscolaLmsCsvUsersServiceProvider::class)) { + $this->app->register(CsvUsersTemplatesServiceProvider::class); + } } public function boot() diff --git a/src/Providers/CsvUsersTemplatesServiceProvider.php b/src/Providers/CsvUsersTemplatesServiceProvider.php new file mode 100644 index 0000000..b0865b6 --- /dev/null +++ b/src/Providers/CsvUsersTemplatesServiceProvider.php @@ -0,0 +1,17 @@ +markTestSkipped('Auth package not installed'); + } + } + + public function testImportNewUserNotification(): void + { + Notification::fake(); + Event::fake(); + Mail::fake(); + + $userToImport = collect([ + 'email' => 'import.user@poczta.com', + 'first_name' => 'Import', + 'last_name' => 'User', + ]); + + $service = app(CsvUserServiceContract::class); + $user = $service->saveUserFromImport($userToImport, 'http://localhost/set-password'); + + Event::assertDispatched(EscolaLmsImportedNewUserTemplateEvent::class, + function (EscolaLmsImportedNewUserTemplateEvent $event) use ($userToImport) { + return $event->getUser()->email === $userToImport['email']; + }); + + $listener = app(TemplateEventListener::class); + $listener->handle(new EscolaLmsImportedNewUserTemplateEvent($user,'http://localhost/set-password')); + + Mail::assertSent(EmailMailable::class, function (EmailMailable $mailable) use ($user) { + $this->assertEquals('User Import Notification', $mailable->subject); + $this->assertTrue($mailable->hasTo($user->email)); + $this->assertStringContainsString('token=' . $user->password_reset_token, $mailable->getHtml()); + $this->assertStringContainsString('email=' . $user->email, $mailable->getHtml()); + $this->assertStringContainsString('http://localhost/set-password', $mailable->getHtml()); + return true; + }); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 9774526..b79fb2e 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -7,6 +7,7 @@ use EscolaLms\Auth\Tests\Models\Client; use EscolaLms\Core\Tests\TestCase as CoreTestCase; use EscolaLms\Courses\EscolaLmsCourseServiceProvider; +use EscolaLms\CsvUsers\EscolaLmsCsvUsersServiceProvider; use EscolaLms\Scorm\EscolaLmsScormServiceProvider; use EscolaLms\Settings\EscolaLmsSettingsServiceProvider; use EscolaLms\Templates\EscolaLmsTemplatesServiceProvider; @@ -58,6 +59,9 @@ protected function getPackageProviders($app) if (class_exists(\EscolaLms\Settings\EscolaLmsSettingsServiceProvider::class)) { $providers[] = EscolaLmsSettingsServiceProvider::class; } + if (class_exists(\EscolaLms\CsvUsers\EscolaLmsCsvUsersServiceProvider::class)) { + $providers[] = EscolaLmsCsvUsersServiceProvider::class; + } return $providers; }