Skip to content

Commit

Permalink
Add template for new imported user (#15)
Browse files Browse the repository at this point in the history
* Add template for new imported user

* Add csv-users to require-dev

* Change csv-users test
  • Loading branch information
mako321 authored Jan 14, 2022
1 parent 044ad49 commit 2f4a332
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
29 changes: 29 additions & 0 deletions src/CsvUsers/ImportedNewUserVariables.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace EscolaLms\TemplatesEmail\CsvUsers;

use EscolaLms\TemplatesEmail\Auth\ResetPasswordVariables;
use Illuminate\Support\Facades\Lang;

class ImportedNewUserVariables extends ResetPasswordVariables
{
public static function defaultSectionsContent(): array
{
return [
'title' => Lang::get('User Import Notification'),
'content' => self::wrapWithMjml(
'<mj-text>'
. '<p>'
. Lang::get('You are receiving this email because you have been added to the system.')
. '</p>'
. '</mj-text>'
. '<mj-button href="' . self::VAR_ACTION_LINK . '">' . Lang::get('Set Password') . '</mj-button>'
. '<mj-text>'
. '<p>'
. Lang::get('This link will expire in :count minutes.', ['count' => self::VAR_ACTION_LINK_EXPIRATION])
. '</p>'
. '</mj-text>'
)
];
}
}
4 changes: 4 additions & 0 deletions src/EscolaLmsTemplatesEmailServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
17 changes: 17 additions & 0 deletions src/Providers/CsvUsersTemplatesServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace EscolaLms\TemplatesEmail\Providers;

use EscolaLms\CsvUsers\Events\EscolaLmsImportedNewUserTemplateEvent;
use EscolaLms\Templates\Facades\Template;
use EscolaLms\TemplatesEmail\Core\EmailChannel;
use EscolaLms\TemplatesEmail\CsvUsers\ImportedNewUserVariables;
use Illuminate\Support\ServiceProvider;

class CsvUsersTemplatesServiceProvider extends ServiceProvider
{
public function boot()
{
Template::register(EscolaLmsImportedNewUserTemplateEvent::class, EmailChannel::class, ImportedNewUserVariables::class);
}
}
60 changes: 60 additions & 0 deletions tests/Services/CsvUsersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace EscolaLms\TemplatesEmail\Tests\Services;

use EscolaLms\Core\Tests\CreatesUsers;
use EscolaLms\CsvUsers\Events\EscolaLmsImportedNewUserTemplateEvent;
use EscolaLms\CsvUsers\Services\Contracts\CsvUserServiceContract;
use EscolaLms\Templates\Listeners\TemplateEventListener;
use EscolaLms\TemplatesEmail\Core\EmailMailable;
use EscolaLms\TemplatesEmail\Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Notification;

class CsvUsersTest extends TestCase
{
use CreatesUsers, DatabaseTransactions;

public function setUp(): void
{
parent::setUp();
if (!class_exists(\EscolaLms\CsvUsers\EscolaLmsCsvUsersServiceProvider::class)) {
$this->markTestSkipped('Auth package not installed');
}
}

public function testImportNewUserNotification(): void
{
Notification::fake();
Event::fake();
Mail::fake();

$userToImport = collect([
'email' => '[email protected]',
'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;
});
}
}
4 changes: 4 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 2f4a332

Please sign in to comment.