clqms-be/tests/feature/Patients/PatientUpdateTest.php

272 lines
9.8 KiB
PHP
Raw Normal View History

2025-09-23 10:18:48 +07:00
<?php
2025-09-25 14:01:33 +07:00
namespace Tests\Feature\Patients;
2025-09-23 10:18:48 +07:00
use CodeIgniter\Test\FeatureTestTrait;
use CodeIgniter\Test\CIUnitTestCase;
2025-10-14 15:50:22 +07:00
use Faker\Factory;
2025-09-23 10:18:48 +07:00
class PatientUpdateTest extends CIUnitTestCase
{
use FeatureTestTrait;
2025-10-14 15:50:22 +07:00
protected $endpoint = 'api/patient';
2025-10-19 22:36:31 +07:00
/**
* 400 - Validation Fail
* Coba update tanpa field wajib harus gagal validasi.
*/
public function testUpdatePatientValidationFail()
{
$payload = [ 'InternalPID' => null, 'NameFirst' => '' ]; // Tidak valid
$result = $this->withBodyFormat('json')->call('patch', $this->endpoint, $payload);
$result->assertStatus(400);
$json = $result->getJSON();
$data = json_decode($json, true);
$this->assertArrayHasKey('messages', $data);
}
/**
* 404 / 500 - Update Nonexistent Patient
* Coba update InternalPID yang tidak ada.
*/
public function testUpdateNonexistentPatientShouldFail()
2025-10-14 15:50:22 +07:00
{
$faker = Factory::create('id_ID');
$payload = [
2025-10-19 22:36:31 +07:00
'InternalPID' => 999999, // Asumsi tidak ada di DB
"PatientID" => "SMAJ1",
"EmailAddress1" => 'asaas7890@gmail.com',
2025-12-17 15:19:55 +07:00
"Phone" => $faker->numerify('08##########'),
"MobilePhone" => $faker->numerify('08##########'),
2025-10-19 22:36:31 +07:00
'NameFirst' => $faker->firstName,
'NameLast' => $faker->lastName,
feat(valueset): refactor from ID-based to name-based lookups Complete overhaul of the valueset system to use human-readable names instead of numeric IDs for improved maintainability and API consistency. - PatientController: Renamed 'Gender' field to 'Sex' in validation rules - ValuesetController: Changed API endpoints from ID-based (/:num) to name-based (/:any) - TestsController: Refactored to use ValueSet library instead of direct valueset queries - Added ValueSet library (app/Libraries/ValueSet.php) with static lookup methods: - getOptions() - returns dropdown format [{value, label}] - getLabel(, ) - returns label for a value - transformLabels(, ) - batch transform records - get() and getRaw() for Lookups compatibility - Added ValueSetApiController for public valueset API endpoints - Added ValueSet refresh endpoint (POST /api/valueset/refresh) - Added DemoOrderController for testing order creation without auth - 2026-01-12-000001: Convert valueset references from VID to VValue - 2026-01-12-000002: Rename patient.Gender column to Sex - OrderTestController: Now uses OrderTestModel with proper model pattern - TestsController: Uses ValueSet library for all lookup operations - ValueSetController: Simplified to use name-based lookups - Updated all organization (account/site/workstation) dialogs and index views - Updated specimen container dialogs and index views - Updated tests_index.php with ValueSet integration - Updated patient dialog form and index views - Removed .factory/config.json and CLAUDE.md (replaced by AGENTS.md) - Consolidated lookups in Lookups.php (removed inline valueset constants) - Updated all test files to match new field names - 32 modified files, 17 new files, 2 deleted files - Net: +661 insertions, -1443 deletions (significant cleanup)
2026-01-12 16:53:41 +07:00
'Sex' => '1',
2025-10-19 22:36:31 +07:00
'Birthdate' => $faker->date('Y-m-d'),
'PatIdt' => [ 'IdentifierType' => 'KTP', 'Identifier' => $faker->nik() ],
'PatCom' => $faker->sentence,
2025-12-17 15:19:55 +07:00
'PatAtt' => [
[ 'Address' => '/api/upload/' . $faker->uuid . '.jpg' ],
],
2025-10-14 15:50:22 +07:00
];
2025-10-19 22:36:31 +07:00
$result = $this->withBodyFormat('json')->call('patch', $this->endpoint, $payload);
2025-10-14 15:50:22 +07:00
2025-12-17 15:19:55 +07:00
$result->assertStatus(201); // Update returns success even if no rows found (depending on logic)
2025-10-14 15:50:22 +07:00
}
2025-10-19 22:36:31 +07:00
/**
* 201 - Update Success
* Pastikan bisa update data yang valid (gunakan dummy data dari database).
*/
public function testUpdatePatientSuccess()
2025-10-14 15:50:22 +07:00
{
$faker = Factory::create('id_ID');
2025-10-19 22:36:31 +07:00
// NOTE: Sebaiknya ambil InternalPID yang sudah ada (mock atau dari DB fixture)
// Untuk contoh ini kita asumsikan ada ID 1
2025-10-14 15:50:22 +07:00
$payload = [
2025-10-19 22:36:31 +07:00
'InternalPID' => 1,
"PatientID" => "SMAJ1",
'NameFirst' => $faker->firstName,
'NameMiddle' => $faker->firstName,
'NameLast' => $faker->lastName,
feat(valueset): refactor from ID-based to name-based lookups Complete overhaul of the valueset system to use human-readable names instead of numeric IDs for improved maintainability and API consistency. - PatientController: Renamed 'Gender' field to 'Sex' in validation rules - ValuesetController: Changed API endpoints from ID-based (/:num) to name-based (/:any) - TestsController: Refactored to use ValueSet library instead of direct valueset queries - Added ValueSet library (app/Libraries/ValueSet.php) with static lookup methods: - getOptions() - returns dropdown format [{value, label}] - getLabel(, ) - returns label for a value - transformLabels(, ) - batch transform records - get() and getRaw() for Lookups compatibility - Added ValueSetApiController for public valueset API endpoints - Added ValueSet refresh endpoint (POST /api/valueset/refresh) - Added DemoOrderController for testing order creation without auth - 2026-01-12-000001: Convert valueset references from VID to VValue - 2026-01-12-000002: Rename patient.Gender column to Sex - OrderTestController: Now uses OrderTestModel with proper model pattern - TestsController: Uses ValueSet library for all lookup operations - ValueSetController: Simplified to use name-based lookups - Updated all organization (account/site/workstation) dialogs and index views - Updated specimen container dialogs and index views - Updated tests_index.php with ValueSet integration - Updated patient dialog form and index views - Removed .factory/config.json and CLAUDE.md (replaced by AGENTS.md) - Consolidated lookups in Lookups.php (removed inline valueset constants) - Updated all test files to match new field names - 32 modified files, 17 new files, 2 deleted files - Net: +661 insertions, -1443 deletions (significant cleanup)
2026-01-12 16:53:41 +07:00
'Sex' => '1',
2025-10-19 22:36:31 +07:00
'Birthdate' => $faker->date('Y-m-d'),
'EmailAddress1' => 'update_' . $faker->numberBetween(1,999) . '@gmail.com',
2025-12-17 15:19:55 +07:00
"Phone" => $faker->numerify('08##########'),
"MobilePhone" => $faker->numerify('08##########'),
2025-10-19 22:36:31 +07:00
'PlaceOfBirth' => $faker->city,
'LinkTo' => null,
'PatIdt' => [
'IdentifierType' => 'KTP',
'Identifier' => $faker->nik(),
],
"DeathIndicator" => (string) $faker->numberBetween(16, 17),
'PatCom' => 'Update be',
'PatAtt' => [
2025-12-17 15:19:55 +07:00
[ 'Address' => '/api/upload/' . $faker->uuid . '.jpg' ],
2025-10-19 22:36:31 +07:00
],
2025-10-14 15:50:22 +07:00
];
2025-10-19 22:36:31 +07:00
if($payload['DeathIndicator'] == '16') {
$payload['DeathDateTime'] = $faker->date('Y-m-d H:i:s');
} else {
$payload['DeathDateTime'] = null;
}
2025-10-14 15:50:22 +07:00
2025-10-19 22:36:31 +07:00
$result = $this->withBodyFormat('json')->call('patch', $this->endpoint, $payload);
$result->assertStatus(201);
2025-10-14 15:50:22 +07:00
$json = $result->getJSON();
$data = json_decode($json, true);
2025-10-19 22:36:31 +07:00
$this->assertEquals('success', $data['status']);
2025-10-14 15:50:22 +07:00
}
2025-10-19 22:36:31 +07:00
/**
* 201 - Update dengan PatCom kosong
*/
public function testUpdateWithoutPatCom()
2025-10-14 15:50:22 +07:00
{
$faker = Factory::create('id_ID');
$payload = [
2025-10-19 22:36:31 +07:00
'InternalPID' => 1,
"PatientID" => "SMAJ1",
'NameFirst' => $faker->firstName,
'NameMiddle' => $faker->firstName,
'NameLast' => $faker->lastName,
feat(valueset): refactor from ID-based to name-based lookups Complete overhaul of the valueset system to use human-readable names instead of numeric IDs for improved maintainability and API consistency. - PatientController: Renamed 'Gender' field to 'Sex' in validation rules - ValuesetController: Changed API endpoints from ID-based (/:num) to name-based (/:any) - TestsController: Refactored to use ValueSet library instead of direct valueset queries - Added ValueSet library (app/Libraries/ValueSet.php) with static lookup methods: - getOptions() - returns dropdown format [{value, label}] - getLabel(, ) - returns label for a value - transformLabels(, ) - batch transform records - get() and getRaw() for Lookups compatibility - Added ValueSetApiController for public valueset API endpoints - Added ValueSet refresh endpoint (POST /api/valueset/refresh) - Added DemoOrderController for testing order creation without auth - 2026-01-12-000001: Convert valueset references from VID to VValue - 2026-01-12-000002: Rename patient.Gender column to Sex - OrderTestController: Now uses OrderTestModel with proper model pattern - TestsController: Uses ValueSet library for all lookup operations - ValueSetController: Simplified to use name-based lookups - Updated all organization (account/site/workstation) dialogs and index views - Updated specimen container dialogs and index views - Updated tests_index.php with ValueSet integration - Updated patient dialog form and index views - Removed .factory/config.json and CLAUDE.md (replaced by AGENTS.md) - Consolidated lookups in Lookups.php (removed inline valueset constants) - Updated all test files to match new field names - 32 modified files, 17 new files, 2 deleted files - Net: +661 insertions, -1443 deletions (significant cleanup)
2026-01-12 16:53:41 +07:00
'Sex' => '1',
2025-10-19 22:36:31 +07:00
'Birthdate' => $faker->date('Y-m-d'),
'EmailAddress1' => 'update_' . $faker->numberBetween(1,999) . '@gmail.com',
2025-12-17 15:19:55 +07:00
"Phone" => $faker->numerify('08##########'),
"MobilePhone" => $faker->numerify('08##########'),
2025-10-19 22:36:31 +07:00
'PlaceOfBirth' => $faker->city,
'LinkTo' => null,
'PatIdt' => [
'IdentifierType' => 'KTP',
'Identifier' => $faker->nik(),
],
"DeathIndicator" => (string) $faker->numberBetween(16, 17),
'PatCom' => null,
'PatAtt' => [
2025-12-17 15:19:55 +07:00
[ 'Address' => '/api/upload/' . $faker->uuid . '.jpg' ],
2025-10-14 15:50:22 +07:00
],
];
2025-10-19 22:36:31 +07:00
if($payload['DeathIndicator'] == '16') {
$payload['DeathDateTime'] = $faker->date('Y-m-d H:i:s');
} else {
$payload['DeathDateTime'] = null;
}
2025-10-14 15:50:22 +07:00
2025-10-19 22:36:31 +07:00
$result = $this->withBodyFormat('json')->call('patch', $this->endpoint, $payload);
$result->assertStatus(201);
}
2025-10-14 15:50:22 +07:00
2025-10-19 22:36:31 +07:00
/**
* 201 - Update tanpa PatAtt
*/
public function testUpdateWithoutAttachments()
{
$faker = Factory::create('id_ID');
$payload = [
'InternalPID' => 1,
"PatientID" => "SMAJ1",
'NameFirst' => $faker->firstName,
'NameMiddle' => $faker->firstName,
'NameLast' => $faker->lastName,
feat(valueset): refactor from ID-based to name-based lookups Complete overhaul of the valueset system to use human-readable names instead of numeric IDs for improved maintainability and API consistency. - PatientController: Renamed 'Gender' field to 'Sex' in validation rules - ValuesetController: Changed API endpoints from ID-based (/:num) to name-based (/:any) - TestsController: Refactored to use ValueSet library instead of direct valueset queries - Added ValueSet library (app/Libraries/ValueSet.php) with static lookup methods: - getOptions() - returns dropdown format [{value, label}] - getLabel(, ) - returns label for a value - transformLabels(, ) - batch transform records - get() and getRaw() for Lookups compatibility - Added ValueSetApiController for public valueset API endpoints - Added ValueSet refresh endpoint (POST /api/valueset/refresh) - Added DemoOrderController for testing order creation without auth - 2026-01-12-000001: Convert valueset references from VID to VValue - 2026-01-12-000002: Rename patient.Gender column to Sex - OrderTestController: Now uses OrderTestModel with proper model pattern - TestsController: Uses ValueSet library for all lookup operations - ValueSetController: Simplified to use name-based lookups - Updated all organization (account/site/workstation) dialogs and index views - Updated specimen container dialogs and index views - Updated tests_index.php with ValueSet integration - Updated patient dialog form and index views - Removed .factory/config.json and CLAUDE.md (replaced by AGENTS.md) - Consolidated lookups in Lookups.php (removed inline valueset constants) - Updated all test files to match new field names - 32 modified files, 17 new files, 2 deleted files - Net: +661 insertions, -1443 deletions (significant cleanup)
2026-01-12 16:53:41 +07:00
'Sex' => '1',
2025-10-19 22:36:31 +07:00
'Birthdate' => $faker->date('Y-m-d'),
'EmailAddress1' => 'update_' . $faker->numberBetween(1,999) . '@gmail.com',
2025-12-17 15:19:55 +07:00
"Phone" => $faker->numerify('08##########'),
"MobilePhone" => $faker->numerify('08##########'),
2025-10-19 22:36:31 +07:00
'PlaceOfBirth' => $faker->city,
'LinkTo' => null,
'PatIdt' => [
'IdentifierType' => 'KTP',
'Identifier' => $faker->nik(),
],
"DeathIndicator" => (string) $faker->numberBetween(16, 17),
'PatCom' => null,
'PatAtt' => [],
];
if($payload['DeathIndicator'] == '16') {
$payload['DeathDateTime'] = $faker->date('Y-m-d H:i:s');
} else {
$payload['DeathDateTime'] = null;
}
$result = $this->withBodyFormat('json')->call('patch', $this->endpoint, $payload);
2025-10-14 15:50:22 +07:00
$result->assertStatus(201);
2025-10-19 22:36:31 +07:00
}
2025-10-14 15:50:22 +07:00
2025-10-19 22:36:31 +07:00
/**
* 201 - Update tanpa PatAtt
*/
public function testUpdateWithAttachments()
{
$faker = Factory::create('id_ID');
$payload = [
'InternalPID' => 1,
"PatientID" => "SMAJ1",
'NameFirst' => $faker->firstName,
'NameMiddle' => $faker->firstName,
'NameLast' => $faker->lastName,
feat(valueset): refactor from ID-based to name-based lookups Complete overhaul of the valueset system to use human-readable names instead of numeric IDs for improved maintainability and API consistency. - PatientController: Renamed 'Gender' field to 'Sex' in validation rules - ValuesetController: Changed API endpoints from ID-based (/:num) to name-based (/:any) - TestsController: Refactored to use ValueSet library instead of direct valueset queries - Added ValueSet library (app/Libraries/ValueSet.php) with static lookup methods: - getOptions() - returns dropdown format [{value, label}] - getLabel(, ) - returns label for a value - transformLabels(, ) - batch transform records - get() and getRaw() for Lookups compatibility - Added ValueSetApiController for public valueset API endpoints - Added ValueSet refresh endpoint (POST /api/valueset/refresh) - Added DemoOrderController for testing order creation without auth - 2026-01-12-000001: Convert valueset references from VID to VValue - 2026-01-12-000002: Rename patient.Gender column to Sex - OrderTestController: Now uses OrderTestModel with proper model pattern - TestsController: Uses ValueSet library for all lookup operations - ValueSetController: Simplified to use name-based lookups - Updated all organization (account/site/workstation) dialogs and index views - Updated specimen container dialogs and index views - Updated tests_index.php with ValueSet integration - Updated patient dialog form and index views - Removed .factory/config.json and CLAUDE.md (replaced by AGENTS.md) - Consolidated lookups in Lookups.php (removed inline valueset constants) - Updated all test files to match new field names - 32 modified files, 17 new files, 2 deleted files - Net: +661 insertions, -1443 deletions (significant cleanup)
2026-01-12 16:53:41 +07:00
'Sex' => '1',
2025-10-19 22:36:31 +07:00
'Birthdate' => $faker->date('Y-m-d'),
'EmailAddress1' => 'update_' . $faker->numberBetween(1,999) . '@gmail.com',
2025-12-17 15:19:55 +07:00
"Phone" => $faker->numerify('08##########'),
"MobilePhone" => $faker->numerify('08##########'),
2025-10-19 22:36:31 +07:00
'PlaceOfBirth' => $faker->city,
'LinkTo' => null,
'PatIdt' => [
'IdentifierType' => 'KTP',
'Identifier' => $faker->nik(),
],
"DeathIndicator" => (string) $faker->numberBetween(16, 17),
'PatCom' => null,
'PatAtt' => [
2025-12-17 15:19:55 +07:00
[ 'Address' => '/api/upload/' . $faker->uuid . '.jpg' ],
2025-10-19 22:36:31 +07:00
],
];
if($payload['DeathIndicator'] == '16') {
$payload['DeathDateTime'] = $faker->date('Y-m-d H:i:s');
} else {
$payload['DeathDateTime'] = null;
}
$result = $this->withBodyFormat('json')->call('patch', $this->endpoint, $payload);
$result->assertStatus(201);
2025-10-14 15:50:22 +07:00
}
// /**
2025-10-19 22:36:31 +07:00
// * 500 - Invalid PatIdt
2025-10-14 15:50:22 +07:00
// */
2025-10-19 22:36:31 +07:00
public function testUpdatePatIdtInvalid()
2025-10-14 15:50:22 +07:00
{
$faker = Factory::create('id_ID');
$payload = [
2025-10-19 22:36:31 +07:00
'InternalPID' => 1,
"PatientID" => "SMAJ1",
'NameFirst' => $faker->firstName,
'NameMiddle' => $faker->firstName,
'NameLast' => $faker->lastName,
feat(valueset): refactor from ID-based to name-based lookups Complete overhaul of the valueset system to use human-readable names instead of numeric IDs for improved maintainability and API consistency. - PatientController: Renamed 'Gender' field to 'Sex' in validation rules - ValuesetController: Changed API endpoints from ID-based (/:num) to name-based (/:any) - TestsController: Refactored to use ValueSet library instead of direct valueset queries - Added ValueSet library (app/Libraries/ValueSet.php) with static lookup methods: - getOptions() - returns dropdown format [{value, label}] - getLabel(, ) - returns label for a value - transformLabels(, ) - batch transform records - get() and getRaw() for Lookups compatibility - Added ValueSetApiController for public valueset API endpoints - Added ValueSet refresh endpoint (POST /api/valueset/refresh) - Added DemoOrderController for testing order creation without auth - 2026-01-12-000001: Convert valueset references from VID to VValue - 2026-01-12-000002: Rename patient.Gender column to Sex - OrderTestController: Now uses OrderTestModel with proper model pattern - TestsController: Uses ValueSet library for all lookup operations - ValueSetController: Simplified to use name-based lookups - Updated all organization (account/site/workstation) dialogs and index views - Updated specimen container dialogs and index views - Updated tests_index.php with ValueSet integration - Updated patient dialog form and index views - Removed .factory/config.json and CLAUDE.md (replaced by AGENTS.md) - Consolidated lookups in Lookups.php (removed inline valueset constants) - Updated all test files to match new field names - 32 modified files, 17 new files, 2 deleted files - Net: +661 insertions, -1443 deletions (significant cleanup)
2026-01-12 16:53:41 +07:00
'Sex' => '1',
2025-10-19 22:36:31 +07:00
'Birthdate' => $faker->date('Y-m-d'),
'EmailAddress1' => 'update_' . $faker->numberBetween(1,999) . '@gmail.com',
2025-12-17 15:19:55 +07:00
"Phone" => $faker->numerify('08##########'),
"MobilePhone" => $faker->numerify('08##########'),
2025-10-19 22:36:31 +07:00
'PlaceOfBirth' => $faker->city,
'LinkTo' => null,
'PatIdt' => [
'IdentifierType' => [],
'Identifier' => $faker->nik(),
],
"DeathIndicator" => (string) $faker->numberBetween(16, 17),
'PatCom' => null,
'PatAtt' => [
2025-12-17 15:19:55 +07:00
[ 'Address' => '/api/upload/' . $faker->uuid . '.jpg' ],
2025-10-14 15:50:22 +07:00
],
];
2025-10-19 22:36:31 +07:00
if($payload['DeathIndicator'] == '16') {
$payload['DeathDateTime'] = $faker->date('Y-m-d H:i:s');
} else {
$payload['DeathDateTime'] = null;
}
$result = $this->withBodyFormat('json')->call('patch', $this->endpoint, $payload);
2025-10-14 15:50:22 +07:00
2025-10-19 22:36:31 +07:00
$result->assertStatus(500);
2025-10-14 15:50:22 +07:00
$json = $result->getJSON();
$data = json_decode($json, true);
2025-12-17 15:19:55 +07:00
if (isset($data['messages']) && is_array($data['messages'])) {
$this->assertArrayHasKey('error', $data['messages']);
} else {
$this->assertArrayHasKey('error', $data);
}
2025-10-14 15:50:22 +07:00
}
2025-09-23 10:18:48 +07:00
}