Files
2meet-data-optimizer-hivepr…/tests/unit/HivePressEntityGroupsTest.php
T
wpdev 3c57fb0ee6 fix(2meet-data-optimizer-hivepress-addon): declare post_type scopes on HivePress post groups
- hp_listing_core / hp_request_core / hp_vendor_core declare hp_listing / hp_request / hp_vendor, matching core (PR-033), so the AddOn keeps scoped routing once core stops registering them (PR-020b)
- Test: with tmdo_post_scoped_routing on, every key of each group resolves to its own post type's group; legacy owner and hash pins unchanged
- Verify: HP unit 145 / integration 49 OK
- PR: PR-033b
2026-09-25 23:57:52 +08:00

83 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
/**
* The AddOn owns the HivePress entity groups core used to register.
*
* Their definitions are persisted (wpdo_registry_meta schema hash) and the
* flat tables on live sites were built from them, so they are pinned to the
* persisted hashes: any drift would ALTER flat tables that may hold
* aeav_only data. (Values recorded on dev20, 2026-09-25.)
*/
class HivePressEntityGroupsTest extends TestCase {
private const PERSISTED_HASHES = array(
'post/hp_listing_core' => 'fb5ab760',
'post/hp_request_core' => '27d6f4f5',
'post/hp_vendor_core' => '5366e5c8',
'user/hp_user' => '61ac526f',
);
/**
* Lookups that do not pass a post type (or run with scoped routing off,
* until a site's rows are reconciled) keep the legacy owner of keys shared
* by several post groups: the LAST group registered. Pinned so the
* registration order cannot change silently.
*/
private const SHARED_KEY_OWNERS = array(
'hp_status' => 'hp_request_core',
'hp_expired_time' => 'hp_request_core',
'hp_view_count' => 'hp_request_core',
'hp_verified' => 'hp_vendor_core',
'hp_rating' => 'hp_vendor_core',
'hp_rating_count' => 'hp_vendor_core',
'hp_hourly_rate' => 'hp_vendor_core',
'hp_user' => 'hp_vendor_core',
);
protected function setUp(): void {
TMDO_Entity_Registry::init();
TMDO_Entity_Registry::register_adapter( 'post', $this->createMock( TMDO_Entity_Adapter_Interface::class ) );
TMDO_Entity_Registry::register_adapter( 'user', $this->createMock( TMDO_Entity_Adapter_Interface::class ) );
TMDO_HivePress_Entity_Groups::register_entity_fields();
}
protected function tearDown(): void {
unset( $GLOBALS['_wp_options'][ TMDO_Entity_Registry::SCOPED_ROUTING_OPTION ] );
}
public function test_definitions_match_persisted_schema_hashes(): void {
foreach ( self::PERSISTED_HASHES as $id => $hash ) {
list( $type, $group ) = explode( '/', $id );
$fields = TMDO_Entity_Registry::get_group_fields( $type, $group );
$this->assertNotEmpty( $fields, "{$id} not registered" );
$this->assertSame( $hash, substr( TMDO_Schema_Manager::calculate_schema_hash( $fields ), 0, 8 ), "{$id} drifted from the persisted definition" );
}
}
public function test_shared_keys_keep_their_current_owner(): void {
foreach ( self::SHARED_KEY_OWNERS as $key => $group ) {
$this->assertSame( $group, TMDO_Entity_Registry::get_field( 'post', $key )['group'], $key );
}
}
public function test_shared_keys_follow_the_post_type_when_scoped_routing_is_on(): void {
$GLOBALS['_wp_options'][ TMDO_Entity_Registry::SCOPED_ROUTING_OPTION ] = '1';
foreach ( array( 'hp_listing' => 'hp_listing_core', 'hp_request' => 'hp_request_core', 'hp_vendor' => 'hp_vendor_core' ) as $post_type => $group ) {
foreach ( TMDO_Entity_Registry::get_group_keys( 'post', $group ) as $key ) {
$this->assertSame( $group, TMDO_Entity_Registry::get_field( 'post', $key, $post_type )['group'], "{$post_type}:{$key}" );
}
}
$this->assertNull( TMDO_Entity_Registry::get_field( 'post', 'hp_rating', 'post' ), 'HivePress keys stay native on other post types' );
}
public function test_core_adapter_does_not_register_a_conflicting_group(): void {
( new TMDO_HivePress_Core_Adapter() )->on_register_entity_fields();
$this->assertSame( array(), TMDO_Entity_Registry::get_collisions() );
}
}