Files
2meet-data-optimizer/tests/integration/PostQueryRouterOrderTest.php
T
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

140 lines
5.7 KiB
PHP

<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
/**
* Meta orderby on the flat table: the "sort by an attribute, keep posts
* without it" shape (relation OR of NOT EXISTS + EXISTS on one key, named
* clause in orderby — HivePress search sorting), orderby on a named routed
* clause, and meta_key + meta_value(_num).
*
* Only when the flat column orders the way native CASTs meta_value;
* otherwise the whole query stays native.
*/
class PostQueryRouterOrderTest extends TestCase {
private const ALIAS = '`wpdoflat_product_wc_product`';
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();
}
protected function setUp(): void {
$cache = new ReflectionProperty( WPDO_Mode_Manager::class, 'cache' );
$cache->setAccessible( true );
$cache->setValue( null, array( 'post' => 'aeav_only', 'user' => 'disabled', 'term' => 'disabled', 'comment' => 'disabled' ) );
}
public static function tearDownAfterClass(): void {
$cache = new ReflectionProperty( WPDO_Mode_Manager::class, 'cache' );
$cache->setAccessible( true );
$cache->setValue( null, null );
WPDO_Entity_Registry::init();
}
/**
* @param array<string, mixed> $vars Query vars (post_type defaults to product).
* @return array{query:WP_Query, join:string, where:string, orderby:string}
*/
private function route( array $vars ): array {
$query = new WP_Query();
foreach ( $vars + array( 'post_type' => 'product' ) as $k => $v ) {
$query->set( $k, $v );
}
$router = new TMDO_Post_Query_Router();
$router->pre_get_posts( $query );
return array(
'query' => $query,
'join' => $router->posts_join( '', $query ),
'where' => $router->posts_where( '', $query ),
'orderby' => $router->posts_orderby( 'wp_posts.post_date DESC', $query ),
);
}
/**
* @param string $type Clause type.
* @return array<int|string, mixed>
*/
private function sort_shape( string $type ): array {
return array(
array(
'relation' => 'OR',
'price__order' => array( 'key' => '_price', 'type' => $type, 'compare' => 'NOT EXISTS' ),
array( 'key' => '_price', 'type' => $type, 'compare' => 'EXISTS' ),
),
);
}
public function test_sort_shape_left_joins_and_orders_by_the_flat_column_without_filtering(): void {
$sql = $this->route( array( 'meta_query' => $this->sort_shape( 'DECIMAL' ), 'orderby' => 'price__order', 'order' => 'ASC' ) );
$this->assertSame( array(), $sql['query']->get( 'meta_query' ), 'sort group removed from the native meta_query' );
$this->assertStringContainsString( 'LEFT JOIN `wp_itest_wpdo_post_wc_product` AS ' . self::ALIAS, $sql['join'] );
$this->assertStringNotContainsString( '_price', $sql['where'], 'every post matches: no condition' );
$this->assertStringStartsWith( self::ALIAS . '.`_price` ASC', $sql['orderby'] );
}
public function test_sort_shape_with_char_type_on_a_numeric_column_stays_native(): void {
$vars = array( 'meta_query' => $this->sort_shape( 'CHAR' ), 'orderby' => 'price__order', 'order' => 'ASC' );
$sql = $this->route( $vars );
$this->assertSame( $vars['meta_query'], $sql['query']->get( 'meta_query' ) );
$this->assertSame( 'wp_posts.post_date DESC', $sql['orderby'] );
}
public function test_orderby_on_a_named_routed_clause_orders_by_its_column(): void {
$sql = $this->route(
array(
'meta_query' => array( 'price' => array( 'key' => '_price', 'compare' => 'EXISTS', 'type' => 'NUMERIC' ) ),
'orderby' => array( 'price' => 'DESC' ),
)
);
$this->assertStringContainsString( self::ALIAS . '.`_price` IS NOT NULL', $sql['where'] );
$this->assertStringStartsWith( self::ALIAS . '.`_price` DESC', $sql['orderby'] );
}
public function test_orderby_naming_a_routed_clause_among_other_keys_stays_native(): void {
$vars = array(
'meta_query' => array( 'price' => array( 'key' => '_price', 'compare' => 'EXISTS', 'type' => 'NUMERIC' ) ),
'orderby' => array( 'price' => 'DESC', 'title' => 'ASC' ),
);
$sql = $this->route( $vars );
$this->assertSame( $vars['meta_query'], $sql['query']->get( 'meta_query' ) );
$this->assertSame( 'wp_posts.post_date DESC', $sql['orderby'] );
}
public function test_meta_value_num_on_a_numeric_key_requires_the_key_and_orders_by_its_column(): void {
$sql = $this->route( array( 'meta_key' => '_price', 'orderby' => 'meta_value_num', 'order' => 'DESC' ) );
$this->assertSame( '', $sql['query']->get( 'meta_key' ), 'primary meta clause taken over' );
$this->assertStringContainsString( self::ALIAS . '.`_price` IS NOT NULL', $sql['where'], 'native INNER JOIN drops posts without the key' );
$this->assertStringStartsWith( self::ALIAS . '.`_price` DESC', $sql['orderby'] );
}
public function test_meta_value_on_a_string_key_is_routed(): void {
$sql = $this->route( array( 'meta_key' => '_sku', 'orderby' => 'meta_value', 'order' => 'ASC' ) );
$this->assertStringStartsWith( self::ALIAS . '.`_sku` ASC', $sql['orderby'] );
}
public function test_meta_value_on_a_numeric_key_stays_native(): void {
$sql = $this->route( array( 'meta_key' => '_price', 'orderby' => 'meta_value', 'order' => 'ASC' ) );
$this->assertSame( '_price', $sql['query']->get( 'meta_key' ), 'native string order' );
$this->assertSame( 'wp_posts.post_date DESC', $sql['orderby'] );
}
public function test_meta_key_with_a_meta_value_filter_stays_native(): void {
$sql = $this->route( array( 'meta_key' => '_price', 'meta_value' => '5', 'orderby' => 'meta_value_num' ) );
$this->assertSame( '_price', $sql['query']->get( 'meta_key' ) );
$this->assertSame( 'wp_posts.post_date DESC', $sql['orderby'] );
}
}