d36bb954d1
Baseline before backporting wp-data-optimizer v3.0.1-v3.4.6. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TbG1keQQ7XBa7qMQY16KCY
179 lines
7.2 KiB
PHP
179 lines
7.2 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 seven
|
|
* 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_seven_groups_are_registered(): void {
|
|
WPDO_Post_Fields::register_entity_fields();
|
|
|
|
$expected = array(
|
|
'wp_core',
|
|
'attachment',
|
|
'wc_product',
|
|
'hp_listing_core',
|
|
'hp_request_core',
|
|
'hp_vendor_core',
|
|
'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'] );
|
|
}
|
|
|
|
// ── hp_listing_core group ────────────────────────────────────────────────
|
|
|
|
public function test_hp_listing_core_critical_keys_present(): void {
|
|
WPDO_Post_Fields::register_entity_fields();
|
|
$keys = $this->get_field_keys( 'hp_listing_core' );
|
|
foreach ( array( 'hp_price', 'hp_status', 'hp_featured', 'hp_verified', 'hp_vendor' ) as $key ) {
|
|
$this->assertContains( $key, $keys, "hp_listing_core missing critical key: {$key}" );
|
|
}
|
|
}
|
|
|
|
public function test_hp_listing_price_is_searchable_decimal(): void {
|
|
WPDO_Post_Fields::register_entity_fields();
|
|
$field = $this->find_field( 'hp_listing_core', 'hp_price' );
|
|
$this->assertNotNull( $field );
|
|
$this->assertSame( 'decimal', $field['type'] );
|
|
$this->assertTrue( (bool) ( $field['searchable'] ?? false ) );
|
|
}
|
|
|
|
// ── 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;
|
|
}
|
|
}
|