140 lines
5.1 KiB
PHP
140 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit;
|
|
|
|
use App\Models\FileModel;
|
|
use CodeIgniter\Test\CIUnitTestCase;
|
|
use Tests\Support\TestUploadedFile;
|
|
|
|
/**
|
|
* Tests employeesUplodWithEvents enrollment date handling (d-m-Y / d/m/Y) and file upload validation.
|
|
*
|
|
* @internal
|
|
*/
|
|
class EmployeesUploadWithEventsTest extends CIUnitTestCase
|
|
{
|
|
private const SAMPLE_EXCEL = ROOTPATH . 'public/sample_excel/enrollment.xlsx';
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
helper('utility');
|
|
}
|
|
|
|
/**
|
|
* @dataProvider enrollmentDateFormatProvider
|
|
*/
|
|
public function testChangeDateFormatAcceptsDashAndSlashFormats(
|
|
string $openDate,
|
|
string $closeDate,
|
|
string $expectedOpen,
|
|
string $expectedClose
|
|
): void {
|
|
$this->assertSame($expectedOpen, change_date_format($openDate, null, 'Y-m-d'));
|
|
$this->assertSame($expectedClose, change_date_format($closeDate, null, 'Y-m-d'));
|
|
}
|
|
|
|
/**
|
|
* Mirrors employeesUplodWithEvents date + files insert payload (EmployeeController.php ~395-403).
|
|
*
|
|
* @dataProvider enrollmentDateFormatProvider
|
|
*/
|
|
public function testEnrollmentDatesAreConvertedBeforeFilesTableInsert(
|
|
string $openDate,
|
|
string $closeDate,
|
|
string $expectedOpen,
|
|
string $expectedClose
|
|
): void {
|
|
$postData = [
|
|
'client_id' => 12,
|
|
'policy_id' => 34,
|
|
'client_branch_id' => 56,
|
|
'created_by' => 78,
|
|
'enrollment_open_date' => $openDate,
|
|
'enrollment_close_date' => $closeDate,
|
|
];
|
|
|
|
$insertPayload = $this->buildFilesInsertPayload($postData, 'enrollment_test.xlsx', 78);
|
|
|
|
$this->assertSame($expectedOpen, $insertPayload['enrollment_open_date']);
|
|
$this->assertSame($expectedClose, $insertPayload['enrollment_close_date']);
|
|
$this->assertSame('enrollment', $insertPayload['action']);
|
|
$this->assertSame('inprogress', $insertPayload['status']);
|
|
$this->assertSame(12, $insertPayload['client_id']);
|
|
$this->assertSame(34, $insertPayload['policy_id']);
|
|
$this->assertSame(56, $insertPayload['client_branch_id']);
|
|
$this->assertSame(78, $insertPayload['created_by']);
|
|
$this->assertSame(78, $insertPayload['hr_id']);
|
|
}
|
|
|
|
public function testValidateExcelFileAcceptsSampleEnrollmentUpload(): void
|
|
{
|
|
if (!is_file(self::SAMPLE_EXCEL)) {
|
|
$this->markTestSkipped('Sample enrollment excel not found.');
|
|
}
|
|
|
|
$uploadedFile = $this->createTestUploadedFile(self::SAMPLE_EXCEL);
|
|
|
|
$this->assertTrue(validateExcelFile($uploadedFile));
|
|
$this->assertTrue($uploadedFile->move(WRITEPATH . 'uploads/excel/'));
|
|
|
|
$movedPath = WRITEPATH . 'uploads/excel/' . $uploadedFile->getName();
|
|
$this->assertFileExists($movedPath);
|
|
@unlink($movedPath);
|
|
}
|
|
|
|
public function testFileModelAllowedFieldsIncludeEnrollmentDates(): void
|
|
{
|
|
$reflection = new \ReflectionClass(FileModel::class);
|
|
$property = $reflection->getProperty('allowedFields');
|
|
$property->setAccessible(true);
|
|
$allowedFields = $property->getValue(new FileModel());
|
|
|
|
$this->assertContains('enrollment_open_date', $allowedFields);
|
|
$this->assertContains('enrollment_close_date', $allowedFields);
|
|
}
|
|
|
|
public static function enrollmentDateFormatProvider(): array
|
|
{
|
|
return [
|
|
'd-m-Y format' => ['01-07-2026', '31-07-2026', '2026-07-01', '2026-07-31'],
|
|
'd/m/Y format' => ['01/07/2026', '31/07/2026', '2026-07-01', '2026-07-31'],
|
|
'mixed formats' => ['15-08-2026', '30/08/2026', '2026-08-15', '2026-08-30'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
private function buildFilesInsertPayload(array $postData, string $filename, int $loggedInUserId): array
|
|
{
|
|
return [
|
|
'file_name' => $filename,
|
|
'client_id' => $postData['client_id'] ?? null,
|
|
'policy_id' => $postData['policy_id'] ?? null,
|
|
'created_by' => $loggedInUserId,
|
|
'status' => 'inprogress',
|
|
'action' => 'enrollment',
|
|
'client_branch_id' => $postData['client_branch_id'] ?? null,
|
|
'uploaded_by' => 1,
|
|
'enrollment_open_date' => change_date_format($postData['enrollment_open_date'] ?? null, null, 'Y-m-d'),
|
|
'enrollment_close_date' => change_date_format($postData['enrollment_close_date'] ?? null, null, 'Y-m-d'),
|
|
'hr_id' => $postData['created_by'] ?? null,
|
|
];
|
|
}
|
|
|
|
private function createTestUploadedFile(string $sourcePath): TestUploadedFile
|
|
{
|
|
$tmpPath = tempnam(sys_get_temp_dir(), 'enroll_test_') . '.xlsx';
|
|
copy($sourcePath, $tmpPath);
|
|
|
|
return new TestUploadedFile(
|
|
$tmpPath,
|
|
basename($sourcePath),
|
|
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
filesize($tmpPath),
|
|
UPLOAD_ERR_OK
|
|
);
|
|
}
|
|
}
|