'clear', 'hook' => $hook ]; } } if ( ! function_exists( 'wp_schedule_event' ) ) { function wp_schedule_event( int $timestamp, string $recurrence, string $hook ): void { $GLOBALS['pr_core_cron_calls'][] = [ 'action' => 'schedule', 'hook' => $hook, 'recurrence' => $recurrence ]; } } // ── Load the settings class ───────────────────────────────────────────── require_once __DIR__ . '/../../includes/cpt/class-pr-core-peptide-cpt.php'; require_once __DIR__ . '/../../includes/admin/class-pr-core-settings.php'; echo "== PR_Core_Settings verification methods unit tests ==\n\n"; // ── sanitize_cadence() ────────────────────────────────────────────────── echo "sanitize_cadence():\n"; pr_assert_equals( 'daily', PR_Core_Settings::sanitize_cadence( 'daily' ), 'daily → daily' ); pr_assert_equals( 'weekly', PR_Core_Settings::sanitize_cadence( 'weekly' ), 'weekly → weekly' ); pr_assert_equals( 'monthly', PR_Core_Settings::sanitize_cadence( 'monthly' ), 'monthly → monthly' ); pr_assert_equals( 'weekly', PR_Core_Settings::sanitize_cadence( 'hourly' ), 'hourly (invalid) → weekly default' ); pr_assert_equals( 'weekly', PR_Core_Settings::sanitize_cadence( '' ), 'empty → weekly default' ); pr_assert_equals( 'weekly', PR_Core_Settings::sanitize_cadence( 'DAILY' ), 'uppercase → weekly (case-sensitive)' ); // ── sanitize_emails() ─────────────────────────────────────────────────── echo "\nsanitize_emails():\n"; pr_assert_equals( '', PR_Core_Settings::sanitize_emails( '' ), 'empty → empty' ); pr_assert_equals( 'admin@example.com', PR_Core_Settings::sanitize_emails( 'admin@example.com' ), 'valid single → kept' ); pr_assert_equals( '', PR_Core_Settings::sanitize_emails( 'not-an-email' ), 'invalid single → empty' ); pr_assert_equals( 'a@test.com, b@test.com', PR_Core_Settings::sanitize_emails( 'a@test.com, b@test.com' ), 'valid pair → both kept' ); pr_assert_equals( 'good@test.com', PR_Core_Settings::sanitize_emails( 'good@test.com, bad-email' ), 'mixed → only valid kept' ); pr_assert_equals( 'trimmed@test.com', PR_Core_Settings::sanitize_emails( ' trimmed@test.com ' ), 'whitespace trimmed around valid email' ); // ── reschedule_cron(): no-op when same value ──────────────────────────── echo "\nreschedule_cron() — no-op when value unchanged:\n"; $GLOBALS['pr_core_cron_calls'] = []; PR_Core_Settings::reschedule_cron( 'weekly', 'weekly' ); pr_assert( empty( $GLOBALS['pr_core_cron_calls'] ), 'no cron calls when old === new' ); // ── reschedule_cron(): clears + reschedules on change ─────────────────── echo "\nreschedule_cron() — reschedules when value changes:\n"; $GLOBALS['pr_core_cron_calls'] = []; PR_Core_Settings::reschedule_cron( 'weekly', 'daily' ); $actions = array_column( $GLOBALS['pr_core_cron_calls'], 'action' ); pr_assert( in_array( 'clear', $actions, true ), 'wp_clear_scheduled_hook was called' ); pr_assert( in_array( 'schedule', $actions, true ), 'wp_schedule_event was called' ); $schedule_call = null; foreach ( $GLOBALS['pr_core_cron_calls'] as $call ) { if ( 'schedule' === $call['action'] ) { $schedule_call = $call; break; } } pr_assert( null !== $schedule_call, 'schedule call found' ); pr_assert_equals( 'pr_core_verification_scan', $schedule_call['hook'] ?? '', 'rescheduled hook = pr_core_verification_scan' ); pr_assert_equals( 'daily', $schedule_call['recurrence'] ?? '', 'rescheduled with new cadence "daily"' ); // Clear fires before schedule (correct ordering). $clear_idx = array_search( 'clear', $actions, true ); $schedule_idx = array_search( 'schedule', $actions, true ); pr_assert( $clear_idx < $schedule_idx, 'clear fires before schedule' ); // ── Summary ───────────────────────────────────────────────────────────── exit( pr_test_summary() );