Files
2meet-data-optimizer/tests/integration/PostQueryRouterDriftTest.php
wpdev f48df1a33f refactor(2meet-data-optimizer): stop registering the wc_product post group
- WooCommerce reads product meta directly and writes stock by SQL, so a flat copy drifted (R-2.1 V1 T4); product meta now stays in native only (plan decision 2). TMDO_Post_Fields registers wp_core, attachment and nav_menu_item. Existing wp_wpdo_post_wc_product tables are left in place and still dropped on uninstall; the orphan restore registers nothing for them while native meta holds the keys; `wp tmdo bridge-restore-native post --group=wc_product --retire` archives them.
- Tests that route products register the former group themselves (tmdo_test_register_wc_product_group(), same name and table); live router Q06 runs on wp_core's _wp_page_template instead of _sku. The post stress tester already skips flat tables that do not exist.
- Verify: PostFieldsRegistrationTest (RED before); dev36: wc_product absent from the registry and not restored as an orphan, a CLI order took stock 452 → 450 in native with no DB error (reverted), drift post 10 groups 0.
- PR: PR-077
2026-09-27 01:11:52 +08:00

134 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();
tmdo_test_register_wc_product_group();
}
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' ) );
}
}