1b21ee9add
- In aeav_only flat is the source of truth and native meta is stale by design, so drift there means nothing: drift-check --fix refuses (it would overwrite current flat values with stale native ones), the daily sample skips such entities, and the router keeps routing a drifting group instead of falling back to stale native meta. - Found before PR-074 on hardening/2.2 (PR-071–073 not released; dev21 still runs aeav_only). - Verify: DriftCheckTest / PostQueryRouterDriftTest aeav_only cases (RED before); dev36 fix() in a simulated aeav_only refused. - PR: PR-073b
133 lines
3.8 KiB
PHP
133 lines
3.8 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
/**
|
|
* The post query router leaves a query native while the last drift-check
|
|
* found a routed clause's group out of step with native meta (2.2 PR-072).
|
|
* All or nothing: one drifting group keeps the whole query native. A group
|
|
* never checked keeps routing (decision 2026-09-27).
|
|
*/
|
|
class PostQueryRouterDriftTest extends TestCase {
|
|
|
|
public static function setUpBeforeClass(): void {
|
|
WPDO_Entity_Registry::init();
|
|
WPDO_Entity_Registry::register_adapter( 'post', new WPDO_Adapter_Post() );
|
|
WPDO_Post_Fields::register_entity_fields();
|
|
}
|
|
|
|
public static function tearDownAfterClass(): void {
|
|
self::set_modes( null );
|
|
WPDO_Entity_Registry::init();
|
|
}
|
|
|
|
protected function setUp(): void {
|
|
$GLOBALS['_wp_options'] = array();
|
|
self::set_modes(
|
|
array(
|
|
'post' => 'shadow_read',
|
|
'user' => 'disabled',
|
|
'term' => 'disabled',
|
|
'comment' => 'disabled',
|
|
)
|
|
);
|
|
}
|
|
|
|
private static function set_modes( ?array $modes ): void {
|
|
$cache = ( new ReflectionClass( WPDO_Mode_Manager::class ) )->getProperty( 'cache' );
|
|
$cache->setAccessible( true );
|
|
$cache->setValue( null, $modes );
|
|
}
|
|
|
|
private function drift( string $group, int $diff ): void {
|
|
$GLOBALS['_wp_options'][ TMDO_Drift_Check::OPTION ]['post'][ $group ] = array(
|
|
'miss' => 0,
|
|
'diff' => $diff,
|
|
'extra' => 0,
|
|
'sample' => 0,
|
|
'checked_at' => '2026-09-27 00:00:00',
|
|
);
|
|
}
|
|
|
|
private function price_query( array $extra = array() ): WP_Query {
|
|
$query = new WP_Query();
|
|
foreach ( array(
|
|
'post_type' => 'product',
|
|
'meta_query' => array(
|
|
array( 'key' => '_price', 'value' => '50', 'compare' => '>=', 'type' => 'NUMERIC' ),
|
|
),
|
|
) + $extra as $k => $v ) {
|
|
$query->set( $k, $v );
|
|
}
|
|
return $query;
|
|
}
|
|
|
|
public function test_a_drifting_group_keeps_the_query_native(): void {
|
|
$this->drift( 'wc_product', 3 );
|
|
$query = $this->price_query();
|
|
$original = $query->get( 'meta_query' );
|
|
|
|
( new WPDO_Post_Query_Router() )->pre_get_posts( $query );
|
|
|
|
$this->assertSame( $original, $query->get( 'meta_query' ) );
|
|
$this->assertSame( '', $query->get( 'wpdo_post_clauses' ) );
|
|
}
|
|
|
|
public function test_a_clean_group_is_routed(): void {
|
|
$this->drift( 'wc_product', 0 );
|
|
$query = $this->price_query();
|
|
|
|
( new WPDO_Post_Query_Router() )->pre_get_posts( $query );
|
|
|
|
$this->assertNotEmpty( $query->get( 'wpdo_post_clauses' ) );
|
|
}
|
|
|
|
public function test_a_group_never_checked_is_routed(): void {
|
|
$query = $this->price_query();
|
|
|
|
( new WPDO_Post_Query_Router() )->pre_get_posts( $query );
|
|
|
|
$this->assertNotEmpty( $query->get( 'wpdo_post_clauses' ) );
|
|
}
|
|
|
|
public function test_drift_in_another_group_does_not_matter(): void {
|
|
$this->drift( 'hp_listing_core', 5 );
|
|
$query = $this->price_query();
|
|
|
|
( new WPDO_Post_Query_Router() )->pre_get_posts( $query );
|
|
|
|
$this->assertNotEmpty( $query->get( 'wpdo_post_clauses' ) );
|
|
}
|
|
|
|
public function test_aeav_only_ignores_drift_because_native_is_stale_by_design(): void {
|
|
self::set_modes(
|
|
array(
|
|
'post' => 'aeav_only',
|
|
'user' => 'disabled',
|
|
'term' => 'disabled',
|
|
'comment' => 'disabled',
|
|
)
|
|
);
|
|
$this->drift( 'wc_product', 3 );
|
|
$query = $this->price_query();
|
|
|
|
( new WPDO_Post_Query_Router() )->pre_get_posts( $query );
|
|
|
|
$this->assertNotEmpty( $query->get( 'wpdo_post_clauses' ) );
|
|
}
|
|
|
|
public function test_sorting_by_a_drifting_key_stays_native(): void {
|
|
$this->drift( 'wc_product', 1 );
|
|
$query = new WP_Query();
|
|
$query->set( 'post_type', 'product' );
|
|
$query->set( 'meta_key', '_price' );
|
|
$query->set( 'orderby', 'meta_value_num' );
|
|
|
|
( new WPDO_Post_Query_Router() )->pre_get_posts( $query );
|
|
|
|
$this->assertSame( '_price', $query->get( 'meta_key' ) );
|
|
$this->assertSame( '', $query->get( 'wpdo_post_sort' ) );
|
|
}
|
|
}
|