nhance-enrollment/tests/unit/GetReminderMailConfigTest.php

318 lines
11 KiB
PHP

<?php
namespace Tests\Unit;
use App\Controllers\EmployeeRestController;
use App\Models\ClientPolicyModel;
use App\Models\ReminderMailConfigModel;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\Test\CIUnitTestCase;
use Config\Services;
use ReflectionClass;
class GetReminderMailConfigTest extends CIUnitTestCase
{
private function createNotificationModelStub(?array $result): object
{
return new class($result) {
private ?array $result;
public function __construct(?array $result)
{
$this->result = $result;
}
public function where(...$args)
{
return $this;
}
public function first(): ?array
{
return $this->result;
}
};
}
private function buildController(
array $getParams,
$clientPolicyModel,
$reminderMailConfigModel,
?object $notificationModel = null
): EmployeeRestController {
$controller = new EmployeeRestController();
$request = $this->createMock(IncomingRequest::class);
$request->method('getGet')
->willReturnCallback(function ($key = null) use ($getParams) {
if ($key === null) {
return $getParams;
}
return array_key_exists($key, $getParams) ? $getParams[$key] : null;
});
$controller->initController($request, Services::response(), Services::logger());
$reflection = new ReflectionClass($controller);
foreach ([
'clientPolicyModel' => $clientPolicyModel,
'reminderMailConfigModel' => $reminderMailConfigModel,
'notificationModel' => $notificationModel,
] as $property => $value) {
if ($value === null) {
continue;
}
$prop = $reflection->getProperty($property);
$prop->setAccessible(true);
$prop->setValue($controller, $value);
}
return $controller;
}
private function decodeResponse($response): array
{
return json_decode($response->getBody(), true);
}
private function mockReminderMailConfigModel(array $methods): ReminderMailConfigModel
{
return $this->getMockBuilder(ReminderMailConfigModel::class)
->onlyMethods(array_keys($methods))
->getMock();
}
public function testMissingClientPolicyIdReturns400()
{
$clientPolicyModel = $this->createMock(ClientPolicyModel::class);
$reminderMailConfigModel = $this->mockReminderMailConfigModel([]);
$controller = $this->buildController([], $clientPolicyModel, $reminderMailConfigModel);
$body = $this->decodeResponse($controller->getReminderMailConfig());
$this->assertFalse($body['status']);
$this->assertSame(400, $body['code']);
$this->assertSame('client_policy_id is required', $body['message']);
}
public function testClientPolicyNotFoundReturns404()
{
$clientPolicyModel = $this->createMock(ClientPolicyModel::class);
$clientPolicyModel->method('find')->with(999)->willReturn(null);
$reminderMailConfigModel = $this->mockReminderMailConfigModel([]);
$controller = $this->buildController(
['client_policy_id' => '999'],
$clientPolicyModel,
$reminderMailConfigModel
);
$body = $this->decodeResponse($controller->getReminderMailConfig());
$this->assertFalse($body['status']);
$this->assertSame(404, $body['code']);
$this->assertSame('Client policy not found', $body['message']);
}
public function testExistingConfigReturnsEnrichedDataAndWorkingDayOptions()
{
$config = [
'id' => 10,
'client_policy_id' => 123,
'frequency' => ReminderMailConfigModel::FREQUENCY_WORKING_DAYS,
'reminder_days' => '1,3,5',
'email_subject' => 'Reminder',
'email_body' => '<p>Please enroll</p>',
'is_enabled' => 1,
'is_active' => 1,
];
$clientPolicyModel = $this->createMock(ClientPolicyModel::class);
$clientPolicyModel->method('find')->with(123)->willReturn([
'id' => 123,
'client_id' => 42,
]);
$reminderMailConfigModel = $this->mockReminderMailConfigModel([
'getByClientPolicyId' => null,
]);
$reminderMailConfigModel->method('getByClientPolicyId')->with(123)->willReturn($config);
$controller = $this->buildController(
['client_policy_id' => '123'],
$clientPolicyModel,
$reminderMailConfigModel
);
$body = $this->decodeResponse($controller->getReminderMailConfig());
$this->assertTrue($body['status']);
$this->assertSame(200, $body['code']);
$this->assertSame(123, $body['data']['client_policy_id']);
$this->assertSame(['Mon', 'Wed', 'Fri'], $body['data']['working_day_labels']);
$this->assertTrue($body['data']['has_custom_template']);
$this->assertCount(7, $body['working_day_options']);
$this->assertSame('Mon', $body['working_day_options'][0]['label']);
}
public function testLegacyFallbackWhenNoConfigButPolicyHasReminderDate()
{
$clientPolicyModel = $this->createMock(ClientPolicyModel::class);
$clientPolicyModel->method('find')->with(123)->willReturn([
'id' => 123,
'client_id' => 42,
'reminder_date' => '1,15,28',
]);
$reminderMailConfigModel = $this->mockReminderMailConfigModel([
'getByClientPolicyId' => null,
]);
$reminderMailConfigModel->method('getByClientPolicyId')->with(123)->willReturn(null);
$controller = $this->buildController(
['client_policy_id' => '123'],
$clientPolicyModel,
$reminderMailConfigModel
);
$body = $this->decodeResponse($controller->getReminderMailConfig());
$this->assertTrue($body['status']);
$this->assertSame(200, $body['code']);
$this->assertSame(123, $body['data']['client_policy_id']);
$this->assertSame(ReminderMailConfigModel::FREQUENCY_CUSTOM, $body['data']['frequency']);
$this->assertSame('1,15,28', $body['data']['reminder_days']);
$this->assertSame('legacy_client_policy', $body['data']['source']);
$this->assertArrayHasKey('working_day_options', $body);
}
public function testNoConfigAndNoLegacyReminderDateReturnsNullData()
{
$clientPolicyModel = $this->createMock(ClientPolicyModel::class);
$clientPolicyModel->method('find')->with(123)->willReturn([
'id' => 123,
'client_id' => 42,
]);
$reminderMailConfigModel = $this->mockReminderMailConfigModel([
'getByClientPolicyId' => null,
]);
$reminderMailConfigModel->method('getByClientPolicyId')->with(123)->willReturn(null);
$controller = $this->buildController(
['client_policy_id' => '123'],
$clientPolicyModel,
$reminderMailConfigModel
);
$body = $this->decodeResponse($controller->getReminderMailConfig());
$this->assertTrue($body['status']);
$this->assertSame(200, $body['code']);
$this->assertNull($body['data']);
$this->assertCount(7, $body['working_day_options']);
}
public function testEmailTemplateModeReturnsConfigTemplate()
{
$config = [
'client_policy_id' => 123,
'email_subject' => 'Custom subject',
'email_body' => '<p>Custom body</p>',
];
$clientPolicyModel = $this->createMock(ClientPolicyModel::class);
$clientPolicyModel->method('find')->with(123)->willReturn([
'id' => 123,
'client_id' => 42,
]);
$reminderMailConfigModel = $this->mockReminderMailConfigModel([
'getByClientPolicyId' => null,
]);
$reminderMailConfigModel->method('getByClientPolicyId')->with(123)->willReturn($config);
$controller = $this->buildController(
[
'client_policy_id' => '123',
'is_email_template' => '1',
],
$clientPolicyModel,
$reminderMailConfigModel
);
$body = $this->decodeResponse($controller->getReminderMailConfig());
$this->assertTrue($body['status']);
$this->assertSame(200, $body['code']);
$this->assertSame('Custom subject', $body['data']['email_subject']);
$this->assertSame('<p>Custom body</p>', $body['data']['email_body']);
$this->assertArrayNotHasKey('working_day_options', $body);
}
public function testEmailTemplateModeFallsBackToNotificationWhenNoConfig()
{
$clientPolicyModel = $this->createMock(ClientPolicyModel::class);
$clientPolicyModel->method('find')->with(123)->willReturn([
'id' => 123,
'client_id' => 42,
]);
$reminderMailConfigModel = $this->mockReminderMailConfigModel([
'getByClientPolicyId' => null,
]);
$reminderMailConfigModel->method('getByClientPolicyId')->with(123)->willReturn(null);
$notificationModel = $this->createNotificationModelStub([
'subject' => 'Default subject',
'mail_content' => '<p>Default body</p>',
]);
$controller = $this->buildController(
[
'client_policy_id' => '123',
'is_email_template' => '1',
],
$clientPolicyModel,
$reminderMailConfigModel,
$notificationModel
);
$body = $this->decodeResponse($controller->getReminderMailConfig());
$this->assertTrue($body['status']);
$this->assertSame(200, $body['code']);
$this->assertSame('Default subject', $body['data']['email_subject']);
$this->assertSame('<p>Default body</p>', $body['data']['email_body']);
}
public function testEmailTemplateModeReturns404WhenNotificationMissing()
{
$clientPolicyModel = $this->createMock(ClientPolicyModel::class);
$clientPolicyModel->method('find')->with(123)->willReturn([
'id' => 123,
'client_id' => 42,
]);
$reminderMailConfigModel = $this->mockReminderMailConfigModel([
'getByClientPolicyId' => null,
]);
$reminderMailConfigModel->method('getByClientPolicyId')->with(123)->willReturn(null);
$notificationModel = $this->createNotificationModelStub(null);
$controller = $this->buildController(
[
'client_policy_id' => '123',
'is_email_template' => '1',
],
$clientPolicyModel,
$reminderMailConfigModel,
$notificationModel
);
$body = $this->decodeResponse($controller->getReminderMailConfig());
$this->assertFalse($body['status']);
$this->assertSame(404, $body['code']);
$this->assertSame('Member reminder mail notification not found', $body['message']);
}
}