Skip to content

Commit

Permalink
Add video email templates (#76)
Browse files Browse the repository at this point in the history
Co-authored-by: Tomasz Smolarek <tomasz.smolarek@escolasoft.com>
  • Loading branch information
dyfero and dyfero committed Sep 8, 2023
1 parent 60e5cd1 commit 83181a0
Show file tree
Hide file tree
Showing 9 changed files with 322 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"escolalms/tasks": "^0",
"escolalms/topic-type-project": "^0",
"escolalms/assign-without-account": "^0",
"escolalms/video": "^0",
"orchestra/testbench": "^5.0|^6.0",
"phpunit/phpunit": "^9.0"
},
Expand Down
6 changes: 6 additions & 0 deletions src/EscolaLmsTemplatesEmailServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@
use EscolaLms\TemplatesEmail\Providers\TaskTemplatesServiceProvider;
use EscolaLms\TemplatesEmail\Providers\TemplateServiceProvider;
use EscolaLms\TemplatesEmail\Providers\TopicTypeProjectTemplatesServiceProvider;
use EscolaLms\TemplatesEmail\Providers\VideoTemplatesServiceProvider;
use EscolaLms\TemplatesEmail\Providers\WebinarTemplatesServiceProvider;
use EscolaLms\TemplatesEmail\Providers\YoutubeTemplatesServiceProvider;
use EscolaLms\TemplatesEmail\Rules\MjmlRule;
use EscolaLms\TemplatesEmail\Services\Contracts\MjmlServiceContract;
use EscolaLms\TemplatesEmail\Services\MjmlService;
use EscolaLms\TopicTypeProject\EscolaLmsTopicTypeProjectServiceProvider;
use EscolaLms\Video\EscolaLmsVideoServiceProvider;
use EscolaLms\Webinar\EscolaLmsWebinarServiceProvider;
use EscolaLms\Youtube\EscolaLmsYoutubeServiceProvider;
use Illuminate\Support\ServiceProvider;
Expand Down Expand Up @@ -105,6 +107,10 @@ class_exists(EscolaLmsTemplatesServiceProvider::class) &&
$this->app->register(TopicTypeProjectTemplatesServiceProvider::class);
}

if (class_exists(EscolaLmsVideoServiceProvider::class)) {
$this->app->register(VideoTemplatesServiceProvider::class);
}

$this->app->register(TemplateServiceProvider::class);

$this->loadViewsFrom(__DIR__.'/../resources/views', 'templates-email');
Expand Down
43 changes: 43 additions & 0 deletions src/Providers/VideoTemplatesServiceProvider.php
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
);
}
}
63 changes: 63 additions & 0 deletions src/Video/CommonVideoVariables.php
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 [];
}
}
19 changes: 19 additions & 0 deletions src/Video/VideoProcessFailedVariables.php
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,
])),
];
}
}
19 changes: 19 additions & 0 deletions src/Video/VideoProcessFinishedVariables.php
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,
])),
];
}
}
19 changes: 19 additions & 0 deletions src/Video/VideoProcessStartedVariables.php
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,
])),
];
}
}
41 changes: 41 additions & 0 deletions src/Video/VideoProcessStateVariables.php
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,
])),
];
}
}
111 changes: 111 additions & 0 deletions tests/Feature/VideoTest.php
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();
}
}

0 comments on commit 83181a0

Please sign in to comment.