-
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.
Add notification on task assigned (#60)
* Add notification on task assigned * fix test * rename service provider
- Loading branch information
Showing
8 changed files
with
176 additions
and
1 deletion.
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,21 @@ | ||
<?php | ||
|
||
namespace EscolaLms\TemplatesEmail\Providers; | ||
|
||
use EscolaLms\Tasks\Events\TaskAssignedEvent; | ||
use EscolaLms\Templates\Facades\Template; | ||
use EscolaLms\TemplatesEmail\Core\EmailChannel; | ||
use EscolaLms\TemplatesEmail\Tasks\TaskAssignedVariables; | ||
use Illuminate\Support\ServiceProvider; | ||
|
||
class TaskTemplatesServiceProvider extends ServiceProvider | ||
{ | ||
public function boot(): void | ||
{ | ||
Template::register( | ||
TaskAssignedEvent::class, | ||
EmailChannel::class, | ||
TaskAssignedVariables::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,59 @@ | ||
<?php | ||
|
||
namespace EscolaLms\TemplatesEmail\Tasks; | ||
|
||
use EscolaLms\Core\Models\User; | ||
use EscolaLms\Tasks\Models\Task; | ||
use EscolaLms\Templates\Events\EventWrapper; | ||
use EscolaLms\TemplatesEmail\Core\EmailVariables; | ||
|
||
abstract class CommonTasksVariables extends EmailVariables | ||
{ | ||
const VAR_USER_NAME = '@VarUserName'; | ||
const VAR_ASSIGNEE_NAME = '@VarAssigneeName'; | ||
const VAR_CREATOR_NAME = '@VarCreatorName'; | ||
const VAR_TASK_TITLE = '@VarTaskTitle'; | ||
|
||
public static function mockedVariables(?User $user = null): array | ||
{ | ||
$faker = \Faker\Factory::create(); | ||
return array_merge(parent::mockedVariables(), [ | ||
self::VAR_USER_NAME => $faker->name(), | ||
self::VAR_ASSIGNEE_NAME => $faker->name(), | ||
self::VAR_CREATOR_NAME => $faker->name(), | ||
self::VAR_TASK_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_ASSIGNEE_NAME => $event->getTask()->user->name, | ||
self::VAR_CREATOR_NAME => $event->getTask()->createdBy->name, | ||
self::VAR_TASK_TITLE => $event->getTask()->title, | ||
]); | ||
} | ||
|
||
public static function requiredVariables(): array | ||
{ | ||
return [ | ||
self::VAR_TASK_TITLE, | ||
]; | ||
} | ||
|
||
public static function requiredVariablesInSection(string $sectionKey): array | ||
{ | ||
if ($sectionKey === 'content') { | ||
[ | ||
self::VAR_TASK_TITLE, | ||
]; | ||
} | ||
return []; | ||
} | ||
|
||
public static function assignableClass(): ?string | ||
{ | ||
return Task::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,20 @@ | ||
<?php | ||
|
||
namespace EscolaLms\TemplatesEmail\Tasks; | ||
|
||
class TaskAssignedVariables extends CommonTasksVariables | ||
{ | ||
public static function defaultSectionsContent(): array | ||
{ | ||
return [ | ||
'title' => __('Task ":task" assigned', [ | ||
'task' => self::VAR_TASK_TITLE, | ||
]), | ||
'content' => self::wrapWithMjml(__('<h1>Hello :user_name!</h1><p>":task" task has been assigned to you by :creator</p>', [ | ||
'user_name' => self::VAR_USER_NAME, | ||
'task' => self::VAR_TASK_TITLE, | ||
'creator_name' => self::VAR_CREATOR_NAME, | ||
])), | ||
]; | ||
} | ||
} |
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,64 @@ | ||
<?php | ||
|
||
namespace EscolaLms\TemplatesEmail\Tests\Api; | ||
|
||
use EscolaLms\Tasks\Database\Seeders\TaskPermissionSeeder; | ||
use EscolaLms\Tasks\Events\TaskAssignedEvent; | ||
use EscolaLms\Tasks\Models\Task; | ||
use EscolaLms\Tasks\Tests\CreatesUsers; | ||
use EscolaLms\Templates\Listeners\TemplateEventListener; | ||
use EscolaLms\TemplatesEmail\Core\EmailMailable; | ||
use EscolaLms\TemplatesEmail\Tests\TestCase; | ||
use Illuminate\Foundation\Testing\DatabaseTransactions; | ||
use Illuminate\Foundation\Testing\WithFaker; | ||
use Illuminate\Support\Facades\Event; | ||
use Illuminate\Support\Facades\Mail; | ||
use Illuminate\Support\Facades\Notification; | ||
|
||
class TaskTest extends TestCase | ||
{ | ||
use CreatesUsers, DatabaseTransactions, WithFaker; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
if (!class_exists(\EscolaLms\Tasks\EscolaLmsTasksServiceProvider::class)) { | ||
$this->markTestSkipped('Task package not installed'); | ||
} | ||
|
||
$this->seed(TaskPermissionSeeder::class); | ||
Notification::fake(); | ||
Mail::fake(); | ||
} | ||
|
||
public function testNotificationOnTaskAssignment(): void | ||
{ | ||
Event::fake([TaskAssignedEvent::class]); | ||
|
||
$admin = $this->makeAdmin(); | ||
$student = $this->makeStudent(); | ||
|
||
$this->actingAs($admin, 'api') | ||
->postJson('api/admin/tasks', [ | ||
'title' => $this->faker->title, | ||
'user_id' => $student->getKey(), | ||
]) | ||
->assertCreated(); | ||
|
||
Event::assertDispatched(function (TaskAssignedEvent $event) use ($student) { | ||
$this->assertEquals($student->getKey(), $event->getUser()->getKey()); | ||
return true; | ||
}); | ||
|
||
$task = Task::latest()->first(); | ||
$listener = app(TemplateEventListener::class); | ||
$listener->handle(new TaskAssignedEvent($task->user, $task)); | ||
|
||
Mail::assertSent(EmailMailable::class, function (EmailMailable $mailable) use ($student, $task) { | ||
$this->assertEquals(__('Task ":task" assigned', ['task' => $task->title]), $mailable->subject); | ||
$this->assertTrue($mailable->hasTo($student->email)); | ||
return true; | ||
}); | ||
} | ||
} |
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