-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
322 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace EscolaLms\TemplatesEmail\Providers; | ||
|
||
use EscolaLms\Templates\Facades\Template; | ||
use EscolaLms\TemplatesEmail\Core\EmailChannel; | ||
use EscolaLms\TemplatesEmail\Video\VideoProcessFailedVariables; | ||
use EscolaLms\TemplatesEmail\Video\VideoProcessFinishedVariables; | ||
use EscolaLms\TemplatesEmail\Video\VideoProcessStartedVariables; | ||
use EscolaLms\TemplatesEmail\Video\VideoProcessStateVariables; | ||
use EscolaLms\Video\Events\ProcessVideoFailed; | ||
use EscolaLms\Video\Events\ProcessVideoFinished; | ||
use EscolaLms\Video\Events\ProcessVideoStarted; | ||
use EscolaLms\Video\Events\ProcessVideoState; | ||
use Illuminate\Support\ServiceProvider; | ||
|
||
class VideoTemplatesServiceProvider extends ServiceProvider | ||
{ | ||
|
||
public function boot(): void | ||
{ | ||
Template::register( | ||
ProcessVideoStarted::class, | ||
EmailChannel::class, | ||
VideoProcessStartedVariables::class | ||
); | ||
Template::register( | ||
ProcessVideoFailed::class, | ||
EmailChannel::class, | ||
VideoProcessFailedVariables::class | ||
); | ||
Template::register( | ||
ProcessVideoFinished::class, | ||
EmailChannel::class, | ||
VideoProcessFinishedVariables::class | ||
); | ||
Template::register( | ||
ProcessVideoState::class, | ||
EmailChannel::class, | ||
VideoProcessStateVariables::class | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace EscolaLms\TemplatesEmail\Video; | ||
|
||
use EscolaLms\Core\Models\User; | ||
use EscolaLms\Templates\Events\EventWrapper; | ||
use EscolaLms\TemplatesEmail\Core\EmailVariables; | ||
|
||
abstract class CommonVideoVariables extends EmailVariables | ||
{ | ||
|
||
const VAR_USER_NAME = '@VarUserName'; | ||
|
||
const VAR_TOPIC_TITLE = '@VarTopicTitle'; | ||
|
||
public static function mockedVariables(?User $user = null): array | ||
{ | ||
$faker = \Faker\Factory::create(); | ||
return array_merge(parent::mockedVariables($user), [ | ||
self::VAR_USER_NAME => $faker->word, | ||
self::VAR_TOPIC_TITLE => $faker->word, | ||
]); | ||
} | ||
|
||
public static function variablesFromEvent(EventWrapper $event): array | ||
{ | ||
return array_merge(parent::variablesFromEvent($event), [ | ||
self::VAR_USER_NAME => $event->getUser()->name, | ||
self::VAR_TOPIC_TITLE => $event->getTopic()->title, | ||
]); | ||
} | ||
|
||
public static function defaultSectionsContent(): array | ||
{ | ||
return [ | ||
'title' => '', | ||
'content' => '' | ||
]; | ||
} | ||
|
||
public static function assignableClass(): ?string | ||
{ | ||
return null; | ||
} | ||
|
||
public static function requiredVariables(): array | ||
{ | ||
return [ | ||
self::VAR_TOPIC_TITLE | ||
]; | ||
} | ||
|
||
public static function requiredVariablesInSection(string $sectionKey): array | ||
{ | ||
if ($sectionKey === 'content') { | ||
return [ | ||
self::VAR_TOPIC_TITLE, | ||
]; | ||
} | ||
|
||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace EscolaLms\TemplatesEmail\Video; | ||
|
||
class VideoProcessFailedVariables extends CommonVideoVariables | ||
{ | ||
public static function defaultSectionsContent(): array | ||
{ | ||
return [ | ||
'title' => __('Video processing progress failed for topic - :topic', [ | ||
'topic' => self::VAR_TOPIC_TITLE | ||
]), | ||
'content' => self::wrapWithMjml(__('<h1>Hello :user_name!</h1><p>Your video in topic :topic was not processed successfully.</p>', [ | ||
'user_name' => self::VAR_USER_NAME, | ||
'topic' => self::VAR_TOPIC_TITLE, | ||
])), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace EscolaLms\TemplatesEmail\Video; | ||
|
||
class VideoProcessFinishedVariables extends CommonVideoVariables | ||
{ | ||
public static function defaultSectionsContent(): array | ||
{ | ||
return [ | ||
'title' => __('Video processing finished for topic - :topic', [ | ||
'topic' => self::VAR_TOPIC_TITLE | ||
]), | ||
'content' => self::wrapWithMjml(__('<h1>Hello :user_name!</h1><p>Your video in topic :topic has been processed successfully!</p>', [ | ||
'user_name' => self::VAR_USER_NAME, | ||
'topic' => self::VAR_TOPIC_TITLE, | ||
])), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace EscolaLms\TemplatesEmail\Video; | ||
|
||
class VideoProcessStartedVariables extends CommonVideoVariables | ||
{ | ||
public static function defaultSectionsContent(): array | ||
{ | ||
return [ | ||
'title' => __('Video processing progress starting for topic - :topic', [ | ||
'topic' => self::VAR_TOPIC_TITLE | ||
]), | ||
'content' => self::wrapWithMjml(__('<h1>Hello :user_name!</h1><p>The processing of your video has started for topic - :topic.</p>', [ | ||
'user_name' => self::VAR_USER_NAME, | ||
'topic' => self::VAR_TOPIC_TITLE, | ||
])), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace EscolaLms\TemplatesEmail\Video; | ||
|
||
use EscolaLms\Core\Models\User; | ||
use EscolaLms\Templates\Events\EventWrapper; | ||
|
||
class VideoProcessStateVariables extends CommonVideoVariables | ||
{ | ||
const VAR_VIDEO_PERCENTAGE = '@VarVideoPercentage'; | ||
|
||
public static function mockedVariables(?User $user = null): array | ||
{ | ||
$faker = \Faker\Factory::create(); | ||
return array_merge(parent::mockedVariables($user), [ | ||
self::VAR_VIDEO_PERCENTAGE => $faker->numberBetween(0, 100), | ||
]); | ||
} | ||
|
||
public static function variablesFromEvent(EventWrapper $event): array | ||
{ | ||
return array_merge(parent::variablesFromEvent($event), [ | ||
self::VAR_VIDEO_PERCENTAGE => $event->getPercentage(), | ||
]); | ||
} | ||
|
||
public static function defaultSectionsContent(): array | ||
{ | ||
return [ | ||
'title' => __('Video processing progress update for topic - :topic - :percentage%', [ | ||
'topic' => self::VAR_TOPIC_TITLE, | ||
'percentage' => self::VAR_VIDEO_PERCENTAGE, | ||
]), | ||
'content' => self::wrapWithMjml(__('<h1>Hello :user_name!</h1><p>The progress of processing your video in :topic is currently :percentage%.</p>', [ | ||
'user_name' => self::VAR_USER_NAME, | ||
'percentage' => self::VAR_VIDEO_PERCENTAGE, | ||
'topic' => self::VAR_TOPIC_TITLE, | ||
])), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
|
||
namespace EscolaLms\TemplatesEmail\Tests\Feature; | ||
|
||
use EscolaLms\Core\Tests\CreatesUsers; | ||
use EscolaLms\Courses\Models\Course; | ||
use EscolaLms\Courses\Models\Lesson; | ||
use EscolaLms\Courses\Models\Topic; | ||
use EscolaLms\TemplatesEmail\Core\EmailMailable; | ||
use EscolaLms\TemplatesEmail\Tests\TestCase; | ||
use EscolaLms\Video\Events\ProcessVideoFailed; | ||
use EscolaLms\Video\Events\ProcessVideoFinished; | ||
use EscolaLms\Video\Events\ProcessVideoStarted; | ||
use EscolaLms\Video\Events\ProcessVideoState; | ||
use EscolaLms\Video\Models\Video; | ||
use Illuminate\Foundation\Testing\DatabaseTransactions; | ||
use Illuminate\Support\Facades\Event; | ||
use Illuminate\Support\Facades\Mail; | ||
|
||
class VideoTest extends TestCase | ||
{ | ||
use DatabaseTransactions, CreatesUsers; | ||
|
||
public function testNotificationSentWhenVideoProcessingStarted(): void | ||
{ | ||
Mail::fake(); | ||
|
||
$tutor = $this->makeInstructor(); | ||
$topic = $this->createTopicVideo(); | ||
|
||
Event::dispatch(new ProcessVideoStarted($tutor, $topic)); | ||
|
||
Mail::assertSent(EmailMailable::class, function (EmailMailable $mailable) use ($topic, $tutor) { | ||
$this->assertTrue($mailable->hasTo($tutor->email)); | ||
$this->assertEquals(__('Video processing progress starting for topic - :topic', ['topic' => $topic->title]), $mailable->subject); | ||
$this->assertStringContainsString($tutor->name, $mailable->getHtml()); | ||
$this->assertStringContainsString($topic->title, $mailable->getHtml()); | ||
|
||
return true; | ||
}); | ||
} | ||
public function testNotificationSentWhenVideoProcessingSucceed(): void | ||
{ | ||
Mail::fake(); | ||
|
||
$tutor = $this->makeInstructor(); | ||
$topic = $this->createTopicVideo(); | ||
|
||
Event::dispatch(new ProcessVideoFinished($tutor, $topic)); | ||
|
||
Mail::assertSent(EmailMailable::class, function (EmailMailable $mailable) use ($topic, $tutor) { | ||
$this->assertTrue($mailable->hasTo($tutor->email)); | ||
$this->assertEquals(__('Video processing finished for topic - :topic', ['topic' => $topic->title]), $mailable->subject); | ||
$this->assertStringContainsString($tutor->name, $mailable->getHtml()); | ||
$this->assertStringContainsString($topic->title, $mailable->getHtml()); | ||
|
||
return true; | ||
}); | ||
} | ||
|
||
public function testNotificationSentWhenVideoProcessingFailed(): void | ||
{ | ||
Mail::fake(); | ||
|
||
$tutor = $this->makeInstructor(); | ||
$topic = $this->createTopicVideo(); | ||
|
||
Event::dispatch(new ProcessVideoFailed($tutor, $topic)); | ||
|
||
Mail::assertSent(EmailMailable::class, function (EmailMailable $mailable) use ($topic, $tutor) { | ||
$this->assertTrue($mailable->hasTo($tutor->email)); | ||
$this->assertEquals(__('Video processing progress failed for topic - :topic', ['topic' => $topic->title]), $mailable->subject); | ||
$this->assertStringContainsString($tutor->name, $mailable->getHtml()); | ||
$this->assertStringContainsString($topic->title, $mailable->getHtml()); | ||
|
||
return true; | ||
}); | ||
} | ||
|
||
public function testNotificationSentWhenVideoProcessingState(): void | ||
{ | ||
Mail::fake(); | ||
|
||
$tutor = $this->makeInstructor(); | ||
$topic = $this->createTopicVideo(); | ||
|
||
Event::dispatch(new ProcessVideoState($tutor, $topic, 60)); | ||
|
||
Mail::assertSent(EmailMailable::class, function (EmailMailable $mailable) use ($topic, $tutor) { | ||
$this->assertTrue($mailable->hasTo($tutor->email)); | ||
$this->assertEquals(__('Video processing progress update for topic - :topic - :percentage%', ['topic' => $topic->title, 'percentage' => 60]), $mailable->subject); | ||
$this->assertStringContainsString('60%', $mailable->getHtml()); | ||
$this->assertStringContainsString($tutor->name, $mailable->getHtml()); | ||
$this->assertStringContainsString($topic->title, $mailable->getHtml()); | ||
|
||
return true; | ||
}); | ||
} | ||
|
||
private function createTopicVideo(): Topic | ||
{ | ||
/** @var Topic */ | ||
return Topic::factory() | ||
->for(Lesson::factory()->for(Course::factory())) | ||
->state(fn() => [ | ||
'topicable_type' => \EscolaLms\TopicTypes\Models\TopicContent\Video::class, | ||
'topicable_id' => Video::factory()->create()->getKey() | ||
]) | ||
->create(); | ||
} | ||
} |