Skip to content

Commit

Permalink
Incomplete task notification (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
mako321 authored Feb 24, 2023
1 parent 49a95f3 commit ab930bb
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Providers/TaskTemplatesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
use EscolaLms\Tasks\Events\TaskAssignedEvent;
use EscolaLms\Tasks\Events\TaskCompleteRequestEvent;
use EscolaLms\Tasks\Events\TaskCompleteUserConfirmationEvent;
use EscolaLms\Tasks\Events\TaskIncompleteEvent;
use EscolaLms\Tasks\Events\TaskOverdueEvent;
use EscolaLms\Templates\Facades\Template;
use EscolaLms\TemplatesEmail\Core\EmailChannel;
use EscolaLms\TemplatesEmail\Tasks\TaskAssignedVariables;
use EscolaLms\TemplatesEmail\Tasks\TaskCompleteRequestVariables;
use EscolaLms\TemplatesEmail\Tasks\TaskCompleteUserConfirmationVariables;
use EscolaLms\TemplatesEmail\Tasks\TaskIncompleteVariables;
use EscolaLms\TemplatesEmail\Tasks\TaskOverdueVariables;
use Illuminate\Support\ServiceProvider;

Expand Down Expand Up @@ -41,5 +43,11 @@ public function boot(): void
EmailChannel::class,
TaskOverdueVariables::class
);

Template::register(
TaskIncompleteEvent::class,
EmailChannel::class,
TaskIncompleteVariables::class
);
}
}
19 changes: 19 additions & 0 deletions src/Tasks/TaskIncompleteVariables.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace EscolaLms\TemplatesEmail\Tasks;

class TaskIncompleteVariables extends CommonTasksVariables
{
public static function defaultSectionsContent(): array
{
return [
'title' => __('Incomplete task ":task"', [
'task' => self::VAR_TASK_TITLE,
]),
'content' => self::wrapWithMjml(__('<h1>Hello :user_name!</h1><p>Task ":task" has been marked as incomplete.</p>', [
'user_name' => self::VAR_USER_NAME,
'task' => self::VAR_TASK_TITLE,
])),
];
}
}
30 changes: 30 additions & 0 deletions tests/Api/TaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use EscolaLms\Tasks\Events\TaskAssignedEvent;
use EscolaLms\Tasks\Events\TaskCompleteRequestEvent;
use EscolaLms\Tasks\Events\TaskCompleteUserConfirmationEvent;
use EscolaLms\Tasks\Events\TaskIncompleteEvent;
use EscolaLms\Tasks\Models\Task;
use EscolaLms\Tasks\Tests\CreatesUsers;
use EscolaLms\Templates\Listeners\TemplateEventListener;
Expand Down Expand Up @@ -128,4 +129,33 @@ public function testNotificationSentToCreatorWhenStudentCompletesTask(): void
return true;
});
}

public function testNotificationOnTaskMarkedIncomplete(): void
{
Event::fake([TaskIncompleteEvent::class]);

$student = $this->makeStudent();

/** @var Task $task */
$task = Task::factory()
->state(['user_id' => $student->getKey()])
->create();

$this->actingAs($this->makeAdmin(), 'api')
->postJson('api/admin/tasks/incomplete/' . $task->getKey())
->assertOk();

Event::assertDispatched(function (TaskIncompleteEvent $event) use ($task) {
return $event->getUser()->getKey() === $task->user_id && $event->getTask()->getKey() === $task->getKey();
});

$listener = app(TemplateEventListener::class);
$listener->handle(new TaskIncompleteEvent($task->user, $task));

Mail::assertSent(EmailMailable::class, function (EmailMailable $mailable) use ($student, $task) {
$this->assertEquals(__('Incomplete task ":task"', ['task' => $task->title]), $mailable->subject);
$this->assertTrue($mailable->hasTo($student->email));
return true;
});
}
}

0 comments on commit ab930bb

Please sign in to comment.