781f171770
- TMDO_Zone_Router::delete_post() deleted from wp_wpdo_hot_/cold_{post_type}
whenever fields were registered for that zone, so every post delete on a
HivePress site (cold fields registered, module idle, table never created)
logged a "table doesn't exist" error. Hot and cold deletes now also need
the table to exist; module state is not checked, so a table left by a
rolled-back module still loses the rows of deleted posts.
- Live suite zero_side_effect: saving / updating / deleting an hp_listing
issues no query on missing zone tables (covers PR-054 and this PR).
- Verify: ZoneRouterTest 22 OK; live zero_side_effect + shadow_write +
serialized 9/9 PASS (old code: zero_side_effect FAIL); unit 687 OK,
integration 484 OK, phpcs (phpcs.xml) 0, stan OK
- PR: PR-054b
299 lines
12 KiB
PHP
299 lines
12 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
/**
|
|
* Unit tests for WPDO_Zone_Router — central zone dispatch and module-name resolution.
|
|
*/
|
|
class ZoneRouterTest extends TestCase {
|
|
|
|
/** Last SQL passed to $wpdb->query(). */
|
|
public static string $last_query = '';
|
|
|
|
/** Last $wpdb->delete() call args: [table, where, formats]. */
|
|
public static array $last_delete = [];
|
|
|
|
/** Stub return for $wpdb->get_var(). */
|
|
public static mixed $get_var_return = null;
|
|
|
|
protected function setUp(): void {
|
|
self::$last_query = '';
|
|
self::$last_delete = [];
|
|
self::$get_var_return = null;
|
|
WPDO_Schema_Manager::flush_table_exists_cache();
|
|
$this->setup_wpdb_mock();
|
|
}
|
|
|
|
/**
|
|
* $wpdb mock recording delete() tables; SHOW TABLES reports $existing.
|
|
*
|
|
* @param string[] $existing Tables that exist.
|
|
*/
|
|
private function delete_logger( array &$captured, array $existing ): void {
|
|
global $wpdb;
|
|
$wpdb = new class( $captured, $existing ) {
|
|
public string $prefix = 'wp_';
|
|
public array $log;
|
|
public function __construct( array &$ref, private array $existing ) { $this->log = &$ref; }
|
|
public function prepare( string $s, mixed ...$a ): string { return str_replace( '%s', (string) ( $a[0] ?? '' ), $s ); }
|
|
public function query( string $s ): int { return 1; }
|
|
public function get_var( string $s ): mixed {
|
|
foreach ( $this->existing as $t ) {
|
|
if ( "SHOW TABLES LIKE {$t}" === $s ) {
|
|
return $t;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
public function insert( string $t, array $d, mixed $f = null ): int { return 1; }
|
|
public function delete( string $table, array $where, array $formats ): void {
|
|
$this->log[] = $table;
|
|
}
|
|
};
|
|
}
|
|
|
|
private function register_hot_and_cold(): void {
|
|
$sr = new ReflectionClass( WPDO_Schema_Registry::class );
|
|
$sr->getProperty( 'instance' )->setValue( null, null );
|
|
WPDO_Schema_Registry::instance()->register( 'test', array( 'post_type' => 'hp_listing', 'meta_key' => 'hp_price', 'zone' => 'hot', 'column' => 'hp_price', 'type' => 'decimal' ) );
|
|
WPDO_Schema_Registry::instance()->register( 'test', array( 'post_type' => 'hp_listing', 'meta_key' => 'hp_images', 'zone' => 'cold' ) );
|
|
}
|
|
|
|
/** Registered fields do not mean the zone tables exist (zero side effect). */
|
|
public function test_delete_post_skips_hot_and_cold_tables_that_do_not_exist(): void {
|
|
$this->register_hot_and_cold();
|
|
$captured = array();
|
|
$this->delete_logger( $captured, array() );
|
|
|
|
WPDO_Zone_Router::delete_post( 3, 'hp_listing' );
|
|
|
|
$this->assertNotContains( 'wp_wpdo_hot_hp_listing', $captured );
|
|
$this->assertNotContains( 'wp_wpdo_cold_hp_listing', $captured );
|
|
$this->assertContains( 'wp_wpdo_warm', $captured );
|
|
}
|
|
|
|
public function test_delete_post_deletes_cold_rows_when_the_table_exists(): void {
|
|
$this->register_hot_and_cold();
|
|
$captured = array();
|
|
$this->delete_logger( $captured, array( 'wp_wpdo_cold_hp_listing' ) );
|
|
|
|
WPDO_Zone_Router::delete_post( 4, 'hp_listing' );
|
|
|
|
$this->assertContains( 'wp_wpdo_cold_hp_listing', $captured );
|
|
$this->assertNotContains( 'wp_wpdo_hot_hp_listing', $captured );
|
|
}
|
|
|
|
private function setup_wpdb_mock(): void {
|
|
global $wpdb;
|
|
$wpdb = new class {
|
|
public string $prefix = 'wp_';
|
|
public function prepare( string $sql, mixed ...$args ): string {
|
|
$i = 0;
|
|
return preg_replace_callback( '/%([sd])/', static function ( $m ) use ( &$i, $args ) {
|
|
$val = $args[ $i++ ] ?? '';
|
|
return $m[1] === 'd' ? (string) (int) $val : "'" . addslashes( (string) $val ) . "'";
|
|
}, $sql );
|
|
}
|
|
public function query( string $sql ): int {
|
|
ZoneRouterTest::$last_query = $sql;
|
|
return 1;
|
|
}
|
|
public function get_var( string $sql ): mixed {
|
|
ZoneRouterTest::$last_query = $sql;
|
|
return ZoneRouterTest::$get_var_return;
|
|
}
|
|
public function insert( string $table, array $data, mixed $formats = null ): int {
|
|
ZoneRouterTest::$last_query = "INSERT INTO `{$table}`";
|
|
return 1;
|
|
}
|
|
public function delete( string $table, array $where, array $formats ): void {
|
|
ZoneRouterTest::$last_delete = [ $table, $where, $formats ];
|
|
}
|
|
public function get_row( string $sql, string $output = OBJECT ): mixed {
|
|
ZoneRouterTest::$last_query = $sql;
|
|
return null;
|
|
}
|
|
};
|
|
}
|
|
|
|
// ── module_name ───────────────────────────────────────────────────────────
|
|
|
|
public function test_module_name_hot_prepends_zone(): void {
|
|
$this->assertSame( 'hot_hp_listing', WPDO_Zone_Router::module_name( 'hot', 'hp_listing' ) );
|
|
}
|
|
|
|
public function test_module_name_cold_prepends_zone(): void {
|
|
$this->assertSame( 'cold_hp_listing', WPDO_Zone_Router::module_name( 'cold', 'hp_listing' ) );
|
|
}
|
|
|
|
public function test_module_name_warm_ignores_post_type(): void {
|
|
$this->assertSame( 'warm', WPDO_Zone_Router::module_name( 'warm', 'hp_listing' ) );
|
|
}
|
|
|
|
public function test_module_name_archive_ignores_post_type(): void {
|
|
$this->assertSame( 'archive', WPDO_Zone_Router::module_name( 'archive', 'hp_listing' ) );
|
|
}
|
|
|
|
public function test_module_name_unknown_zone_returns_unknown(): void {
|
|
$this->assertSame( 'unknown', WPDO_Zone_Router::module_name( 'other', 'hp_listing' ) );
|
|
}
|
|
|
|
public function test_module_name_sanitizes_post_type(): void {
|
|
// sanitize_key strips spaces and lowercases; 'HP Listing' → 'hplisting'.
|
|
$this->assertSame( 'hot_hplisting', WPDO_Zone_Router::module_name( 'hot', 'HP Listing' ) );
|
|
}
|
|
|
|
// ── read ─────────────────────────────────────────────────────────────────
|
|
|
|
public function test_read_hot_calls_zone_hot_get(): void {
|
|
$field = array( 'zone' => 'hot', 'column' => 'hp_price' );
|
|
WPDO_Zone_Router::read( $field, 42, 'hp_listing', 'hp_price' );
|
|
$this->assertStringContainsString( 'wpdo_hot_hp_listing', self::$last_query );
|
|
$this->assertStringContainsString( 'hp_price', self::$last_query );
|
|
$this->assertStringContainsString( '42', self::$last_query );
|
|
}
|
|
|
|
public function test_read_warm_calls_zone_warm_get(): void {
|
|
$field = array( 'zone' => 'warm' );
|
|
WPDO_Zone_Router::read( $field, 7, 'hp_listing', 'views' );
|
|
$this->assertStringContainsString( 'wpdo_warm', self::$last_query );
|
|
}
|
|
|
|
public function test_read_cold_calls_zone_cold_get(): void {
|
|
$field = array( 'zone' => 'cold' );
|
|
WPDO_Zone_Router::read( $field, 5, 'hp_listing', 'hp_desc' );
|
|
$this->assertStringContainsString( 'wpdo_cold_hp_listing', self::$last_query );
|
|
}
|
|
|
|
public function test_read_unknown_zone_returns_null(): void {
|
|
$field = array( 'zone' => 'other' );
|
|
$result = WPDO_Zone_Router::read( $field, 1, 'hp_listing', 'key' );
|
|
$this->assertNull( $result );
|
|
}
|
|
|
|
// ── write ─────────────────────────────────────────────────────────────────
|
|
|
|
public function test_write_hot_calls_zone_hot_set(): void {
|
|
$field = array( 'zone' => 'hot', 'column' => 'hp_price' );
|
|
WPDO_Zone_Router::write( $field, 42, 'hp_listing', 'hp_price', '100' );
|
|
$this->assertStringContainsString( 'wpdo_hot_hp_listing', self::$last_query );
|
|
$this->assertStringContainsString( 'hp_price', self::$last_query );
|
|
}
|
|
|
|
public function test_write_warm_calls_zone_warm_set(): void {
|
|
$field = array( 'zone' => 'warm', 'ttl' => null );
|
|
WPDO_Zone_Router::write( $field, 7, 'hp_listing', 'views', '5' );
|
|
$this->assertStringContainsString( 'wpdo_warm', self::$last_query );
|
|
}
|
|
|
|
public function test_write_warm_json_encodes_non_string_value(): void {
|
|
$field = array( 'zone' => 'warm', 'ttl' => null );
|
|
WPDO_Zone_Router::write( $field, 7, 'hp_listing', 'data', array( 'a' => 1 ) );
|
|
$this->assertStringContainsString( 'wpdo_warm', self::$last_query );
|
|
}
|
|
|
|
public function test_write_unknown_zone_is_noop(): void {
|
|
$field = array( 'zone' => 'other' );
|
|
WPDO_Zone_Router::write( $field, 1, 'hp_listing', 'key', 'val' );
|
|
$this->assertSame( '', self::$last_query );
|
|
}
|
|
|
|
// ── delete_field ──────────────────────────────────────────────────────────
|
|
|
|
public function test_delete_field_hot_nulls_column(): void {
|
|
$field = array( 'zone' => 'hot', 'column' => 'hp_price' );
|
|
WPDO_Zone_Router::delete_field( $field, 42, 'hp_listing', 'hp_price' );
|
|
$this->assertStringContainsString( 'wpdo_hot_hp_listing', self::$last_query );
|
|
// Hot delete sets the column to NULL via an UPDATE.
|
|
$this->assertStringContainsString( 'NULL', self::$last_query );
|
|
}
|
|
|
|
public function test_delete_field_warm_calls_delete(): void {
|
|
$field = array( 'zone' => 'warm' );
|
|
WPDO_Zone_Router::delete_field( $field, 7, 'hp_listing', 'views' );
|
|
// Table name includes $wpdb->prefix ('wp_') + 'wpdo_warm'.
|
|
$this->assertSame( 'wp_wpdo_warm', self::$last_delete[0] ?? '' );
|
|
}
|
|
|
|
public function test_delete_field_unknown_zone_is_noop(): void {
|
|
$field = array( 'zone' => 'other' );
|
|
WPDO_Zone_Router::delete_field( $field, 1, 'hp_listing', 'key' );
|
|
$this->assertSame( '', self::$last_query );
|
|
$this->assertSame( [], self::$last_delete );
|
|
}
|
|
|
|
// ── delete_post ───────────────────────────────────────────────────────────
|
|
|
|
public function test_delete_post_skips_hot_when_no_hot_columns(): void {
|
|
$sr = new ReflectionClass( WPDO_Schema_Registry::class );
|
|
$sr->getProperty( 'instance' )->setValue( null, null );
|
|
|
|
$captured = array();
|
|
global $wpdb;
|
|
$wpdb = new class( $captured ) {
|
|
public string $prefix = 'wp_';
|
|
public array $log;
|
|
public function __construct( array &$ref ) { $this->log = &$ref; }
|
|
public function prepare( string $s, mixed ...$a ): string { return $s; }
|
|
public function query( string $s ): int { return 1; }
|
|
public function get_var( string $s ): mixed { return null; }
|
|
public function insert( string $t, array $d, mixed $f = null ): int { return 1; }
|
|
public function delete( string $table, array $where, array $formats ): void {
|
|
$this->log[] = $table;
|
|
}
|
|
};
|
|
|
|
WPDO_Zone_Router::delete_post( 1, 'hp_listing' );
|
|
|
|
$this->assertNotContains( 'wp_wpdo_hot_hp_listing', $captured, 'Hot should be skipped when no columns registered' );
|
|
$this->assertContains( 'wp_wpdo_warm', $captured );
|
|
$this->assertContains( 'wp_wpdo_archive', $captured );
|
|
}
|
|
|
|
public function test_delete_post_deletes_hot_when_columns_registered(): void {
|
|
$sr = new ReflectionClass( WPDO_Schema_Registry::class );
|
|
$sr->getProperty( 'instance' )->setValue( null, null );
|
|
|
|
WPDO_Schema_Registry::instance()->register( 'test', array(
|
|
'post_type' => 'hp_listing',
|
|
'meta_key' => 'hp_price',
|
|
'zone' => 'hot',
|
|
'column' => 'hp_price',
|
|
'type' => 'decimal',
|
|
) );
|
|
|
|
$captured = array();
|
|
$this->delete_logger( $captured, array( 'wp_wpdo_hot_hp_listing' ) );
|
|
|
|
WPDO_Zone_Router::delete_post( 10, 'hp_listing' );
|
|
|
|
$this->assertContains( 'wp_wpdo_hot_hp_listing', $captured );
|
|
}
|
|
|
|
public function test_delete_post_always_clears_warm_and_archive(): void {
|
|
$sr = new ReflectionClass( WPDO_Schema_Registry::class );
|
|
$sr->getProperty( 'instance' )->setValue( null, null );
|
|
|
|
$captured = array();
|
|
global $wpdb;
|
|
$wpdb = new class( $captured ) {
|
|
public string $prefix = 'wp_';
|
|
public array $log;
|
|
public function __construct( array &$ref ) { $this->log = &$ref; }
|
|
public function prepare( string $s, mixed ...$a ): string { return $s; }
|
|
public function query( string $s ): int { return 1; }
|
|
public function get_var( string $s ): mixed { return null; }
|
|
public function insert( string $t, array $d, mixed $f = null ): int { return 1; }
|
|
public function delete( string $table, array $where, array $formats ): void {
|
|
$this->log[] = $table;
|
|
}
|
|
};
|
|
|
|
WPDO_Zone_Router::delete_post( 5, 'hp_listing' );
|
|
|
|
$this->assertContains( 'wp_wpdo_warm', $captured );
|
|
$this->assertContains( 'wp_wpdo_archive', $captured );
|
|
}
|
|
}
|