- 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>
351 lines
13 KiB
PHP
351 lines
13 KiB
PHP
/**
|
|
* Descomplicar® Crescimento Digital
|
|
* https://descomplicar.pt
|
|
*/
|
|
|
|
<?php
|
|
/**
|
|
* Restriction model CRUD tests for Care Booking Block plugin
|
|
*
|
|
* @package CareBookingBlock
|
|
*/
|
|
|
|
/**
|
|
* Test restriction model CRUD operations
|
|
*/
|
|
class Test_Restriction_Model extends Care_Booking_Test_Case
|
|
{
|
|
|
|
/**
|
|
* Test restriction model class exists and can be instantiated
|
|
*/
|
|
public function test_restriction_model_class_exists()
|
|
{
|
|
$this->assertTrue(class_exists('Care_Booking_Restriction_Model'), 'Care_Booking_Restriction_Model class should exist');
|
|
|
|
$model = new Care_Booking_Restriction_Model();
|
|
$this->assertInstanceOf('Care_Booking_Restriction_Model', $model);
|
|
}
|
|
|
|
/**
|
|
* Test create doctor restriction
|
|
*/
|
|
public function test_create_doctor_restriction()
|
|
{
|
|
$model = new Care_Booking_Restriction_Model();
|
|
|
|
$restriction_data = [
|
|
'restriction_type' => 'doctor',
|
|
'target_id' => 999,
|
|
'is_blocked' => true
|
|
];
|
|
|
|
$restriction_id = $model->create($restriction_data);
|
|
$this->assertIsInt($restriction_id, 'Should return integer restriction ID');
|
|
$this->assertGreaterThan(0, $restriction_id, 'Restriction ID should be greater than 0');
|
|
|
|
// Verify restriction was created in database
|
|
$created_restriction = $model->get($restriction_id);
|
|
$this->assertNotFalse($created_restriction, 'Should retrieve created restriction');
|
|
$this->assertEquals('doctor', $created_restriction->restriction_type);
|
|
$this->assertEquals(999, $created_restriction->target_id);
|
|
$this->assertNull($created_restriction->doctor_id);
|
|
$this->assertEquals(1, $created_restriction->is_blocked);
|
|
}
|
|
|
|
/**
|
|
* Test create service restriction
|
|
*/
|
|
public function test_create_service_restriction()
|
|
{
|
|
$model = new Care_Booking_Restriction_Model();
|
|
|
|
$restriction_data = [
|
|
'restriction_type' => 'service',
|
|
'target_id' => 888,
|
|
'doctor_id' => 999,
|
|
'is_blocked' => true
|
|
];
|
|
|
|
$restriction_id = $model->create($restriction_data);
|
|
$this->assertIsInt($restriction_id);
|
|
$this->assertGreaterThan(0, $restriction_id);
|
|
|
|
// Verify restriction was created
|
|
$created_restriction = $model->get($restriction_id);
|
|
$this->assertEquals('service', $created_restriction->restriction_type);
|
|
$this->assertEquals(888, $created_restriction->target_id);
|
|
$this->assertEquals(999, $created_restriction->doctor_id);
|
|
$this->assertEquals(1, $created_restriction->is_blocked);
|
|
}
|
|
|
|
/**
|
|
* Test read restriction by ID
|
|
*/
|
|
public function test_read_restriction_by_id()
|
|
{
|
|
$model = new Care_Booking_Restriction_Model();
|
|
|
|
// Create test restriction
|
|
$restriction_id = $this->create_test_doctor_restriction(999, true);
|
|
$this->assertNotFalse($restriction_id);
|
|
|
|
// Read restriction
|
|
$restriction = $model->get($restriction_id);
|
|
$this->assertNotFalse($restriction, 'Should retrieve restriction by ID');
|
|
$this->assertEquals($restriction_id, $restriction->id);
|
|
$this->assertEquals('doctor', $restriction->restriction_type);
|
|
$this->assertEquals(999, $restriction->target_id);
|
|
}
|
|
|
|
/**
|
|
* Test get restrictions by type
|
|
*/
|
|
public function test_get_restrictions_by_type()
|
|
{
|
|
$model = new Care_Booking_Restriction_Model();
|
|
|
|
// Create test restrictions
|
|
$this->create_test_doctor_restriction(999, true);
|
|
$this->create_test_doctor_restriction(998, false);
|
|
$this->create_test_service_restriction(888, 999, true);
|
|
|
|
// Get doctor restrictions
|
|
$doctor_restrictions = $model->get_by_type('doctor');
|
|
$this->assertIsArray($doctor_restrictions, 'Should return array of doctor restrictions');
|
|
$this->assertCount(2, $doctor_restrictions, 'Should return 2 doctor restrictions');
|
|
|
|
// Verify all are doctor type
|
|
foreach ($doctor_restrictions as $restriction) {
|
|
$this->assertEquals('doctor', $restriction->restriction_type);
|
|
}
|
|
|
|
// Get service restrictions
|
|
$service_restrictions = $model->get_by_type('service');
|
|
$this->assertIsArray($service_restrictions);
|
|
$this->assertCount(1, $service_restrictions, 'Should return 1 service restriction');
|
|
$this->assertEquals('service', $service_restrictions[0]->restriction_type);
|
|
}
|
|
|
|
/**
|
|
* Test get blocked doctors
|
|
*/
|
|
public function test_get_blocked_doctors()
|
|
{
|
|
$model = new Care_Booking_Restriction_Model();
|
|
|
|
// Create test restrictions
|
|
$this->create_test_doctor_restriction(999, true); // Blocked
|
|
$this->create_test_doctor_restriction(998, false); // Not blocked
|
|
$this->create_test_doctor_restriction(997, true); // Blocked
|
|
|
|
$blocked_doctors = $model->get_blocked_doctors();
|
|
$this->assertIsArray($blocked_doctors, 'Should return array of blocked doctor IDs');
|
|
$this->assertCount(2, $blocked_doctors, 'Should return 2 blocked doctors');
|
|
|
|
// Check that correct doctors are blocked
|
|
$this->assertContains(999, $blocked_doctors);
|
|
$this->assertContains(997, $blocked_doctors);
|
|
$this->assertNotContains(998, $blocked_doctors, 'Non-blocked doctor should not be in list');
|
|
}
|
|
|
|
/**
|
|
* Test get blocked services for specific doctor
|
|
*/
|
|
public function test_get_blocked_services_by_doctor()
|
|
{
|
|
$model = new Care_Booking_Restriction_Model();
|
|
|
|
// Create test service restrictions for doctor 999
|
|
$this->create_test_service_restriction(888, 999, true); // Blocked
|
|
$this->create_test_service_restriction(887, 999, false); // Not blocked
|
|
$this->create_test_service_restriction(886, 998, true); // Different doctor
|
|
|
|
$blocked_services = $model->get_blocked_services(999);
|
|
$this->assertIsArray($blocked_services);
|
|
$this->assertCount(1, $blocked_services, 'Should return 1 blocked service for doctor 999');
|
|
$this->assertContains(888, $blocked_services);
|
|
$this->assertNotContains(887, $blocked_services, 'Non-blocked service should not be in list');
|
|
$this->assertNotContains(886, $blocked_services, 'Service for different doctor should not be in list');
|
|
}
|
|
|
|
/**
|
|
* Test update restriction
|
|
*/
|
|
public function test_update_restriction()
|
|
{
|
|
$model = new Care_Booking_Restriction_Model();
|
|
|
|
// Create test restriction
|
|
$restriction_id = $this->create_test_doctor_restriction(999, true);
|
|
|
|
// Update restriction
|
|
$update_data = ['is_blocked' => false];
|
|
$result = $model->update($restriction_id, $update_data);
|
|
$this->assertTrue($result, 'Update should return true on success');
|
|
|
|
// Verify update
|
|
$updated_restriction = $model->get($restriction_id);
|
|
$this->assertEquals(0, $updated_restriction->is_blocked, 'is_blocked should be updated to false');
|
|
|
|
// Verify updated_at timestamp changed
|
|
$this->assertNotNull($updated_restriction->updated_at);
|
|
}
|
|
|
|
/**
|
|
* Test delete restriction
|
|
*/
|
|
public function test_delete_restriction()
|
|
{
|
|
$model = new Care_Booking_Restriction_Model();
|
|
|
|
// Create test restriction
|
|
$restriction_id = $this->create_test_doctor_restriction(999, true);
|
|
|
|
// Verify restriction exists
|
|
$restriction = $model->get($restriction_id);
|
|
$this->assertNotFalse($restriction);
|
|
|
|
// Delete restriction
|
|
$result = $model->delete($restriction_id);
|
|
$this->assertTrue($result, 'Delete should return true on success');
|
|
|
|
// Verify restriction no longer exists
|
|
$deleted_restriction = $model->get($restriction_id);
|
|
$this->assertFalse($deleted_restriction, 'Restriction should not exist after deletion');
|
|
}
|
|
|
|
/**
|
|
* Test find existing restriction
|
|
*/
|
|
public function test_find_existing_restriction()
|
|
{
|
|
$model = new Care_Booking_Restriction_Model();
|
|
|
|
// Create doctor restriction
|
|
$doctor_restriction_id = $this->create_test_doctor_restriction(999, true);
|
|
|
|
// Find existing doctor restriction
|
|
$found_doctor = $model->find_existing('doctor', 999);
|
|
$this->assertNotFalse($found_doctor, 'Should find existing doctor restriction');
|
|
$this->assertEquals($doctor_restriction_id, $found_doctor->id);
|
|
|
|
// Create service restriction
|
|
$service_restriction_id = $this->create_test_service_restriction(888, 999, true);
|
|
|
|
// Find existing service restriction
|
|
$found_service = $model->find_existing('service', 888, 999);
|
|
$this->assertNotFalse($found_service, 'Should find existing service restriction');
|
|
$this->assertEquals($service_restriction_id, $found_service->id);
|
|
|
|
// Try to find non-existing restriction
|
|
$not_found = $model->find_existing('doctor', 123);
|
|
$this->assertFalse($not_found, 'Should return false for non-existing restriction');
|
|
}
|
|
|
|
/**
|
|
* Test toggle restriction (create or update)
|
|
*/
|
|
public function test_toggle_restriction()
|
|
{
|
|
$model = new Care_Booking_Restriction_Model();
|
|
|
|
// Toggle non-existing restriction (should create)
|
|
$result = $model->toggle('doctor', 999, null, true);
|
|
$this->assertIsInt($result, 'Should return restriction ID when creating');
|
|
|
|
// Verify restriction was created
|
|
$restriction = $model->find_existing('doctor', 999);
|
|
$this->assertNotFalse($restriction);
|
|
$this->assertEquals(1, $restriction->is_blocked);
|
|
|
|
// Toggle existing restriction (should update)
|
|
$result2 = $model->toggle('doctor', 999, null, false);
|
|
$this->assertTrue($result2, 'Should return true when updating existing');
|
|
|
|
// Verify restriction was updated
|
|
$updated_restriction = $model->find_existing('doctor', 999);
|
|
$this->assertEquals(0, $updated_restriction->is_blocked);
|
|
}
|
|
|
|
/**
|
|
* Test validation errors
|
|
*/
|
|
public function test_validation_errors()
|
|
{
|
|
$model = new Care_Booking_Restriction_Model();
|
|
|
|
// Test invalid restriction type
|
|
$invalid_data = [
|
|
'restriction_type' => 'invalid',
|
|
'target_id' => 999,
|
|
'is_blocked' => true
|
|
];
|
|
|
|
$result = $model->create($invalid_data);
|
|
$this->assertFalse($result, 'Should return false for invalid restriction type');
|
|
|
|
// Test missing target_id
|
|
$invalid_data2 = [
|
|
'restriction_type' => 'doctor',
|
|
'is_blocked' => true
|
|
];
|
|
|
|
$result2 = $model->create($invalid_data2);
|
|
$this->assertFalse($result2, 'Should return false for missing target_id');
|
|
|
|
// Test service restriction without doctor_id
|
|
$invalid_data3 = [
|
|
'restriction_type' => 'service',
|
|
'target_id' => 888,
|
|
'is_blocked' => true
|
|
];
|
|
|
|
$result3 = $model->create($invalid_data3);
|
|
$this->assertFalse($result3, 'Should return false for service restriction without doctor_id');
|
|
}
|
|
|
|
/**
|
|
* Test bulk operations
|
|
*/
|
|
public function test_bulk_operations()
|
|
{
|
|
$model = new Care_Booking_Restriction_Model();
|
|
|
|
$bulk_data = [
|
|
[
|
|
'restriction_type' => 'doctor',
|
|
'target_id' => 999,
|
|
'is_blocked' => true
|
|
],
|
|
[
|
|
'restriction_type' => 'doctor',
|
|
'target_id' => 998,
|
|
'is_blocked' => true
|
|
],
|
|
[
|
|
'restriction_type' => 'service',
|
|
'target_id' => 888,
|
|
'doctor_id' => 999,
|
|
'is_blocked' => false
|
|
]
|
|
];
|
|
|
|
$results = $model->bulk_create($bulk_data);
|
|
$this->assertIsArray($results, 'Should return array of results');
|
|
$this->assertCount(3, $results, 'Should return 3 results');
|
|
|
|
// Verify all were created successfully
|
|
foreach ($results as $result) {
|
|
$this->assertIsInt($result, 'Each result should be a restriction ID');
|
|
$this->assertGreaterThan(0, $result);
|
|
}
|
|
|
|
// Verify restrictions exist
|
|
$doctor_restrictions = $model->get_by_type('doctor');
|
|
$this->assertCount(2, $doctor_restrictions);
|
|
|
|
$service_restrictions = $model->get_by_type('service');
|
|
$this->assertCount(1, $service_restrictions);
|
|
}
|
|
} |