refactor(2meet-data-optimizer-hivepress-addon): remove comment mirror writes to never-created tables

- Favorites/Messages adapters reduced to detection only; Requests keeps hot_hp_request fields, drops hp_offer mirror
- Delete TMDO_HivePress_Comment_Router (read path to wpdo_comment_hp_* tables that no installer creates)
- Verify: addon unit 141 OK, integration 52 OK; phpcs 41E/57W → 35E/48W
- PR: PR-013
This commit is contained in:
2026-09-25 22:58:34 +08:00
parent f089b0934b
commit 5777210184
17 changed files with 77 additions and 1108 deletions
-1
View File
@@ -38,7 +38,6 @@ $tmdo_hp_files = array(
'includes/hivepress/class-tmdo-hivepress-detector.php',
'includes/hivepress/class-tmdo-hivepress-conflict-guard.php',
'includes/hivepress/class-tmdo-hivepress-bootstrap.php',
'includes/hivepress/class-tmdo-hivepress-comment-router.php',
'includes/hivepress/class-tmdo-hivepress-cron-optimizer.php',
'includes/hivepress/class-tmdo-hivepress-attribute-bridge.php',
'includes/hivepress/class-tmdo-hivepress-suitability-scorer.php',
@@ -12,147 +12,17 @@ use PHPUnit\Framework\TestCase;
* Exercises against real MariaDB (via tests/integration/bootstrap.php) the
* scenarios that unit tests can only stub:
*
* - Shadow table CREATE actually works with the declared expected_columns
* - UNIQUE constraint on (user_id, listing_id) really prevents dup favorites
* - mirror_insert() roundtrips through real $wpdb (SQL syntax valid)
* - 7-state FSM lifecycle moves WPDO_Feature_Flags correctly
*
* @group integration
*/
class HivepressIntegrationTest extends TestCase {
/** Test-scope shadow table for favorites. */
private const TABLE_FAVORITE = 'wp_itest_wpdo_comment_hp_favorite';
/** Test-scope shadow table for messages. */
private const TABLE_MESSAGE = 'wp_itest_wpdo_comment_hp_message';
public static function setUpBeforeClass(): void {
// HivePress integration files are loaded at file-top level so the
// Testable* subclass definitions below can resolve their parents at
// parse time. Schema setup happens here.
global $wpdb;
$wpdb->query( 'DROP TABLE IF EXISTS `' . self::TABLE_FAVORITE . '`' );
$wpdb->query( "CREATE TABLE `" . self::TABLE_FAVORITE . "` (
comment_id BIGINT UNSIGNED NOT NULL PRIMARY KEY,
user_id BIGINT UNSIGNED NOT NULL,
listing_id BIGINT UNSIGNED NOT NULL,
created_at DATETIME NOT NULL,
UNIQUE KEY unique_user_listing (user_id, listing_id),
KEY idx_listing (listing_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4" );
$wpdb->query( 'DROP TABLE IF EXISTS `' . self::TABLE_MESSAGE . '`' );
$wpdb->query( "CREATE TABLE `" . self::TABLE_MESSAGE . "` (
comment_id BIGINT UNSIGNED NOT NULL PRIMARY KEY,
sender_id BIGINT UNSIGNED NOT NULL,
recipient_id BIGINT UNSIGNED NOT NULL,
listing_id BIGINT UNSIGNED NOT NULL,
is_read TINYINT(1) NOT NULL DEFAULT 0,
sent_at DATETIME NOT NULL,
KEY idx_recipient_unread (recipient_id, is_read),
KEY idx_thread (listing_id, sent_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4" );
}
public static function tearDownAfterClass(): void {
global $wpdb;
$wpdb->query( 'DROP TABLE IF EXISTS `' . self::TABLE_FAVORITE . '`' );
$wpdb->query( 'DROP TABLE IF EXISTS `' . self::TABLE_MESSAGE . '`' );
}
protected function setUp(): void {
global $wpdb;
$wpdb->query( 'TRUNCATE TABLE `' . self::TABLE_FAVORITE . '`' );
$wpdb->query( 'TRUNCATE TABLE `' . self::TABLE_MESSAGE . '`' );
// Clear FSM module state between tests.
$GLOBALS['_wp_options']['wpdo_features'] = array();
}
// ── Schema tests ────────────────────────────────────────────────────────
public function test_favorite_table_has_unique_user_listing_constraint(): void {
global $wpdb;
$row = $wpdb->get_row( 'SHOW CREATE TABLE `' . self::TABLE_FAVORITE . '`', 'ARRAY_A' );
$this->assertNotNull( $row );
$ddl = $row['Create Table'] ?? '';
$this->assertStringContainsString( 'UNIQUE KEY', $ddl );
$this->assertStringContainsString( 'unique_user_listing', $ddl );
}
public function test_message_table_has_recipient_unread_index(): void {
global $wpdb;
$row = $wpdb->get_row( 'SHOW CREATE TABLE `' . self::TABLE_MESSAGE . '`', 'ARRAY_A' );
$this->assertNotNull( $row );
$ddl = $row['Create Table'] ?? '';
$this->assertStringContainsString( 'idx_recipient_unread', $ddl );
$this->assertStringContainsString( 'recipient_id', $ddl );
}
// ── mirror_insert real roundtrip ────────────────────────────────────────
public function test_favorite_mirror_insert_writes_real_row(): void {
global $wpdb;
$adapter = new WPDO_HivePress_Favorites_Adapter();
$comment = new stdClass();
$comment->comment_type = 'hp_favorite';
$comment->user_id = 11;
$comment->comment_post_ID = 222;
$comment->comment_date = '2026-05-04 12:00:00';
$adapter->mirror_insert( 1001, $comment );
$rows = (int) $wpdb->get_var( 'SELECT COUNT(*) FROM `' . self::TABLE_FAVORITE . '`' );
$this->assertSame( 1, $rows );
$saved = $wpdb->get_row( 'SELECT * FROM `' . self::TABLE_FAVORITE . '` WHERE comment_id = 1001', 'ARRAY_A' );
$this->assertSame( '11', (string) $saved['user_id'] );
$this->assertSame( '222', (string) $saved['listing_id'] );
}
public function test_favorite_unique_constraint_blocks_double_tap(): void {
global $wpdb;
$adapter = new WPDO_HivePress_Favorites_Adapter();
// Two different comment_ids but same user+listing — UNIQUE should win.
$comment = new stdClass();
$comment->comment_type = 'hp_favorite';
$comment->user_id = 7;
$comment->comment_post_ID = 99;
$comment->comment_date = '2026-05-04 12:00:00';
$adapter->mirror_insert( 2001, $comment );
$adapter->mirror_insert( 2002, $comment ); // Race-style duplicate.
$rows = (int) $wpdb->get_var( 'SELECT COUNT(*) FROM `' . self::TABLE_FAVORITE . '` WHERE user_id = 7 AND listing_id = 99' );
$this->assertSame( 1, $rows, 'UNIQUE(user_id, listing_id) must reject duplicate.' );
}
public function test_message_mirror_insert_promotes_recipient_id(): void {
global $wpdb;
$adapter = new WPDO_HivePress_Messages_Adapter();
$comment = new stdClass();
$comment->comment_type = 'hp_message';
$comment->user_id = 5; // sender
$comment->comment_karma = 42; // recipient (HP hack)
$comment->comment_post_ID = 99;
$comment->comment_approved = 0;
$comment->comment_date = '2026-05-04 12:00:00';
$adapter->mirror_insert( 3001, $comment );
$saved = $wpdb->get_row( 'SELECT * FROM `' . self::TABLE_MESSAGE . '` WHERE comment_id = 3001', 'ARRAY_A' );
$this->assertNotNull( $saved );
$this->assertSame( '42', (string) $saved['recipient_id'] );
$this->assertSame( '5', (string) $saved['sender_id'] );
// recipient_unread index supports the dashboard query.
$unread_count = (int) $wpdb->get_var( 'SELECT COUNT(*) FROM `' . self::TABLE_MESSAGE . '` WHERE recipient_id = 42 AND is_read = 0' );
$this->assertSame( 1, $unread_count );
}
// ── FSM lifecycle ───────────────────────────────────────────────────────
public function test_fsm_lifecycle_progression(): void {
@@ -189,9 +59,3 @@ class HivepressIntegrationTest extends TestCase {
$this->assertSame( 0, $count, 'Optimizer must short-circuit when module is idle.' );
}
}
// Production adapters use `$wpdb->prefix . self::TABLE` to compose the full
// table name. Integration test bootstrap sets prefix to `wp_itest_`, so the
// production mirror_insert() naturally writes to wp_itest_wpdo_comment_hp_*.
// No subclassing or test-double needed — the production code path is exercised
// directly against real MariaDB with isolated prefix.
-1
View File
@@ -39,7 +39,6 @@ $tmdo_hp_files = array(
'includes/hivepress/class-tmdo-hivepress-detector.php',
'includes/hivepress/class-tmdo-hivepress-conflict-guard.php',
'includes/hivepress/class-tmdo-hivepress-bootstrap.php',
'includes/hivepress/class-tmdo-hivepress-comment-router.php',
'includes/hivepress/class-tmdo-hivepress-cron-optimizer.php',
'includes/hivepress/class-tmdo-hivepress-attribute-bridge.php',
'includes/hivepress/class-tmdo-hivepress-suitability-scorer.php',
-92
View File
@@ -1,92 +0,0 @@
<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
/**
* Tests for WPDO_HivePress_Comment_Router.
*
* @covers WPDO_HivePress_Comment_Router
*/
class HivePressCommentRouterTest extends TestCase {
use WpdbMockTrait;
protected function setUp(): void {
$this->install_wpdb_mock();
WPDO_HivePress_Comment_Router::reset_for_tests();
// Default disabled.
$GLOBALS['_wp_options'][ WPDO_HivePress_Comment_Router::OPTION_ENABLED ] = 0;
}
public function test_disabled_by_default(): void {
$this->assertFalse( WPDO_HivePress_Comment_Router::is_enabled() );
}
public function test_enabled_when_option_set(): void {
$GLOBALS['_wp_options'][ WPDO_HivePress_Comment_Router::OPTION_ENABLED ] = 1;
$this->assertTrue( WPDO_HivePress_Comment_Router::is_enabled() );
}
public function test_register_is_idempotent(): void {
$GLOBALS['_wp_options'][ WPDO_HivePress_Comment_Router::OPTION_ENABLED ] = 1;
WPDO_HivePress_Comment_Router::register();
WPDO_HivePress_Comment_Router::register(); // Second call must be no-op.
$this->assertTrue( true ); // No throw = pass
}
public function test_rewrite_skips_unknown_comment_type(): void {
$query = new stdClass();
$query->query_vars = array( 'type' => 'comment' ); // not in shadow map
$clauses = array( 'join' => '', 'where' => '1=1' );
$out = WPDO_HivePress_Comment_Router::rewrite( $clauses, $query );
$this->assertSame( $clauses, $out );
}
public function test_rewrite_adds_left_join_for_hp_favorite(): void {
$query = new stdClass();
$query->query_vars = array( 'type' => 'hp_favorite' );
$clauses = array( 'join' => '', 'where' => '1=1' );
$out = WPDO_HivePress_Comment_Router::rewrite( $clauses, $query );
$this->assertStringContainsString( 'LEFT JOIN', $out['join'] );
$this->assertStringContainsString( 'wpdo_comment_hp_favorite', $out['join'] );
$this->assertStringContainsString( 'AS wpdo_shadow', $out['join'] );
}
public function test_rewrite_promotes_recipient_for_hp_message(): void {
$query = new stdClass();
$query->query_vars = array( 'type' => 'hp_message' );
$clauses = array(
'join' => '',
'where' => 'wp_comments.comment_karma = 42 AND comment_approved = 0',
);
$out = WPDO_HivePress_Comment_Router::rewrite( $clauses, $query );
// recipient lookup should be rewritten to use shadow column.
$this->assertStringContainsString( 'wpdo_shadow.recipient_id = 42', $out['where'] );
// Other predicates preserved.
$this->assertStringContainsString( 'comment_approved = 0', $out['where'] );
}
public function test_rewrite_handles_array_type(): void {
$query = new stdClass();
$query->query_vars = array( 'type' => array( 'hp_review', 'comment' ) );
$clauses = array( 'join' => '', 'where' => '' );
$out = WPDO_HivePress_Comment_Router::rewrite( $clauses, $query );
// First element of type array should match (hp_review).
$this->assertStringContainsString( 'wpdo_comment_hp_review', $out['join'] );
}
public function test_rewrite_returns_unchanged_when_query_invalid(): void {
$clauses = array( 'join' => '', 'where' => '1=1' );
$out = WPDO_HivePress_Comment_Router::rewrite( $clauses, 'not an object' );
$this->assertSame( $clauses, $out );
}
}
+12 -59
View File
@@ -6,6 +6,9 @@ use PHPUnit\Framework\TestCase;
/**
* Tests for WPDO_HivePress_Favorites_Adapter.
*
* The adapter no longer mirrors favorites into a shadow table: that table was
* never created, so every mirror write failed.
*
* @covers WPDO_HivePress_Favorites_Adapter
*/
class HivePressFavoritesAdapterTest extends TestCase {
@@ -32,72 +35,22 @@ class HivePressFavoritesAdapterTest extends TestCase {
public function test_constants_exposed(): void {
$this->assertSame( 'hp_favorite', WPDO_HivePress_Favorites_Adapter::COMMENT_TYPE );
$this->assertSame( 'wpdo_comment_hp_favorite', WPDO_HivePress_Favorites_Adapter::TABLE );
}
public function test_on_register_custom_tables_declares_shadow_table(): void {
public function test_registers_no_shadow_table(): void {
$registry = WPDO_Custom_Table_Registry::instance();
$this->adapter->on_register_custom_tables( $registry );
$tables = $registry->all();
$this->assertCount( 1, $tables );
$cfg = reset( $tables );
$this->assertSame( 'hivepress-favorites', $cfg['provider'] );
$this->assertSame( 'wpdo_comment_hp_favorite', $cfg['table_name'] );
$this->assertSame( 'comment_id', $cfg['primary_key'] );
$this->assertArrayHasKey( 'unique_user_listing', $cfg['indexes'] );
$this->assertStringContainsString( 'UNIQUE', $cfg['indexes']['unique_user_listing'] );
$this->assertSame( array(), $registry->all() );
}
public function test_mirror_insert_skips_non_favorite_comments(): void {
// Stub $wpdb tracks queries.
global $wpdb;
$wpdb->queries = array();
public function test_binds_no_comment_hooks(): void {
$GLOBALS['_wp_filter_callbacks'] = array();
$comment = new stdClass();
$comment->comment_type = 'comment'; // ← not hp_favorite
$comment->user_id = 1;
$comment->comment_post_ID = 99;
$this->adapter->on_register_event_hooks();
$this->adapter->mirror_insert( 123, $comment );
$this->assertSame( array(), $wpdb->queries );
}
public function test_mirror_insert_writes_for_hp_favorite(): void {
global $wpdb;
$wpdb->queries = array();
$comment = new stdClass();
$comment->comment_type = 'hp_favorite';
$comment->user_id = 7;
$comment->comment_post_ID = 42;
$comment->comment_date = '2026-05-04 12:00:00';
$this->adapter->mirror_insert( 100, $comment );
$this->assertCount( 1, $wpdb->queries );
$this->assertStringContainsString( 'INSERT IGNORE', $wpdb->queries[0] );
$this->assertStringContainsString( 'wpdo_comment_hp_favorite', $wpdb->queries[0] );
}
public function test_mirror_insert_skips_when_user_or_listing_missing(): void {
global $wpdb;
$wpdb->queries = array();
$comment = new stdClass();
$comment->comment_type = 'hp_favorite';
$comment->user_id = 0;
$comment->comment_post_ID = 99;
$this->adapter->mirror_insert( 100, $comment );
$this->assertSame( array(), $wpdb->queries );
}
public function test_doctor_check_reports_missing_table_in_stub_env(): void {
$result = $this->adapter->doctor_check();
$this->assertFalse( $result['ok'] );
$this->assertStringContainsString( 'wpdo_comment_hp_favorite', $result['message'] );
$this->assertArrayNotHasKey( 'wp_insert_comment', $GLOBALS['_wp_filter_callbacks'] );
$this->assertArrayNotHasKey( 'deleted_comment', $GLOBALS['_wp_filter_callbacks'] );
}
public function test_score_aggregates_to_perfect_ten(): void {
@@ -105,7 +58,7 @@ class HivePressFavoritesAdapterTest extends TestCase {
$this->assertSame( 10.0, $score['aggregate'] );
}
public function test_migrations_lists_comment_module(): void {
$this->assertSame( array( 'comment_hp_favorite' ), $this->adapter->migrations() );
public function test_migrations_is_empty(): void {
$this->assertSame( array(), $this->adapter->migrations() );
}
}
+13 -61
View File
@@ -6,6 +6,9 @@ use PHPUnit\Framework\TestCase;
/**
* Tests for WPDO_HivePress_Messages_Adapter.
*
* The adapter no longer mirrors messages into a shadow table: that table was
* never created, so every mirror write failed.
*
* @covers WPDO_HivePress_Messages_Adapter
*/
class HivePressMessagesAdapterTest extends TestCase {
@@ -28,77 +31,26 @@ class HivePressMessagesAdapterTest extends TestCase {
$this->assertSame( 'hivepress-messages', $this->adapter->plugin_slug() );
$this->assertSame( 'HivePress\\Messages\\Plugin', $this->adapter->detection_class() );
$this->assertSame( 'hp_message', WPDO_HivePress_Messages_Adapter::COMMENT_TYPE );
$this->assertSame( 'wpdo_comment_hp_message', WPDO_HivePress_Messages_Adapter::TABLE );
}
public function test_on_register_custom_tables_declares_indexed_recipient(): void {
public function test_registers_no_shadow_table(): void {
$registry = WPDO_Custom_Table_Registry::instance();
$this->adapter->on_register_custom_tables( $registry );
$tables = $registry->all();
$cfg = reset( $tables );
$this->assertSame( 'wpdo_comment_hp_message', $cfg['table_name'] );
$this->assertArrayHasKey( 'recipient_id', $cfg['expected_columns'] );
$this->assertArrayHasKey( 'idx_recipient_unread', $cfg['indexes'] );
$this->assertStringContainsString( 'recipient_id, is_read', $cfg['indexes']['idx_recipient_unread'] );
$this->assertSame( array(), $registry->all() );
}
public function test_mirror_insert_skips_non_message_comments(): void {
global $wpdb;
$wpdb->queries = array();
public function test_binds_no_comment_hooks(): void {
$GLOBALS['_wp_filter_callbacks'] = array();
$comment = new stdClass();
$comment->comment_type = 'comment';
$comment->user_id = 1;
$comment->comment_karma = 5;
$this->adapter->on_register_event_hooks();
$this->adapter->mirror_insert( 1, $comment );
$this->assertSame( array(), $wpdb->queries );
$this->assertArrayNotHasKey( 'wp_insert_comment', $GLOBALS['_wp_filter_callbacks'] );
$this->assertArrayNotHasKey( 'wp_set_comment_status', $GLOBALS['_wp_filter_callbacks'] );
$this->assertArrayNotHasKey( 'deleted_comment', $GLOBALS['_wp_filter_callbacks'] );
}
public function test_mirror_insert_promotes_recipient_from_comment_karma(): void {
global $wpdb;
$wpdb->queries = array();
$comment = new stdClass();
$comment->comment_type = 'hp_message';
$comment->user_id = 7; // sender
$comment->comment_karma = 42; // ← recipient hack lives here
$comment->comment_post_ID = 99;
$comment->comment_approved = 0;
$comment->comment_date = '2026-05-04 12:00:00';
$this->adapter->mirror_insert( 100, $comment );
$this->assertCount( 1, $wpdb->queries );
$sql = $wpdb->queries[0];
$this->assertStringContainsString( 'INSERT INTO', $sql );
$this->assertStringContainsString( 'wpdo_comment_hp_message', $sql );
// The promoted recipient_id should appear in the bound SQL.
$this->assertStringContainsString( '42', $sql );
}
public function test_mirror_insert_skips_when_sender_or_recipient_zero(): void {
global $wpdb;
$wpdb->queries = array();
$comment = new stdClass();
$comment->comment_type = 'hp_message';
$comment->user_id = 0; // bad sender
$comment->comment_karma = 42;
$comment->comment_post_ID = 99;
$this->adapter->mirror_insert( 100, $comment );
$this->assertSame( array(), $wpdb->queries );
}
public function test_doctor_check_reports_missing_table_in_stub_env(): void {
$result = $this->adapter->doctor_check();
$this->assertFalse( $result['ok'] );
$this->assertStringContainsString( 'wpdo_comment_hp_message', $result['message'] );
}
public function test_score_aggregates_to_perfect_ten(): void {
$this->assertSame( 10.0, $this->adapter->suitability_score()['aggregate'] );
public function test_migrations_is_empty(): void {
$this->assertSame( array(), $this->adapter->migrations() );
}
}
+11 -36
View File
@@ -48,54 +48,29 @@ class HivePressRequestsAdapterTest extends TestCase {
$this->assertTrue( (bool) $expired['indexed'] );
}
public function test_on_register_custom_tables_declares_offer_shadow(): void {
public function test_registers_no_offer_shadow_table(): void {
// The offer shadow table was never created, so every mirror write failed.
$registry = WPDO_Custom_Table_Registry::instance();
$this->adapter->on_register_custom_tables( $registry );
$tables = $registry->all();
$cfg = reset( $tables );
$this->assertSame( 'wpdo_comment_hp_offer', $cfg['table_name'] );
$this->assertSame( 'hp_request', $cfg['post_type_link'] );
$this->assertArrayHasKey( 'idx_request_approved', $cfg['indexes'] );
$this->assertSame( array(), $registry->all() );
}
public function test_mirror_offer_insert_skips_other_comment_types(): void {
global $wpdb;
$wpdb->queries = array();
public function test_binds_no_comment_hooks(): void {
$GLOBALS['_wp_filter_callbacks'] = array();
$comment = new stdClass();
$comment->comment_type = 'comment'; // ← not hp_offer
$comment->comment_post_ID = 99;
$comment->user_id = 1;
$this->adapter->on_register_event_hooks();
$this->adapter->mirror_offer_insert( 1, $comment );
$this->assertSame( array(), $wpdb->queries );
}
public function test_mirror_offer_insert_writes_for_hp_offer(): void {
global $wpdb;
$wpdb->queries = array();
$comment = new stdClass();
$comment->comment_type = 'hp_offer';
$comment->comment_post_ID = 42;
$comment->user_id = 7;
$comment->comment_approved = 0;
$comment->comment_date = '2026-05-04 12:00:00';
$this->adapter->mirror_offer_insert( 100, $comment );
$this->assertCount( 1, $wpdb->queries );
$this->assertStringContainsString( 'INSERT INTO', $wpdb->queries[0] );
$this->assertStringContainsString( 'wpdo_comment_hp_offer', $wpdb->queries[0] );
$this->assertArrayNotHasKey( 'wp_insert_comment', $GLOBALS['_wp_filter_callbacks'] );
$this->assertArrayNotHasKey( 'edit_comment', $GLOBALS['_wp_filter_callbacks'] );
$this->assertArrayNotHasKey( 'deleted_comment', $GLOBALS['_wp_filter_callbacks'] );
}
public function test_score_aggregates_to_perfect_ten(): void {
$this->assertSame( 10.0, $this->adapter->suitability_score()['aggregate'] );
}
public function test_migrations_lists_request_and_offer_modules(): void {
$mods = $this->adapter->migrations();
$this->assertContains( 'hot_hp_request', $mods );
$this->assertContains( 'comment_hp_offer', $mods );
public function test_migrations_lists_only_request_hot_module(): void {
$this->assertSame( array( 'hot_hp_request' ), $this->adapter->migrations() );
}
}
+1 -1
View File
@@ -7,7 +7,7 @@ declare(strict_types=1);
* Several existing wpdo tests (ZoneHotTest, SyncBridgeTest, etc.) replace the
* global $wpdb with their own anonymous-class mock and never restore it. When
* our adapter tests run after those, $wpdb lacks the query/get_col/delete
* methods our mirror_insert / cron-optimizer code paths call, causing
* methods our cron-optimizer code paths call, causing
* cross-test pollution failures.
*
* Trait reinstalls a known $wpdb mock + clears the wpdo_features option so