nhance-enrollment/tests/unit/ReminderMailConfigModelTest.php

162 lines
5.0 KiB
PHP

<?php
namespace Tests\Unit;
use App\Models\ReminderMailConfigModel;
use CodeIgniter\Test\CIUnitTestCase;
class ReminderMailConfigModelTest extends CIUnitTestCase
{
private ReminderMailConfigModel $model;
protected function setUp(): void
{
parent::setUp();
$this->model = new ReminderMailConfigModel();
}
public function testHasCustomTemplateRequiresBothSubjectAndBody()
{
$this->assertFalse($this->model->hasCustomTemplate(null));
$this->assertFalse($this->model->hasCustomTemplate([]));
$this->assertFalse($this->model->hasCustomTemplate([
'email_subject' => 'Reminder',
'email_body' => '',
]));
$this->assertFalse($this->model->hasCustomTemplate([
'email_subject' => ' ',
'email_body' => '<p>Body</p>',
]));
$this->assertTrue($this->model->hasCustomTemplate([
'email_subject' => 'Reminder',
'email_body' => '<p>Body</p>',
]));
}
public function testResolveNotificationTemplateUsesCustomTemplateWhenConfigured()
{
$config = [
'email_subject' => 'Custom subject',
'email_body' => '<p>Custom body</p>',
];
$notification = [
'id' => 5,
'enabled' => 0,
'subject' => 'Default subject',
'mail_content' => '<p>Default body</p>',
];
$resolved = $this->model->resolveNotificationTemplate($config, $notification);
$this->assertSame('Custom subject', $resolved['subject']);
$this->assertSame('<p>Custom body</p>', $resolved['mail_content']);
$this->assertSame(1, $resolved['enabled']);
$this->assertSame(5, $resolved['id']);
}
public function testResolveNotificationTemplateReturnsDefaultWhenCustomTemplateMissing()
{
$notification = [
'subject' => 'Default subject',
'mail_content' => '<p>Default body</p>',
];
$resolved = $this->model->resolveNotificationTemplate(
['email_subject' => 'Only subject'],
$notification
);
$this->assertSame($notification, $resolved);
}
public function testCanSendReminderMailAllowsCustomTemplateEvenWhenNotificationDisabled()
{
$config = [
'email_subject' => 'Custom subject',
'email_body' => '<p>Custom body</p>',
];
$notification = [
'enabled' => 0,
'mail_content' => '',
];
$this->assertTrue($this->model->canSendReminderMail($config, $notification));
}
public function testCanSendReminderMailRequiresEnabledDefaultNotification()
{
$this->assertFalse($this->model->canSendReminderMail(null, null));
$this->assertFalse($this->model->canSendReminderMail(null, [
'enabled' => 0,
'mail_content' => '<p>Body</p>',
]));
$this->assertFalse($this->model->canSendReminderMail(null, [
'enabled' => 1,
'mail_content' => '',
]));
$this->assertTrue($this->model->canSendReminderMail(null, [
'enabled' => 1,
'mail_content' => '<p>Body</p>',
]));
}
public function testValidateConfigRejectsInvalidFrequency()
{
$error = $this->model->validateConfig('hourly', '1');
$this->assertSame(
'Invalid frequency. Allowed values: daily, weekly, monthly, custom, working_days.',
$error
);
}
public function testValidateConfigDailyDoesNotRequireReminderDays()
{
$this->assertNull($this->model->validateConfig(
ReminderMailConfigModel::FREQUENCY_DAILY,
null
));
}
public function testValidateConfigWorkingDaysAcceptsDayNames()
{
$this->assertNull($this->model->validateConfig(
ReminderMailConfigModel::FREQUENCY_WORKING_DAYS,
'Mon,Wed,Fri'
));
}
public function testValidateConfigWeeklyRejectsInvalidDay()
{
$error = $this->model->validateConfig(
ReminderMailConfigModel::FREQUENCY_WEEKLY,
'0,8'
);
$this->assertSame(
'Weekly reminder_days must be between 0 (Sunday) and 6 (Saturday).',
$error
);
}
public function testEnrichConfigAddsCustomTemplateFlagAndWorkingDayLabels()
{
$config = $this->model->enrichConfig([
'frequency' => ReminderMailConfigModel::FREQUENCY_WORKING_DAYS,
'reminder_days' => '1,3,5',
'email_subject' => 'Reminder',
'email_body' => '<p>Please enroll</p>',
]);
$this->assertTrue($config['has_custom_template']);
$this->assertSame(['Mon', 'Wed', 'Fri'], $config['working_day_labels']);
}
public function testNormalizeWorkingDaysSortsAndDeduplicates()
{
$this->assertSame('1,3,5', $this->model->normalizeWorkingDays('Fri,Mon,Wed,Mon'));
}
}