From 4a573074bee39a9c0f4a51762ace96be82ce4f2c Mon Sep 17 00:00:00 2001 From: Maciej Rymarz <59456825+mako321@users.noreply.github.com> Date: Mon, 21 Mar 2022 12:04:10 +0100 Subject: [PATCH] Add ManuallyTrigeredEvent (#31) * Add ManuallyTrigeredEvent * fix * Remove course base_price --- src/Core/UserVariables.php | 63 +++++++++++++++++++ ...EscolaLmsTemplatesEmailServiceProvider.php | 2 + src/Providers/TemplateServiceProvider.php | 21 +++++++ tests/Api/CoursesTest.php | 2 - tests/Api/TemplateApiTest.php | 51 +++++++++++++++ 5 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 src/Core/UserVariables.php create mode 100644 src/Providers/TemplateServiceProvider.php create mode 100644 tests/Api/TemplateApiTest.php diff --git a/src/Core/UserVariables.php b/src/Core/UserVariables.php new file mode 100644 index 0000000..1138abf --- /dev/null +++ b/src/Core/UserVariables.php @@ -0,0 +1,63 @@ + $faker->name(), + ]); + } + + public static function variablesFromEvent(EventWrapper $event): array + { + return array_merge(parent::variablesFromEvent($event), [ + self::VAR_USER_NAME => $event->getUser()->name, + ]); + } + + public static function requiredVariables(): array + { + return [ + self::VAR_USER_NAME, + ]; + } + + public static function requiredVariablesInSection(string $sectionKey): array + { + if ($sectionKey === 'content') { + [ + self::VAR_USER_NAME, + ]; + } + return []; + } + + public static function defaultSectionsContent(): array + { + return [ + 'title' => Lang::get('User variables'), + 'content' => self::wrapWithMjml( + '' + . '

' + . Lang::get('Custom mail to :user', ['user' => self::VAR_USER_NAME]) + . '

' + . '
' + ) + ]; + } + + public static function assignableClass(): ?string + { + return null; + } +} diff --git a/src/EscolaLmsTemplatesEmailServiceProvider.php b/src/EscolaLmsTemplatesEmailServiceProvider.php index 7de8ed1..8853db2 100644 --- a/src/EscolaLmsTemplatesEmailServiceProvider.php +++ b/src/EscolaLmsTemplatesEmailServiceProvider.php @@ -18,6 +18,7 @@ use EscolaLms\TemplatesEmail\Providers\ConsultationTemplatesServiceProvider; use EscolaLms\TemplatesEmail\Providers\CourseTemplatesServiceProvider; use EscolaLms\TemplatesEmail\Providers\CsvUsersTemplatesServiceProvider; +use EscolaLms\TemplatesEmail\Providers\TemplateServiceProvider; use EscolaLms\TemplatesEmail\Rules\MjmlRule; use EscolaLms\TemplatesEmail\Services\Contracts\MjmlServiceContract; use EscolaLms\TemplatesEmail\Services\MjmlService; @@ -64,6 +65,7 @@ class_exists(EscolaLmsTemplatesServiceProvider::class) && if (class_exists(EscolaLmsConsultationsServiceProvider::class)) { $this->app->register(ConsultationTemplatesServiceProvider::class); } + $this->app->register(TemplateServiceProvider::class); } public function boot() diff --git a/src/Providers/TemplateServiceProvider.php b/src/Providers/TemplateServiceProvider.php new file mode 100644 index 0000000..4c17e3d --- /dev/null +++ b/src/Providers/TemplateServiceProvider.php @@ -0,0 +1,21 @@ +create([ 'author_id' => $admin->id, - 'base_price' => 1337, 'status' => CourseStatusEnum::PUBLISHED ]); @@ -125,7 +124,6 @@ public function testUserUnassignedFromCourseNotification() $course = Course::factory()->create([ 'author_id' => $admin->id, - 'base_price' => 1337, 'status' => CourseStatusEnum::PUBLISHED ]); $student = User::factory()->create(); diff --git a/tests/Api/TemplateApiTest.php b/tests/Api/TemplateApiTest.php new file mode 100644 index 0000000..046571b --- /dev/null +++ b/tests/Api/TemplateApiTest.php @@ -0,0 +1,51 @@ +seed(PermissionTableSeeder::class); + } + + public function testManuallyTriggeredEvent(): void + { + Event::fake(ManuallyTriggeredEvent::class); + Mail::fake(); + + $admin = $this->makeAdmin(); + $student = $this->makeStudent(); + + $this->response = $this->actingAs($admin, 'api')->postJson( + '/api/admin/events/trigger-manually', + ['users' => [$student->getKey()]] + )->assertOk(); + + Event::assertDispatched(ManuallyTriggeredEvent::class, function (ManuallyTriggeredEvent $event) use ($student) { + $this->assertEquals($student->getKey(), $event->getUser()->getKey()); + return true; + }); + + $listener = app(TemplateEventListener::class); + $listener->handle(new ManuallyTriggeredEvent($student)); + + Mail::assertSent(EmailMailable::class, function (EmailMailable $mailable) use ($student) { + $this->assertTrue($mailable->hasTo($student->email)); + return true; + }); + } +}