test: 建立 phpunit 測試基建並移植 25 個 HivePress 測試
AddOn 先前 tests=0。新增: - composer.json(dev 依賴 + phpunit/phpcs script) - phpunit.xml(failOnWarning=true) - tests/bootstrap.php:直接 require 核心 plugin 的 unit bootstrap,避免複製 ~700 行 WP stub,再載入本 AddOn 的類別;補 trait wrapper 與自動 WPDO_ alias (includes/back-compat-aliases.php 掛在 plugins_loaded:7,PHPUnit 下不會跑) - tests/unit/ 25 個測試 + WpdbMockTrait(自 A 移植) 生產碼一併修對外契約:interface-tmdo-hp-adapter.php 改為先宣告空的 WPDO_HivePress_Adapter、再讓 TMDO_HivePress_Adapter extends 它(PHP 無法 class_alias 介面,只有繼承能讓 instanceof WPDO_HivePress_Adapter 對 implements TMDO_ 名稱的 adapter 成立)。與核心 interface-entity-adapter.php 同一模式。這也讓第三方自訂 adapter 用舊介面名仍可通過核心檢查。 145 tests / 357 assertions GREEN Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TbG1keQQ7XBa7qMQY16KCY
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Tests for WPDO_HivePress_Adapter_Trait — score composition + helper sugar.
|
||||
*
|
||||
* Uses an anonymous adapter to exercise trait behaviour without requiring
|
||||
* a concrete production adapter.
|
||||
*
|
||||
* @covers WPDO_HivePress_Adapter_Trait
|
||||
*/
|
||||
class HivePressAdapterTraitTest extends TestCase {
|
||||
|
||||
/**
|
||||
* Build a fresh anonymous adapter with optional score override.
|
||||
*/
|
||||
private function make_adapter( array $score_override = array() ): object {
|
||||
return new class( $score_override ) implements WPDO_HivePress_Adapter {
|
||||
use WPDO_HivePress_Adapter_Trait;
|
||||
|
||||
private array $override;
|
||||
|
||||
public function __construct( array $override ) {
|
||||
$this->override = $override;
|
||||
}
|
||||
|
||||
public function plugin_slug(): string {
|
||||
return 'test-adapter';
|
||||
}
|
||||
|
||||
public function detection_class(): string {
|
||||
return 'Test\\Adapter';
|
||||
}
|
||||
|
||||
public function detection_const(): string {
|
||||
return 'TEST_ADAPTER_VERSION';
|
||||
}
|
||||
|
||||
public function suitability_score(): array {
|
||||
return $this->compose_score( $this->override );
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public function test_default_score_is_perfect_ten(): void {
|
||||
$score = $this->make_adapter()->suitability_score();
|
||||
$this->assertSame( 10.0, $score['aggregate'] );
|
||||
foreach ( array( 'd1_meta_calls', 'd2_meta_sql', 'd3_table_coverage', 'd4_registry_meta', 'd5_hook_bus', 'd6_options', 'd7_coupling', 'd8_perf' ) as $k ) {
|
||||
$this->assertSame( 1.0, $score[ $k ] );
|
||||
}
|
||||
}
|
||||
|
||||
public function test_partial_override_lowers_aggregate(): void {
|
||||
$score = $this->make_adapter( array( 'd5_hook_bus' => 0.5 ) )->suitability_score();
|
||||
// 7 × 1.0 + 1 × 0.5 = 7.5; aggregate = 7.5 / 8 * 10 = 9.375 → rounded 9.38.
|
||||
$this->assertSame( 9.38, $score['aggregate'] );
|
||||
}
|
||||
|
||||
public function test_score_clamped_to_unit_interval(): void {
|
||||
$score = $this->make_adapter( array(
|
||||
'd1_meta_calls' => 1.5, // → clamped to 1.0
|
||||
'd2_meta_sql' => -0.3, // → clamped to 0.0
|
||||
) )->suitability_score();
|
||||
$this->assertSame( 1.0, $score['d1_meta_calls'] );
|
||||
$this->assertSame( 0.0, $score['d2_meta_sql'] );
|
||||
}
|
||||
|
||||
public function test_default_implementations_are_no_op(): void {
|
||||
$adapter = $this->make_adapter();
|
||||
|
||||
// All optional methods should be safe to invoke on a minimal adapter.
|
||||
$this->assertSame( array(), $adapter->migrations() );
|
||||
$this->assertSame( '', $adapter->minimum_addon_version() );
|
||||
|
||||
$result = $adapter->doctor_check();
|
||||
$this->assertTrue( $result['ok'] );
|
||||
|
||||
// Hook handlers must not throw on a no-op adapter.
|
||||
$adapter->on_register_query_hooks();
|
||||
$adapter->on_register_event_hooks();
|
||||
$adapter->on_register_entity_fields();
|
||||
$this->assertTrue( true ); // reaching here = no exception
|
||||
}
|
||||
|
||||
public function test_aggregate_rounding_is_two_decimals(): void {
|
||||
$score = $this->make_adapter( array(
|
||||
'd1_meta_calls' => 0.85,
|
||||
'd2_meta_sql' => 0.92,
|
||||
'd3_table_coverage' => 0.78,
|
||||
) )->suitability_score();
|
||||
|
||||
// All other dimensions stay at 1.0 → sum = 0.85+0.92+0.78+5*1.0 = 7.55.
|
||||
// aggregate = 7.55 / 8 * 10 = 9.4375 → round(2) = 9.44.
|
||||
$this->assertSame( 9.44, $score['aggregate'] );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Tests for WPDO_Admin_HivePress.
|
||||
*
|
||||
* @covers WPDO_Admin_HivePress
|
||||
*/
|
||||
class AdminHivePressTest extends TestCase {
|
||||
|
||||
protected function setUp(): void {
|
||||
WPDO_HivePress_Bootstrap::reset_for_tests();
|
||||
WPDO_HivePress_Detector::reset_for_tests();
|
||||
WPDO_HivePress_Conflict_Guard::reset_for_tests();
|
||||
}
|
||||
|
||||
public function test_render_outputs_addon_catalog(): void {
|
||||
$out = self::capture( static function () {
|
||||
WPDO_Admin_HivePress::render();
|
||||
} );
|
||||
|
||||
$this->assertStringContainsString( 'HivePress', $out );
|
||||
$this->assertStringContainsString( 'hivepress', $out );
|
||||
$this->assertStringContainsString( 'wp wpdo hivepress', $out );
|
||||
}
|
||||
|
||||
public function test_render_shows_clean_when_no_conflicts(): void {
|
||||
$out = self::capture( static function () {
|
||||
WPDO_Admin_HivePress::render();
|
||||
} );
|
||||
|
||||
$this->assertStringContainsString( 'Conflict guard', $out );
|
||||
$this->assertStringContainsString( 'notice-success', $out );
|
||||
}
|
||||
|
||||
/**
|
||||
* Capture echo output of a callable in a single buffer level.
|
||||
*
|
||||
* Avoids PHPUnit's "did not close output buffers" warning by using
|
||||
* the level-checked open/close pattern.
|
||||
*/
|
||||
private static function capture( callable $callable ): string {
|
||||
$start_level = ob_get_level();
|
||||
ob_start();
|
||||
try {
|
||||
$callable();
|
||||
} finally {
|
||||
$out = '';
|
||||
while ( ob_get_level() > $start_level ) {
|
||||
$out = (string) ob_get_clean() . $out;
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Tests for WPDO_HivePress_Attribute_Bridge.
|
||||
*
|
||||
* @covers WPDO_HivePress_Attribute_Bridge
|
||||
*/
|
||||
class HivePressAttributeBridgeTest extends TestCase {
|
||||
|
||||
private WPDO_HivePress_Attribute_Bridge $bridge;
|
||||
|
||||
protected function setUp(): void {
|
||||
$this->bridge = new WPDO_HivePress_Attribute_Bridge();
|
||||
}
|
||||
|
||||
public function test_discover_returns_empty_when_no_attributes_registered(): void {
|
||||
// apply_filters stub returns the value unchanged → empty array.
|
||||
$this->assertSame( array(), $this->bridge->discover_attributes() );
|
||||
}
|
||||
|
||||
public function test_suggest_zone_configs_returns_empty_when_no_attributes(): void {
|
||||
$this->assertSame( array(), $this->bridge->suggest_zone_configs() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify the SQL type mapping is sensible for known HivePress field types.
|
||||
*
|
||||
* Indirect test via reflection — the public API only returns full configs
|
||||
* but we want fast feedback if the type table drifts.
|
||||
*/
|
||||
public function test_sql_type_mapping_via_reflection(): void {
|
||||
$reflect = new ReflectionClass( $this->bridge );
|
||||
$method = $reflect->getMethod( 'sql_type_for' );
|
||||
$method->setAccessible( true );
|
||||
|
||||
$this->assertStringContainsString( 'int', $method->invoke( $this->bridge, 'number' ) );
|
||||
$this->assertStringContainsString( 'decimal', $method->invoke( $this->bridge, 'price' ) );
|
||||
$this->assertStringContainsString( 'tinyint', $method->invoke( $this->bridge, 'checkbox' ) );
|
||||
$this->assertStringContainsString( 'date', $method->invoke( $this->bridge, 'date' ) );
|
||||
$this->assertStringContainsString( 'varchar', $method->invoke( $this->bridge, 'text' ) );
|
||||
// Unknown type → fallback to varchar.
|
||||
$this->assertStringContainsString( 'varchar', $method->invoke( $this->bridge, 'unknown_type' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify build_record normalises HP attribute config correctly.
|
||||
*/
|
||||
public function test_build_record_normalises_attribute_config(): void {
|
||||
$reflect = new ReflectionClass( $this->bridge );
|
||||
$method = $reflect->getMethod( 'build_record' );
|
||||
$method->setAccessible( true );
|
||||
|
||||
$record = $method->invoke( $this->bridge, 'listing', 'beds', array(
|
||||
'edit_field' => array( 'type' => 'integer' ),
|
||||
'filterable' => true,
|
||||
'sortable' => false,
|
||||
'searchable' => true,
|
||||
) );
|
||||
|
||||
$this->assertSame( 'listing', $record['model'] );
|
||||
$this->assertSame( 'beds', $record['slug'] );
|
||||
$this->assertSame( 'hp_listing_beds', $record['meta_key'] );
|
||||
$this->assertSame( 'integer', $record['type'] );
|
||||
$this->assertTrue( $record['filterable'] );
|
||||
$this->assertFalse( $record['sortable'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Promotion candidate logic: filterable OR sortable triggers promotion.
|
||||
*/
|
||||
public function test_promotion_candidate_logic(): void {
|
||||
$reflect = new ReflectionClass( $this->bridge );
|
||||
$method = $reflect->getMethod( 'is_promotion_candidate' );
|
||||
$method->setAccessible( true );
|
||||
|
||||
// Filterable but not sortable → promote.
|
||||
$this->assertTrue( $method->invoke( $this->bridge, array(
|
||||
'model' => 'listing', 'slug' => 'x', 'meta_key' => 'hp_listing_x',
|
||||
'type' => 'text', 'filterable' => true, 'sortable' => false, 'searchable' => true,
|
||||
) ) );
|
||||
|
||||
// Sortable but not filterable → promote.
|
||||
$this->assertTrue( $method->invoke( $this->bridge, array(
|
||||
'model' => 'listing', 'slug' => 'x', 'meta_key' => 'hp_listing_x',
|
||||
'type' => 'text', 'filterable' => false, 'sortable' => true, 'searchable' => false,
|
||||
) ) );
|
||||
|
||||
// Searchable only (no filter, no sort) → don't promote (full-text would
|
||||
// be a different optimization).
|
||||
$this->assertFalse( $method->invoke( $this->bridge, array(
|
||||
'model' => 'listing', 'slug' => 'x', 'meta_key' => 'hp_listing_x',
|
||||
'type' => 'text', 'filterable' => false, 'sortable' => false, 'searchable' => true,
|
||||
) ) );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Tests for WPDO_HivePress_Benchmark.
|
||||
*
|
||||
* @covers WPDO_HivePress_Benchmark
|
||||
*/
|
||||
class HivePressBenchmarkTest extends TestCase {
|
||||
|
||||
public function test_default_iterations_is_fifty(): void {
|
||||
$this->assertSame( 50, WPDO_HivePress_Benchmark::DEFAULT_ITERATIONS );
|
||||
}
|
||||
|
||||
public function test_run_returns_mean_milliseconds(): void {
|
||||
$ms = WPDO_HivePress_Benchmark::run( static function () {
|
||||
usleep( 1000 ); // 1 ms
|
||||
}, 5 );
|
||||
|
||||
// Allow generous slack for slow CI runners; we just need it to be > 0.
|
||||
$this->assertGreaterThan( 0.0, $ms );
|
||||
}
|
||||
|
||||
public function test_compare_returns_speedup_ratio(): void {
|
||||
$result = WPDO_HivePress_Benchmark::compare(
|
||||
'sample',
|
||||
static function () { usleep( 4000 ); }, // ~4 ms baseline
|
||||
static function () { usleep( 1000 ); }, // ~1 ms target
|
||||
3
|
||||
);
|
||||
|
||||
$this->assertSame( 'sample', $result['name'] );
|
||||
$this->assertGreaterThan( 0.0, $result['baseline_ms'] );
|
||||
$this->assertGreaterThan( 0.0, $result['target_ms'] );
|
||||
// Baseline is roughly 4× target — ratio should be > 1 (faster).
|
||||
$this->assertGreaterThan( 1.0, $result['ratio'] );
|
||||
$this->assertSame( 3, $result['iterations'] );
|
||||
}
|
||||
|
||||
public function test_compare_handles_zero_target_time(): void {
|
||||
// If target is suspiciously fast, ratio falls back to 0 (no division).
|
||||
$result = WPDO_HivePress_Benchmark::compare(
|
||||
'instant',
|
||||
static function () { /* noop */ },
|
||||
static function () { /* noop */ },
|
||||
1
|
||||
);
|
||||
// Either ratio > 0 (both nonzero) or 0 (target_ms was 0); never NaN/inf.
|
||||
$this->assertIsFloat( $result['ratio'] );
|
||||
$this->assertFalse( is_nan( $result['ratio'] ) );
|
||||
$this->assertFalse( is_infinite( $result['ratio'] ) );
|
||||
}
|
||||
|
||||
public function test_run_suite_aggregates_samples(): void {
|
||||
$report = WPDO_HivePress_Benchmark::run_suite( array(
|
||||
array(
|
||||
'name' => 'a',
|
||||
'baseline' => static function () { usleep( 2000 ); },
|
||||
'target' => static function () { usleep( 500 ); },
|
||||
'iterations' => 2,
|
||||
),
|
||||
array(
|
||||
'name' => 'b',
|
||||
'baseline' => static function () { usleep( 4000 ); },
|
||||
'target' => static function () { usleep( 1000 ); },
|
||||
'iterations' => 2,
|
||||
),
|
||||
) );
|
||||
|
||||
$this->assertSame( 2, $report['sample_count'] );
|
||||
$this->assertGreaterThan( 0.0, $report['geomean_ratio'] );
|
||||
}
|
||||
|
||||
public function test_run_suite_skips_malformed_sample(): void {
|
||||
$report = WPDO_HivePress_Benchmark::run_suite( array(
|
||||
array( 'name' => 'incomplete' ), // missing baseline + target
|
||||
array(
|
||||
'name' => 'ok',
|
||||
'baseline' => static function () { usleep( 100 ); },
|
||||
'target' => static function () { usleep( 100 ); },
|
||||
'iterations' => 1,
|
||||
),
|
||||
) );
|
||||
|
||||
$this->assertSame( 1, $report['sample_count'] );
|
||||
}
|
||||
|
||||
public function test_geometric_mean_with_empty_values_returns_zero(): void {
|
||||
$this->assertSame( 0.0, WPDO_HivePress_Benchmark::geometric_mean( array() ) );
|
||||
}
|
||||
|
||||
public function test_geometric_mean_resists_outliers(): void {
|
||||
// Geomean of (2, 8) = sqrt(16) = 4 (vs arithmetic mean 5).
|
||||
$this->assertSame( 4.0, WPDO_HivePress_Benchmark::geometric_mean( array( 2.0, 8.0 ) ) );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Tests for WPDO_HivePress_Blocks_Adapter.
|
||||
*
|
||||
* @covers WPDO_HivePress_Blocks_Adapter
|
||||
*/
|
||||
class HivePressBlocksAdapterTest extends TestCase {
|
||||
|
||||
private WPDO_HivePress_Blocks_Adapter $adapter;
|
||||
|
||||
protected function setUp(): void {
|
||||
$this->adapter = new WPDO_HivePress_Blocks_Adapter();
|
||||
}
|
||||
|
||||
public function test_implements_adapter_interface(): void {
|
||||
$this->assertInstanceOf( WPDO_HivePress_Adapter::class, $this->adapter );
|
||||
}
|
||||
|
||||
public function test_identity(): void {
|
||||
$this->assertSame( 'hivepress-blocks', $this->adapter->plugin_slug() );
|
||||
$this->assertSame( 'HivePress\\Blocks\\Plugin', $this->adapter->detection_class() );
|
||||
}
|
||||
|
||||
public function test_doctor_check_is_always_ok(): void {
|
||||
$result = $this->adapter->doctor_check();
|
||||
$this->assertTrue( $result['ok'] );
|
||||
$this->assertStringContainsString( 'presentation-only', $result['message'] );
|
||||
}
|
||||
|
||||
public function test_score_aggregates_to_perfect_ten(): void {
|
||||
$this->assertSame( 10.0, $this->adapter->suitability_score()['aggregate'] );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Tests for WPDO_HivePress_Bookings_Adapter (detect-only).
|
||||
*
|
||||
* @covers WPDO_HivePress_Bookings_Adapter
|
||||
*/
|
||||
class HivePressBookingsAdapterTest extends TestCase {
|
||||
|
||||
use WpdbMockTrait;
|
||||
|
||||
private WPDO_HivePress_Bookings_Adapter $adapter;
|
||||
|
||||
protected function setUp(): void {
|
||||
$this->install_wpdb_mock();
|
||||
$reflect = new ReflectionClass( WPDO_Schema_Registry::class );
|
||||
$prop = $reflect->getProperty( 'instance' );
|
||||
$prop->setAccessible( true );
|
||||
$prop->setValue( null, null );
|
||||
|
||||
$this->adapter = new WPDO_HivePress_Bookings_Adapter();
|
||||
}
|
||||
|
||||
public function test_implements_adapter_interface(): void {
|
||||
$this->assertInstanceOf( WPDO_HivePress_Adapter::class, $this->adapter );
|
||||
}
|
||||
|
||||
public function test_identity(): void {
|
||||
$this->assertSame( 'hivepress-bookings', $this->adapter->plugin_slug() );
|
||||
$this->assertSame( 'HivePress\\Bookings\\Plugin', $this->adapter->detection_class() );
|
||||
}
|
||||
|
||||
public function test_on_register_fields_reserves_booking_hot_columns(): void {
|
||||
$registry = WPDO_Schema_Registry::instance();
|
||||
$this->adapter->on_register_fields( $registry );
|
||||
|
||||
$start = $registry->get_field( 'hp_booking', 'hp_start_time' );
|
||||
$this->assertSame( 'hot', $start['zone'] );
|
||||
$this->assertTrue( (bool) $start['indexed'] );
|
||||
|
||||
$end = $registry->get_field( 'hp_booking', 'hp_end_time' );
|
||||
$this->assertSame( 'hot', $end['zone'] );
|
||||
$this->assertTrue( (bool) $end['indexed'] );
|
||||
}
|
||||
|
||||
public function test_score_reflects_unverified_perf_dimension(): void {
|
||||
$score = $this->adapter->suitability_score();
|
||||
// D8 reduced to 0.9 because schema is best-effort until addon installs.
|
||||
$this->assertSame( 0.9, $score['d8_perf'] );
|
||||
// Aggregate = (7 × 1.0 + 0.9) / 8 × 10 = 9.875 → 9.88.
|
||||
$this->assertSame( 9.88, $score['aggregate'] );
|
||||
}
|
||||
|
||||
public function test_migrations_lists_booking_module(): void {
|
||||
$this->assertSame( array( 'hot_hp_booking' ), $this->adapter->migrations() );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Tests for WPDO_HivePress_Bootstrap — orchestration entry point.
|
||||
*
|
||||
* @covers WPDO_HivePress_Bootstrap
|
||||
*/
|
||||
class HivePressBootstrapTest extends TestCase {
|
||||
|
||||
protected function setUp(): void {
|
||||
WPDO_HivePress_Bootstrap::reset_for_tests();
|
||||
WPDO_HivePress_Detector::reset_for_tests();
|
||||
WPDO_HivePress_Conflict_Guard::reset_for_tests();
|
||||
// Clear detector transient.
|
||||
unset( $GLOBALS['_wp_options']['_transient_wpdo_hivepress_detector_cache'] );
|
||||
}
|
||||
|
||||
public function test_boot_is_idempotent_on_non_hivepress_site(): void {
|
||||
WPDO_HivePress_Bootstrap::boot();
|
||||
$this->assertSame( array(), WPDO_HivePress_Bootstrap::adapters() );
|
||||
$this->assertSame( array(), WPDO_HivePress_Bootstrap::detected() );
|
||||
|
||||
// Second call must not fail or change state.
|
||||
WPDO_HivePress_Bootstrap::boot();
|
||||
$this->assertSame( array(), WPDO_HivePress_Bootstrap::adapters() );
|
||||
}
|
||||
|
||||
public function test_adapter_for_returns_null_when_not_booted(): void {
|
||||
$this->assertNull( WPDO_HivePress_Bootstrap::adapter_for( 'hivepress' ) );
|
||||
$this->assertNull( WPDO_HivePress_Bootstrap::adapter_for( 'hivepress-reviews' ) );
|
||||
$this->assertNull( WPDO_HivePress_Bootstrap::adapter_for( '' ) );
|
||||
}
|
||||
|
||||
public function test_reset_clears_internal_state(): void {
|
||||
WPDO_HivePress_Bootstrap::boot();
|
||||
WPDO_HivePress_Bootstrap::reset_for_tests();
|
||||
|
||||
// After reset boot() should run again; detected() should be re-populated
|
||||
// (still empty in the test env, but the call must not be skipped).
|
||||
WPDO_HivePress_Bootstrap::boot();
|
||||
$this->assertIsArray( WPDO_HivePress_Bootstrap::detected() );
|
||||
}
|
||||
|
||||
public function test_should_bind_filter_short_circuits_binding(): void {
|
||||
// Even if HivePress were detected, the filter must be honoured.
|
||||
// We can't fake detection cleanly here without reflection, so instead
|
||||
// we assert the filter is at least exposed and called: no detection
|
||||
// → no adapter binding → empty adapters list regardless of filter.
|
||||
add_filter( 'wpdo_hivepress_should_bind', '__return_false' );
|
||||
WPDO_HivePress_Bootstrap::boot();
|
||||
$this->assertSame( array(), WPDO_HivePress_Bootstrap::adapters() );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Tests for WPDO_CLI_HivePress (smoke).
|
||||
*
|
||||
* Real CLI execution requires WP_CLI runtime — these smoke tests just verify
|
||||
* the class loads, methods exist, and dispatch table aligns with what
|
||||
* 2meet-data-optimizer.php registers.
|
||||
*
|
||||
* @covers WPDO_CLI_HivePress
|
||||
*/
|
||||
class CliHivePressTest extends TestCase {
|
||||
|
||||
public function test_class_exists_and_methods_present(): void {
|
||||
$this->assertTrue( class_exists( 'WPDO_CLI_HivePress' ) );
|
||||
|
||||
$expected = array( 'detect', 'score', 'doctor', 'migrate', 'rollback', 'benchmark' );
|
||||
foreach ( $expected as $method ) {
|
||||
$this->assertTrue(
|
||||
method_exists( 'WPDO_CLI_HivePress', $method ),
|
||||
sprintf( 'Method %s missing on WPDO_CLI_HivePress', $method )
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function test_set_state_helper_is_private(): void {
|
||||
$reflect = new ReflectionClass( WPDO_CLI_HivePress::class );
|
||||
$this->assertTrue( $reflect->hasMethod( 'set_state' ) );
|
||||
$method = $reflect->getMethod( 'set_state' );
|
||||
$this->assertTrue( $method->isPrivate() );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Tests for WPDO_HivePress_Comment_Router.
|
||||
*
|
||||
* @covers WPDO_HivePress_Comment_Router
|
||||
*/
|
||||
class HivePressCommentRouterTest extends TestCase {
|
||||
|
||||
use WpdbMockTrait;
|
||||
|
||||
protected function setUp(): void {
|
||||
$this->install_wpdb_mock();
|
||||
WPDO_HivePress_Comment_Router::reset_for_tests();
|
||||
// Default disabled.
|
||||
$GLOBALS['_wp_options'][ WPDO_HivePress_Comment_Router::OPTION_ENABLED ] = 0;
|
||||
}
|
||||
|
||||
public function test_disabled_by_default(): void {
|
||||
$this->assertFalse( WPDO_HivePress_Comment_Router::is_enabled() );
|
||||
}
|
||||
|
||||
public function test_enabled_when_option_set(): void {
|
||||
$GLOBALS['_wp_options'][ WPDO_HivePress_Comment_Router::OPTION_ENABLED ] = 1;
|
||||
$this->assertTrue( WPDO_HivePress_Comment_Router::is_enabled() );
|
||||
}
|
||||
|
||||
public function test_register_is_idempotent(): void {
|
||||
$GLOBALS['_wp_options'][ WPDO_HivePress_Comment_Router::OPTION_ENABLED ] = 1;
|
||||
WPDO_HivePress_Comment_Router::register();
|
||||
WPDO_HivePress_Comment_Router::register(); // Second call must be no-op.
|
||||
$this->assertTrue( true ); // No throw = pass
|
||||
}
|
||||
|
||||
public function test_rewrite_skips_unknown_comment_type(): void {
|
||||
$query = new stdClass();
|
||||
$query->query_vars = array( 'type' => 'comment' ); // not in shadow map
|
||||
|
||||
$clauses = array( 'join' => '', 'where' => '1=1' );
|
||||
$out = WPDO_HivePress_Comment_Router::rewrite( $clauses, $query );
|
||||
|
||||
$this->assertSame( $clauses, $out );
|
||||
}
|
||||
|
||||
public function test_rewrite_adds_left_join_for_hp_favorite(): void {
|
||||
$query = new stdClass();
|
||||
$query->query_vars = array( 'type' => 'hp_favorite' );
|
||||
|
||||
$clauses = array( 'join' => '', 'where' => '1=1' );
|
||||
$out = WPDO_HivePress_Comment_Router::rewrite( $clauses, $query );
|
||||
|
||||
$this->assertStringContainsString( 'LEFT JOIN', $out['join'] );
|
||||
$this->assertStringContainsString( 'wpdo_comment_hp_favorite', $out['join'] );
|
||||
$this->assertStringContainsString( 'AS wpdo_shadow', $out['join'] );
|
||||
}
|
||||
|
||||
public function test_rewrite_promotes_recipient_for_hp_message(): void {
|
||||
$query = new stdClass();
|
||||
$query->query_vars = array( 'type' => 'hp_message' );
|
||||
|
||||
$clauses = array(
|
||||
'join' => '',
|
||||
'where' => 'wp_comments.comment_karma = 42 AND comment_approved = 0',
|
||||
);
|
||||
$out = WPDO_HivePress_Comment_Router::rewrite( $clauses, $query );
|
||||
|
||||
// recipient lookup should be rewritten to use shadow column.
|
||||
$this->assertStringContainsString( 'wpdo_shadow.recipient_id = 42', $out['where'] );
|
||||
// Other predicates preserved.
|
||||
$this->assertStringContainsString( 'comment_approved = 0', $out['where'] );
|
||||
}
|
||||
|
||||
public function test_rewrite_handles_array_type(): void {
|
||||
$query = new stdClass();
|
||||
$query->query_vars = array( 'type' => array( 'hp_review', 'comment' ) );
|
||||
|
||||
$clauses = array( 'join' => '', 'where' => '' );
|
||||
$out = WPDO_HivePress_Comment_Router::rewrite( $clauses, $query );
|
||||
|
||||
// First element of type array should match (hp_review).
|
||||
$this->assertStringContainsString( 'wpdo_comment_hp_review', $out['join'] );
|
||||
}
|
||||
|
||||
public function test_rewrite_returns_unchanged_when_query_invalid(): void {
|
||||
$clauses = array( 'join' => '', 'where' => '1=1' );
|
||||
$out = WPDO_HivePress_Comment_Router::rewrite( $clauses, 'not an object' );
|
||||
$this->assertSame( $clauses, $out );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Tests for WPDO_HivePress_Conflict_Guard — legacy hp-* plugin detection.
|
||||
*
|
||||
* @covers WPDO_HivePress_Conflict_Guard
|
||||
*/
|
||||
class HivePressConflictGuardTest extends TestCase {
|
||||
|
||||
protected function setUp(): void {
|
||||
WPDO_HivePress_Conflict_Guard::reset_for_tests();
|
||||
}
|
||||
|
||||
public function test_no_conflicts_when_legacy_plugins_absent(): void {
|
||||
// Neither HPCT_Core nor HP_Info_Cards are loaded in the test env.
|
||||
$conflicts = WPDO_HivePress_Conflict_Guard::detect_conflicts();
|
||||
$this->assertSame( array(), $conflicts );
|
||||
$this->assertFalse( WPDO_HivePress_Conflict_Guard::has_conflict() );
|
||||
}
|
||||
|
||||
public function test_conflict_detected_when_hpct_const_defined(): void {
|
||||
// Simulate hp-custom-tables presence via constant probe.
|
||||
if ( ! defined( 'HPCT_VERSION' ) ) {
|
||||
define( 'HPCT_VERSION', '1.0.0' );
|
||||
}
|
||||
WPDO_HivePress_Conflict_Guard::reset_for_tests();
|
||||
$conflicts = WPDO_HivePress_Conflict_Guard::detect_conflicts();
|
||||
$this->assertContains( 'hp-custom-tables', $conflicts );
|
||||
$this->assertTrue( WPDO_HivePress_Conflict_Guard::has_conflict() );
|
||||
}
|
||||
|
||||
public function test_check_and_warn_safe_when_no_conflicts(): void {
|
||||
WPDO_HivePress_Conflict_Guard::reset_for_tests();
|
||||
// Should not throw or render anything when clean.
|
||||
$this->expectOutputString( '' );
|
||||
WPDO_HivePress_Conflict_Guard::check_and_warn();
|
||||
}
|
||||
|
||||
public function test_render_notice_does_nothing_when_not_admin(): void {
|
||||
// is_admin() stub returns false by default.
|
||||
ob_start();
|
||||
WPDO_HivePress_Conflict_Guard::render_notice();
|
||||
$out = ob_get_clean();
|
||||
$this->assertSame( '', $out );
|
||||
}
|
||||
|
||||
public function test_memo_cache_avoids_repeat_probing(): void {
|
||||
WPDO_HivePress_Conflict_Guard::reset_for_tests();
|
||||
$first = WPDO_HivePress_Conflict_Guard::detect_conflicts();
|
||||
$second = WPDO_HivePress_Conflict_Guard::detect_conflicts();
|
||||
$this->assertSame( $first, $second );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Tests for WPDO_HivePress_Core_Adapter — Listing / Vendor field registration.
|
||||
*
|
||||
* @covers WPDO_HivePress_Core_Adapter
|
||||
*/
|
||||
class HivePressCoreAdapterTest extends TestCase {
|
||||
|
||||
private WPDO_HivePress_Core_Adapter $adapter;
|
||||
|
||||
protected function setUp(): void {
|
||||
// WPDO_Schema_Registry has no reset_for_tests() — use reflection to
|
||||
// clear its singleton so each test starts with a fresh registry.
|
||||
$reflect = new ReflectionClass( WPDO_Schema_Registry::class );
|
||||
if ( $reflect->hasProperty( 'instance' ) ) {
|
||||
$prop = $reflect->getProperty( 'instance' );
|
||||
$prop->setAccessible( true );
|
||||
$prop->setValue( null, null );
|
||||
}
|
||||
WPDO_Custom_Table_Registry::reset_for_tests();
|
||||
$this->adapter = new WPDO_HivePress_Core_Adapter();
|
||||
}
|
||||
|
||||
public function test_implements_adapter_interface(): void {
|
||||
$this->assertInstanceOf( WPDO_HivePress_Adapter::class, $this->adapter );
|
||||
}
|
||||
|
||||
public function test_plugin_slug_is_hivepress(): void {
|
||||
$this->assertSame( 'hivepress', $this->adapter->plugin_slug() );
|
||||
}
|
||||
|
||||
public function test_detection_class_targets_hivepress_core(): void {
|
||||
$this->assertSame( 'HivePress\\Core', $this->adapter->detection_class() );
|
||||
}
|
||||
|
||||
public function test_detection_const_targets_hivepress_version(): void {
|
||||
$this->assertSame( 'HIVEPRESS_VERSION', $this->adapter->detection_const() );
|
||||
}
|
||||
|
||||
public function test_minimum_addon_version_floor(): void {
|
||||
// 1.7.0 introduced the field-alias convention this adapter relies on.
|
||||
$this->assertSame( '1.7.0', $this->adapter->minimum_addon_version() );
|
||||
}
|
||||
|
||||
public function test_migrations_returns_listing_and_vendor_modules(): void {
|
||||
$mods = $this->adapter->migrations();
|
||||
$this->assertContains( 'hot_hp_listing', $mods );
|
||||
$this->assertContains( 'hot_hp_vendor', $mods );
|
||||
}
|
||||
|
||||
public function test_on_register_fields_populates_schema_registry(): void {
|
||||
$registry = WPDO_Schema_Registry::instance();
|
||||
$this->adapter->on_register_fields( $registry );
|
||||
|
||||
// Listing hot fields.
|
||||
$drafted = $registry->get_field( 'hp_listing', 'hp_drafted' );
|
||||
$this->assertIsArray( $drafted );
|
||||
$this->assertSame( 'hot', $drafted['zone'] );
|
||||
$this->assertTrue( (bool) $drafted['indexed'] );
|
||||
|
||||
$expired = $registry->get_field( 'hp_listing', 'hp_expired_time' );
|
||||
$this->assertSame( 'hot', $expired['zone'] );
|
||||
$this->assertStringContainsString( 'bigint', $expired['data_type'] );
|
||||
|
||||
$featured_time = $registry->get_field( 'hp_listing', 'hp_featured_time' );
|
||||
$this->assertSame( 'hot', $featured_time['zone'] );
|
||||
|
||||
// Vendor cold blob.
|
||||
$vendor_image = $registry->get_field( 'hp_vendor', 'hp_image' );
|
||||
$this->assertIsArray( $vendor_image );
|
||||
$this->assertSame( 'cold', $vendor_image['zone'] );
|
||||
}
|
||||
|
||||
public function test_suitability_score_aggregates_to_ten(): void {
|
||||
$score = $this->adapter->suitability_score();
|
||||
$this->assertArrayHasKey( 'aggregate', $score );
|
||||
$this->assertSame( 10.0, $score['aggregate'] );
|
||||
// All eight dimensions present.
|
||||
foreach ( array( 'd1_meta_calls', 'd2_meta_sql', 'd3_table_coverage', 'd4_registry_meta', 'd5_hook_bus', 'd6_options', 'd7_coupling', 'd8_perf' ) as $key ) {
|
||||
$this->assertArrayHasKey( $key, $score );
|
||||
$this->assertGreaterThanOrEqual( 0.0, $score[ $key ] );
|
||||
$this->assertLessThanOrEqual( 1.0, $score[ $key ] );
|
||||
}
|
||||
}
|
||||
|
||||
public function test_doctor_check_reports_table_status(): void {
|
||||
$result = $this->adapter->doctor_check();
|
||||
$this->assertIsArray( $result );
|
||||
$this->assertArrayHasKey( 'ok', $result );
|
||||
$this->assertArrayHasKey( 'message', $result );
|
||||
$this->assertArrayHasKey( 'details', $result );
|
||||
// The stub $wpdb returns null for SHOW TABLES, so tables appear missing → ok=false.
|
||||
$this->assertFalse( $result['ok'] );
|
||||
}
|
||||
|
||||
public function test_optimize_query_extracts_hot_clauses(): void {
|
||||
$registry = WPDO_Schema_Registry::instance();
|
||||
$this->adapter->on_register_fields( $registry );
|
||||
|
||||
$query = new WP_Query( array(
|
||||
'post_type' => 'hp_listing',
|
||||
'meta_query' => array(
|
||||
array( 'key' => 'hp_drafted', 'value' => 1, 'compare' => '=' ),
|
||||
array( 'key' => 'unrelated_key', 'value' => 'foo', 'compare' => '=' ),
|
||||
),
|
||||
) );
|
||||
|
||||
$this->adapter->optimize_query( $query );
|
||||
|
||||
// Hot clause should have been moved to wpdo_hot_clauses.
|
||||
$hot = $query->get( 'wpdo_hot_clauses' );
|
||||
// Note: in the test env the WPDO_Feature_Flags check may short-circuit
|
||||
// (no module active), so the assertion is "either the clause moved OR
|
||||
// it stayed put; never silently corrupted".
|
||||
$remaining = (array) $query->get( 'meta_query' );
|
||||
$this->assertIsArray( $remaining );
|
||||
// `unrelated_key` must always remain in meta_query regardless of routing.
|
||||
$found_unrelated = false;
|
||||
foreach ( $remaining as $clause ) {
|
||||
if ( is_array( $clause ) && ( $clause['key'] ?? '' ) === 'unrelated_key' ) {
|
||||
$found_unrelated = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
$this->assertTrue( $found_unrelated, 'Non-hot meta_query clause must always be preserved.' );
|
||||
// hot_clauses is either populated (when feature flag active) or empty (when not).
|
||||
$this->assertTrue( $hot === '' || is_array( $hot ) );
|
||||
}
|
||||
|
||||
public function test_optimize_query_handles_empty_meta_query(): void {
|
||||
$query = new WP_Query( array( 'post_type' => 'hp_listing' ) );
|
||||
$this->adapter->optimize_query( $query );
|
||||
// Must not crash; hot_clauses should remain unset.
|
||||
$this->assertEmpty( $query->get( 'wpdo_hot_clauses' ) );
|
||||
}
|
||||
|
||||
public function test_optimize_search_delegates_to_optimize_query(): void {
|
||||
$query = new WP_Query( array( 'post_type' => 'hp_listing' ) );
|
||||
// Delegation is implementation detail; verify just that it doesn't crash
|
||||
// and that an irrelevant attribute_fields argument is ignored.
|
||||
$this->adapter->optimize_search( $query, array() );
|
||||
$this->assertEmpty( $query->get( 'wpdo_hot_clauses' ) );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Tests for WPDO_HivePress_Cron_Optimizer.
|
||||
*
|
||||
* @covers WPDO_HivePress_Cron_Optimizer
|
||||
*/
|
||||
class HivePressCronOptimizerTest extends TestCase {
|
||||
|
||||
use WpdbMockTrait;
|
||||
|
||||
protected function setUp(): void {
|
||||
$this->install_wpdb_mock();
|
||||
WPDO_HivePress_Cron_Optimizer::reset_for_tests();
|
||||
$GLOBALS['_wp_options'][ WPDO_HivePress_Cron_Optimizer::OPTION_ENABLED ] = 0;
|
||||
}
|
||||
|
||||
public function test_disabled_by_default(): void {
|
||||
$this->assertFalse( WPDO_HivePress_Cron_Optimizer::is_enabled() );
|
||||
}
|
||||
|
||||
public function test_enabled_when_option_set(): void {
|
||||
$GLOBALS['_wp_options'][ WPDO_HivePress_Cron_Optimizer::OPTION_ENABLED ] = 1;
|
||||
$this->assertTrue( WPDO_HivePress_Cron_Optimizer::is_enabled() );
|
||||
}
|
||||
|
||||
public function test_module_constant(): void {
|
||||
$this->assertSame( 'hot_hp_listing', WPDO_HivePress_Cron_Optimizer::MODULE );
|
||||
}
|
||||
|
||||
public function test_register_is_idempotent(): void {
|
||||
$GLOBALS['_wp_options'][ WPDO_HivePress_Cron_Optimizer::OPTION_ENABLED ] = 1;
|
||||
WPDO_HivePress_Cron_Optimizer::register();
|
||||
WPDO_HivePress_Cron_Optimizer::register(); // No throw = pass.
|
||||
$this->assertTrue( true );
|
||||
}
|
||||
|
||||
public function test_maybe_run_returns_zero_when_module_inactive(): void {
|
||||
// Feature_Flags::is_query_active('hot_hp_listing') is false in test env.
|
||||
$count = WPDO_HivePress_Cron_Optimizer::maybe_run();
|
||||
$this->assertSame( 0, $count );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Tests for WPDO_HivePress_Detector — addon detection + caching.
|
||||
*
|
||||
* @covers WPDO_HivePress_Detector
|
||||
*/
|
||||
class HivePressDetectorTest extends TestCase {
|
||||
|
||||
protected function setUp(): void {
|
||||
WPDO_HivePress_Detector::reset_for_tests();
|
||||
// Clear transient cache (in-memory in test bootstrap).
|
||||
unset( $GLOBALS['_wp_options']['_transient_wpdo_hivepress_detector_cache'] );
|
||||
}
|
||||
|
||||
public function test_returns_empty_array_when_hivepress_core_absent(): void {
|
||||
// HivePress\Core is not declared in the test environment.
|
||||
$result = WPDO_HivePress_Detector::detect();
|
||||
$this->assertSame( array(), $result );
|
||||
}
|
||||
|
||||
public function test_short_circuit_caches_negative_result(): void {
|
||||
WPDO_HivePress_Detector::detect();
|
||||
// Second call should hit memo cache; verify identical.
|
||||
$this->assertSame( array(), WPDO_HivePress_Detector::detect() );
|
||||
}
|
||||
|
||||
public function test_catalog_returns_thirteen_known_addons(): void {
|
||||
$catalog = WPDO_HivePress_Detector::catalog();
|
||||
$this->assertCount( 13, $catalog );
|
||||
$this->assertArrayHasKey( 'hivepress', $catalog );
|
||||
$this->assertArrayHasKey( 'hivepress-bookings', $catalog );
|
||||
$this->assertArrayHasKey( 'hivepress-marketplace', $catalog );
|
||||
$this->assertArrayHasKey( 'hivepress-statistics', $catalog );
|
||||
}
|
||||
|
||||
public function test_is_known_recognises_supported_addons(): void {
|
||||
$this->assertTrue( WPDO_HivePress_Detector::is_known( 'hivepress' ) );
|
||||
$this->assertTrue( WPDO_HivePress_Detector::is_known( 'hivepress-reviews' ) );
|
||||
$this->assertFalse( WPDO_HivePress_Detector::is_known( 'hivepress-frobnicator' ) );
|
||||
$this->assertFalse( WPDO_HivePress_Detector::is_known( '' ) );
|
||||
}
|
||||
|
||||
public function test_bust_cache_clears_memo_and_transient(): void {
|
||||
WPDO_HivePress_Detector::detect();
|
||||
// Seed transient with bogus data to verify bust clears it.
|
||||
$GLOBALS['_wp_options']['_transient_wpdo_hivepress_detector_cache'] = array( 'fake' => '9.9.9' );
|
||||
WPDO_HivePress_Detector::bust_cache();
|
||||
$this->assertArrayNotHasKey( '_transient_wpdo_hivepress_detector_cache', $GLOBALS['_wp_options'] ?? array() );
|
||||
}
|
||||
|
||||
public function test_detects_addon_via_active_plugin_signal(): void {
|
||||
// HivePress addons publish no class/const — they register via the
|
||||
// `hivepress/v1/extensions` filter and are detected purely by being
|
||||
// active plugins. Seed active_plugins with core + one addon and assert
|
||||
// both are detected even though no HivePress classes are declared.
|
||||
$GLOBALS['_wp_options']['active_plugins'] = array(
|
||||
'hivepress/hivepress.php',
|
||||
'hivepress-messages/hivepress-messages.php',
|
||||
);
|
||||
WPDO_HivePress_Detector::reset_for_tests();
|
||||
unset( $GLOBALS['_wp_options']['_transient_wpdo_hivepress_detector_cache'] );
|
||||
|
||||
$result = WPDO_HivePress_Detector::detect();
|
||||
|
||||
$this->assertArrayHasKey( 'hivepress', $result, 'core detected via active plugin' );
|
||||
$this->assertArrayHasKey( 'hivepress-messages', $result, 'addon detected via active plugin' );
|
||||
$this->assertArrayNotHasKey( 'hivepress-reviews', $result, 'inactive addon not detected' );
|
||||
|
||||
// Cleanup so other tests observe no active plugins. bust_cache() clears
|
||||
// BOTH the memo and the seeded transient (reset_for_tests clears only
|
||||
// the memo) — otherwise the cached detection leaks into later suites.
|
||||
unset( $GLOBALS['_wp_options']['active_plugins'] );
|
||||
WPDO_HivePress_Detector::bust_cache();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Tests for WPDO_HivePress_Favorites_Adapter.
|
||||
*
|
||||
* @covers WPDO_HivePress_Favorites_Adapter
|
||||
*/
|
||||
class HivePressFavoritesAdapterTest extends TestCase {
|
||||
|
||||
use WpdbMockTrait;
|
||||
|
||||
private WPDO_HivePress_Favorites_Adapter $adapter;
|
||||
|
||||
protected function setUp(): void {
|
||||
$this->install_wpdb_mock();
|
||||
WPDO_Custom_Table_Registry::reset_for_tests();
|
||||
$this->adapter = new WPDO_HivePress_Favorites_Adapter();
|
||||
}
|
||||
|
||||
public function test_implements_adapter_interface(): void {
|
||||
$this->assertInstanceOf( WPDO_HivePress_Adapter::class, $this->adapter );
|
||||
}
|
||||
|
||||
public function test_identity(): void {
|
||||
$this->assertSame( 'hivepress-favorites', $this->adapter->plugin_slug() );
|
||||
$this->assertSame( 'HivePress\\Favorites\\Plugin', $this->adapter->detection_class() );
|
||||
$this->assertSame( 'HIVEPRESS_FAVORITES_VERSION', $this->adapter->detection_const() );
|
||||
}
|
||||
|
||||
public function test_constants_exposed(): void {
|
||||
$this->assertSame( 'hp_favorite', WPDO_HivePress_Favorites_Adapter::COMMENT_TYPE );
|
||||
$this->assertSame( 'wpdo_comment_hp_favorite', WPDO_HivePress_Favorites_Adapter::TABLE );
|
||||
}
|
||||
|
||||
public function test_on_register_custom_tables_declares_shadow_table(): void {
|
||||
$registry = WPDO_Custom_Table_Registry::instance();
|
||||
$this->adapter->on_register_custom_tables( $registry );
|
||||
|
||||
$tables = $registry->all();
|
||||
$this->assertCount( 1, $tables );
|
||||
|
||||
$cfg = reset( $tables );
|
||||
$this->assertSame( 'hivepress-favorites', $cfg['provider'] );
|
||||
$this->assertSame( 'wpdo_comment_hp_favorite', $cfg['table_name'] );
|
||||
$this->assertSame( 'comment_id', $cfg['primary_key'] );
|
||||
$this->assertArrayHasKey( 'unique_user_listing', $cfg['indexes'] );
|
||||
$this->assertStringContainsString( 'UNIQUE', $cfg['indexes']['unique_user_listing'] );
|
||||
}
|
||||
|
||||
public function test_mirror_insert_skips_non_favorite_comments(): void {
|
||||
// Stub $wpdb tracks queries.
|
||||
global $wpdb;
|
||||
$wpdb->queries = array();
|
||||
|
||||
$comment = new stdClass();
|
||||
$comment->comment_type = 'comment'; // ← not hp_favorite
|
||||
$comment->user_id = 1;
|
||||
$comment->comment_post_ID = 99;
|
||||
|
||||
$this->adapter->mirror_insert( 123, $comment );
|
||||
$this->assertSame( array(), $wpdb->queries );
|
||||
}
|
||||
|
||||
public function test_mirror_insert_writes_for_hp_favorite(): void {
|
||||
global $wpdb;
|
||||
$wpdb->queries = array();
|
||||
|
||||
$comment = new stdClass();
|
||||
$comment->comment_type = 'hp_favorite';
|
||||
$comment->user_id = 7;
|
||||
$comment->comment_post_ID = 42;
|
||||
$comment->comment_date = '2026-05-04 12:00:00';
|
||||
|
||||
$this->adapter->mirror_insert( 100, $comment );
|
||||
|
||||
$this->assertCount( 1, $wpdb->queries );
|
||||
$this->assertStringContainsString( 'INSERT IGNORE', $wpdb->queries[0] );
|
||||
$this->assertStringContainsString( 'wpdo_comment_hp_favorite', $wpdb->queries[0] );
|
||||
}
|
||||
|
||||
public function test_mirror_insert_skips_when_user_or_listing_missing(): void {
|
||||
global $wpdb;
|
||||
$wpdb->queries = array();
|
||||
|
||||
$comment = new stdClass();
|
||||
$comment->comment_type = 'hp_favorite';
|
||||
$comment->user_id = 0;
|
||||
$comment->comment_post_ID = 99;
|
||||
|
||||
$this->adapter->mirror_insert( 100, $comment );
|
||||
$this->assertSame( array(), $wpdb->queries );
|
||||
}
|
||||
|
||||
public function test_doctor_check_reports_missing_table_in_stub_env(): void {
|
||||
$result = $this->adapter->doctor_check();
|
||||
$this->assertFalse( $result['ok'] );
|
||||
$this->assertStringContainsString( 'wpdo_comment_hp_favorite', $result['message'] );
|
||||
}
|
||||
|
||||
public function test_score_aggregates_to_perfect_ten(): void {
|
||||
$score = $this->adapter->suitability_score();
|
||||
$this->assertSame( 10.0, $score['aggregate'] );
|
||||
}
|
||||
|
||||
public function test_migrations_lists_comment_module(): void {
|
||||
$this->assertSame( array( 'comment_hp_favorite' ), $this->adapter->migrations() );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Tests for WPDO_HivePress_Marketplace_Adapter (detect-only).
|
||||
*
|
||||
* @covers WPDO_HivePress_Marketplace_Adapter
|
||||
*/
|
||||
class HivePressMarketplaceAdapterTest extends TestCase {
|
||||
|
||||
use WpdbMockTrait;
|
||||
|
||||
private WPDO_HivePress_Marketplace_Adapter $adapter;
|
||||
|
||||
protected function setUp(): void {
|
||||
$this->install_wpdb_mock();
|
||||
$reflect = new ReflectionClass( WPDO_Schema_Registry::class );
|
||||
$prop = $reflect->getProperty( 'instance' );
|
||||
$prop->setAccessible( true );
|
||||
$prop->setValue( null, null );
|
||||
|
||||
$this->adapter = new WPDO_HivePress_Marketplace_Adapter();
|
||||
}
|
||||
|
||||
public function test_implements_adapter_interface(): void {
|
||||
$this->assertInstanceOf( WPDO_HivePress_Adapter::class, $this->adapter );
|
||||
}
|
||||
|
||||
public function test_identity(): void {
|
||||
$this->assertSame( 'hivepress-marketplace', $this->adapter->plugin_slug() );
|
||||
$this->assertSame( 'HivePress\\Marketplace\\Plugin', $this->adapter->detection_class() );
|
||||
}
|
||||
|
||||
public function test_on_register_fields_registers_hot_price_and_warm_counter(): void {
|
||||
$registry = WPDO_Schema_Registry::instance();
|
||||
$this->adapter->on_register_fields( $registry );
|
||||
|
||||
$price = $registry->get_field( 'hp_listing', 'hp_purchase_price' );
|
||||
$this->assertSame( 'hot', $price['zone'] );
|
||||
$this->assertTrue( (bool) $price['indexed'] );
|
||||
$this->assertStringContainsString( 'decimal', $price['data_type'] );
|
||||
|
||||
$count = $registry->get_field( 'hp_listing', 'hp_purchase_count' );
|
||||
$this->assertSame( 'warm', $count['zone'] );
|
||||
}
|
||||
|
||||
public function test_score_aggregates_to_nine_eight_eight(): void {
|
||||
// D8 = 0.9 → aggregate = (7×1.0 + 0.9)/8 × 10 = 9.875 → 9.88.
|
||||
$this->assertSame( 9.88, $this->adapter->suitability_score()['aggregate'] );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Tests for WPDO_HivePress_Memberships_Adapter.
|
||||
*
|
||||
* @covers WPDO_HivePress_Memberships_Adapter
|
||||
*/
|
||||
class HivePressMembershipsAdapterTest extends TestCase {
|
||||
|
||||
private WPDO_HivePress_Memberships_Adapter $adapter;
|
||||
|
||||
protected function setUp(): void {
|
||||
// Reset Schema_Registry singleton via reflection.
|
||||
$reflect = new ReflectionClass( WPDO_Schema_Registry::class );
|
||||
$prop = $reflect->getProperty( 'instance' );
|
||||
$prop->setAccessible( true );
|
||||
$prop->setValue( null, null );
|
||||
|
||||
$this->adapter = new WPDO_HivePress_Memberships_Adapter();
|
||||
}
|
||||
|
||||
public function test_implements_adapter_interface(): void {
|
||||
$this->assertInstanceOf( WPDO_HivePress_Adapter::class, $this->adapter );
|
||||
}
|
||||
|
||||
public function test_identity(): void {
|
||||
$this->assertSame( 'hivepress-memberships', $this->adapter->plugin_slug() );
|
||||
$this->assertSame( 'HivePress\\Memberships\\Plugin', $this->adapter->detection_class() );
|
||||
$this->assertSame( 'HIVEPRESS_MEMBERSHIPS_VERSION', $this->adapter->detection_const() );
|
||||
$this->assertSame( '2.0.0', $this->adapter->minimum_addon_version() );
|
||||
}
|
||||
|
||||
public function test_on_register_fields_registers_three_hot_fields(): void {
|
||||
$registry = WPDO_Schema_Registry::instance();
|
||||
$this->adapter->on_register_fields( $registry );
|
||||
|
||||
// Plan fields.
|
||||
$expire_period = $registry->get_field( 'hp_membership_plan', 'hp_expire_period' );
|
||||
$this->assertSame( 'hot', $expire_period['zone'] );
|
||||
$this->assertStringContainsString( 'int', $expire_period['data_type'] );
|
||||
|
||||
$primary = $registry->get_field( 'hp_membership_plan', 'hp_primary' );
|
||||
$this->assertSame( 'hot', $primary['zone'] );
|
||||
$this->assertTrue( (bool) $primary['indexed'] );
|
||||
|
||||
// Membership field — the hottest path.
|
||||
$expired = $registry->get_field( 'hp_membership', 'hp_expired_time' );
|
||||
$this->assertSame( 'hot', $expired['zone'] );
|
||||
$this->assertTrue( (bool) $expired['indexed'] );
|
||||
$this->assertStringContainsString( 'bigint', $expired['data_type'] );
|
||||
}
|
||||
|
||||
public function test_doctor_check_reports_missing_tables_in_stub_env(): void {
|
||||
$result = $this->adapter->doctor_check();
|
||||
$this->assertFalse( $result['ok'] );
|
||||
$this->assertArrayHasKey( 'hp_membership_plan', $result['details'] );
|
||||
$this->assertArrayHasKey( 'hp_membership', $result['details'] );
|
||||
}
|
||||
|
||||
public function test_score_aggregates_to_perfect_ten(): void {
|
||||
$score = $this->adapter->suitability_score();
|
||||
$this->assertSame( 10.0, $score['aggregate'] );
|
||||
}
|
||||
|
||||
public function test_migrations_lists_both_hot_modules(): void {
|
||||
$mods = $this->adapter->migrations();
|
||||
$this->assertContains( 'hot_hp_membership_plan', $mods );
|
||||
$this->assertContains( 'hot_hp_membership', $mods );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Tests for WPDO_HivePress_Messages_Adapter.
|
||||
*
|
||||
* @covers WPDO_HivePress_Messages_Adapter
|
||||
*/
|
||||
class HivePressMessagesAdapterTest extends TestCase {
|
||||
|
||||
use WpdbMockTrait;
|
||||
|
||||
private WPDO_HivePress_Messages_Adapter $adapter;
|
||||
|
||||
protected function setUp(): void {
|
||||
$this->install_wpdb_mock();
|
||||
WPDO_Custom_Table_Registry::reset_for_tests();
|
||||
$this->adapter = new WPDO_HivePress_Messages_Adapter();
|
||||
}
|
||||
|
||||
public function test_implements_adapter_interface(): void {
|
||||
$this->assertInstanceOf( WPDO_HivePress_Adapter::class, $this->adapter );
|
||||
}
|
||||
|
||||
public function test_identity(): void {
|
||||
$this->assertSame( 'hivepress-messages', $this->adapter->plugin_slug() );
|
||||
$this->assertSame( 'HivePress\\Messages\\Plugin', $this->adapter->detection_class() );
|
||||
$this->assertSame( 'hp_message', WPDO_HivePress_Messages_Adapter::COMMENT_TYPE );
|
||||
$this->assertSame( 'wpdo_comment_hp_message', WPDO_HivePress_Messages_Adapter::TABLE );
|
||||
}
|
||||
|
||||
public function test_on_register_custom_tables_declares_indexed_recipient(): void {
|
||||
$registry = WPDO_Custom_Table_Registry::instance();
|
||||
$this->adapter->on_register_custom_tables( $registry );
|
||||
|
||||
$tables = $registry->all();
|
||||
$cfg = reset( $tables );
|
||||
$this->assertSame( 'wpdo_comment_hp_message', $cfg['table_name'] );
|
||||
$this->assertArrayHasKey( 'recipient_id', $cfg['expected_columns'] );
|
||||
$this->assertArrayHasKey( 'idx_recipient_unread', $cfg['indexes'] );
|
||||
$this->assertStringContainsString( 'recipient_id, is_read', $cfg['indexes']['idx_recipient_unread'] );
|
||||
}
|
||||
|
||||
public function test_mirror_insert_skips_non_message_comments(): void {
|
||||
global $wpdb;
|
||||
$wpdb->queries = array();
|
||||
|
||||
$comment = new stdClass();
|
||||
$comment->comment_type = 'comment';
|
||||
$comment->user_id = 1;
|
||||
$comment->comment_karma = 5;
|
||||
|
||||
$this->adapter->mirror_insert( 1, $comment );
|
||||
$this->assertSame( array(), $wpdb->queries );
|
||||
}
|
||||
|
||||
public function test_mirror_insert_promotes_recipient_from_comment_karma(): void {
|
||||
global $wpdb;
|
||||
$wpdb->queries = array();
|
||||
|
||||
$comment = new stdClass();
|
||||
$comment->comment_type = 'hp_message';
|
||||
$comment->user_id = 7; // sender
|
||||
$comment->comment_karma = 42; // ← recipient hack lives here
|
||||
$comment->comment_post_ID = 99;
|
||||
$comment->comment_approved = 0;
|
||||
$comment->comment_date = '2026-05-04 12:00:00';
|
||||
|
||||
$this->adapter->mirror_insert( 100, $comment );
|
||||
|
||||
$this->assertCount( 1, $wpdb->queries );
|
||||
$sql = $wpdb->queries[0];
|
||||
$this->assertStringContainsString( 'INSERT INTO', $sql );
|
||||
$this->assertStringContainsString( 'wpdo_comment_hp_message', $sql );
|
||||
// The promoted recipient_id should appear in the bound SQL.
|
||||
$this->assertStringContainsString( '42', $sql );
|
||||
}
|
||||
|
||||
public function test_mirror_insert_skips_when_sender_or_recipient_zero(): void {
|
||||
global $wpdb;
|
||||
$wpdb->queries = array();
|
||||
|
||||
$comment = new stdClass();
|
||||
$comment->comment_type = 'hp_message';
|
||||
$comment->user_id = 0; // bad sender
|
||||
$comment->comment_karma = 42;
|
||||
$comment->comment_post_ID = 99;
|
||||
|
||||
$this->adapter->mirror_insert( 100, $comment );
|
||||
$this->assertSame( array(), $wpdb->queries );
|
||||
}
|
||||
|
||||
public function test_doctor_check_reports_missing_table_in_stub_env(): void {
|
||||
$result = $this->adapter->doctor_check();
|
||||
$this->assertFalse( $result['ok'] );
|
||||
$this->assertStringContainsString( 'wpdo_comment_hp_message', $result['message'] );
|
||||
}
|
||||
|
||||
public function test_score_aggregates_to_perfect_ten(): void {
|
||||
$this->assertSame( 10.0, $this->adapter->suitability_score()['aggregate'] );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Tests for WPDO_HivePress_Requests_Adapter.
|
||||
*
|
||||
* @covers WPDO_HivePress_Requests_Adapter
|
||||
*/
|
||||
class HivePressRequestsAdapterTest extends TestCase {
|
||||
|
||||
use WpdbMockTrait;
|
||||
|
||||
private WPDO_HivePress_Requests_Adapter $adapter;
|
||||
|
||||
protected function setUp(): void {
|
||||
$this->install_wpdb_mock();
|
||||
$reflect = new ReflectionClass( WPDO_Schema_Registry::class );
|
||||
$prop = $reflect->getProperty( 'instance' );
|
||||
$prop->setAccessible( true );
|
||||
$prop->setValue( null, null );
|
||||
WPDO_Custom_Table_Registry::reset_for_tests();
|
||||
|
||||
$this->adapter = new WPDO_HivePress_Requests_Adapter();
|
||||
}
|
||||
|
||||
public function test_implements_adapter_interface(): void {
|
||||
$this->assertInstanceOf( WPDO_HivePress_Adapter::class, $this->adapter );
|
||||
}
|
||||
|
||||
public function test_identity(): void {
|
||||
$this->assertSame( 'hivepress-requests', $this->adapter->plugin_slug() );
|
||||
$this->assertSame( 'HivePress\\Requests\\Plugin', $this->adapter->detection_class() );
|
||||
$this->assertSame( 'hp_offer', WPDO_HivePress_Requests_Adapter::OFFER_COMMENT_TYPE );
|
||||
}
|
||||
|
||||
public function test_on_register_fields_registers_request_hot_columns(): void {
|
||||
$registry = WPDO_Schema_Registry::instance();
|
||||
$this->adapter->on_register_fields( $registry );
|
||||
|
||||
$drafted = $registry->get_field( 'hp_request', 'hp_drafted' );
|
||||
$this->assertSame( 'hot', $drafted['zone'] );
|
||||
$this->assertTrue( (bool) $drafted['indexed'] );
|
||||
|
||||
$expired = $registry->get_field( 'hp_request', 'hp_expired_time' );
|
||||
$this->assertSame( 'hot', $expired['zone'] );
|
||||
$this->assertTrue( (bool) $expired['indexed'] );
|
||||
}
|
||||
|
||||
public function test_on_register_custom_tables_declares_offer_shadow(): void {
|
||||
$registry = WPDO_Custom_Table_Registry::instance();
|
||||
$this->adapter->on_register_custom_tables( $registry );
|
||||
|
||||
$tables = $registry->all();
|
||||
$cfg = reset( $tables );
|
||||
$this->assertSame( 'wpdo_comment_hp_offer', $cfg['table_name'] );
|
||||
$this->assertSame( 'hp_request', $cfg['post_type_link'] );
|
||||
$this->assertArrayHasKey( 'idx_request_approved', $cfg['indexes'] );
|
||||
}
|
||||
|
||||
public function test_mirror_offer_insert_skips_other_comment_types(): void {
|
||||
global $wpdb;
|
||||
$wpdb->queries = array();
|
||||
|
||||
$comment = new stdClass();
|
||||
$comment->comment_type = 'comment'; // ← not hp_offer
|
||||
$comment->comment_post_ID = 99;
|
||||
$comment->user_id = 1;
|
||||
|
||||
$this->adapter->mirror_offer_insert( 1, $comment );
|
||||
$this->assertSame( array(), $wpdb->queries );
|
||||
}
|
||||
|
||||
public function test_mirror_offer_insert_writes_for_hp_offer(): void {
|
||||
global $wpdb;
|
||||
$wpdb->queries = array();
|
||||
|
||||
$comment = new stdClass();
|
||||
$comment->comment_type = 'hp_offer';
|
||||
$comment->comment_post_ID = 42;
|
||||
$comment->user_id = 7;
|
||||
$comment->comment_approved = 0;
|
||||
$comment->comment_date = '2026-05-04 12:00:00';
|
||||
|
||||
$this->adapter->mirror_offer_insert( 100, $comment );
|
||||
$this->assertCount( 1, $wpdb->queries );
|
||||
$this->assertStringContainsString( 'INSERT INTO', $wpdb->queries[0] );
|
||||
$this->assertStringContainsString( 'wpdo_comment_hp_offer', $wpdb->queries[0] );
|
||||
}
|
||||
|
||||
public function test_score_aggregates_to_perfect_ten(): void {
|
||||
$this->assertSame( 10.0, $this->adapter->suitability_score()['aggregate'] );
|
||||
}
|
||||
|
||||
public function test_migrations_lists_request_and_offer_modules(): void {
|
||||
$mods = $this->adapter->migrations();
|
||||
$this->assertContains( 'hot_hp_request', $mods );
|
||||
$this->assertContains( 'comment_hp_offer', $mods );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Tests for WPDO_HivePress_REST.
|
||||
*
|
||||
* @covers WPDO_HivePress_REST
|
||||
*/
|
||||
class HivePressRestTest extends TestCase {
|
||||
|
||||
private WPDO_HivePress_REST $rest;
|
||||
|
||||
protected function setUp(): void {
|
||||
WPDO_HivePress_Bootstrap::reset_for_tests();
|
||||
WPDO_HivePress_Detector::reset_for_tests();
|
||||
// Default: caller is admin so permission check passes in unit env.
|
||||
$GLOBALS['_wp_current_user_can']['manage_options'] = true;
|
||||
$this->rest = new WPDO_HivePress_REST();
|
||||
}
|
||||
|
||||
public function test_namespace_constant(): void {
|
||||
$this->assertSame( 'wpdo/v1', WPDO_HivePress_REST::NAMESPACE );
|
||||
}
|
||||
|
||||
public function test_register_routes_does_not_throw(): void {
|
||||
// register_rest_route is stubbed in tests/bootstrap.php → returns true.
|
||||
$this->rest->register_routes();
|
||||
$this->assertTrue( true );
|
||||
}
|
||||
|
||||
public function test_check_permission_returns_true_for_admin(): void {
|
||||
$this->assertTrue( $this->rest->check_permission() );
|
||||
}
|
||||
|
||||
public function test_check_permission_returns_error_for_non_admin(): void {
|
||||
$GLOBALS['_wp_current_user_can']['manage_options'] = false;
|
||||
$result = $this->rest->check_permission();
|
||||
$this->assertInstanceOf( WP_Error::class, $result );
|
||||
}
|
||||
|
||||
public function test_get_status_returns_addon_catalog(): void {
|
||||
$response = $this->rest->get_status();
|
||||
$this->assertInstanceOf( WP_REST_Response::class, $response );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertArrayHasKey( 'addons', $data );
|
||||
$this->assertArrayHasKey( 'conflicts', $data );
|
||||
$this->assertArrayHasKey( 'totals', $data );
|
||||
// Catalog should list all 13 known addons regardless of detection state.
|
||||
$this->assertSame( 13, $data['totals']['catalog'] );
|
||||
// Without HivePress installed, detected = 0.
|
||||
$this->assertSame( 0, $data['totals']['detected'] );
|
||||
}
|
||||
|
||||
public function test_get_score_returns_report_structure(): void {
|
||||
$response = $this->rest->get_score();
|
||||
$this->assertInstanceOf( WP_REST_Response::class, $response );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertArrayHasKey( 'aggregate', $data );
|
||||
$this->assertArrayHasKey( 'adapter_count', $data );
|
||||
$this->assertArrayHasKey( 'per_dimension', $data );
|
||||
$this->assertArrayHasKey( 'adapters', $data );
|
||||
}
|
||||
|
||||
public function test_get_health_returns_ok_when_no_adapters_active(): void {
|
||||
$response = $this->rest->get_health();
|
||||
$this->assertInstanceOf( WP_REST_Response::class, $response );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertArrayHasKey( 'ok', $data );
|
||||
$this->assertArrayHasKey( 'probes', $data );
|
||||
// No active adapters → vacuously ok.
|
||||
$this->assertTrue( $data['ok'] );
|
||||
$this->assertSame( array(), $data['probes'] );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Tests for WPDO_HivePress_Reviews_Adapter.
|
||||
*
|
||||
* @covers WPDO_HivePress_Reviews_Adapter
|
||||
*/
|
||||
class HivePressReviewsAdapterTest extends TestCase {
|
||||
|
||||
private WPDO_HivePress_Reviews_Adapter $adapter;
|
||||
|
||||
protected function setUp(): void {
|
||||
$this->adapter = new WPDO_HivePress_Reviews_Adapter();
|
||||
}
|
||||
|
||||
public function test_implements_adapter_interface(): void {
|
||||
$this->assertInstanceOf( WPDO_HivePress_Adapter::class, $this->adapter );
|
||||
}
|
||||
|
||||
public function test_identity(): void {
|
||||
$this->assertSame( 'hivepress-reviews', $this->adapter->plugin_slug() );
|
||||
$this->assertSame( 'HivePress\\Reviews\\Plugin', $this->adapter->detection_class() );
|
||||
$this->assertSame( 'HIVEPRESS_REVIEWS_VERSION', $this->adapter->detection_const() );
|
||||
$this->assertSame( '1.4.0', $this->adapter->minimum_addon_version() );
|
||||
}
|
||||
|
||||
public function test_comment_type_constant_exposed(): void {
|
||||
$this->assertSame( 'hp_review', WPDO_HivePress_Reviews_Adapter::COMMENT_TYPE );
|
||||
}
|
||||
|
||||
public function test_doctor_check_reports_missing_table_in_stub_env(): void {
|
||||
$result = $this->adapter->doctor_check();
|
||||
$this->assertIsArray( $result );
|
||||
$this->assertArrayHasKey( 'ok', $result );
|
||||
// Stub $wpdb returns null for SHOW TABLES → ok=false.
|
||||
$this->assertFalse( $result['ok'] );
|
||||
$this->assertStringContainsString( 'wpdo_comment_hp_review', $result['message'] );
|
||||
}
|
||||
|
||||
public function test_score_aggregates_to_perfect_ten(): void {
|
||||
$score = $this->adapter->suitability_score();
|
||||
$this->assertSame( 10.0, $score['aggregate'] );
|
||||
}
|
||||
|
||||
public function test_no_fsm_modules_owned(): void {
|
||||
// Comment entity-bridge owns lifecycle; adapter is read-only.
|
||||
$this->assertSame( array(), $this->adapter->migrations() );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Tests for WPDO_HivePress_Seo_Adapter.
|
||||
*
|
||||
* @covers WPDO_HivePress_Seo_Adapter
|
||||
*/
|
||||
class HivePressSeoAdapterTest extends TestCase {
|
||||
|
||||
private WPDO_HivePress_Seo_Adapter $adapter;
|
||||
|
||||
protected function setUp(): void {
|
||||
$this->adapter = new WPDO_HivePress_Seo_Adapter();
|
||||
}
|
||||
|
||||
public function test_implements_adapter_interface(): void {
|
||||
$this->assertInstanceOf( WPDO_HivePress_Adapter::class, $this->adapter );
|
||||
}
|
||||
|
||||
public function test_identity(): void {
|
||||
$this->assertSame( 'hivepress-seo', $this->adapter->plugin_slug() );
|
||||
$this->assertSame( 'HivePress\\Seo\\Plugin', $this->adapter->detection_class() );
|
||||
}
|
||||
|
||||
public function test_doctor_check_is_always_ok(): void {
|
||||
$result = $this->adapter->doctor_check();
|
||||
$this->assertTrue( $result['ok'] );
|
||||
$this->assertStringContainsString( 'filter-based', $result['message'] );
|
||||
}
|
||||
|
||||
public function test_score_aggregates_to_perfect_ten(): void {
|
||||
$this->assertSame( 10.0, $this->adapter->suitability_score()['aggregate'] );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Tests for WPDO_HivePress_Social_Links_Adapter.
|
||||
*
|
||||
* @covers WPDO_HivePress_Social_Links_Adapter
|
||||
*/
|
||||
class HivePressSocialLinksAdapterTest extends TestCase {
|
||||
|
||||
private WPDO_HivePress_Social_Links_Adapter $adapter;
|
||||
|
||||
protected function setUp(): void {
|
||||
$reflect = new ReflectionClass( WPDO_Schema_Registry::class );
|
||||
$prop = $reflect->getProperty( 'instance' );
|
||||
$prop->setAccessible( true );
|
||||
$prop->setValue( null, null );
|
||||
|
||||
$this->adapter = new WPDO_HivePress_Social_Links_Adapter();
|
||||
}
|
||||
|
||||
public function test_implements_adapter_interface(): void {
|
||||
$this->assertInstanceOf( WPDO_HivePress_Adapter::class, $this->adapter );
|
||||
}
|
||||
|
||||
public function test_identity(): void {
|
||||
$this->assertSame( 'hivepress-social-links', $this->adapter->plugin_slug() );
|
||||
$this->assertSame( 'HivePress\\SocialLinks\\Plugin', $this->adapter->detection_class() );
|
||||
}
|
||||
|
||||
public function test_on_register_fields_registers_seven_cold_fields(): void {
|
||||
$registry = WPDO_Schema_Registry::instance();
|
||||
$this->adapter->on_register_fields( $registry );
|
||||
|
||||
// All seven social URL fields should land in cold zone on hp_vendor.
|
||||
$facebook = $registry->get_field( 'hp_vendor', 'hp_facebook_url' );
|
||||
$this->assertSame( 'cold', $facebook['zone'] );
|
||||
|
||||
$instagram = $registry->get_field( 'hp_vendor', 'hp_instagram_url' );
|
||||
$this->assertSame( 'cold', $instagram['zone'] );
|
||||
|
||||
$whatsapp = $registry->get_field( 'hp_vendor', 'hp_whatsapp_url' );
|
||||
$this->assertSame( 'cold', $whatsapp['zone'] );
|
||||
}
|
||||
|
||||
public function test_doctor_check_reports_field_count(): void {
|
||||
$result = $this->adapter->doctor_check();
|
||||
$this->assertTrue( $result['ok'] );
|
||||
$this->assertStringContainsString( '7 cold fields', $result['message'] );
|
||||
}
|
||||
|
||||
public function test_score_aggregates_to_perfect_ten(): void {
|
||||
$this->assertSame( 10.0, $this->adapter->suitability_score()['aggregate'] );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Tests for WPDO_HivePress_Statistics_Adapter (detect-only).
|
||||
*
|
||||
* @covers WPDO_HivePress_Statistics_Adapter
|
||||
*/
|
||||
class HivePressStatisticsAdapterTest extends TestCase {
|
||||
|
||||
use WpdbMockTrait;
|
||||
|
||||
private WPDO_HivePress_Statistics_Adapter $adapter;
|
||||
|
||||
protected function setUp(): void {
|
||||
$this->install_wpdb_mock();
|
||||
$reflect = new ReflectionClass( WPDO_Schema_Registry::class );
|
||||
$prop = $reflect->getProperty( 'instance' );
|
||||
$prop->setAccessible( true );
|
||||
$prop->setValue( null, null );
|
||||
|
||||
$this->adapter = new WPDO_HivePress_Statistics_Adapter();
|
||||
}
|
||||
|
||||
public function test_implements_adapter_interface(): void {
|
||||
$this->assertInstanceOf( WPDO_HivePress_Adapter::class, $this->adapter );
|
||||
}
|
||||
|
||||
public function test_identity(): void {
|
||||
$this->assertSame( 'hivepress-statistics', $this->adapter->plugin_slug() );
|
||||
$this->assertSame( 'HivePress\\Statistics\\Plugin', $this->adapter->detection_class() );
|
||||
}
|
||||
|
||||
public function test_on_register_fields_registers_warm_counters(): void {
|
||||
$registry = WPDO_Schema_Registry::instance();
|
||||
$this->adapter->on_register_fields( $registry );
|
||||
|
||||
$views = $registry->get_field( 'hp_listing', 'hp_view_count' );
|
||||
$this->assertSame( 'warm', $views['zone'] );
|
||||
|
||||
$clicks = $registry->get_field( 'hp_listing', 'hp_click_count' );
|
||||
$this->assertSame( 'warm', $clicks['zone'] );
|
||||
}
|
||||
|
||||
public function test_score_aggregates_to_nine_eight_eight(): void {
|
||||
$this->assertSame( 9.88, $this->adapter->suitability_score()['aggregate'] );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Tests for WPDO_HivePress_Suitability_Scorer.
|
||||
*
|
||||
* @covers WPDO_HivePress_Suitability_Scorer
|
||||
*/
|
||||
class HivePressSuitabilityScorerTest extends TestCase {
|
||||
|
||||
public function test_dimensions_constant_lists_eight_keys(): void {
|
||||
$this->assertCount( 8, WPDO_HivePress_Suitability_Scorer::DIMENSIONS );
|
||||
$this->assertContains( 'd1_meta_calls', WPDO_HivePress_Suitability_Scorer::DIMENSIONS );
|
||||
$this->assertContains( 'd8_perf', WPDO_HivePress_Suitability_Scorer::DIMENSIONS );
|
||||
}
|
||||
|
||||
public function test_compute_aggregate_with_empty_input_returns_zero(): void {
|
||||
$this->assertSame( 0.0, WPDO_HivePress_Suitability_Scorer::compute_aggregate( array() ) );
|
||||
}
|
||||
|
||||
public function test_compute_aggregate_averages_adapter_scores(): void {
|
||||
$scores = array(
|
||||
'a' => array( 'aggregate' => 10.0 ),
|
||||
'b' => array( 'aggregate' => 9.0 ),
|
||||
'c' => array( 'aggregate' => 8.0 ),
|
||||
);
|
||||
// (10 + 9 + 8) / 3 = 9.0
|
||||
$this->assertSame( 9.0, WPDO_HivePress_Suitability_Scorer::compute_aggregate( $scores ) );
|
||||
}
|
||||
|
||||
public function test_compute_per_dimension_averages_each_dimension(): void {
|
||||
$scores = array(
|
||||
'a' => array(
|
||||
'd1_meta_calls' => 1.0, 'd2_meta_sql' => 1.0, 'd3_table_coverage' => 1.0,
|
||||
'd4_registry_meta' => 1.0, 'd5_hook_bus' => 1.0, 'd6_options' => 1.0,
|
||||
'd7_coupling' => 1.0, 'd8_perf' => 0.9,
|
||||
),
|
||||
'b' => array(
|
||||
'd1_meta_calls' => 1.0, 'd2_meta_sql' => 1.0, 'd3_table_coverage' => 1.0,
|
||||
'd4_registry_meta' => 1.0, 'd5_hook_bus' => 1.0, 'd6_options' => 1.0,
|
||||
'd7_coupling' => 1.0, 'd8_perf' => 1.0,
|
||||
),
|
||||
);
|
||||
$dim = WPDO_HivePress_Suitability_Scorer::compute_per_dimension( $scores );
|
||||
|
||||
// d1-d7 are 1.0 across both adapters → average 1.0.
|
||||
$this->assertSame( 1.0, $dim['d1_meta_calls'] );
|
||||
$this->assertSame( 1.0, $dim['d7_coupling'] );
|
||||
|
||||
// d8: (0.9 + 1.0) / 2 = 0.95.
|
||||
$this->assertSame( 0.95, $dim['d8_perf'] );
|
||||
}
|
||||
|
||||
public function test_compute_per_dimension_with_empty_input_returns_all_zero(): void {
|
||||
$dim = WPDO_HivePress_Suitability_Scorer::compute_per_dimension( array() );
|
||||
foreach ( WPDO_HivePress_Suitability_Scorer::DIMENSIONS as $d ) {
|
||||
$this->assertSame( 0.0, $dim[ $d ] );
|
||||
}
|
||||
}
|
||||
|
||||
public function test_report_with_no_active_adapters(): void {
|
||||
// Bootstrap not booted in unit env → adapters() = empty.
|
||||
WPDO_HivePress_Bootstrap::reset_for_tests();
|
||||
$report = WPDO_HivePress_Suitability_Scorer::report();
|
||||
|
||||
$this->assertSame( 0, $report['adapter_count'] );
|
||||
$this->assertSame( 0.0, $report['aggregate'] );
|
||||
$this->assertSame( array(), $report['adapters'] );
|
||||
}
|
||||
|
||||
public function test_adapters_below_threshold_returns_empty_for_unknown_dimension(): void {
|
||||
$this->assertSame( array(), WPDO_HivePress_Suitability_Scorer::adapters_below_threshold( 'd99_garbage' ) );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Tests for WPDO_HivePress_Tags_Adapter.
|
||||
*
|
||||
* @covers WPDO_HivePress_Tags_Adapter
|
||||
*/
|
||||
class HivePressTagsAdapterTest extends TestCase {
|
||||
|
||||
private WPDO_HivePress_Tags_Adapter $adapter;
|
||||
|
||||
protected function setUp(): void {
|
||||
$this->adapter = new WPDO_HivePress_Tags_Adapter();
|
||||
}
|
||||
|
||||
public function test_implements_adapter_interface(): void {
|
||||
$this->assertInstanceOf( WPDO_HivePress_Adapter::class, $this->adapter );
|
||||
}
|
||||
|
||||
public function test_identity(): void {
|
||||
$this->assertSame( 'hivepress-tags', $this->adapter->plugin_slug() );
|
||||
$this->assertSame( 'HivePress\\Tags\\Plugin', $this->adapter->detection_class() );
|
||||
$this->assertSame( 'HIVEPRESS_TAGS_VERSION', $this->adapter->detection_const() );
|
||||
$this->assertSame( '1.1.0', $this->adapter->minimum_addon_version() );
|
||||
}
|
||||
|
||||
public function test_doctor_check_is_always_ok(): void {
|
||||
$result = $this->adapter->doctor_check();
|
||||
$this->assertTrue( $result['ok'] );
|
||||
$this->assertStringContainsString( 'term-based', $result['message'] );
|
||||
}
|
||||
|
||||
public function test_score_aggregates_to_perfect_ten(): void {
|
||||
$this->assertSame( 10.0, $this->adapter->suitability_score()['aggregate'] );
|
||||
}
|
||||
|
||||
public function test_no_fsm_modules_owned(): void {
|
||||
$this->assertSame( array(), $this->adapter->migrations() );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Shared $wpdb mock helper for HivePress adapter tests.
|
||||
*
|
||||
* Several existing wpdo tests (ZoneHotTest, SyncBridgeTest, etc.) replace the
|
||||
* global $wpdb with their own anonymous-class mock and never restore it. When
|
||||
* our adapter tests run after those, $wpdb lacks the query/get_col/delete
|
||||
* methods our mirror_insert / cron-optimizer code paths call, causing
|
||||
* cross-test pollution failures.
|
||||
*
|
||||
* Trait reinstalls a known $wpdb mock + clears the wpdo_features option so
|
||||
* Feature_Flags reports idle — the safe default state.
|
||||
*/
|
||||
trait WpdbMockTrait {
|
||||
|
||||
/**
|
||||
* Install a fresh $wpdb mock with the methods adapter code paths call.
|
||||
*
|
||||
* Idempotent — call from setUp() in test classes that touch $wpdb.
|
||||
*/
|
||||
protected function install_wpdb_mock(): void {
|
||||
global $wpdb;
|
||||
$wpdb = new class {
|
||||
public string $prefix = 'wp_';
|
||||
public string $comments = 'wp_comments';
|
||||
public string $postmeta = 'wp_postmeta';
|
||||
public string $posts = 'wp_posts';
|
||||
public string $options = 'wp_options';
|
||||
|
||||
/** @var array<int,string> */
|
||||
public array $queries = array();
|
||||
|
||||
public function prepare( string $sql, ...$args ): string {
|
||||
$i = 0;
|
||||
return (string) preg_replace_callback(
|
||||
'/%[sd]/',
|
||||
static function () use ( &$i, $args ) {
|
||||
return $args[ $i++ ] ?? '?';
|
||||
},
|
||||
$sql
|
||||
);
|
||||
}
|
||||
|
||||
public function query( string $sql ): int|bool {
|
||||
$this->queries[] = $sql;
|
||||
return 1;
|
||||
}
|
||||
|
||||
public function get_var( string $sql ): ?string {
|
||||
return null;
|
||||
}
|
||||
|
||||
public function get_col( string $sql ): array {
|
||||
return array();
|
||||
}
|
||||
|
||||
public function get_results( string $sql, $output = OBJECT ): array {
|
||||
return array();
|
||||
}
|
||||
|
||||
public function insert( string $table, array $data, $format = null ): int|false {
|
||||
return 1;
|
||||
}
|
||||
|
||||
public function update( string $table, array $data, array $where, $format = null, $where_format = null ): int|false {
|
||||
return 1;
|
||||
}
|
||||
|
||||
public function delete( string $table, array $where, $format = null ): int|false {
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
|
||||
// Reset Feature_Flags state so is_query_active() returns false in tests.
|
||||
$GLOBALS['_wp_options']['wpdo_features'] = array();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user