Files
2meet-data-optimizer/tests/unit/PostFields/PostFieldsRegistrationTest.php
T
wpdev 0603537f0a 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
2026-09-26 00:22:20 +08:00

185 lines
8.0 KiB
PHP

<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
/**
* Unit tests for WPDO_Post_Fields entity group registration (v2.9.1).
*
* Verifies that register_entity_fields() correctly registers all four
* post groups with the expected field counts, types, and post_type targets.
*
* Mirrors MemberFieldsRegistrationTest's structure.
*/
class PostFieldsRegistrationTest extends TestCase {
protected function setUp(): void {
if ( ! class_exists( 'WPDO_Post_Fields' ) ) {
require_once WPDO_PLUGIN_DIR . 'includes/integrations/class-tmdo-post-fields.php';
}
if ( ! class_exists( 'WPDO_Adapter_Post' ) ) {
require_once WPDO_PLUGIN_DIR . 'includes/adapters/class-tmdo-adapter-post.php';
}
WPDO_Entity_Registry::init();
WPDO_Entity_Registry::register_adapter( 'post', new WPDO_Adapter_Post() );
}
// ── Group presence ───────────────────────────────────────────────────────
public function test_register_entity_fields_does_not_throw_on_double_call(): void {
WPDO_Post_Fields::register_entity_fields();
WPDO_Post_Fields::register_entity_fields(); // dedup guard
$this->assertTrue( true );
}
public function test_all_four_groups_are_registered(): void {
WPDO_Post_Fields::register_entity_fields();
$expected = array(
'wp_core',
'attachment',
'wc_product',
'nav_menu_item',
);
foreach ( $expected as $group ) {
$fields = WPDO_Entity_Registry::get_group_fields( 'post', $group );
$this->assertNotEmpty( $fields, "Group '{$group}' should have registered fields." );
}
}
public function test_user_entity_groups_are_not_touched(): void {
// 🔒 Frozen contract: post fields registration must not register
// any group under entity_type='user'.
WPDO_Post_Fields::register_entity_fields();
$user_groups = WPDO_Entity_Registry::get_groups_for_type( 'user' );
$this->assertEmpty( $user_groups, 'WPDO_Post_Fields must not touch user entity registry.' );
}
// ── wp_core group (cross post_type) ──────────────────────────────────────
public function test_wp_core_group_has_expected_keys(): void {
WPDO_Post_Fields::register_entity_fields();
$keys = $this->get_field_keys( 'wp_core' );
$this->assertContains( '_thumbnail_id', $keys );
$this->assertContains( '_wp_page_template', $keys );
$this->assertContains( '_edit_last', $keys );
}
public function test_wp_core_thumbnail_is_searchable(): void {
WPDO_Post_Fields::register_entity_fields();
$field = $this->find_field( 'wp_core', '_thumbnail_id' );
$this->assertNotNull( $field );
$this->assertSame( 'integer', $field['type'] );
$this->assertTrue( (bool) ( $field['searchable'] ?? false ) );
}
// ── attachment group ─────────────────────────────────────────────────────
public function test_attachment_group_has_expected_keys(): void {
WPDO_Post_Fields::register_entity_fields();
$keys = $this->get_field_keys( 'attachment' );
$this->assertContains( '_wp_attached_file', $keys );
$this->assertContains( '_wp_attachment_metadata', $keys );
$this->assertContains( '_wp_attachment_image_alt', $keys );
}
public function test_attachment_metadata_is_json_type(): void {
WPDO_Post_Fields::register_entity_fields();
$field = $this->find_field( 'attachment', '_wp_attachment_metadata' );
$this->assertNotNull( $field );
$this->assertSame( 'json', $field['type'] );
}
// ── wc_product group ─────────────────────────────────────────────────────
public function test_wc_product_group_has_19_keys(): void {
WPDO_Post_Fields::register_entity_fields();
$keys = $this->get_field_keys( 'wc_product' );
$this->assertCount( 19, $keys, 'wc_product group should register exactly 19 keys.' );
}
public function test_wc_product_critical_keys_present(): void {
WPDO_Post_Fields::register_entity_fields();
$keys = $this->get_field_keys( 'wc_product' );
foreach ( array( '_price', '_regular_price', '_sale_price', '_stock', '_stock_status', '_sku' ) as $key ) {
$this->assertContains( $key, $keys, "wc_product missing critical key: {$key}" );
}
}
public function test_wc_product_price_is_searchable_decimal(): void {
WPDO_Post_Fields::register_entity_fields();
$field = $this->find_field( 'wc_product', '_price' );
$this->assertNotNull( $field );
$this->assertSame( 'decimal', $field['type'] );
$this->assertTrue( (bool) ( $field['searchable'] ?? false ) );
}
public function test_wc_product_stock_status_is_enum_searchable(): void {
WPDO_Post_Fields::register_entity_fields();
$field = $this->find_field( 'wc_product', '_stock_status' );
$this->assertNotNull( $field );
$this->assertSame( 'enum', $field['type'] );
$this->assertTrue( (bool) ( $field['searchable'] ?? false ) );
$this->assertContains( 'instock', $field['options'] );
$this->assertContains( 'outofstock', $field['options'] );
}
// ── HivePress groups moved to the HivePress AddOn ───────────────────────
public function test_core_registers_no_hivepress_groups(): void {
WPDO_Post_Fields::register_entity_fields();
foreach ( array( 'hp_listing_core', 'hp_request_core', 'hp_vendor_core' ) as $group ) {
$this->assertSame( array(), WPDO_Entity_Registry::get_group_fields( 'post', $group ), "{$group} is owned by the HivePress AddOn" );
}
}
// ── 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 {
WPDO_Post_Fields::register_entity_fields();
$keys = $this->get_field_keys( 'nav_menu_item' );
$this->assertCount( 8, $keys );
$this->assertContains( '_menu_item_type', $keys );
$this->assertContains( '_menu_item_object_id', $keys );
$this->assertContains( '_menu_item_url', $keys );
}
// ── Helpers ──────────────────────────────────────────────────────────────
/** @return string[] */
private function get_field_keys( string $group ): array {
$fields = WPDO_Entity_Registry::get_group_fields( 'post', $group );
return array_map( static fn( $f ) => $f['key'], $fields );
}
private function find_field( string $group, string $key ): ?array {
$fields = WPDO_Entity_Registry::get_group_fields( 'post', $group );
foreach ( $fields as $f ) {
if ( ( $f['key'] ?? '' ) === $key ) {
return $f;
}
}
return null;
}
}