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
-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