'1', 'atk_options' => [ 'activity_types' => [ 'user' => 1, 'woocommerce' => 1 ], 'atk_logs_days' => 30 ] ]; $hooks = []; function add_action( $name, $callback, $priority = 10, $args = 1 ) { $GLOBALS['hooks'][$name] = $callback; } function get_option( $name, $default = false ) { return $GLOBALS['options'][$name] ?? $default; } function apply_filters( $name, $value ) { return $value; } function wp_generate_uuid4() { static $id = 0; return 'request-' . ++$id; } function absint( $value ) { return abs( (int) $value ); } function get_current_user_id() { return 7; } function get_current_blog_id() { return 1; } function wp_doing_ajax() { return false; } function is_admin() { return false; } function get_users( $args ) { $GLOBALS['lookup'] = $args; return $GLOBALS['matched_users'] ?? []; } function wc_get_order( $id ) { return new WC_Order(); } class WC_Order { public function get_customer_id() { return 42; } public function get_id() { return 123; } public function get_transaction_id() { return 'ch_Test'; } } class TestDB { public $prefix = 'wp_'; public $events = []; public $throw = false; public $suppressed = false; public $last_query; public function suppress_errors( $value ) { $old = $this->suppressed; $this->suppressed = $value; return $old; } public function prepare( $sql, ...$args ) { return [ $sql, count($args) === 1 && is_array($args[0]) ? $args[0] : $args ]; } public function query( $query ) { if ($this->throw) throw new Exception('simulated storage outage'); $this->last_query = $query; if (strpos($query[0], 'INSERT') !== 0) return 1; preg_match('/\(`(.+)`\) VALUES/', $query[0], $match); $data = array_combine(explode('`,`', $match[1]), array_slice($query[1], 1)); $this->events[$data['event_key']] = $data; return 1; } } $wpdb = new TestDB(); require __DIR__ . '/../includes/class-options.php'; require __DIR__ . '/../includes/class-atk-security-events.php'; function check($condition, $message) { if (!$condition) throw new Exception($message); } function last_event() { return end($GLOBALS['wpdb']->events); } $collector = new ATKAI_Security_Events(); check(isset($hooks['woocommerce_store_api_checkout_order_processed'], $hooks['woocommerce_checkout_order_processed']), 'Classic and block checkout hooks registered'); $collector->registered(42); check(last_event()['subject_id'] === 42 && last_event()['actor_id'] === 7, 'Subject and actor preserved separately'); $collector->login('ignored', (object)['ID'=>42]); $collector->profile(42); $collector->role(42, 'customer', []); check(count($wpdb->events) === 3, 'Initial role assignment is not reported as privilege change'); $collector->role(42, 'administrator', ['customer']); check(last_event()['event_type'] === 'role_changed', 'Later role changes recorded'); $collector->classic_checkout(123, ['card'=>'SECRET'], new WC_Order()); $collector->checkout(new WC_Order()); check(count($wpdb->events) === 5, 'Same request checkout hooks deduplicated'); $collector->payment_error(new Exception('SECRET payment payload'), new WC_Order()); check(last_event()['outcome'] === 'error' && last_event()['reason'] === '', 'Gateway errors do not masquerade as declines'); $notification = (object)[ 'id'=>'evt_One', 'type'=>'payment_intent.payment_failed', 'created'=>time(), 'livemode'=>true, 'data'=>(object)['object'=>(object)['id'=>'pi_One', 'customer'=>'cus_One', 'last_payment_error'=>(object)['decline_code'=>'insufficient_funds', 'message'=>'SECRET'], 'payment_method'=>(object)['card'=>'SECRET']]] ]; $collector->stripe_webhook($notification->type, $notification, new WC_Order()); $before = count($wpdb->events); $collector->stripe_webhook($notification->type, $notification, new WC_Order()); check(count($wpdb->events) === $before, 'Webhook replays deduplicated'); $notification->id = 'evt_Two'; $collector->stripe_webhook($notification->type, $notification, new WC_Order()); check(count($wpdb->events) === $before + 1, 'Distinct failures in same second preserved'); check(last_event()['actor_id'] === 0 && last_event()['subject_id'] === 42, 'Webhook never attributed to logged-in actor'); check(last_event()['reason'] === 'insufficient_funds', 'Allowlisted decline code retained'); check(strpos(json_encode($wpdb->events), 'SECRET') === false, 'Raw form, card and exception data never stored'); $notification->id = 'evt_Three'; $notification->type = 'setup_intent.setup_failed'; $notification->data->object->id = 'seti_One'; $notification->livemode = false; $GLOBALS['matched_users'] = [42]; $collector->stripe_webhook($notification->type, $notification); check(last_event()['event_type'] === 'card_setup' && last_event()['subject_id'] === 42 && last_event()['order_id'] === 0, 'Saved-card failure links to account without an order'); check(last_event()['mode'] === 'test', 'Test events remain labeled'); check($GLOBALS['lookup']['meta_key'] === 'wp__stripe_customer_id', 'Customer lookup respects site-specific user option'); $before = count($wpdb->events); $GLOBALS['matched_users'] = [42, 43]; $notification->id = 'evt_Ambiguous'; $collector->stripe_webhook($notification->type, $notification); check(count($wpdb->events) === $before, 'Ambiguous or unlinked customers are not attributed to this store'); $GLOBALS['matched_users'] = []; $notification->id = 'evt_Unlinked'; $collector->stripe_webhook($notification->type, $notification); check(count($wpdb->events) === $before, 'Unlinked events from shared Stripe accounts excluded'); $options['atk_options']['activity_types']['woocommerce'] = 0; $collector->payment_error(null, new WC_Order()); $notification->id = 'evt_Disabled'; $collector->stripe_webhook($notification->type, $notification); check(count($wpdb->events) === $before, 'Disabled WooCommerce tracking respected'); $options['atk_options']['activity_types']['user'] = 0; $collector->registered(43); check(count($wpdb->events) === $before, 'Disabled user tracking respected'); $options['atk_options']['activity_types']['user'] = 1; $wpdb->throw = true; $collector->registered(44); check(!$wpdb->suppressed, 'Database failure is isolated and error state restored'); $wpdb->throw = false; $options['atk_security_db_version'] = false; $collector->registered(45); check(count($wpdb->events) === $before, 'No writes before schema installation'); $options['atk_security_db_version'] = '1'; $collector->cleanup(); check(strpos($wpdb->last_query[0], 'DELETE') === 0 && $wpdb->last_query[1][0] === 'wp_atk_security_events', 'Retention cleanup targets only security events'); echo "PASS: Free-only hooks, actor attribution, registration, classic/block checkout, gateway errors, webhook replay and distinct failures, saved cards, privacy, tracking preferences, database failure isolation, retention.\n";