|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Pure-PHP smoke test for datamachine_resolve_system_agent_context(). |
| 4 | + * |
| 5 | + * Run with: php tests/system-agent-context-resolution-smoke.php |
| 6 | + * |
| 7 | + * Regression coverage for the "queued task requires agent context" flood |
| 8 | + * produced when media/SEO/linking abilities enqueued agent-owned batch tasks |
| 9 | + * (alt_text_generation, image_optimization, meta_description_generation, |
| 10 | + * internal_linking) from a context with no authenticated user — WP-Cron, |
| 11 | + * WP-CLI without --user, or a system pipeline step. |
| 12 | + * |
| 13 | + * The old resolution resolved identity solely from get_current_user_id(), |
| 14 | + * which returns 0 outside a web request. That zeroed the enqueue context, so |
| 15 | + * every batched item hit TaskScheduler's agent-context gate and was rejected |
| 16 | + * (one ERROR log line per item). The fix teaches the enqueue path to fall back |
| 17 | + * to the install's default agent owner via |
| 18 | + * DirectoryManager::get_default_agent_user_id(). |
| 19 | + * |
| 20 | + * This test verifies the resolver's decision table without a WP bootstrap by |
| 21 | + * mirroring its production logic byte-for-byte in a harness whose two inputs |
| 22 | + * (current-user id, default-agent user id → agent id) are injectable. |
| 23 | + * |
| 24 | + * @package DataMachine\Tests |
| 25 | + */ |
| 26 | + |
| 27 | +if ( ! defined( 'ABSPATH' ) ) { |
| 28 | + define( 'ABSPATH', __DIR__ . '/' ); |
| 29 | +} |
| 30 | + |
| 31 | +// ─── Harness mirroring datamachine_resolve_system_agent_context() ──── |
| 32 | +// |
| 33 | +// Mirrors data-machine.php:datamachine_resolve_system_agent_context(). |
| 34 | +// The two collaborators the real function calls — get_current_user_id() and |
| 35 | +// DirectoryManager::get_default_agent_user_id() — are supplied as inputs, and |
| 36 | +// the user_id → agent_id resolution (datamachine_resolve_or_create_agent_id) |
| 37 | +// is supplied as a closure so the branch logic is exercised in isolation. |
| 38 | +// |
| 39 | +// @param int $current_user_id What get_current_user_id() would return. |
| 40 | +// @param int $default_user_id What DirectoryManager::get_default_agent_user_id() would return. |
| 41 | +// @param callable $resolve_agent_id user_id => agent_id (0 for unresolvable users). |
| 42 | +function resolve_system_agent_context_for_test( int $current_user_id, int $default_user_id, callable $resolve_agent_id ): array { |
| 43 | + $user_id = $current_user_id; |
| 44 | + |
| 45 | + // No authenticated user: fall back to the install's default agent owner. |
| 46 | + if ( $user_id <= 0 ) { |
| 47 | + $user_id = $default_user_id; |
| 48 | + } |
| 49 | + |
| 50 | + $agent_id = $user_id > 0 ? (int) $resolve_agent_id( $user_id ) : 0; |
| 51 | + |
| 52 | + return array( |
| 53 | + 'user_id' => $user_id, |
| 54 | + 'agent_id' => $agent_id, |
| 55 | + ); |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Mirror of TaskScheduler's agent-context gate for a task that |
| 60 | + * requiresAgentContext() === true: reject when neither agent_id nor |
| 61 | + * agent_slug is present in the enqueue context. |
| 62 | + */ |
| 63 | +function task_scheduler_gate_for_test( array $context ): bool { |
| 64 | + return ! empty( $context['agent_slug'] ) || ! empty( $context['agent_id'] ); |
| 65 | +} |
| 66 | + |
| 67 | +// ─── Tiny assertion helpers ───────────────────────────────────────── |
| 68 | + |
| 69 | +$failures = 0; |
| 70 | +$total = 0; |
| 71 | + |
| 72 | +$assert = function ( string $label, bool $cond ) use ( &$failures, &$total ): void { |
| 73 | + $total++; |
| 74 | + if ( $cond ) { |
| 75 | + echo " [PASS] {$label}\n"; |
| 76 | + } else { |
| 77 | + $failures++; |
| 78 | + echo " [FAIL] {$label}\n"; |
| 79 | + } |
| 80 | +}; |
| 81 | + |
| 82 | +// Single-agent install: default owner is user 1 → agent 1. |
| 83 | +$resolve = static function ( int $user_id ): int { |
| 84 | + $map = array( 1 => 1, 7 => 25 ); // owner_id => agent_id |
| 85 | + return $map[ $user_id ] ?? 0; |
| 86 | +}; |
| 87 | + |
| 88 | +// ─── Test cases ───────────────────────────────────────────────────── |
| 89 | + |
| 90 | +echo "\n[1] Authenticated web request resolves the current user's agent\n"; |
| 91 | +$ctx = resolve_system_agent_context_for_test( 7, 1, $resolve ); |
| 92 | +$assert( 'user_id is the current user', 7 === $ctx['user_id'] ); |
| 93 | +$assert( 'agent_id resolved for current user', 25 === $ctx['agent_id'] ); |
| 94 | +$assert( 'enqueue context passes the agent-context gate', task_scheduler_gate_for_test( $ctx ) ); |
| 95 | + |
| 96 | +echo "\n[2] Cron/CLI/system context (no current user) falls back to default agent owner\n"; |
| 97 | +$ctx = resolve_system_agent_context_for_test( 0, 1, $resolve ); |
| 98 | +$assert( 'user_id falls back to default agent user', 1 === $ctx['user_id'] ); |
| 99 | +$assert( 'agent_id resolved from default owner', 1 === $ctx['agent_id'] ); |
| 100 | +$assert( |
| 101 | + 'REGRESSION: fallback context is NOT gate-rejected (was agent_id 0 before fix)', |
| 102 | + task_scheduler_gate_for_test( $ctx ) |
| 103 | +); |
| 104 | + |
| 105 | +echo "\n[3] Old behaviour would have produced a zeroed, gate-rejected context\n"; |
| 106 | +// Before the fix, a no-user context resolved to user_id 0 / agent_id 0. |
| 107 | +$old_ctx = array( 'user_id' => 0, 'agent_id' => 0 ); |
| 108 | +$assert( |
| 109 | + 'old zeroed context is exactly what the gate rejected', |
| 110 | + false === task_scheduler_gate_for_test( $old_ctx ) |
| 111 | +); |
| 112 | + |
| 113 | +echo "\n[4] Install with no resolvable owner at all still returns zeros (no fatal)\n"; |
| 114 | +$ctx = resolve_system_agent_context_for_test( 0, 0, $resolve ); |
| 115 | +$assert( 'user_id is 0 when no default owner exists', 0 === $ctx['user_id'] ); |
| 116 | +$assert( 'agent_id is 0 when no default owner exists', 0 === $ctx['agent_id'] ); |
| 117 | + |
| 118 | +echo "\n[5] Production sources use the shared resolver, not raw get_current_user_id()\n"; |
| 119 | +$root = dirname( __DIR__ ); |
| 120 | +$sources = array( |
| 121 | + 'AltTextAbilities' => $root . '/inc/Abilities/Media/AltTextAbilities.php', |
| 122 | + 'ImageOptimizationAbilities' => $root . '/inc/Abilities/Media/ImageOptimizationAbilities.php', |
| 123 | + 'ImageGenerationAbilities' => $root . '/inc/Abilities/Media/ImageGenerationAbilities.php', |
| 124 | + 'MetaDescriptionAbilities' => $root . '/inc/Abilities/SEO/MetaDescriptionAbilities.php', |
| 125 | + 'InternalLinkingAbilities' => $root . '/inc/Abilities/InternalLinkingAbilities.php', |
| 126 | +); |
| 127 | +foreach ( $sources as $name => $path ) { |
| 128 | + $src = (string) file_get_contents( $path ); |
| 129 | + $assert( "{$name} calls datamachine_resolve_system_agent_context()", str_contains( $src, 'datamachine_resolve_system_agent_context()' ) ); |
| 130 | + $assert( |
| 131 | + "{$name} no longer resolves system-task agent id from a user-gated ternary", |
| 132 | + ! str_contains( $src, 'datamachine_resolve_or_create_agent_id( $user_id ) : 0' ) |
| 133 | + ); |
| 134 | +} |
| 135 | + |
| 136 | +echo "\n[6] The resolver is defined and documents the cron/CLI fallback\n"; |
| 137 | +$plugin_src = (string) file_get_contents( $root . '/data-machine.php' ); |
| 138 | +$assert( 'datamachine_resolve_system_agent_context() defined', str_contains( $plugin_src, 'function datamachine_resolve_system_agent_context(): array' ) ); |
| 139 | +$assert( 'resolver falls back to the default agent user', str_contains( $plugin_src, 'get_default_agent_user_id()' ) ); |
| 140 | + |
| 141 | +echo "\n"; |
| 142 | +if ( $failures > 0 ) { |
| 143 | + echo "=== system-agent-context-resolution-smoke: {$failures}/{$total} FAILED ===\n"; |
| 144 | + exit( 1 ); |
| 145 | +} |
| 146 | +echo "=== system-agent-context-resolution-smoke: ALL PASS ({$total} assertions) ===\n"; |
0 commit comments