Skip to content

Commit

Permalink
Add email templates for approved consultation access enquiry (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
mako321 authored Mar 16, 2023
1 parent bf988dd commit 0457ace
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace EscolaLms\TemplatesEmail\ConsultationAccess;

use EscolaLms\Core\Models\User;
use EscolaLms\Templates\Events\EventWrapper;

class ConsultationAccessEnquiryApprovedVariables extends CommonConsultationAccessEnquiryVariables
{
const VAR_APPROVED_TERM = '@VarApprovedTerm';
const VAR_MEETING_LINK = '@VarMeetingLink';

public static function mockedVariables(?User $user = null): array
{
$faker = \Faker\Factory::create();
return array_merge(parent::mockedVariables(), [
self::VAR_APPROVED_TERM => $faker->dateTime->format('Y-m-d H:i'),
self::VAR_MEETING_LINK => $faker->url,
]);
}

public static function requiredVariables(): array
{
return array_merge(parent::requiredVariables(), [
self::VAR_APPROVED_TERM,
self::VAR_MEETING_LINK,
]);
}

public static function requiredVariablesInSection(string $sectionKey): array
{
if ($sectionKey === 'content') {
return array_merge(parent::requiredVariablesInSection($sectionKey), [
self::VAR_APPROVED_TERM,
self::VAR_MEETING_LINK,
]);
}
return [];
}

public static function variablesFromEvent(EventWrapper $event): array
{
return array_merge(parent::variablesFromEvent($event), [
self::VAR_APPROVED_TERM => $event->getConsultationAccessEnquiry()->consultationUser->executed_at,
self::VAR_MEETING_LINK => $event->getConsultationAccessEnquiry()->meeting_link,
]);
}

public static function defaultSectionsContent(): array
{
return [
'title' => __('Approved term ":consultation"', [
'consultation' => self::VAR_CONSULTATION_NAME,
]),
'content' => self::wrapWithMjml(__('<h1>Hello :user_name!</h1><p>Reported term :approved_term for consultation ":consultation" was approved. Meeting link: :link</p>', [
'user_name' => self::VAR_USER_NAME,
'consultation' => self::VAR_CONSULTATION_NAME,
'approved_term' => self::VAR_APPROVED_TERM,
'link' => self::VAR_MEETING_LINK,
])),
];
}
}
3 changes: 3 additions & 0 deletions src/Providers/ConsultationAccessTemplatesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace EscolaLms\TemplatesEmail\Providers;

use EscolaLms\ConsultationAccess\Events\ConsultationAccessEnquiryAdminCreatedEvent;
use EscolaLms\ConsultationAccess\Events\ConsultationAccessEnquiryApprovedEvent;
use EscolaLms\ConsultationAccess\Events\ConsultationAccessEnquiryDisapprovedEvent;
use EscolaLms\TemplatesEmail\ConsultationAccess\ConsultationAccessEnquiryAdminCreatedVariables;
use EscolaLms\TemplatesEmail\ConsultationAccess\ConsultationAccessEnquiryApprovedVariables;
use EscolaLms\TemplatesEmail\ConsultationAccess\ConsultationAccessEnquiryDisapprovedVariables;
use EscolaLms\TemplatesEmail\Core\EmailChannel;
use Illuminate\Support\ServiceProvider;
Expand All @@ -16,5 +18,6 @@ public function boot()
{
Template::register(ConsultationAccessEnquiryAdminCreatedEvent::class, EmailChannel::class, ConsultationAccessEnquiryAdminCreatedVariables::class);
Template::register(ConsultationAccessEnquiryDisapprovedEvent::class, EmailChannel::class, ConsultationAccessEnquiryDisapprovedVariables::class);
Template::register(ConsultationAccessEnquiryApprovedEvent::class, EmailChannel::class, ConsultationAccessEnquiryApprovedVariables::class);
}
}
37 changes: 36 additions & 1 deletion tests/Api/ConsultationAccessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,25 @@

use EscolaLms\ConsultationAccess\Database\Seeders\ConsultationAccessPermissionSeeder;
use EscolaLms\ConsultationAccess\Events\ConsultationAccessEnquiryAdminCreatedEvent;
use EscolaLms\ConsultationAccess\Events\ConsultationAccessEnquiryApprovedEvent;
use EscolaLms\ConsultationAccess\Events\ConsultationAccessEnquiryDisapprovedEvent;
use EscolaLms\ConsultationAccess\Models\Consultation;
use EscolaLms\ConsultationAccess\Models\ConsultationAccessEnquiry;
use EscolaLms\ConsultationAccess\Models\ConsultationAccessEnquiryProposedTerm;
use EscolaLms\Core\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\Carbon;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Notification;

class ConsultationAccessTest extends TestCase
{
use CreatesUsers, DatabaseTransactions;
use CreatesUsers, DatabaseTransactions, WithFaker;

public function setUp(): void
{
Expand Down Expand Up @@ -100,4 +103,36 @@ public function testNotificationOnConsultationEnquiryDisapproved(): void
return true;
});
}

public function testNotificationOnConsultationEnquiryApproved(): void
{
Notification::fake();
Event::fake();
Mail::fake();

/** @var ConsultationAccessEnquiryProposedTerm $proposedTerm */
$proposedTerm = ConsultationAccessEnquiryProposedTerm::factory()->create();
$meetingLink = $this->faker->url;
$this->actingAs($this->makeAdmin(), 'api')
->postJson('api/admin/consultation-access-enquiries/approve/' . $proposedTerm->getKey(), [
'meeting_link' => $meetingLink,
])
->assertOk();

$enquiry = $proposedTerm->consultationAccessEnquiry;

Event::assertDispatched(function (ConsultationAccessEnquiryApprovedEvent $event) use ($enquiry) {
$this->assertEquals($event->getConsultationAccessEnquiry()->getKey(), $enquiry->getKey());
return true;
});

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

Mail::assertSent(EmailMailable::class, function (EmailMailable $mailable) use ($enquiry) {
$this->assertEquals(__('Approved term ":consultation"', ['consultation' => $enquiry->consultation->name]), $mailable->subject);
$this->assertTrue($mailable->hasTo($enquiry->user->email));
return true;
});
}
}

0 comments on commit 0457ace

Please sign in to comment.