chore: add spec-kit and standardize signatures
- Added GitHub spec-kit for development workflow - Standardized file signatures to Descomplicar® format - Updated development configuration 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
316
tests/contract/test-appointment-endpoints.php
Normal file
316
tests/contract/test-appointment-endpoints.php
Normal file
@@ -0,0 +1,316 @@
|
||||
/**
|
||||
* Descomplicar® Crescimento Digital
|
||||
* https://descomplicar.pt
|
||||
*/
|
||||
|
||||
<?php
|
||||
/**
|
||||
* Contract tests for Appointment endpoints.
|
||||
*
|
||||
* These tests define the API contract and MUST FAIL initially (TDD RED phase).
|
||||
*
|
||||
* @package KiviCare_API\Tests\Contract
|
||||
*/
|
||||
|
||||
/**
|
||||
* Appointment endpoints contract tests.
|
||||
*/
|
||||
class Test_Appointment_Endpoints_Contract extends KiviCare_API_Test_Case {
|
||||
|
||||
/**
|
||||
* Test GET /wp-json/kivicare/v1/appointments endpoint contract.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_get_appointments_endpoint_contract() {
|
||||
// This test will fail initially as the endpoint doesn't exist yet
|
||||
$this->markTestIncomplete( 'Appointments GET endpoint not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Authenticated doctor
|
||||
wp_set_current_user( $this->doctor_user );
|
||||
|
||||
// ACT: Make GET request to appointments endpoint
|
||||
$response = $this->make_request( '/wp-json/kivicare/v1/appointments' );
|
||||
|
||||
// ASSERT: Response contract
|
||||
$this->assertRestResponse( $response, 200 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertIsArray( $data );
|
||||
|
||||
// Validate pagination structure
|
||||
if ( ! empty( $data ) ) {
|
||||
$this->assertArrayHasKey( 'data', $data );
|
||||
$this->assertArrayHasKey( 'total', $data );
|
||||
$this->assertArrayHasKey( 'page', $data );
|
||||
$this->assertArrayHasKey( 'per_page', $data );
|
||||
|
||||
// Validate appointment data structure
|
||||
$appointment = $data['data'][0];
|
||||
$this->assertAppointmentStructure( $appointment );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test POST /wp-json/kivicare/v1/appointments endpoint contract.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_create_appointment_endpoint_contract() {
|
||||
// This test will fail initially as the endpoint doesn't exist yet
|
||||
$this->markTestIncomplete( 'Appointments POST endpoint not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Valid appointment data
|
||||
$clinic_id = $this->create_test_clinic();
|
||||
$appointment_data = array(
|
||||
'appointment_start_date' => gmdate( 'Y-m-d', strtotime( '+1 day' ) ),
|
||||
'appointment_start_time' => '14:30:00',
|
||||
'appointment_end_date' => gmdate( 'Y-m-d', strtotime( '+1 day' ) ),
|
||||
'appointment_end_time' => '15:00:00',
|
||||
'doctor_id' => $this->doctor_user,
|
||||
'patient_id' => $this->patient_user,
|
||||
'clinic_id' => $clinic_id,
|
||||
'visit_type' => 'consultation',
|
||||
'description' => 'Consulta de rotina',
|
||||
);
|
||||
|
||||
// ACT: Make POST request as receptionist
|
||||
$response = $this->make_request( '/wp-json/kivicare/v1/appointments', 'POST', $appointment_data, $this->receptionist_user );
|
||||
|
||||
// ASSERT: Response contract
|
||||
$this->assertRestResponse( $response, 201 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertAppointmentStructure( $data );
|
||||
$this->assertEquals( $appointment_data['doctor_id'], $data['doctor_id'] );
|
||||
$this->assertEquals( $appointment_data['patient_id'], $data['patient_id'] );
|
||||
$this->assertIsInt( $data['id'] );
|
||||
$this->assertGreaterThan( 0, $data['id'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test POST /wp-json/kivicare/v1/appointments with scheduling conflict.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_create_appointment_time_conflict() {
|
||||
// This test will fail initially as the endpoint doesn't exist yet
|
||||
$this->markTestIncomplete( 'Appointment time conflict validation not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Existing appointment and conflicting data
|
||||
$clinic_id = $this->create_test_clinic();
|
||||
$existing_appointment = $this->create_test_appointment( $clinic_id, $this->doctor_user, $this->patient_user );
|
||||
|
||||
$conflicting_data = array(
|
||||
'appointment_start_date' => gmdate( 'Y-m-d', strtotime( '+1 day' ) ),
|
||||
'appointment_start_time' => '14:45:00', // Conflicts with existing 14:30-15:00
|
||||
'appointment_end_date' => gmdate( 'Y-m-d', strtotime( '+1 day' ) ),
|
||||
'appointment_end_time' => '15:15:00',
|
||||
'doctor_id' => $this->doctor_user,
|
||||
'patient_id' => $this->factory->user->create( array( 'role' => 'patient' ) ),
|
||||
'clinic_id' => $clinic_id,
|
||||
'visit_type' => 'consultation',
|
||||
);
|
||||
|
||||
// ACT: Make POST request with conflicting time
|
||||
$response = $this->make_request( '/wp-json/kivicare/v1/appointments', 'POST', $conflicting_data, $this->receptionist_user );
|
||||
|
||||
// ASSERT: Time conflict error contract
|
||||
$this->assertRestResponse( $response, 409 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertArrayHasKey( 'code', $data );
|
||||
$this->assertEquals( 'appointment_time_conflict', $data['code'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test GET /wp-json/kivicare/v1/appointments/{id} endpoint contract.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_get_appointment_by_id_endpoint_contract() {
|
||||
// This test will fail initially as the endpoint doesn't exist yet
|
||||
$this->markTestIncomplete( 'Appointment by ID endpoint not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Existing appointment
|
||||
$clinic_id = $this->create_test_clinic();
|
||||
$appointment_id = $this->create_test_appointment( $clinic_id, $this->doctor_user, $this->patient_user );
|
||||
|
||||
// ACT: Make GET request for specific appointment
|
||||
$response = $this->make_request( "/wp-json/kivicare/v1/appointments/{$appointment_id}", 'GET', array(), $this->doctor_user );
|
||||
|
||||
// ASSERT: Response contract
|
||||
$this->assertRestResponse( $response, 200 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertAppointmentStructure( $data );
|
||||
$this->assertEquals( $appointment_id, $data['id'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test PUT /wp-json/kivicare/v1/appointments/{id} endpoint contract.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_update_appointment_endpoint_contract() {
|
||||
// This test will fail initially as the endpoint doesn't exist yet
|
||||
$this->markTestIncomplete( 'Appointment PUT endpoint not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Existing appointment and update data
|
||||
$clinic_id = $this->create_test_clinic();
|
||||
$appointment_id = $this->create_test_appointment( $clinic_id, $this->doctor_user, $this->patient_user );
|
||||
$update_data = array(
|
||||
'appointment_start_time' => '15:30:00',
|
||||
'appointment_end_time' => '16:00:00',
|
||||
'description' => 'Updated appointment description',
|
||||
);
|
||||
|
||||
// ACT: Make PUT request to update appointment
|
||||
$response = $this->make_request( "/wp-json/kivicare/v1/appointments/{$appointment_id}", 'PUT', $update_data, $this->receptionist_user );
|
||||
|
||||
// ASSERT: Response contract
|
||||
$this->assertRestResponse( $response, 200 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertAppointmentStructure( $data );
|
||||
$this->assertEquals( $update_data['appointment_start_time'], $data['appointment_start_time'] );
|
||||
$this->assertEquals( $update_data['description'], $data['description'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test DELETE /wp-json/kivicare/v1/appointments/{id} endpoint contract.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_delete_appointment_endpoint_contract() {
|
||||
// This test will fail initially as the endpoint doesn't exist yet
|
||||
$this->markTestIncomplete( 'Appointment DELETE endpoint not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Existing appointment
|
||||
$clinic_id = $this->create_test_clinic();
|
||||
$appointment_id = $this->create_test_appointment( $clinic_id, $this->doctor_user, $this->patient_user );
|
||||
|
||||
// ACT: Make DELETE request
|
||||
$response = $this->make_request( "/wp-json/kivicare/v1/appointments/{$appointment_id}", 'DELETE', array(), $this->receptionist_user );
|
||||
|
||||
// ASSERT: Response contract
|
||||
$this->assertRestResponse( $response, 200 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertArrayHasKey( 'deleted', $data );
|
||||
$this->assertTrue( $data['deleted'] );
|
||||
$this->assertEquals( $appointment_id, $data['id'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test GET /wp-json/kivicare/v1/appointments/available-slots endpoint contract.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_get_available_slots_contract() {
|
||||
// This test will fail initially as the endpoint doesn't exist yet
|
||||
$this->markTestIncomplete( 'Available slots endpoint not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Query parameters for slot availability
|
||||
$query_params = array(
|
||||
'doctor_id' => $this->doctor_user,
|
||||
'date' => gmdate( 'Y-m-d', strtotime( '+1 day' ) ),
|
||||
'clinic_id' => $this->create_test_clinic(),
|
||||
);
|
||||
|
||||
// ACT: Make GET request for available slots
|
||||
$response = $this->make_request( '/wp-json/kivicare/v1/appointments/available-slots', 'GET', $query_params );
|
||||
|
||||
// ASSERT: Response contract
|
||||
$this->assertRestResponse( $response, 200 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertIsArray( $data );
|
||||
$this->assertArrayHasKey( 'date', $data );
|
||||
$this->assertArrayHasKey( 'doctor_id', $data );
|
||||
$this->assertArrayHasKey( 'available_slots', $data );
|
||||
|
||||
// Validate slot structure
|
||||
if ( ! empty( $data['available_slots'] ) ) {
|
||||
$slot = $data['available_slots'][0];
|
||||
$this->assertArrayHasKey( 'start_time', $slot );
|
||||
$this->assertArrayHasKey( 'end_time', $slot );
|
||||
$this->assertArrayHasKey( 'available', $slot );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test appointment filtering and search capabilities.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_appointment_filtering_contract() {
|
||||
// This test will fail initially as filtering isn't implemented
|
||||
$this->markTestIncomplete( 'Appointment filtering not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Multiple appointments with different attributes
|
||||
$clinic_id = $this->create_test_clinic();
|
||||
|
||||
// ACT: Test date filtering
|
||||
$filter_params = array(
|
||||
'start_date' => gmdate( 'Y-m-d' ),
|
||||
'end_date' => gmdate( 'Y-m-d', strtotime( '+7 days' ) ),
|
||||
);
|
||||
$response = $this->make_request( '/wp-json/kivicare/v1/appointments', 'GET', $filter_params, $this->doctor_user );
|
||||
|
||||
// ASSERT: Filtered response contract
|
||||
$this->assertRestResponse( $response, 200 );
|
||||
|
||||
// ACT: Test doctor filtering
|
||||
$filter_params = array( 'doctor_id' => $this->doctor_user );
|
||||
$response = $this->make_request( '/wp-json/kivicare/v1/appointments', 'GET', $filter_params, $this->admin_user );
|
||||
|
||||
// ASSERT: Doctor-filtered response contract
|
||||
$this->assertRestResponse( $response, 200 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to assert appointment data structure.
|
||||
*
|
||||
* @param array $appointment Appointment data to validate.
|
||||
*/
|
||||
private function assertAppointmentStructure( $appointment ) {
|
||||
$this->assertIsArray( $appointment );
|
||||
|
||||
// Required fields
|
||||
$expected_fields = array(
|
||||
'id', 'appointment_start_date', 'appointment_start_time',
|
||||
'appointment_end_date', 'appointment_end_time', 'doctor_id',
|
||||
'patient_id', 'clinic_id', 'status', 'visit_type', 'created_at'
|
||||
);
|
||||
|
||||
foreach ( $expected_fields as $field ) {
|
||||
$this->assertArrayHasKey( $field, $appointment );
|
||||
}
|
||||
|
||||
// Data type validations
|
||||
$this->assertIsInt( $appointment['id'] );
|
||||
$this->assertIsInt( $appointment['doctor_id'] );
|
||||
$this->assertIsInt( $appointment['patient_id'] );
|
||||
$this->assertIsInt( $appointment['clinic_id'] );
|
||||
$this->assertIsInt( $appointment['status'] );
|
||||
|
||||
// Date/time format validations
|
||||
$this->assertMatchesRegularExpression( '/^\d{4}-\d{2}-\d{2}$/', $appointment['appointment_start_date'] );
|
||||
$this->assertMatchesRegularExpression( '/^\d{2}:\d{2}:\d{2}$/', $appointment['appointment_start_time'] );
|
||||
|
||||
// Optional fields that might be present
|
||||
$optional_fields = array( 'description', 'patient', 'doctor', 'clinic' );
|
||||
|
||||
// If expanded data is included, validate structure
|
||||
if ( isset( $appointment['patient'] ) ) {
|
||||
$this->assertIsArray( $appointment['patient'] );
|
||||
$this->assertArrayHasKey( 'display_name', $appointment['patient'] );
|
||||
}
|
||||
|
||||
if ( isset( $appointment['doctor'] ) ) {
|
||||
$this->assertIsArray( $appointment['doctor'] );
|
||||
$this->assertArrayHasKey( 'display_name', $appointment['doctor'] );
|
||||
}
|
||||
}
|
||||
}
|
||||
194
tests/contract/test-auth-endpoints.php
Normal file
194
tests/contract/test-auth-endpoints.php
Normal file
@@ -0,0 +1,194 @@
|
||||
/**
|
||||
* Descomplicar® Crescimento Digital
|
||||
* https://descomplicar.pt
|
||||
*/
|
||||
|
||||
<?php
|
||||
/**
|
||||
* Contract tests for Authentication endpoints.
|
||||
*
|
||||
* These tests define the API contract and MUST FAIL initially (TDD RED phase).
|
||||
*
|
||||
* @package KiviCare_API\Tests\Contract
|
||||
*/
|
||||
|
||||
/**
|
||||
* Authentication endpoints contract tests.
|
||||
*/
|
||||
class Test_Auth_Endpoints_Contract extends KiviCare_API_Test_Case {
|
||||
|
||||
/**
|
||||
* Test POST /wp-json/kivicare/v1/auth/login endpoint contract.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_auth_login_endpoint_contract() {
|
||||
// ARRANGE: Valid login credentials
|
||||
$login_data = array(
|
||||
'username' => 'test_doctor',
|
||||
'password' => 'password123',
|
||||
);
|
||||
|
||||
// ACT: Make POST request to login endpoint
|
||||
$response = $this->make_request( '/wp-json/kivicare/v1/auth/login', 'POST', $login_data );
|
||||
|
||||
// ASSERT: Response contract
|
||||
$this->assertRestResponse( $response, 200 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertArrayHasKey( 'token', $data );
|
||||
$this->assertArrayHasKey( 'user_id', $data );
|
||||
$this->assertArrayHasKey( 'role', $data );
|
||||
$this->assertArrayHasKey( 'expires_in', $data );
|
||||
|
||||
// Validate token format (JWT)
|
||||
$this->assertIsString( $data['token'] );
|
||||
$this->assertMatchesRegularExpression( '/^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+$/', $data['token'] );
|
||||
|
||||
// Validate user data
|
||||
$this->assertIsInt( $data['user_id'] );
|
||||
$this->assertGreaterThan( 0, $data['user_id'] );
|
||||
$this->assertIsString( $data['role'] );
|
||||
$this->assertContains( $data['role'], array( 'administrator', 'doctor', 'patient', 'kivicare_receptionist' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test POST /wp-json/kivicare/v1/auth/login with invalid credentials.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_auth_login_invalid_credentials() {
|
||||
// ARRANGE: Invalid credentials
|
||||
$invalid_data = array(
|
||||
'username' => 'nonexistent_user',
|
||||
'password' => 'wrong_password',
|
||||
);
|
||||
|
||||
// ACT: Make POST request with invalid data
|
||||
$response = $this->make_request( '/wp-json/kivicare/v1/auth/login', 'POST', $invalid_data );
|
||||
|
||||
// ASSERT: Error response contract
|
||||
$this->assertRestResponse( $response, 401 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertArrayHasKey( 'code', $data );
|
||||
$this->assertArrayHasKey( 'message', $data );
|
||||
$this->assertEquals( 'invalid_credentials', $data['code'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test POST /wp-json/kivicare/v1/auth/login with missing fields.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_auth_login_missing_fields() {
|
||||
// ARRANGE: Missing username
|
||||
$incomplete_data = array(
|
||||
'password' => 'password123',
|
||||
);
|
||||
|
||||
// ACT: Make POST request with incomplete data
|
||||
$response = $this->make_request( '/wp-json/kivicare/v1/auth/login', 'POST', $incomplete_data );
|
||||
|
||||
// ASSERT: Validation error contract
|
||||
$this->assertRestResponse( $response, 400 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertArrayHasKey( 'code', $data );
|
||||
$this->assertEquals( 'rest_missing_callback_param', $data['code'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test POST /wp-json/kivicare/v1/auth/refresh endpoint contract.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_auth_refresh_endpoint_contract() {
|
||||
// This test will fail initially as the endpoint doesn't exist yet
|
||||
$this->markTestIncomplete( 'Refresh endpoint not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Valid refresh token (will be implemented)
|
||||
$refresh_data = array(
|
||||
'refresh_token' => 'valid_refresh_token_here',
|
||||
);
|
||||
|
||||
// ACT: Make POST request to refresh endpoint
|
||||
$response = $this->make_request( '/wp-json/kivicare/v1/auth/refresh', 'POST', $refresh_data );
|
||||
|
||||
// ASSERT: Response contract (will fail until implemented)
|
||||
$this->assertRestResponse( $response, 200 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertArrayHasKey( 'token', $data );
|
||||
$this->assertArrayHasKey( 'expires_in', $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test POST /wp-json/kivicare/v1/auth/logout endpoint contract.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_auth_logout_endpoint_contract() {
|
||||
// This test will fail initially as the endpoint doesn't exist yet
|
||||
$this->markTestIncomplete( 'Logout endpoint not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Authenticated user
|
||||
wp_set_current_user( $this->doctor_user );
|
||||
|
||||
// ACT: Make POST request to logout endpoint
|
||||
$response = $this->make_request( '/wp-json/kivicare/v1/auth/logout', 'POST' );
|
||||
|
||||
// ASSERT: Response contract (will fail until implemented)
|
||||
$this->assertRestResponse( $response, 200 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertArrayHasKey( 'message', $data );
|
||||
$this->assertEquals( 'Logout successful', $data['message'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test authentication middleware with invalid token.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_invalid_token_response_contract() {
|
||||
// This test will fail initially as JWT authentication isn't implemented
|
||||
$this->markTestIncomplete( 'JWT authentication not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Invalid JWT token
|
||||
$_SERVER['HTTP_AUTHORIZATION'] = 'Bearer invalid_token_here';
|
||||
|
||||
// ACT: Try to access protected endpoint
|
||||
$response = $this->make_request( '/wp-json/kivicare/v1/patients' );
|
||||
|
||||
// ASSERT: Authentication error contract
|
||||
$this->assertRestResponse( $response, 401 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertArrayHasKey( 'code', $data );
|
||||
$this->assertEquals( 'rest_forbidden', $data['code'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test authentication middleware with expired token.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_expired_token_response_contract() {
|
||||
// This test will fail initially as JWT authentication isn't implemented
|
||||
$this->markTestIncomplete( 'JWT authentication not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Expired JWT token
|
||||
$_SERVER['HTTP_AUTHORIZATION'] = 'Bearer expired_token_here';
|
||||
|
||||
// ACT: Try to access protected endpoint
|
||||
$response = $this->make_request( '/wp-json/kivicare/v1/patients' );
|
||||
|
||||
// ASSERT: Token expiry error contract
|
||||
$this->assertRestResponse( $response, 401 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertArrayHasKey( 'code', $data );
|
||||
$this->assertEquals( 'jwt_auth_token_expired', $data['code'] );
|
||||
}
|
||||
}
|
||||
251
tests/contract/test-clinic-endpoints.php
Normal file
251
tests/contract/test-clinic-endpoints.php
Normal file
@@ -0,0 +1,251 @@
|
||||
/**
|
||||
* Descomplicar® Crescimento Digital
|
||||
* https://descomplicar.pt
|
||||
*/
|
||||
|
||||
<?php
|
||||
/**
|
||||
* Contract tests for Clinic endpoints.
|
||||
*
|
||||
* These tests define the API contract and MUST FAIL initially (TDD RED phase).
|
||||
*
|
||||
* @package KiviCare_API\Tests\Contract
|
||||
*/
|
||||
|
||||
/**
|
||||
* Clinic endpoints contract tests.
|
||||
*/
|
||||
class Test_Clinic_Endpoints_Contract extends KiviCare_API_Test_Case {
|
||||
|
||||
/**
|
||||
* Test GET /wp-json/kivicare/v1/clinics endpoint contract.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_get_clinics_endpoint_contract() {
|
||||
// This test will fail initially as the endpoint doesn't exist yet
|
||||
$this->markTestIncomplete( 'Clinics GET endpoint not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Authenticated administrator
|
||||
wp_set_current_user( $this->admin_user );
|
||||
|
||||
// ACT: Make GET request to clinics endpoint
|
||||
$response = $this->make_request( '/wp-json/kivicare/v1/clinics' );
|
||||
|
||||
// ASSERT: Response contract
|
||||
$this->assertRestResponse( $response, 200 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertIsArray( $data );
|
||||
|
||||
// Validate pagination structure
|
||||
if ( ! empty( $data ) ) {
|
||||
$this->assertArrayHasKey( 'data', $data );
|
||||
$this->assertArrayHasKey( 'total', $data );
|
||||
$this->assertArrayHasKey( 'page', $data );
|
||||
$this->assertArrayHasKey( 'per_page', $data );
|
||||
|
||||
// Validate clinic data structure
|
||||
$clinic = $data['data'][0];
|
||||
$this->assertClinicStructure( $clinic );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test POST /wp-json/kivicare/v1/clinics endpoint contract.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_create_clinic_endpoint_contract() {
|
||||
// This test will fail initially as the endpoint doesn't exist yet
|
||||
$this->markTestIncomplete( 'Clinics POST endpoint not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Valid clinic data
|
||||
$clinic_data = array(
|
||||
'name' => 'Nova Clínica',
|
||||
'email' => 'nova@clinica.com',
|
||||
'telephone_no' => '+351987654321',
|
||||
'address' => 'Rua Nova, 456',
|
||||
'city' => 'Porto',
|
||||
'state' => 'Porto',
|
||||
'country' => 'Portugal',
|
||||
'postal_code' => '4000-001',
|
||||
'specialties' => 'Cardiology,Neurology',
|
||||
);
|
||||
|
||||
// ACT: Make POST request as administrator
|
||||
$response = $this->make_request( '/wp-json/kivicare/v1/clinics', 'POST', $clinic_data, $this->admin_user );
|
||||
|
||||
// ASSERT: Response contract
|
||||
$this->assertRestResponse( $response, 201 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertClinicStructure( $data );
|
||||
$this->assertEquals( $clinic_data['name'], $data['name'] );
|
||||
$this->assertEquals( $clinic_data['email'], $data['email'] );
|
||||
$this->assertIsInt( $data['id'] );
|
||||
$this->assertGreaterThan( 0, $data['id'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test POST /wp-json/kivicare/v1/clinics with invalid data.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_create_clinic_invalid_data() {
|
||||
// This test will fail initially as the endpoint doesn't exist yet
|
||||
$this->markTestIncomplete( 'Clinics POST validation not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Invalid clinic data (missing required fields)
|
||||
$invalid_data = array(
|
||||
'name' => '', // Empty name should fail
|
||||
'email' => 'invalid-email', // Invalid email format
|
||||
);
|
||||
|
||||
// ACT: Make POST request with invalid data
|
||||
$response = $this->make_request( '/wp-json/kivicare/v1/clinics', 'POST', $invalid_data, $this->admin_user );
|
||||
|
||||
// ASSERT: Validation error contract
|
||||
$this->assertRestResponse( $response, 400 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertArrayHasKey( 'code', $data );
|
||||
$this->assertEquals( 'rest_invalid_param', $data['code'] );
|
||||
$this->assertArrayHasKey( 'data', $data );
|
||||
$this->assertArrayHasKey( 'params', $data['data'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test GET /wp-json/kivicare/v1/clinics/{id} endpoint contract.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_get_clinic_by_id_endpoint_contract() {
|
||||
// This test will fail initially as the endpoint doesn't exist yet
|
||||
$this->markTestIncomplete( 'Clinic by ID endpoint not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Existing clinic
|
||||
$clinic_id = $this->create_test_clinic();
|
||||
|
||||
// ACT: Make GET request for specific clinic
|
||||
$response = $this->make_request( "/wp-json/kivicare/v1/clinics/{$clinic_id}", 'GET', array(), $this->admin_user );
|
||||
|
||||
// ASSERT: Response contract
|
||||
$this->assertRestResponse( $response, 200 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertClinicStructure( $data );
|
||||
$this->assertEquals( $clinic_id, $data['id'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test PUT /wp-json/kivicare/v1/clinics/{id} endpoint contract.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_update_clinic_endpoint_contract() {
|
||||
// This test will fail initially as the endpoint doesn't exist yet
|
||||
$this->markTestIncomplete( 'Clinic PUT endpoint not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Existing clinic and update data
|
||||
$clinic_id = $this->create_test_clinic();
|
||||
$update_data = array(
|
||||
'name' => 'Clínica Atualizada',
|
||||
'telephone_no' => '+351111222333',
|
||||
);
|
||||
|
||||
// ACT: Make PUT request to update clinic
|
||||
$response = $this->make_request( "/wp-json/kivicare/v1/clinics/{$clinic_id}", 'PUT', $update_data, $this->admin_user );
|
||||
|
||||
// ASSERT: Response contract
|
||||
$this->assertRestResponse( $response, 200 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertClinicStructure( $data );
|
||||
$this->assertEquals( $update_data['name'], $data['name'] );
|
||||
$this->assertEquals( $update_data['telephone_no'], $data['telephone_no'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test DELETE /wp-json/kivicare/v1/clinics/{id} endpoint contract.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_delete_clinic_endpoint_contract() {
|
||||
// This test will fail initially as the endpoint doesn't exist yet
|
||||
$this->markTestIncomplete( 'Clinic DELETE endpoint not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Existing clinic
|
||||
$clinic_id = $this->create_test_clinic();
|
||||
|
||||
// ACT: Make DELETE request
|
||||
$response = $this->make_request( "/wp-json/kivicare/v1/clinics/{$clinic_id}", 'DELETE', array(), $this->admin_user );
|
||||
|
||||
// ASSERT: Response contract
|
||||
$this->assertRestResponse( $response, 200 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertArrayHasKey( 'deleted', $data );
|
||||
$this->assertTrue( $data['deleted'] );
|
||||
$this->assertEquals( $clinic_id, $data['id'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test clinic permissions for different user roles.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_clinic_permissions_contract() {
|
||||
// This test will fail initially as role-based permissions aren't implemented
|
||||
$this->markTestIncomplete( 'Role-based permissions not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Existing clinic
|
||||
$clinic_id = $this->create_test_clinic();
|
||||
|
||||
// ACT & ASSERT: Doctor should not be able to create clinics
|
||||
$response = $this->make_request( '/wp-json/kivicare/v1/clinics', 'POST', array( 'name' => 'Test' ), $this->doctor_user );
|
||||
$this->assertRestResponse( $response, 403 );
|
||||
|
||||
// ACT & ASSERT: Patient should not be able to access clinics
|
||||
$response = $this->make_request( '/wp-json/kivicare/v1/clinics', 'GET', array(), $this->patient_user );
|
||||
$this->assertRestResponse( $response, 403 );
|
||||
|
||||
// ACT & ASSERT: Administrator should have full access
|
||||
$response = $this->make_request( "/wp-json/kivicare/v1/clinics/{$clinic_id}", 'GET', array(), $this->admin_user );
|
||||
$this->assertRestResponse( $response, 200 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to assert clinic data structure.
|
||||
*
|
||||
* @param array $clinic Clinic data to validate.
|
||||
*/
|
||||
private function assertClinicStructure( $clinic ) {
|
||||
$this->assertIsArray( $clinic );
|
||||
|
||||
// Required fields
|
||||
$this->assertArrayHasKey( 'id', $clinic );
|
||||
$this->assertArrayHasKey( 'name', $clinic );
|
||||
$this->assertArrayHasKey( 'status', $clinic );
|
||||
|
||||
// Optional fields that should be present in response
|
||||
$expected_fields = array(
|
||||
'email', 'telephone_no', 'address', 'city',
|
||||
'state', 'country', 'postal_code', 'specialties',
|
||||
'clinic_admin_id', 'created_at'
|
||||
);
|
||||
|
||||
foreach ( $expected_fields as $field ) {
|
||||
$this->assertArrayHasKey( $field, $clinic );
|
||||
}
|
||||
|
||||
// Data type validations
|
||||
$this->assertIsInt( $clinic['id'] );
|
||||
$this->assertIsString( $clinic['name'] );
|
||||
$this->assertIsInt( $clinic['status'] );
|
||||
|
||||
if ( ! empty( $clinic['email'] ) ) {
|
||||
$this->assertIsString( $clinic['email'] );
|
||||
}
|
||||
}
|
||||
}
|
||||
361
tests/contract/test-encounter-endpoints.php
Normal file
361
tests/contract/test-encounter-endpoints.php
Normal file
@@ -0,0 +1,361 @@
|
||||
/**
|
||||
* Descomplicar® Crescimento Digital
|
||||
* https://descomplicar.pt
|
||||
*/
|
||||
|
||||
<?php
|
||||
/**
|
||||
* Contract tests for Encounter endpoints.
|
||||
*
|
||||
* These tests define the API contract and MUST FAIL initially (TDD RED phase).
|
||||
*
|
||||
* @package KiviCare_API\Tests\Contract
|
||||
*/
|
||||
|
||||
/**
|
||||
* Encounter endpoints contract tests.
|
||||
*/
|
||||
class Test_Encounter_Endpoints_Contract extends KiviCare_API_Test_Case {
|
||||
|
||||
/**
|
||||
* Test GET /wp-json/kivicare/v1/encounters endpoint contract.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_get_encounters_endpoint_contract() {
|
||||
// This test will fail initially as the endpoint doesn't exist yet
|
||||
$this->markTestIncomplete( 'Encounters GET endpoint not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Authenticated doctor
|
||||
wp_set_current_user( $this->doctor_user );
|
||||
|
||||
// ACT: Make GET request to encounters endpoint
|
||||
$response = $this->make_request( '/wp-json/kivicare/v1/encounters' );
|
||||
|
||||
// ASSERT: Response contract
|
||||
$this->assertRestResponse( $response, 200 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertIsArray( $data );
|
||||
|
||||
// Validate pagination structure
|
||||
if ( ! empty( $data ) ) {
|
||||
$this->assertArrayHasKey( 'data', $data );
|
||||
$this->assertArrayHasKey( 'total', $data );
|
||||
$this->assertArrayHasKey( 'page', $data );
|
||||
$this->assertArrayHasKey( 'per_page', $data );
|
||||
|
||||
// Validate encounter data structure
|
||||
$encounter = $data['data'][0];
|
||||
$this->assertEncounterStructure( $encounter );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test POST /wp-json/kivicare/v1/encounters endpoint contract.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_create_encounter_endpoint_contract() {
|
||||
// This test will fail initially as the endpoint doesn't exist yet
|
||||
$this->markTestIncomplete( 'Encounters POST endpoint not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Valid encounter data
|
||||
$clinic_id = $this->create_test_clinic();
|
||||
$appointment_id = $this->create_test_appointment( $clinic_id, $this->doctor_user, $this->patient_user );
|
||||
|
||||
$encounter_data = array(
|
||||
'encounter_date' => gmdate( 'Y-m-d' ),
|
||||
'appointment_id' => $appointment_id,
|
||||
'patient_id' => $this->patient_user,
|
||||
'doctor_id' => $this->doctor_user,
|
||||
'clinic_id' => $clinic_id,
|
||||
'description' => 'Patient presents with mild fever and fatigue. Diagnosed with common cold.',
|
||||
'status' => 1,
|
||||
'chief_complaint' => 'Fever and fatigue',
|
||||
'diagnosis' => 'Common cold (J00)',
|
||||
'treatment_plan' => 'Rest, fluids, symptomatic treatment',
|
||||
);
|
||||
|
||||
// ACT: Make POST request as doctor
|
||||
$response = $this->make_request( '/wp-json/kivicare/v1/encounters', 'POST', $encounter_data, $this->doctor_user );
|
||||
|
||||
// ASSERT: Response contract
|
||||
$this->assertRestResponse( $response, 201 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertEncounterStructure( $data );
|
||||
$this->assertEquals( $encounter_data['appointment_id'], $data['appointment_id'] );
|
||||
$this->assertEquals( $encounter_data['patient_id'], $data['patient_id'] );
|
||||
$this->assertIsInt( $data['id'] );
|
||||
$this->assertGreaterThan( 0, $data['id'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test POST /wp-json/kivicare/v1/encounters with invalid data.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_create_encounter_invalid_data() {
|
||||
// This test will fail initially as the endpoint doesn't exist yet
|
||||
$this->markTestIncomplete( 'Encounters POST validation not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Invalid encounter data
|
||||
$invalid_data = array(
|
||||
'encounter_date' => 'invalid-date',
|
||||
'appointment_id' => 'not_a_number',
|
||||
'description' => '', // Empty description should fail
|
||||
);
|
||||
|
||||
// ACT: Make POST request with invalid data
|
||||
$response = $this->make_request( '/wp-json/kivicare/v1/encounters', 'POST', $invalid_data, $this->doctor_user );
|
||||
|
||||
// ASSERT: Validation error contract
|
||||
$this->assertRestResponse( $response, 400 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertArrayHasKey( 'code', $data );
|
||||
$this->assertEquals( 'rest_invalid_param', $data['code'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test GET /wp-json/kivicare/v1/encounters/{id} endpoint contract.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_get_encounter_by_id_endpoint_contract() {
|
||||
// This test will fail initially as the endpoint doesn't exist yet
|
||||
$this->markTestIncomplete( 'Encounter by ID endpoint not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Existing encounter
|
||||
$clinic_id = $this->create_test_clinic();
|
||||
$appointment_id = $this->create_test_appointment( $clinic_id, $this->doctor_user, $this->patient_user );
|
||||
$encounter_id = $this->create_test_encounter( $appointment_id );
|
||||
|
||||
// ACT: Make GET request for specific encounter
|
||||
$response = $this->make_request( "/wp-json/kivicare/v1/encounters/{$encounter_id}", 'GET', array(), $this->doctor_user );
|
||||
|
||||
// ASSERT: Response contract
|
||||
$this->assertRestResponse( $response, 200 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertEncounterStructure( $data );
|
||||
$this->assertEquals( $encounter_id, $data['id'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test PUT /wp-json/kivicare/v1/encounters/{id} endpoint contract.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_update_encounter_endpoint_contract() {
|
||||
// This test will fail initially as the endpoint doesn't exist yet
|
||||
$this->markTestIncomplete( 'Encounter PUT endpoint not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Existing encounter and update data
|
||||
$clinic_id = $this->create_test_clinic();
|
||||
$appointment_id = $this->create_test_appointment( $clinic_id, $this->doctor_user, $this->patient_user );
|
||||
$encounter_id = $this->create_test_encounter( $appointment_id );
|
||||
|
||||
$update_data = array(
|
||||
'description' => 'Updated encounter notes with additional observations.',
|
||||
'diagnosis' => 'Viral upper respiratory infection (J06.9)',
|
||||
'treatment_plan' => 'Updated treatment plan with additional recommendations.',
|
||||
'status' => 1,
|
||||
);
|
||||
|
||||
// ACT: Make PUT request to update encounter
|
||||
$response = $this->make_request( "/wp-json/kivicare/v1/encounters/{$encounter_id}", 'PUT', $update_data, $this->doctor_user );
|
||||
|
||||
// ASSERT: Response contract
|
||||
$this->assertRestResponse( $response, 200 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertEncounterStructure( $data );
|
||||
$this->assertEquals( $update_data['description'], $data['description'] );
|
||||
$this->assertEquals( $update_data['diagnosis'], $data['diagnosis'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test GET /wp-json/kivicare/v1/encounters/{id}/prescriptions endpoint contract.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_get_encounter_prescriptions_contract() {
|
||||
// This test will fail initially as the endpoint doesn't exist yet
|
||||
$this->markTestIncomplete( 'Encounter prescriptions endpoint not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Encounter with prescriptions
|
||||
$clinic_id = $this->create_test_clinic();
|
||||
$appointment_id = $this->create_test_appointment( $clinic_id, $this->doctor_user, $this->patient_user );
|
||||
$encounter_id = $this->create_test_encounter( $appointment_id );
|
||||
|
||||
// ACT: Make GET request for encounter prescriptions
|
||||
$response = $this->make_request( "/wp-json/kivicare/v1/encounters/{$encounter_id}/prescriptions", 'GET', array(), $this->doctor_user );
|
||||
|
||||
// ASSERT: Response contract
|
||||
$this->assertRestResponse( $response, 200 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertIsArray( $data );
|
||||
|
||||
if ( ! empty( $data ) ) {
|
||||
$prescription = $data[0];
|
||||
$this->assertPrescriptionStructure( $prescription );
|
||||
$this->assertEquals( $encounter_id, $prescription['encounter_id'] );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test medical encounter workflow integration.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_encounter_workflow_contract() {
|
||||
// This test will fail initially as the workflow isn't implemented
|
||||
$this->markTestIncomplete( 'Encounter workflow not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Complete appointment to encounter workflow
|
||||
$clinic_id = $this->create_test_clinic();
|
||||
$appointment_id = $this->create_test_appointment( $clinic_id, $this->doctor_user, $this->patient_user );
|
||||
|
||||
// ACT: Create encounter from appointment
|
||||
$encounter_data = array(
|
||||
'appointment_id' => $appointment_id,
|
||||
'description' => 'Patient consultation completed successfully.',
|
||||
'status' => 1,
|
||||
);
|
||||
|
||||
$response = $this->make_request( '/wp-json/kivicare/v1/encounters', 'POST', $encounter_data, $this->doctor_user );
|
||||
|
||||
// ASSERT: Encounter creation triggers appointment status update
|
||||
$this->assertRestResponse( $response, 201 );
|
||||
|
||||
$encounter = $response->get_data();
|
||||
$this->assertEncounterStructure( $encounter );
|
||||
|
||||
// Verify appointment status was updated
|
||||
$appointment_response = $this->make_request( "/wp-json/kivicare/v1/appointments/{$appointment_id}", 'GET', array(), $this->doctor_user );
|
||||
$appointment = $appointment_response->get_data();
|
||||
$this->assertEquals( 'completed', $appointment['status'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test encounter access permissions by role.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_encounter_permissions_contract() {
|
||||
// This test will fail initially as permissions aren't implemented
|
||||
$this->markTestIncomplete( 'Encounter permissions not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Encounter created by doctor
|
||||
$clinic_id = $this->create_test_clinic();
|
||||
$appointment_id = $this->create_test_appointment( $clinic_id, $this->doctor_user, $this->patient_user );
|
||||
$encounter_id = $this->create_test_encounter( $appointment_id );
|
||||
|
||||
// ACT & ASSERT: Patient should be able to view their encounters (read-only)
|
||||
$response = $this->make_request( "/wp-json/kivicare/v1/encounters/{$encounter_id}", 'GET', array(), $this->patient_user );
|
||||
$this->assertRestResponse( $response, 200 );
|
||||
|
||||
// ACT & ASSERT: Patient should not be able to modify encounters
|
||||
$response = $this->make_request( "/wp-json/kivicare/v1/encounters/{$encounter_id}", 'PUT', array( 'description' => 'Hacked' ), $this->patient_user );
|
||||
$this->assertRestResponse( $response, 403 );
|
||||
|
||||
// ACT & ASSERT: Receptionist should not access medical encounters
|
||||
$response = $this->make_request( "/wp-json/kivicare/v1/encounters/{$encounter_id}", 'GET', array(), $this->receptionist_user );
|
||||
$this->assertRestResponse( $response, 403 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to create test encounter.
|
||||
*
|
||||
* @param int $appointment_id Appointment ID.
|
||||
* @return int Encounter ID.
|
||||
*/
|
||||
private function create_test_encounter( $appointment_id ) {
|
||||
global $wpdb;
|
||||
|
||||
$encounter_data = array(
|
||||
'encounter_date' => gmdate( 'Y-m-d' ),
|
||||
'clinic_id' => get_option( 'kivicare_api_test_clinic_id', 1 ),
|
||||
'doctor_id' => $this->doctor_user,
|
||||
'patient_id' => $this->patient_user,
|
||||
'appointment_id' => $appointment_id,
|
||||
'description' => 'Test medical encounter',
|
||||
'status' => 1,
|
||||
'added_by' => $this->doctor_user,
|
||||
'created_at' => current_time( 'mysql' ),
|
||||
);
|
||||
|
||||
$wpdb->insert( $wpdb->prefix . 'kc_patient_encounters', $encounter_data );
|
||||
return $wpdb->insert_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to assert encounter data structure.
|
||||
*
|
||||
* @param array $encounter Encounter data to validate.
|
||||
*/
|
||||
private function assertEncounterStructure( $encounter ) {
|
||||
$this->assertIsArray( $encounter );
|
||||
|
||||
// Required fields
|
||||
$expected_fields = array(
|
||||
'id', 'encounter_date', 'patient_id', 'doctor_id',
|
||||
'clinic_id', 'appointment_id', 'description', 'status', 'created_at'
|
||||
);
|
||||
|
||||
foreach ( $expected_fields as $field ) {
|
||||
$this->assertArrayHasKey( $field, $encounter );
|
||||
}
|
||||
|
||||
// Data type validations
|
||||
$this->assertIsInt( $encounter['id'] );
|
||||
$this->assertIsInt( $encounter['patient_id'] );
|
||||
$this->assertIsInt( $encounter['doctor_id'] );
|
||||
$this->assertIsInt( $encounter['clinic_id'] );
|
||||
$this->assertIsInt( $encounter['status'] );
|
||||
|
||||
// Date format validation
|
||||
$this->assertMatchesRegularExpression( '/^\d{4}-\d{2}-\d{2}$/', $encounter['encounter_date'] );
|
||||
|
||||
// Optional expanded data
|
||||
if ( isset( $encounter['patient'] ) ) {
|
||||
$this->assertIsArray( $encounter['patient'] );
|
||||
$this->assertArrayHasKey( 'display_name', $encounter['patient'] );
|
||||
}
|
||||
|
||||
if ( isset( $encounter['doctor'] ) ) {
|
||||
$this->assertIsArray( $encounter['doctor'] );
|
||||
$this->assertArrayHasKey( 'display_name', $encounter['doctor'] );
|
||||
}
|
||||
|
||||
if ( isset( $encounter['prescriptions'] ) ) {
|
||||
$this->assertIsArray( $encounter['prescriptions'] );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to assert prescription data structure.
|
||||
*
|
||||
* @param array $prescription Prescription data to validate.
|
||||
*/
|
||||
private function assertPrescriptionStructure( $prescription ) {
|
||||
$this->assertIsArray( $prescription );
|
||||
|
||||
$expected_fields = array(
|
||||
'id', 'encounter_id', 'patient_id', 'name',
|
||||
'frequency', 'duration', 'instruction', 'created_at'
|
||||
);
|
||||
|
||||
foreach ( $expected_fields as $field ) {
|
||||
$this->assertArrayHasKey( $field, $prescription );
|
||||
}
|
||||
|
||||
$this->assertIsInt( $prescription['id'] );
|
||||
$this->assertIsInt( $prescription['encounter_id'] );
|
||||
$this->assertIsInt( $prescription['patient_id'] );
|
||||
$this->assertIsString( $prescription['name'] );
|
||||
}
|
||||
}
|
||||
328
tests/contract/test-patient-endpoints.php
Normal file
328
tests/contract/test-patient-endpoints.php
Normal file
@@ -0,0 +1,328 @@
|
||||
/**
|
||||
* Descomplicar® Crescimento Digital
|
||||
* https://descomplicar.pt
|
||||
*/
|
||||
|
||||
<?php
|
||||
/**
|
||||
* Contract tests for Patient endpoints.
|
||||
*
|
||||
* These tests define the API contract and MUST FAIL initially (TDD RED phase).
|
||||
*
|
||||
* @package KiviCare_API\Tests\Contract
|
||||
*/
|
||||
|
||||
/**
|
||||
* Patient endpoints contract tests.
|
||||
*/
|
||||
class Test_Patient_Endpoints_Contract extends KiviCare_API_Test_Case {
|
||||
|
||||
/**
|
||||
* Test GET /wp-json/kivicare/v1/patients endpoint contract.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_get_patients_endpoint_contract() {
|
||||
// This test will fail initially as the endpoint doesn't exist yet
|
||||
$this->markTestIncomplete( 'Patients GET endpoint not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Authenticated doctor
|
||||
wp_set_current_user( $this->doctor_user );
|
||||
|
||||
// ACT: Make GET request to patients endpoint
|
||||
$response = $this->make_request( '/wp-json/kivicare/v1/patients' );
|
||||
|
||||
// ASSERT: Response contract
|
||||
$this->assertRestResponse( $response, 200 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertIsArray( $data );
|
||||
|
||||
// Validate pagination structure
|
||||
if ( ! empty( $data ) ) {
|
||||
$this->assertArrayHasKey( 'data', $data );
|
||||
$this->assertArrayHasKey( 'total', $data );
|
||||
$this->assertArrayHasKey( 'page', $data );
|
||||
$this->assertArrayHasKey( 'per_page', $data );
|
||||
|
||||
// Validate patient data structure
|
||||
$patient = $data['data'][0];
|
||||
$this->assertPatientStructure( $patient );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test POST /wp-json/kivicare/v1/patients endpoint contract.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_create_patient_endpoint_contract() {
|
||||
// This test will fail initially as the endpoint doesn't exist yet
|
||||
$this->markTestIncomplete( 'Patients POST endpoint not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Valid patient data
|
||||
$patient_data = array(
|
||||
'display_name' => 'João Silva Santos',
|
||||
'user_email' => 'joao.santos@example.com',
|
||||
'clinic_id' => $this->create_test_clinic(),
|
||||
'first_name' => 'João',
|
||||
'last_name' => 'Santos',
|
||||
'phone' => '+351912345678',
|
||||
'address' => 'Rua das Flores, 123',
|
||||
'city' => 'Lisboa',
|
||||
'postal_code' => '1000-001',
|
||||
'birth_date' => '1985-05-15',
|
||||
'gender' => 'M',
|
||||
);
|
||||
|
||||
// ACT: Make POST request as doctor
|
||||
$response = $this->make_request( '/wp-json/kivicare/v1/patients', 'POST', $patient_data, $this->doctor_user );
|
||||
|
||||
// ASSERT: Response contract
|
||||
$this->assertRestResponse( $response, 201 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertPatientStructure( $data );
|
||||
$this->assertEquals( $patient_data['display_name'], $data['display_name'] );
|
||||
$this->assertEquals( $patient_data['user_email'], $data['user_email'] );
|
||||
$this->assertIsInt( $data['id'] );
|
||||
$this->assertGreaterThan( 0, $data['id'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test POST /wp-json/kivicare/v1/patients with invalid data.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_create_patient_invalid_data() {
|
||||
// This test will fail initially as the endpoint doesn't exist yet
|
||||
$this->markTestIncomplete( 'Patients POST validation not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Invalid patient data
|
||||
$invalid_data = array(
|
||||
'display_name' => '', // Empty name should fail
|
||||
'user_email' => 'invalid-email', // Invalid email format
|
||||
'clinic_id' => 'not_a_number', // Invalid clinic ID
|
||||
);
|
||||
|
||||
// ACT: Make POST request with invalid data
|
||||
$response = $this->make_request( '/wp-json/kivicare/v1/patients', 'POST', $invalid_data, $this->doctor_user );
|
||||
|
||||
// ASSERT: Validation error contract
|
||||
$this->assertRestResponse( $response, 400 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertArrayHasKey( 'code', $data );
|
||||
$this->assertEquals( 'rest_invalid_param', $data['code'] );
|
||||
$this->assertArrayHasKey( 'data', $data );
|
||||
$this->assertArrayHasKey( 'params', $data['data'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test GET /wp-json/kivicare/v1/patients/{id} endpoint contract.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_get_patient_by_id_endpoint_contract() {
|
||||
// This test will fail initially as the endpoint doesn't exist yet
|
||||
$this->markTestIncomplete( 'Patient by ID endpoint not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Existing patient
|
||||
$patient_id = $this->patient_user;
|
||||
|
||||
// ACT: Make GET request for specific patient
|
||||
$response = $this->make_request( "/wp-json/kivicare/v1/patients/{$patient_id}", 'GET', array(), $this->doctor_user );
|
||||
|
||||
// ASSERT: Response contract
|
||||
$this->assertRestResponse( $response, 200 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertPatientStructure( $data );
|
||||
$this->assertEquals( $patient_id, $data['id'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test PUT /wp-json/kivicare/v1/patients/{id} endpoint contract.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_update_patient_endpoint_contract() {
|
||||
// This test will fail initially as the endpoint doesn't exist yet
|
||||
$this->markTestIncomplete( 'Patient PUT endpoint not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Existing patient and update data
|
||||
$patient_id = $this->patient_user;
|
||||
$update_data = array(
|
||||
'phone' => '+351987654321',
|
||||
'address' => 'Nova Morada, 456',
|
||||
);
|
||||
|
||||
// ACT: Make PUT request to update patient
|
||||
$response = $this->make_request( "/wp-json/kivicare/v1/patients/{$patient_id}", 'PUT', $update_data, $this->doctor_user );
|
||||
|
||||
// ASSERT: Response contract
|
||||
$this->assertRestResponse( $response, 200 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertPatientStructure( $data );
|
||||
$this->assertEquals( $update_data['phone'], $data['phone'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test GET /wp-json/kivicare/v1/patients/{id}/encounters endpoint contract.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_get_patient_encounters_contract() {
|
||||
// This test will fail initially as the endpoint doesn't exist yet
|
||||
$this->markTestIncomplete( 'Patient encounters endpoint not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Patient with encounters
|
||||
$patient_id = $this->patient_user;
|
||||
$clinic_id = $this->create_test_clinic();
|
||||
$appointment_id = $this->create_test_appointment( $clinic_id, $this->doctor_user, $patient_id );
|
||||
|
||||
// ACT: Make GET request for patient encounters
|
||||
$response = $this->make_request( "/wp-json/kivicare/v1/patients/{$patient_id}/encounters", 'GET', array(), $this->doctor_user );
|
||||
|
||||
// ASSERT: Response contract
|
||||
$this->assertRestResponse( $response, 200 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertIsArray( $data );
|
||||
|
||||
if ( ! empty( $data ) ) {
|
||||
$encounter = $data[0];
|
||||
$this->assertEncounterStructure( $encounter );
|
||||
$this->assertEquals( $patient_id, $encounter['patient_id'] );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test GET /wp-json/kivicare/v1/patients/{id}/prescriptions endpoint contract.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_get_patient_prescriptions_contract() {
|
||||
// This test will fail initially as the endpoint doesn't exist yet
|
||||
$this->markTestIncomplete( 'Patient prescriptions endpoint not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Patient with prescriptions
|
||||
$patient_id = $this->patient_user;
|
||||
|
||||
// ACT: Make GET request for patient prescriptions
|
||||
$response = $this->make_request( "/wp-json/kivicare/v1/patients/{$patient_id}/prescriptions", 'GET', array(), $this->doctor_user );
|
||||
|
||||
// ASSERT: Response contract
|
||||
$this->assertRestResponse( $response, 200 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertIsArray( $data );
|
||||
|
||||
if ( ! empty( $data ) ) {
|
||||
$prescription = $data[0];
|
||||
$this->assertPrescriptionStructure( $prescription );
|
||||
$this->assertEquals( $patient_id, $prescription['patient_id'] );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test patient privacy and data access restrictions.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_patient_privacy_contract() {
|
||||
// This test will fail initially as privacy controls aren't implemented
|
||||
$this->markTestIncomplete( 'Patient privacy controls not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Two different patients
|
||||
$patient1_id = $this->patient_user;
|
||||
$patient2_id = $this->factory->user->create( array( 'role' => 'patient' ) );
|
||||
|
||||
// ACT & ASSERT: Patient should only see their own data
|
||||
$response = $this->make_request( "/wp-json/kivicare/v1/patients/{$patient1_id}", 'GET', array(), $patient1_id );
|
||||
$this->assertRestResponse( $response, 200 );
|
||||
|
||||
// ACT & ASSERT: Patient should not see other patient's data
|
||||
$response = $this->make_request( "/wp-json/kivicare/v1/patients/{$patient2_id}", 'GET', array(), $patient1_id );
|
||||
$this->assertRestResponse( $response, 403 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to assert patient data structure.
|
||||
*
|
||||
* @param array $patient Patient data to validate.
|
||||
*/
|
||||
private function assertPatientStructure( $patient ) {
|
||||
$this->assertIsArray( $patient );
|
||||
|
||||
// Required fields from wp_users
|
||||
$this->assertArrayHasKey( 'id', $patient );
|
||||
$this->assertArrayHasKey( 'display_name', $patient );
|
||||
$this->assertArrayHasKey( 'user_email', $patient );
|
||||
|
||||
// Additional patient fields
|
||||
$expected_fields = array(
|
||||
'first_name', 'last_name', 'phone', 'address',
|
||||
'city', 'postal_code', 'birth_date', 'gender',
|
||||
'clinic_id', 'registration_date'
|
||||
);
|
||||
|
||||
foreach ( $expected_fields as $field ) {
|
||||
$this->assertArrayHasKey( $field, $patient );
|
||||
}
|
||||
|
||||
// Data type validations
|
||||
$this->assertIsInt( $patient['id'] );
|
||||
$this->assertIsString( $patient['display_name'] );
|
||||
$this->assertIsString( $patient['user_email'] );
|
||||
|
||||
if ( ! empty( $patient['clinic_id'] ) ) {
|
||||
$this->assertIsInt( $patient['clinic_id'] );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to assert encounter data structure.
|
||||
*
|
||||
* @param array $encounter Encounter data to validate.
|
||||
*/
|
||||
private function assertEncounterStructure( $encounter ) {
|
||||
$this->assertIsArray( $encounter );
|
||||
|
||||
$expected_fields = array(
|
||||
'id', 'encounter_date', 'patient_id', 'doctor_id',
|
||||
'clinic_id', 'appointment_id', 'description', 'status'
|
||||
);
|
||||
|
||||
foreach ( $expected_fields as $field ) {
|
||||
$this->assertArrayHasKey( $field, $encounter );
|
||||
}
|
||||
|
||||
$this->assertIsInt( $encounter['id'] );
|
||||
$this->assertIsInt( $encounter['patient_id'] );
|
||||
$this->assertIsInt( $encounter['doctor_id'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to assert prescription data structure.
|
||||
*
|
||||
* @param array $prescription Prescription data to validate.
|
||||
*/
|
||||
private function assertPrescriptionStructure( $prescription ) {
|
||||
$this->assertIsArray( $prescription );
|
||||
|
||||
$expected_fields = array(
|
||||
'id', 'encounter_id', 'patient_id', 'name',
|
||||
'frequency', 'duration', 'instruction'
|
||||
);
|
||||
|
||||
foreach ( $expected_fields as $field ) {
|
||||
$this->assertArrayHasKey( $field, $prescription );
|
||||
}
|
||||
|
||||
$this->assertIsInt( $prescription['id'] );
|
||||
$this->assertIsInt( $prescription['patient_id'] );
|
||||
$this->assertIsString( $prescription['name'] );
|
||||
}
|
||||
}
|
||||
381
tests/contract/test-prescription-endpoints.php
Normal file
381
tests/contract/test-prescription-endpoints.php
Normal file
@@ -0,0 +1,381 @@
|
||||
/**
|
||||
* Descomplicar® Crescimento Digital
|
||||
* https://descomplicar.pt
|
||||
*/
|
||||
|
||||
<?php
|
||||
/**
|
||||
* Contract tests for Prescription endpoints.
|
||||
*
|
||||
* These tests define the API contract and MUST FAIL initially (TDD RED phase).
|
||||
*
|
||||
* @package KiviCare_API\Tests\Contract
|
||||
*/
|
||||
|
||||
/**
|
||||
* Prescription endpoints contract tests.
|
||||
*/
|
||||
class Test_Prescription_Endpoints_Contract extends KiviCare_API_Test_Case {
|
||||
|
||||
/**
|
||||
* Test POST /wp-json/kivicare/v1/encounters/{id}/prescriptions endpoint contract.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_create_prescription_endpoint_contract() {
|
||||
// This test will fail initially as the endpoint doesn't exist yet
|
||||
$this->markTestIncomplete( 'Prescriptions POST endpoint not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Valid prescription data
|
||||
$clinic_id = $this->create_test_clinic();
|
||||
$appointment_id = $this->create_test_appointment( $clinic_id, $this->doctor_user, $this->patient_user );
|
||||
$encounter_id = $this->create_test_encounter( $appointment_id );
|
||||
|
||||
$prescription_data = array(
|
||||
'name' => 'Paracetamol 500mg',
|
||||
'frequency' => 'Every 8 hours',
|
||||
'duration' => '7 days',
|
||||
'instruction' => 'Take with water after meals. Do not exceed recommended dose.',
|
||||
'dosage' => '1 tablet',
|
||||
'quantity' => '21 tablets',
|
||||
);
|
||||
|
||||
// ACT: Make POST request as doctor
|
||||
$response = $this->make_request( "/wp-json/kivicare/v1/encounters/{$encounter_id}/prescriptions", 'POST', $prescription_data, $this->doctor_user );
|
||||
|
||||
// ASSERT: Response contract
|
||||
$this->assertRestResponse( $response, 201 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertPrescriptionStructure( $data );
|
||||
$this->assertEquals( $prescription_data['name'], $data['name'] );
|
||||
$this->assertEquals( $prescription_data['frequency'], $data['frequency'] );
|
||||
$this->assertEquals( $encounter_id, $data['encounter_id'] );
|
||||
$this->assertIsInt( $data['id'] );
|
||||
$this->assertGreaterThan( 0, $data['id'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test POST /wp-json/kivicare/v1/encounters/{id}/prescriptions with invalid data.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_create_prescription_invalid_data() {
|
||||
// This test will fail initially as the endpoint doesn't exist yet
|
||||
$this->markTestIncomplete( 'Prescriptions POST validation not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Invalid prescription data
|
||||
$clinic_id = $this->create_test_clinic();
|
||||
$appointment_id = $this->create_test_appointment( $clinic_id, $this->doctor_user, $this->patient_user );
|
||||
$encounter_id = $this->create_test_encounter( $appointment_id );
|
||||
|
||||
$invalid_data = array(
|
||||
'name' => '', // Empty name should fail
|
||||
'frequency' => '', // Empty frequency should fail
|
||||
'duration' => 'invalid duration format',
|
||||
);
|
||||
|
||||
// ACT: Make POST request with invalid data
|
||||
$response = $this->make_request( "/wp-json/kivicare/v1/encounters/{$encounter_id}/prescriptions", 'POST', $invalid_data, $this->doctor_user );
|
||||
|
||||
// ASSERT: Validation error contract
|
||||
$this->assertRestResponse( $response, 400 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertArrayHasKey( 'code', $data );
|
||||
$this->assertEquals( 'rest_invalid_param', $data['code'] );
|
||||
$this->assertArrayHasKey( 'data', $data );
|
||||
$this->assertArrayHasKey( 'params', $data['data'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test GET /wp-json/kivicare/v1/prescriptions/{id} endpoint contract.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_get_prescription_by_id_contract() {
|
||||
// This test will fail initially as the endpoint doesn't exist yet
|
||||
$this->markTestIncomplete( 'Prescription by ID endpoint not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Existing prescription
|
||||
$clinic_id = $this->create_test_clinic();
|
||||
$appointment_id = $this->create_test_appointment( $clinic_id, $this->doctor_user, $this->patient_user );
|
||||
$encounter_id = $this->create_test_encounter( $appointment_id );
|
||||
$prescription_id = $this->create_test_prescription( $encounter_id );
|
||||
|
||||
// ACT: Make GET request for specific prescription
|
||||
$response = $this->make_request( "/wp-json/kivicare/v1/prescriptions/{$prescription_id}", 'GET', array(), $this->doctor_user );
|
||||
|
||||
// ASSERT: Response contract
|
||||
$this->assertRestResponse( $response, 200 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertPrescriptionStructure( $data );
|
||||
$this->assertEquals( $prescription_id, $data['id'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test PUT /wp-json/kivicare/v1/prescriptions/{id} endpoint contract.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_update_prescription_contract() {
|
||||
// This test will fail initially as the endpoint doesn't exist yet
|
||||
$this->markTestIncomplete( 'Prescription PUT endpoint not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Existing prescription and update data
|
||||
$clinic_id = $this->create_test_clinic();
|
||||
$appointment_id = $this->create_test_appointment( $clinic_id, $this->doctor_user, $this->patient_user );
|
||||
$encounter_id = $this->create_test_encounter( $appointment_id );
|
||||
$prescription_id = $this->create_test_prescription( $encounter_id );
|
||||
|
||||
$update_data = array(
|
||||
'frequency' => 'Every 6 hours',
|
||||
'duration' => '10 days',
|
||||
'instruction' => 'Updated instructions: Take with food to avoid stomach irritation.',
|
||||
);
|
||||
|
||||
// ACT: Make PUT request to update prescription
|
||||
$response = $this->make_request( "/wp-json/kivicare/v1/prescriptions/{$prescription_id}", 'PUT', $update_data, $this->doctor_user );
|
||||
|
||||
// ASSERT: Response contract
|
||||
$this->assertRestResponse( $response, 200 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertPrescriptionStructure( $data );
|
||||
$this->assertEquals( $update_data['frequency'], $data['frequency'] );
|
||||
$this->assertEquals( $update_data['duration'], $data['duration'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test DELETE /wp-json/kivicare/v1/prescriptions/{id} endpoint contract.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_delete_prescription_contract() {
|
||||
// This test will fail initially as the endpoint doesn't exist yet
|
||||
$this->markTestIncomplete( 'Prescription DELETE endpoint not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Existing prescription
|
||||
$clinic_id = $this->create_test_clinic();
|
||||
$appointment_id = $this->create_test_appointment( $clinic_id, $this->doctor_user, $this->patient_user );
|
||||
$encounter_id = $this->create_test_encounter( $appointment_id );
|
||||
$prescription_id = $this->create_test_prescription( $encounter_id );
|
||||
|
||||
// ACT: Make DELETE request
|
||||
$response = $this->make_request( "/wp-json/kivicare/v1/prescriptions/{$prescription_id}", 'DELETE', array(), $this->doctor_user );
|
||||
|
||||
// ASSERT: Response contract
|
||||
$this->assertRestResponse( $response, 200 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertArrayHasKey( 'deleted', $data );
|
||||
$this->assertTrue( $data['deleted'] );
|
||||
$this->assertEquals( $prescription_id, $data['id'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test prescription bulk operations contract.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_bulk_prescription_operations_contract() {
|
||||
// This test will fail initially as bulk operations aren't implemented
|
||||
$this->markTestIncomplete( 'Bulk prescription operations not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Multiple prescriptions for an encounter
|
||||
$clinic_id = $this->create_test_clinic();
|
||||
$appointment_id = $this->create_test_appointment( $clinic_id, $this->doctor_user, $this->patient_user );
|
||||
$encounter_id = $this->create_test_encounter( $appointment_id );
|
||||
|
||||
$bulk_prescriptions = array(
|
||||
array(
|
||||
'name' => 'Paracetamol 500mg',
|
||||
'frequency' => 'Every 8 hours',
|
||||
'duration' => '7 days',
|
||||
'instruction' => 'Take with water after meals',
|
||||
),
|
||||
array(
|
||||
'name' => 'Ibuprofen 400mg',
|
||||
'frequency' => 'Every 12 hours',
|
||||
'duration' => '5 days',
|
||||
'instruction' => 'Take with food to prevent stomach upset',
|
||||
),
|
||||
);
|
||||
|
||||
// ACT: Make bulk POST request
|
||||
$response = $this->make_request( "/wp-json/kivicare/v1/encounters/{$encounter_id}/prescriptions/bulk", 'POST', array( 'prescriptions' => $bulk_prescriptions ), $this->doctor_user );
|
||||
|
||||
// ASSERT: Bulk response contract
|
||||
$this->assertRestResponse( $response, 201 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertIsArray( $data );
|
||||
$this->assertArrayHasKey( 'created', $data );
|
||||
$this->assertCount( 2, $data['created'] );
|
||||
|
||||
foreach ( $data['created'] as $prescription ) {
|
||||
$this->assertPrescriptionStructure( $prescription );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test prescription permissions by role.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_prescription_permissions_contract() {
|
||||
// This test will fail initially as permissions aren't implemented
|
||||
$this->markTestIncomplete( 'Prescription permissions not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Prescription created by doctor
|
||||
$clinic_id = $this->create_test_clinic();
|
||||
$appointment_id = $this->create_test_appointment( $clinic_id, $this->doctor_user, $this->patient_user );
|
||||
$encounter_id = $this->create_test_encounter( $appointment_id );
|
||||
$prescription_id = $this->create_test_prescription( $encounter_id );
|
||||
|
||||
// ACT & ASSERT: Only doctors should be able to create prescriptions
|
||||
$prescription_data = array(
|
||||
'name' => 'Test Medicine',
|
||||
'frequency' => 'Daily',
|
||||
'duration' => '5 days',
|
||||
);
|
||||
|
||||
$response = $this->make_request( "/wp-json/kivicare/v1/encounters/{$encounter_id}/prescriptions", 'POST', $prescription_data, $this->patient_user );
|
||||
$this->assertRestResponse( $response, 403 );
|
||||
|
||||
$response = $this->make_request( "/wp-json/kivicare/v1/encounters/{$encounter_id}/prescriptions", 'POST', $prescription_data, $this->receptionist_user );
|
||||
$this->assertRestResponse( $response, 403 );
|
||||
|
||||
// ACT & ASSERT: Patients should be able to view their prescriptions (read-only)
|
||||
$response = $this->make_request( "/wp-json/kivicare/v1/prescriptions/{$prescription_id}", 'GET', array(), $this->patient_user );
|
||||
$this->assertRestResponse( $response, 200 );
|
||||
|
||||
// ACT & ASSERT: Patients should not be able to modify prescriptions
|
||||
$response = $this->make_request( "/wp-json/kivicare/v1/prescriptions/{$prescription_id}", 'PUT', array( 'frequency' => 'Hacked' ), $this->patient_user );
|
||||
$this->assertRestResponse( $response, 403 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test prescription drug interaction warnings.
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function test_prescription_drug_interactions_contract() {
|
||||
// This test will fail initially as drug interaction checking isn't implemented
|
||||
$this->markTestIncomplete( 'Drug interaction checking not implemented yet - TDD RED phase' );
|
||||
|
||||
// ARRANGE: Patient with existing prescription
|
||||
$clinic_id = $this->create_test_clinic();
|
||||
$appointment_id = $this->create_test_appointment( $clinic_id, $this->doctor_user, $this->patient_user );
|
||||
$encounter_id = $this->create_test_encounter( $appointment_id );
|
||||
|
||||
// Create first prescription
|
||||
$first_prescription = array(
|
||||
'name' => 'Warfarin 5mg',
|
||||
'frequency' => 'Daily',
|
||||
'duration' => '30 days',
|
||||
);
|
||||
$this->make_request( "/wp-json/kivicare/v1/encounters/{$encounter_id}/prescriptions", 'POST', $first_prescription, $this->doctor_user );
|
||||
|
||||
// ACT: Try to add potentially interacting drug
|
||||
$interacting_prescription = array(
|
||||
'name' => 'Aspirin 100mg',
|
||||
'frequency' => 'Daily',
|
||||
'duration' => '7 days',
|
||||
);
|
||||
$response = $this->make_request( "/wp-json/kivicare/v1/encounters/{$encounter_id}/prescriptions", 'POST', $interacting_prescription, $this->doctor_user );
|
||||
|
||||
// ASSERT: Should return warning but allow prescription
|
||||
$this->assertRestResponse( $response, 201 );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertArrayHasKey( 'warnings', $data );
|
||||
$this->assertArrayHasKey( 'drug_interactions', $data['warnings'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to create test encounter.
|
||||
*
|
||||
* @param int $appointment_id Appointment ID.
|
||||
* @return int Encounter ID.
|
||||
*/
|
||||
private function create_test_encounter( $appointment_id ) {
|
||||
global $wpdb;
|
||||
|
||||
$encounter_data = array(
|
||||
'encounter_date' => gmdate( 'Y-m-d' ),
|
||||
'clinic_id' => get_option( 'kivicare_api_test_clinic_id', 1 ),
|
||||
'doctor_id' => $this->doctor_user,
|
||||
'patient_id' => $this->patient_user,
|
||||
'appointment_id' => $appointment_id,
|
||||
'description' => 'Test medical encounter',
|
||||
'status' => 1,
|
||||
'added_by' => $this->doctor_user,
|
||||
'created_at' => current_time( 'mysql' ),
|
||||
);
|
||||
|
||||
$wpdb->insert( $wpdb->prefix . 'kc_patient_encounters', $encounter_data );
|
||||
return $wpdb->insert_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to create test prescription.
|
||||
*
|
||||
* @param int $encounter_id Encounter ID.
|
||||
* @return int Prescription ID.
|
||||
*/
|
||||
private function create_test_prescription( $encounter_id ) {
|
||||
global $wpdb;
|
||||
|
||||
$prescription_data = array(
|
||||
'encounter_id' => $encounter_id,
|
||||
'patient_id' => $this->patient_user,
|
||||
'name' => 'Test Medicine 100mg',
|
||||
'frequency' => 'Every 8 hours',
|
||||
'duration' => '7 days',
|
||||
'instruction' => 'Take with water',
|
||||
'added_by' => $this->doctor_user,
|
||||
'created_at' => current_time( 'mysql' ),
|
||||
);
|
||||
|
||||
$wpdb->insert( $wpdb->prefix . 'kc_prescription', $prescription_data );
|
||||
return $wpdb->insert_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to assert prescription data structure.
|
||||
*
|
||||
* @param array $prescription Prescription data to validate.
|
||||
*/
|
||||
private function assertPrescriptionStructure( $prescription ) {
|
||||
$this->assertIsArray( $prescription );
|
||||
|
||||
$expected_fields = array(
|
||||
'id', 'encounter_id', 'patient_id', 'name',
|
||||
'frequency', 'duration', 'instruction', 'added_by', 'created_at'
|
||||
);
|
||||
|
||||
foreach ( $expected_fields as $field ) {
|
||||
$this->assertArrayHasKey( $field, $prescription );
|
||||
}
|
||||
|
||||
// Data type validations
|
||||
$this->assertIsInt( $prescription['id'] );
|
||||
$this->assertIsInt( $prescription['encounter_id'] );
|
||||
$this->assertIsInt( $prescription['patient_id'] );
|
||||
$this->assertIsString( $prescription['name'] );
|
||||
$this->assertIsString( $prescription['frequency'] );
|
||||
$this->assertIsString( $prescription['duration'] );
|
||||
|
||||
// Optional fields that might be present
|
||||
if ( isset( $prescription['encounter'] ) ) {
|
||||
$this->assertIsArray( $prescription['encounter'] );
|
||||
}
|
||||
|
||||
if ( isset( $prescription['patient'] ) ) {
|
||||
$this->assertIsArray( $prescription['patient'] );
|
||||
$this->assertArrayHasKey( 'display_name', $prescription['patient'] );
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user