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 ); } }