Files
2meet-data-optimizer/tests/unit/HookBusBridgeTest.php
T
wpdev fc0f018a94 fix(2meet-data-optimizer): stop auto-activating entity bridge on install
- install() no longer switches the Hook Bus on or puts user/term/comment
  into dual_write; the now-unused maybe_autoactivate_entity_bridge() is gone
- Hook Bus and Mode_Manager defaults are off/disabled; sites with stored
  modes (incl. aeav_only) keep them since only unset keys use defaults
- Guard run_dbdelta()'s upgrade.php require like install_v2_tables() so
  install() can run under the integration test stub

Verify: InstallerV2Test::test_install_does_not_enable_entity_bridge,
  ModeManagerDefaultsTest; unit 602 OK, integration 421 OK
PR: PR-007
2026-09-25 21:48:23 +08:00

113 lines
3.9 KiB
PHP

<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
/**
* Unit tests for WPDO_Hook_Bus_Bridge — PR-3 feature-flag + conflict detection.
*
* @covers WPDO_Hook_Bus_Bridge
*/
class HookBusBridgeTest extends TestCase {
protected function setUp(): void {
WPDO_Hook_Bus_Bridge::reset_cache();
// Reset $GLOBALS['_wp_options'] for each test isolation.
$GLOBALS['_wp_options'] = array();
}
// ── is_enabled() ────────────────────────────────────────────────────────
public function test_is_enabled_default_false(): void {
// Hook Bus is opt-in; option not set → false. Existing sites store '1' explicitly.
$this->assertFalse( WPDO_Hook_Bus_Bridge::is_enabled() );
}
public function test_is_enabled_when_option_set_to_string_one(): void {
update_option( WPDO_Hook_Bus_Bridge::OPTION, '1' );
WPDO_Hook_Bus_Bridge::reset_cache();
$this->assertTrue( WPDO_Hook_Bus_Bridge::is_enabled() );
}
public function test_is_enabled_when_option_set_to_bool_true(): void {
update_option( WPDO_Hook_Bus_Bridge::OPTION, true );
WPDO_Hook_Bus_Bridge::reset_cache();
$this->assertTrue( WPDO_Hook_Bus_Bridge::is_enabled() );
}
public function test_is_enabled_when_option_set_to_zero(): void {
update_option( WPDO_Hook_Bus_Bridge::OPTION, '0' );
WPDO_Hook_Bus_Bridge::reset_cache();
$this->assertFalse( WPDO_Hook_Bus_Bridge::is_enabled() );
}
public function test_is_enabled_caches_result(): void {
update_option( WPDO_Hook_Bus_Bridge::OPTION, '1' );
WPDO_Hook_Bus_Bridge::reset_cache();
$first = WPDO_Hook_Bus_Bridge::is_enabled();
// Mutate the option, but cache should retain previous value until reset.
update_option( WPDO_Hook_Bus_Bridge::OPTION, '0' );
$second = WPDO_Hook_Bus_Bridge::is_enabled();
$this->assertSame( $first, $second, 'Cache must be sticky within a request' );
// After explicit reset → new value visible.
WPDO_Hook_Bus_Bridge::reset_cache();
$this->assertFalse( WPDO_Hook_Bus_Bridge::is_enabled() );
}
// ── maybe_init_hook_bus() ───────────────────────────────────────────────
public function test_maybe_init_hook_bus_noop_when_disabled(): void {
// Disabled → must not throw even if WPDO_Hook_Bus class missing.
WPDO_Hook_Bus_Bridge::maybe_init_hook_bus();
$this->assertTrue( true );
}
// ── detect_intra_wpdo_conflicts() ───────────────────────────────────────
public function test_detect_intra_wpdo_conflicts_returns_empty_when_no_filters(): void {
// Stub bootstrap doesn't populate $wp_filter, so result is empty array.
$conflicts = WPDO_Hook_Bus_Bridge::detect_intra_wpdo_conflicts();
$this->assertIsArray( $conflicts );
$this->assertEmpty( $conflicts );
}
public function test_detect_intra_wpdo_conflicts_flags_multiple_wpdo_callbacks(): void {
// Simulate $wp_filter with two WPDO_* callbacks on the same hook.
$GLOBALS['wp_filter'] = array(
'update_post_metadata' => new class() {
public array $callbacks;
public function __construct() {
$this->callbacks = array(
10 => array(
array(
'function' => array(
new class() {
public function intercept_update() {}
},
'intercept_update',
),
),
),
8 => array(
array(
'function' => array(
new class() {
public function intercept_update() {}
},
'intercept_update',
),
),
),
);
}
},
);
// The anonymous classes won't have WPDO_ prefix → must not flag conflict.
$conflicts = WPDO_Hook_Bus_Bridge::detect_intra_wpdo_conflicts();
$this->assertEmpty( $conflicts, 'Non-WPDO classes must not be flagged' );
}
}