Files
care-book-block-ultimate/BACKUP-ESSENTIALS/PRODUCTION-READY/care-booking-block-ultimate/tests/unit/test-database-schema.php
Emanuel Almeida 38bb926742 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>
2025-09-12 01:27:34 +01:00

287 lines
9.7 KiB
PHP

/**
* Descomplicar® Crescimento Digital
* https://descomplicar.pt
*/
<?php
/**
* Database schema tests for Care Booking Block plugin
*
* @package CareBookingBlock
*/
/**
* Test database schema creation and structure
*/
class Test_Database_Schema extends Care_Booking_Test_Case
{
/**
* Test that wp_care_booking_restrictions table exists
*/
public function test_restrictions_table_exists()
{
global $wpdb;
$table_name = $wpdb->prefix . 'care_booking_restrictions';
$table_exists = $wpdb->get_var("SHOW TABLES LIKE '$table_name'") === $table_name;
$this->assertTrue($table_exists, 'wp_care_booking_restrictions table should exist');
}
/**
* Test table has required columns with correct types
*/
public function test_table_has_correct_columns()
{
global $wpdb;
$table_name = $wpdb->prefix . 'care_booking_restrictions';
$columns = $wpdb->get_results("DESCRIBE $table_name");
// Convert to associative array for easier testing
$column_data = [];
foreach ($columns as $column) {
$column_data[$column->Field] = [
'Type' => $column->Type,
'Null' => $column->Null,
'Key' => $column->Key,
'Default' => $column->Default,
'Extra' => $column->Extra
];
}
// Test required columns exist
$expected_columns = ['id', 'restriction_type', 'target_id', 'doctor_id', 'is_blocked', 'created_at', 'updated_at'];
foreach ($expected_columns as $column) {
$this->assertArrayHasKey($column, $column_data, "Column '$column' should exist");
}
// Test column types
$this->assertEquals('bigint(20) unsigned', $column_data['id']['Type']);
$this->assertEquals("enum('doctor','service')", $column_data['restriction_type']['Type']);
$this->assertEquals('bigint(20) unsigned', $column_data['target_id']['Type']);
$this->assertEquals('bigint(20) unsigned', $column_data['doctor_id']['Type']);
$this->assertEquals('tinyint(1)', $column_data['is_blocked']['Type']);
$this->assertEquals('timestamp', $column_data['created_at']['Type']);
$this->assertEquals('timestamp', $column_data['updated_at']['Type']);
// Test primary key
$this->assertEquals('PRI', $column_data['id']['Key']);
$this->assertEquals('auto_increment', $column_data['id']['Extra']);
// Test null constraints
$this->assertEquals('NO', $column_data['id']['Null']);
$this->assertEquals('NO', $column_data['restriction_type']['Null']);
$this->assertEquals('NO', $column_data['target_id']['Null']);
$this->assertEquals('YES', $column_data['doctor_id']['Null']);
$this->assertEquals('NO', $column_data['is_blocked']['Null']);
// Test default values
$this->assertEquals('0', $column_data['is_blocked']['Default']);
$this->assertEquals('current_timestamp()', $column_data['created_at']['Default']);
}
/**
* Test table has required indexes
*/
public function test_table_has_required_indexes()
{
global $wpdb;
$table_name = $wpdb->prefix . 'care_booking_restrictions';
$indexes = $wpdb->get_results("SHOW INDEX FROM $table_name");
// Convert to associative array for easier testing
$index_data = [];
foreach ($indexes as $index) {
$index_data[$index->Key_name][] = $index->Column_name;
}
// Test required indexes exist
$this->assertArrayHasKey('PRIMARY', $index_data, 'PRIMARY index should exist');
$this->assertArrayHasKey('idx_type_target', $index_data, 'idx_type_target index should exist');
$this->assertArrayHasKey('idx_doctor_service', $index_data, 'idx_doctor_service index should exist');
$this->assertArrayHasKey('idx_blocked', $index_data, 'idx_blocked index should exist');
// Test index columns
$this->assertEquals(['id'], $index_data['PRIMARY']);
$this->assertEquals(['restriction_type', 'target_id'], $index_data['idx_type_target']);
$this->assertEquals(['doctor_id', 'target_id'], $index_data['idx_doctor_service']);
$this->assertEquals(['is_blocked'], $index_data['idx_blocked']);
}
/**
* Test enum constraint on restriction_type
*/
public function test_restriction_type_enum_constraint()
{
global $wpdb;
$table_name = $wpdb->prefix . 'care_booking_restrictions';
// Test valid enum values
$valid_insert = $wpdb->insert(
$table_name,
[
'restriction_type' => 'doctor',
'target_id' => 999,
'is_blocked' => 1
]
);
$this->assertNotFalse($valid_insert, 'Should insert valid restriction_type');
// Clean up
$wpdb->delete($table_name, ['target_id' => 999]);
// Test invalid enum values (this should fail)
$invalid_insert = $wpdb->insert(
$table_name,
[
'restriction_type' => 'invalid_type',
'target_id' => 999,
'is_blocked' => 1
]
);
$this->assertFalse($invalid_insert, 'Should not insert invalid restriction_type');
}
/**
* Test auto-increment behavior on id field
*/
public function test_auto_increment_id_field()
{
global $wpdb;
$table_name = $wpdb->prefix . 'care_booking_restrictions';
// Insert first record
$result1 = $wpdb->insert(
$table_name,
[
'restriction_type' => 'doctor',
'target_id' => 999,
'is_blocked' => 1
]
);
$this->assertNotFalse($result1);
$id1 = $wpdb->insert_id;
// Insert second record
$result2 = $wpdb->insert(
$table_name,
[
'restriction_type' => 'service',
'target_id' => 998,
'doctor_id' => 999,
'is_blocked' => 1
]
);
$this->assertNotFalse($result2);
$id2 = $wpdb->insert_id;
// Test auto-increment
$this->assertGreaterThan($id1, $id2, 'Second ID should be greater than first');
// Clean up
$wpdb->delete($table_name, ['target_id' => 999]);
$wpdb->delete($table_name, ['target_id' => 998]);
}
/**
* Test timestamp fields behavior
*/
public function test_timestamp_fields()
{
global $wpdb;
$table_name = $wpdb->prefix . 'care_booking_restrictions';
// Insert record and check timestamps
$before_insert = current_time('mysql');
$result = $wpdb->insert(
$table_name,
[
'restriction_type' => 'doctor',
'target_id' => 999,
'is_blocked' => 1
]
);
$this->assertNotFalse($result);
$after_insert = current_time('mysql');
// Get the inserted record
$record = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table_name WHERE target_id = %d", 999));
$this->assertNotNull($record);
// Test created_at is set automatically
$this->assertNotNull($record->created_at);
$this->assertGreaterThanOrEqual($before_insert, $record->created_at);
$this->assertLessThanOrEqual($after_insert, $record->created_at);
// Test updated_at matches created_at on insert
$this->assertEquals($record->created_at, $record->updated_at);
// Wait a moment and update record
sleep(1);
$before_update = current_time('mysql');
$wpdb->update(
$table_name,
['is_blocked' => 0],
['target_id' => 999]
);
$after_update = current_time('mysql');
// Get updated record
$updated_record = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table_name WHERE target_id = %d", 999));
// Test updated_at changed automatically
$this->assertNotEquals($record->updated_at, $updated_record->updated_at);
$this->assertGreaterThanOrEqual($before_update, $updated_record->updated_at);
$this->assertLessThanOrEqual($after_update, $updated_record->updated_at);
// Test created_at remained unchanged
$this->assertEquals($record->created_at, $updated_record->created_at);
// Clean up
$wpdb->delete($table_name, ['target_id' => 999]);
}
/**
* Test database table cleanup on plugin deactivation
*/
public function test_plugin_deactivation_preserves_data()
{
global $wpdb;
$table_name = $wpdb->prefix . 'care_booking_restrictions';
// Insert test data
$wpdb->insert(
$table_name,
[
'restriction_type' => 'doctor',
'target_id' => 999,
'is_blocked' => 1
]
);
// Simulate plugin deactivation
$plugin = CareBookingBlock::get_instance();
$plugin->deactivate();
// Check that table and data still exist
$table_exists = $wpdb->get_var("SHOW TABLES LIKE '$table_name'") === $table_name;
$this->assertTrue($table_exists, 'Table should still exist after deactivation');
$record_exists = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $table_name WHERE target_id = %d", 999));
$this->assertEquals('1', $record_exists, 'Data should be preserved after deactivation');
// Clean up
$wpdb->delete($table_name, ['target_id' => 999]);
}
}