fix(tests): rewrite unit tests to plain-PHP bootstrap pattern; extract compute_status() from scanner
This commit is contained in:
@@ -1,93 +1,131 @@
|
||||
<?php
|
||||
/**
|
||||
* Unit tests for PR_Core_Settings verification methods.
|
||||
*
|
||||
* Run: php tests/unit/test-verification-settings.php
|
||||
* Exit code 0 = all pass, 1 = any failure.
|
||||
*
|
||||
* What's covered:
|
||||
* - sanitize_cadence() accepts valid values, rejects invalid → 'weekly'.
|
||||
* - sanitize_emails() filters invalid addresses, preserves valid ones.
|
||||
* - reschedule_cron() no-ops when old === new value.
|
||||
* - reschedule_cron() clears + reschedules when value changes.
|
||||
*
|
||||
* @package PeptideRepoCore
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Tests for PR_Core_Settings cadence and cron scheduling.
|
||||
*
|
||||
* What: Unit tests for settings registration and cron rescheduling.
|
||||
* Dependencies: PHPUnit, WordPress test utilities.
|
||||
*/
|
||||
class Test_Verification_Settings extends \WP_UnitTestCase {
|
||||
require_once __DIR__ . '/../bootstrap.php';
|
||||
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
// Clear any existing scheduled hooks.
|
||||
wp_clear_scheduled_hook( 'pr_core_verification_scan' );
|
||||
}
|
||||
// ── Additional stubs needed by the settings class ───────────────────────
|
||||
|
||||
/**
|
||||
* Test that changing cadence triggers reschedule.
|
||||
*/
|
||||
public function test_reschedule_on_cadence_change(): void {
|
||||
// Set initial cadence.
|
||||
update_option( 'pr_core_scan_cadence', 'weekly' );
|
||||
$GLOBALS['pr_core_options'] = [];
|
||||
$GLOBALS['pr_core_cron_calls'] = [];
|
||||
|
||||
// Schedule initial cron (simulate the activation).
|
||||
if ( ! wp_next_scheduled( 'pr_core_verification_scan' ) ) {
|
||||
wp_schedule_event( time(), 'weekly', 'pr_core_verification_scan' );
|
||||
}
|
||||
|
||||
// Verify it's scheduled.
|
||||
$scheduled = wp_next_scheduled( 'pr_core_verification_scan' );
|
||||
$this->assertNotFalse( $scheduled );
|
||||
|
||||
// Change the cadence (triggers reschedule via hook).
|
||||
update_option( 'pr_core_scan_cadence', 'daily' );
|
||||
|
||||
// The reschedule_cron method should have cleared and rescheduled.
|
||||
// (In a real test, this would be tested via the hook — here we verify the logic.)
|
||||
PR_Core_Settings::reschedule_cron( 'weekly', 'daily' );
|
||||
|
||||
// Verify cron still exists (just with new frequency).
|
||||
$scheduled = wp_next_scheduled( 'pr_core_verification_scan' );
|
||||
$this->assertNotFalse( $scheduled );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that same cadence does not reschedule.
|
||||
*/
|
||||
public function test_no_reschedule_if_unchanged(): void {
|
||||
update_option( 'pr_core_scan_cadence', 'weekly' );
|
||||
|
||||
if ( ! wp_next_scheduled( 'pr_core_verification_scan' ) ) {
|
||||
wp_schedule_event( time(), 'weekly', 'pr_core_verification_scan' );
|
||||
}
|
||||
|
||||
$first_scheduled = wp_next_scheduled( 'pr_core_verification_scan' );
|
||||
|
||||
// Call reschedule with same value.
|
||||
PR_Core_Settings::reschedule_cron( 'weekly', 'weekly' );
|
||||
|
||||
$second_scheduled = wp_next_scheduled( 'pr_core_verification_scan' );
|
||||
|
||||
// Should be unchanged (same cron hook).
|
||||
$this->assertEquals( $first_scheduled, $second_scheduled );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test cadence sanitization.
|
||||
*/
|
||||
public function test_sanitize_cadence(): void {
|
||||
$this->assertEquals( 'daily', PR_Core_Settings::sanitize_cadence( 'daily' ) );
|
||||
$this->assertEquals( 'weekly', PR_Core_Settings::sanitize_cadence( 'weekly' ) );
|
||||
$this->assertEquals( 'monthly', PR_Core_Settings::sanitize_cadence( 'monthly' ) );
|
||||
$this->assertEquals( 'weekly', PR_Core_Settings::sanitize_cadence( 'invalid' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test email sanitization.
|
||||
*/
|
||||
public function test_sanitize_emails(): void {
|
||||
$input = 'admin@example.com, editor@example.com';
|
||||
$output = PR_Core_Settings::sanitize_emails( $input );
|
||||
$this->assertStringContainsString( 'admin@example.com', $output );
|
||||
$this->assertStringContainsString( 'editor@example.com', $output );
|
||||
|
||||
// Invalid emails filtered out.
|
||||
$input_bad = 'admin@example.com, invalid-email, editor@example.com';
|
||||
$output = PR_Core_Settings::sanitize_emails( $input_bad );
|
||||
$this->assertStringContainsString( 'admin@example.com', $output );
|
||||
$this->assertStringContainsString( 'editor@example.com', $output );
|
||||
$this->assertStringNotContainsString( 'invalid-email', $output );
|
||||
if ( ! function_exists( 'get_option' ) ) {
|
||||
function get_option( string $key, $default = false ) {
|
||||
return $GLOBALS['pr_core_options'][ $key ] ?? $default;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'update_option' ) ) {
|
||||
function update_option( string $key, $value ): bool {
|
||||
$GLOBALS['pr_core_options'][ $key ] = $value;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'register_setting' ) ) {
|
||||
function register_setting( string $group, string $option, array $args = [] ): void {}
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'add_settings_section' ) ) {
|
||||
function add_settings_section( string $id, string $title, $cb, string $page ): void {}
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'add_settings_field' ) ) {
|
||||
function add_settings_field( string $id, string $title, $cb, string $page, string $section = '' ): void {}
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'is_email' ) ) {
|
||||
function is_email( string $email ): bool {
|
||||
return (bool) filter_var( $email, FILTER_VALIDATE_EMAIL );
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'wp_clear_scheduled_hook' ) ) {
|
||||
function wp_clear_scheduled_hook( string $hook ): void {
|
||||
$GLOBALS['pr_core_cron_calls'][] = [ 'action' => '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() );
|
||||
|
||||
Reference in New Issue
Block a user