Files
2meet-data-optimizer/tests/unit/PublicApiTest.php
T
wpdev d36bb954d1 chore: initial snapshot of 2meet-data-optimizer v0.1.0
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
2026-07-31 05:06:36 +08:00

143 lines
4.5 KiB
PHP

<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
/**
* Unit tests for WPDO_API — public facade for partner plugins (PR-5).
*
* @covers WPDO_API
*/
class PublicApiTest extends TestCase {
protected function setUp(): void {
$GLOBALS['_wp_postmeta'] = array();
$GLOBALS['_wp_usermeta'] = array();
$GLOBALS['_wp_termmeta'] = array();
$GLOBALS['_wp_commentmeta'] = array();
$GLOBALS['_wp_options'] = array();
// Reset Schema_Registry singleton.
$ref = new ReflectionClass( WPDO_Schema_Registry::class );
$instance = $ref->getProperty( 'instance' );
$instance->setAccessible( true );
$instance->setValue( null, null );
// Reset Feature_Flags caches (state pollution between tests).
$ref = new ReflectionClass( WPDO_Feature_Flags::class );
foreach ( array( 'cache', 'shadow_cache' ) as $prop ) {
$p = $ref->getProperty( $prop );
$p->setAccessible( true );
$p->setValue( null, null );
}
// Reset Entity_Registry static state.
WPDO_Entity_Registry::init();
}
// ── get_field / set_field (post entity) ─────────────────────────────────
public function test_get_field_returns_postmeta_value(): void {
update_post_meta( 100, 'hp_price', '199.99' );
$this->assertSame( '199.99', WPDO_API::get_field( 100, 'hp_price' ) );
}
public function test_set_field_writes_postmeta(): void {
WPDO_API::set_field( 200, 'hp_price', '299.99' );
$this->assertSame( '299.99', get_post_meta( 200, 'hp_price', true ) );
}
public function test_get_field_missing_returns_empty_string_when_single(): void {
$this->assertSame( '', WPDO_API::get_field( 9999, 'nonexistent' ) );
}
// ── get_entity / set_entity (multi-entity) ──────────────────────────────
public function test_get_entity_post_dispatches_correctly(): void {
update_post_meta( 1, 'k', 'pv' );
$this->assertSame( 'pv', WPDO_API::get_entity( 'post', 1, 'k' ) );
}
public function test_get_entity_unknown_type_returns_null(): void {
$this->assertNull( WPDO_API::get_entity( 'invalid', 1, 'k' ) );
}
public function test_set_entity_unknown_type_returns_false(): void {
$this->assertFalse( WPDO_API::set_entity( 'invalid', 1, 'k', 'v' ) );
}
// ── is_field_registered ─────────────────────────────────────────────────
public function test_is_field_registered_returns_false_for_unknown(): void {
$this->assertFalse( WPDO_API::is_field_registered( 'post', 'unregistered_key' ) );
}
public function test_is_field_registered_true_after_schema_register(): void {
WPDO_Schema_Registry::instance()->register(
'test',
array(
'post_type' => 'hp_listing',
'meta_key' => 'hp_price',
'zone' => 'hot',
'data_type' => 'decimal(10,2)',
'column' => 'hp_price',
)
);
$this->assertTrue( WPDO_API::is_field_registered( 'post', 'hp_price' ) );
}
// ── trace_storage ──────────────────────────────────────────────────────
public function test_trace_storage_unregistered_when_no_field(): void {
$this->assertSame(
'unregistered',
WPDO_API::trace_storage( 'post', 'unknown', 'hp_listing' )
);
}
public function test_trace_storage_postmeta_when_registered_but_not_cutover(): void {
WPDO_Schema_Registry::instance()->register(
'test',
array(
'post_type' => 'hp_listing',
'meta_key' => 'hp_price',
'zone' => 'hot',
'data_type' => 'decimal(10,2)',
'column' => 'hp_price',
)
);
// Module starts in 'idle' → not read-custom → returns 'postmeta'.
$this->assertSame(
'postmeta',
WPDO_API::trace_storage( 'post', 'hp_price', 'hp_listing' )
);
}
public function test_trace_storage_zone_after_cutover(): void {
$GLOBALS['_wp_options'] = array();
$ref = new ReflectionClass( WPDO_Feature_Flags::class );
$cache = $ref->getProperty( 'cache' );
$cache->setAccessible( true );
$cache->setValue( null, null );
WPDO_Schema_Registry::instance()->register(
'test',
array(
'post_type' => 'hp_listing',
'meta_key' => 'hp_price',
'zone' => 'hot',
'data_type' => 'decimal(10,2)',
'column' => 'hp_price',
)
);
WPDO_Feature_Flags::set( 'hot_hp_listing', 'cutover' );
$this->assertSame(
'zone_hot',
WPDO_API::trace_storage( 'post', 'hp_price', 'hp_listing' )
);
}
}