/** * Descomplicar® Crescimento Digital * https://descomplicar.pt */ server = $wp_rest_server = new WP_REST_Server; do_action( 'rest_api_init' ); // Create test user with proper roles $this->create_test_users(); } /** * Teardown after each test. */ public function tearDown(): void { // Clear any cached data wp_cache_flush(); // Reset REST server global $wp_rest_server; $wp_rest_server = null; parent::tearDown(); } /** * Create test users for different roles. */ protected function create_test_users() { // Administrator $this->admin_user = $this->factory->user->create( array( 'user_login' => 'test_admin', 'user_email' => 'admin@example.com', 'role' => 'administrator', ) ); // Doctor $this->doctor_user = $this->factory->user->create( array( 'user_login' => 'test_doctor', 'user_email' => 'doctor@example.com', 'role' => 'doctor', ) ); // Patient $this->patient_user = $this->factory->user->create( array( 'user_login' => 'test_patient', 'user_email' => 'patient@example.com', 'role' => 'patient', ) ); // Receptionist $this->receptionist_user = $this->factory->user->create( array( 'user_login' => 'test_receptionist', 'user_email' => 'receptionist@example.com', 'role' => 'kivicare_receptionist', ) ); } /** * Helper method to make REST API requests. * * @param string $endpoint The API endpoint. * @param string $method HTTP method. * @param array $data Request data. * @param int $user_id User ID for authentication. * @return WP_REST_Response */ protected function make_request( $endpoint, $method = 'GET', $data = array(), $user_id = null ) { $request = new WP_REST_Request( $method, $endpoint ); if ( ! empty( $data ) ) { $request->set_body_params( $data ); } if ( $user_id ) { wp_set_current_user( $user_id ); } return $this->server->dispatch( $request ); } /** * Helper method to create test clinic data. * * @return int Clinic ID. */ protected function create_test_clinic() { global $wpdb; $clinic_data = array( 'name' => 'Test Clinic', 'email' => 'test@clinic.com', 'telephone_no' => '+351912345678', 'address' => 'Test Address', 'city' => 'Lisboa', 'state' => 'Lisboa', 'country' => 'Portugal', 'postal_code' => '1000-001', 'status' => 1, 'clinic_admin_id' => $this->admin_user, 'created_at' => current_time( 'mysql' ), ); $wpdb->insert( $wpdb->prefix . 'kc_clinics', $clinic_data ); return $wpdb->insert_id; } /** * Helper method to create test appointment. * * @param int $clinic_id Clinic ID. * @param int $doctor_id Doctor user ID. * @param int $patient_id Patient user ID. * @return int Appointment ID. */ protected function create_test_appointment( $clinic_id, $doctor_id, $patient_id ) { global $wpdb; $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', 'clinic_id' => $clinic_id, 'doctor_id' => $doctor_id, 'patient_id' => $patient_id, 'status' => 1, 'visit_type' => 'consultation', 'description' => 'Test appointment', 'created_at' => current_time( 'mysql' ), ); $wpdb->insert( $wpdb->prefix . 'kc_appointments', $appointment_data ); return $wpdb->insert_id; } /** * Assert that response has correct REST API format. * * @param WP_REST_Response $response The response object. * @param int $status Expected status code. */ protected function assertRestResponse( $response, $status = 200 ) { $this->assertInstanceOf( 'WP_REST_Response', $response ); $this->assertEquals( $status, $response->get_status() ); if ( $status >= 400 ) { $data = $response->get_data(); $this->assertArrayHasKey( 'code', $data ); $this->assertArrayHasKey( 'message', $data ); } } }