refactor(2meet-data-optimizer): resolve post_type/group/table mappings through the registry
- TMDO_Post_Fields::post_type_for_group() / groups_for_post_type() derive HivePress-style groups from their registry post_type scope (core keeps its own attachment / wc_product / nav_menu_item map); the three copy-pasted switches and the query router's POST_TYPE_GROUP_MAP use them - Term/comment backfill, shadow verifier and CLI take their groups from the registry (TMDO_Term_Comment_Backfill::registered_groups()); installer drops the hp_* entries already covered by its table patterns; REST listings default post type is filterable (tmdo_rest_default_post_type, default 'post') - Verify: NoHpMappingsTest guard + mapping/default tests; core unit 640 / integration 449, HP 146/49, WC 10/22 OK - PR: PR-020e
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Core routing code resolves group ↔ post_type / table from the registry.
|
||||
*
|
||||
* HivePress groups are registered by the HivePress AddOn with their post
|
||||
* types; core must not hardcode their names. The HivePress-only legacy
|
||||
* cutover tooling (wpdo_hot_hp_listing → hp_listing_core) and the stress
|
||||
* testers are out of scope and not checked here.
|
||||
*/
|
||||
class NoHpMappingsTest extends TestCase {
|
||||
|
||||
private const FILES = array(
|
||||
'includes/class-tmdo-post-shadow-verifier.php',
|
||||
'includes/migration/class-tmdo-post-migration.php',
|
||||
'includes/query/class-tmdo-post-query-router.php',
|
||||
'includes/class-tmdo-term-comment-backfill.php',
|
||||
'includes/class-tmdo-term-comment-shadow-verifier.php',
|
||||
'cli/class-tmdo-cli-term-comment.php',
|
||||
'includes/class-tmdo-installer.php',
|
||||
'includes/class-tmdo-rest-api.php',
|
||||
);
|
||||
|
||||
/** Stress-test tooling inside the REST API class keeps its sample post types. */
|
||||
private const ALLOWED = array(
|
||||
'includes/class-tmdo-rest-api.php' => array( "array( 'product', 'hp_listing', 'hp_request', 'hp_vendor', 'attachment', 'nav_menu_item', 'post' )" ),
|
||||
);
|
||||
|
||||
public function test_routing_code_has_no_hivepress_names(): void {
|
||||
$root = dirname( __DIR__, 2 );
|
||||
$offences = array();
|
||||
foreach ( self::FILES as $rel ) {
|
||||
$code = '';
|
||||
foreach ( token_get_all( (string) file_get_contents( $root . '/' . $rel ) ) as $token ) {
|
||||
if ( is_array( $token ) && in_array( $token[0], array( T_COMMENT, T_DOC_COMMENT ), true ) ) {
|
||||
continue;
|
||||
}
|
||||
$code .= is_array( $token ) ? $token[1] : $token;
|
||||
}
|
||||
$code = str_replace( self::ALLOWED[ $rel ] ?? array(), '', $code );
|
||||
if ( preg_match_all( '/hp_(listing|request|vendor|taxonomy|review|user)\w*/', $code, $m ) ) {
|
||||
$offences[] = $rel . ': ' . implode( ', ', array_unique( $m[0] ) );
|
||||
}
|
||||
}
|
||||
|
||||
$this->assertSame( array(), $offences, "Hardcoded HivePress names in core routing:\n" . implode( "\n", $offences ) );
|
||||
}
|
||||
|
||||
public function test_term_comment_groups_come_from_the_registry(): void {
|
||||
TMDO_Entity_Registry::init();
|
||||
TMDO_Entity_Registry::register_adapter( 'term', $this->createMock( TMDO_Entity_Adapter_Interface::class ) );
|
||||
TMDO_Entity_Registry::register_adapter( 'comment', $this->createMock( TMDO_Entity_Adapter_Interface::class ) );
|
||||
TMDO_Entity_Registry::register_group( 'term', 'x_tax', array( array( 'key' => 'x_order', 'type' => 'integer' ) ) );
|
||||
TMDO_Entity_Registry::register_group( 'comment', 'x_review', array( array( 'key' => 'x_rating', 'type' => 'integer' ) ) );
|
||||
|
||||
$this->assertSame( array( 'x_tax' => 'term', 'x_review' => 'comment' ), TMDO_Term_Comment_Backfill::registered_groups() );
|
||||
|
||||
$result = TMDO_Term_Comment_Backfill::backfill_group( 'term', 'hp_taxonomy', true );
|
||||
$this->assertSame( 'Unknown group: hp_taxonomy', $result['error'] ?? '', 'unregistered groups are unknown' );
|
||||
}
|
||||
}
|
||||
@@ -135,6 +135,24 @@ class PostFieldsRegistrationTest extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
// ── group ↔ post_type mapping ────────────────────────────────────────────
|
||||
|
||||
public function test_core_groups_map_to_their_post_types(): void {
|
||||
$this->assertSame( 'attachment', WPDO_Post_Fields::post_type_for_group( 'attachment' ) );
|
||||
$this->assertSame( 'product', WPDO_Post_Fields::post_type_for_group( 'wc_product' ) );
|
||||
$this->assertSame( 'nav_menu_item', WPDO_Post_Fields::post_type_for_group( 'nav_menu_item' ) );
|
||||
$this->assertNull( WPDO_Post_Fields::post_type_for_group( 'wp_core' ), 'wp_core spans every post type' );
|
||||
}
|
||||
|
||||
public function test_scoped_groups_map_through_the_registry(): void {
|
||||
WPDO_Entity_Registry::register_group( 'post', 'x_core', array( array( 'key' => 'x_price', 'type' => 'decimal' ) ), array( 'post_types' => array( 'x_type' ) ) );
|
||||
|
||||
$this->assertSame( 'x_type', WPDO_Post_Fields::post_type_for_group( 'x_core' ) );
|
||||
$this->assertSame( array( 'x_core' ), WPDO_Post_Fields::groups_for_post_type( 'x_type' ) );
|
||||
$this->assertSame( array( 'wc_product' ), WPDO_Post_Fields::groups_for_post_type( 'product' ) );
|
||||
$this->assertNull( WPDO_Post_Fields::post_type_for_group( 'hp_listing_core' ), 'unregistered HivePress group is unknown to core' );
|
||||
}
|
||||
|
||||
// ── nav_menu_item group ──────────────────────────────────────────────────
|
||||
|
||||
public function test_nav_menu_item_has_8_keys(): void {
|
||||
|
||||
@@ -155,6 +155,17 @@ class RestApiTest extends TestCase {
|
||||
$this->assertIsArray( $response->get_data() );
|
||||
}
|
||||
|
||||
public function test_default_post_type_is_post_and_filterable(): void {
|
||||
$this->assertSame( 'post', WPDO_REST_API::default_post_type() );
|
||||
|
||||
$GLOBALS['_wp_filter_callbacks']['tmdo_rest_default_post_type'][] = static fn() => 'hp_listing';
|
||||
try {
|
||||
$this->assertSame( 'hp_listing', WPDO_REST_API::default_post_type() );
|
||||
} finally {
|
||||
unset( $GLOBALS['_wp_filter_callbacks']['tmdo_rest_default_post_type'] );
|
||||
}
|
||||
}
|
||||
|
||||
public function test_get_listings_rejects_unregistered_post_type(): void {
|
||||
$req = new WP_REST_Request( 'GET', '/wpdo/v1/listings' );
|
||||
$req->set_param( 'post_type', 'no_such_type' );
|
||||
|
||||
Reference in New Issue
Block a user