a01314cbb1
- Do not bind optimize_query / optimize_search on hivepress/v1/models/{listing,vendor}/{query,search} while core's TMDO_Post_Query_Router is active: core now routes HivePress searches (filters and _sort) to the entity flat tables, and two routers would claim the same clauses
- Version 0.2.0 (released with core 1.1.0); CHANGELOG
- Verify: composer test (144, +2 binding tests); integration 49; dev36 fe.sh 9 URLs ids == native (except known flat drift)
- PR: PR-063
149 lines
5.8 KiB
PHP
149 lines
5.8 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
/**
|
|
* Tests for WPDO_HivePress_Core_Adapter — Listing / Vendor field registration.
|
|
*
|
|
* @covers WPDO_HivePress_Core_Adapter
|
|
*/
|
|
class HivePressCoreAdapterTest extends TestCase {
|
|
|
|
private WPDO_HivePress_Core_Adapter $adapter;
|
|
|
|
protected function setUp(): void {
|
|
// WPDO_Schema_Registry has no reset_for_tests() — use reflection to
|
|
// clear its singleton so each test starts with a fresh registry.
|
|
$reflect = new ReflectionClass( WPDO_Schema_Registry::class );
|
|
if ( $reflect->hasProperty( 'instance' ) ) {
|
|
$prop = $reflect->getProperty( 'instance' );
|
|
$prop->setAccessible( true );
|
|
$prop->setValue( null, null );
|
|
}
|
|
WPDO_Custom_Table_Registry::reset_for_tests();
|
|
$this->adapter = new WPDO_HivePress_Core_Adapter();
|
|
}
|
|
|
|
public function test_implements_adapter_interface(): void {
|
|
$this->assertInstanceOf( WPDO_HivePress_Adapter::class, $this->adapter );
|
|
}
|
|
|
|
public function test_plugin_slug_is_hivepress(): void {
|
|
$this->assertSame( 'hivepress', $this->adapter->plugin_slug() );
|
|
}
|
|
|
|
public function test_detection_class_targets_hivepress_core(): void {
|
|
$this->assertSame( 'HivePress\\Core', $this->adapter->detection_class() );
|
|
}
|
|
|
|
public function test_detection_const_targets_hivepress_version(): void {
|
|
$this->assertSame( 'HIVEPRESS_VERSION', $this->adapter->detection_const() );
|
|
}
|
|
|
|
public function test_minimum_addon_version_floor(): void {
|
|
// 1.7.0 introduced the field-alias convention this adapter relies on.
|
|
$this->assertSame( '1.7.0', $this->adapter->minimum_addon_version() );
|
|
}
|
|
|
|
public function test_on_register_fields_populates_schema_registry(): void {
|
|
$registry = WPDO_Schema_Registry::instance();
|
|
$this->adapter->on_register_fields( $registry );
|
|
|
|
// Vendor cold blob.
|
|
$vendor_image = $registry->get_field( 'hp_vendor', 'hp_image' );
|
|
$this->assertIsArray( $vendor_image );
|
|
$this->assertSame( 'cold', $vendor_image['zone'] );
|
|
}
|
|
|
|
public function test_suitability_score_aggregates_to_ten(): void {
|
|
$score = $this->adapter->suitability_score();
|
|
$this->assertArrayHasKey( 'aggregate', $score );
|
|
$this->assertSame( 10.0, $score['aggregate'] );
|
|
// All eight dimensions present.
|
|
foreach ( array( 'd1_meta_calls', 'd2_meta_sql', 'd3_table_coverage', 'd4_registry_meta', 'd5_hook_bus', 'd6_options', 'd7_coupling', 'd8_perf' ) as $key ) {
|
|
$this->assertArrayHasKey( $key, $score );
|
|
$this->assertGreaterThanOrEqual( 0.0, $score[ $key ] );
|
|
$this->assertLessThanOrEqual( 1.0, $score[ $key ] );
|
|
}
|
|
}
|
|
|
|
public function test_optimize_query_extracts_hot_clauses(): void {
|
|
$registry = WPDO_Schema_Registry::instance();
|
|
$this->adapter->on_register_fields( $registry );
|
|
|
|
$query = new WP_Query( array(
|
|
'post_type' => 'hp_listing',
|
|
'meta_query' => array(
|
|
array( 'key' => 'hp_drafted', 'value' => 1, 'compare' => '=' ),
|
|
array( 'key' => 'unrelated_key', 'value' => 'foo', 'compare' => '=' ),
|
|
),
|
|
) );
|
|
|
|
$this->adapter->optimize_query( $query );
|
|
|
|
// Hot clause should have been moved to wpdo_hot_clauses.
|
|
$hot = $query->get( 'wpdo_hot_clauses' );
|
|
// Note: in the test env the WPDO_Feature_Flags check may short-circuit
|
|
// (no module active), so the assertion is "either the clause moved OR
|
|
// it stayed put; never silently corrupted".
|
|
$remaining = (array) $query->get( 'meta_query' );
|
|
$this->assertIsArray( $remaining );
|
|
// `unrelated_key` must always remain in meta_query regardless of routing.
|
|
$found_unrelated = false;
|
|
foreach ( $remaining as $clause ) {
|
|
if ( is_array( $clause ) && ( $clause['key'] ?? '' ) === 'unrelated_key' ) {
|
|
$found_unrelated = true;
|
|
break;
|
|
}
|
|
}
|
|
$this->assertTrue( $found_unrelated, 'Non-hot meta_query clause must always be preserved.' );
|
|
// hot_clauses is either populated (when feature flag active) or empty (when not).
|
|
$this->assertTrue( $hot === '' || is_array( $hot ) );
|
|
}
|
|
|
|
public function test_optimize_query_handles_empty_meta_query(): void {
|
|
$query = new WP_Query( array( 'post_type' => 'hp_listing' ) );
|
|
$this->adapter->optimize_query( $query );
|
|
// Must not crash; hot_clauses should remain unset.
|
|
$this->assertEmpty( $query->get( 'wpdo_hot_clauses' ) );
|
|
}
|
|
|
|
public function test_optimize_search_delegates_to_optimize_query(): void {
|
|
$query = new WP_Query( array( 'post_type' => 'hp_listing' ) );
|
|
// Delegation is implementation detail; verify just that it doesn't crash
|
|
// and that an irrelevant attribute_fields argument is ignored.
|
|
$this->adapter->optimize_search( $query, array() );
|
|
$this->assertEmpty( $query->get( 'wpdo_hot_clauses' ) );
|
|
}
|
|
|
|
/**
|
|
* Register the query hooks with core's post mode set to $mode.
|
|
*
|
|
* @return array<int, mixed> Callbacks bound to the listing query hooks.
|
|
*/
|
|
private function query_hooks_in_post_mode( string $mode ): array {
|
|
$cache = new ReflectionProperty( TMDO_Mode_Manager::class, 'cache' );
|
|
$cache->setAccessible( true );
|
|
$cache->setValue( null, array( 'post' => $mode, 'user' => 'disabled', 'term' => 'disabled', 'comment' => 'disabled' ) );
|
|
unset( $GLOBALS['_wp_filter_callbacks']['hivepress/v1/models/listing/query'], $GLOBALS['_wp_filter_callbacks']['hivepress/v1/models/listing/search'] );
|
|
try {
|
|
$this->adapter->on_register_query_hooks();
|
|
} finally {
|
|
$cache->setValue( null, null );
|
|
}
|
|
return array_merge(
|
|
$GLOBALS['_wp_filter_callbacks']['hivepress/v1/models/listing/query'] ?? array(),
|
|
$GLOBALS['_wp_filter_callbacks']['hivepress/v1/models/listing/search'] ?? array()
|
|
);
|
|
}
|
|
|
|
public function test_query_hooks_are_bound_while_the_core_post_router_is_idle(): void {
|
|
$this->assertCount( 2, $this->query_hooks_in_post_mode( 'dual_write' ) );
|
|
}
|
|
|
|
public function test_query_hooks_are_left_to_the_core_post_router_when_it_is_active(): void {
|
|
$this->assertSame( array(), $this->query_hooks_in_post_mode( 'shadow_read' ), 'two routers would claim the same clauses' );
|
|
}
|
|
}
|