Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions docs/architecture-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -1323,9 +1323,9 @@ through the plugin's own injector configuration:
method of the scoped lifecycle interfaces it implements, such as
`activate_site()` or `uninstall_network()`, called with the context of site
1 on network 1. The check records
hook additions and removals, option and transient writes, and post, user,
term and comment meta writes. Failures name the service, method and effect;
exceptions in either call are reported as problems.
hook additions and removals, cron events, option and transient writes, and
post, user, term and comment meta writes. Failures name the service, method
and effect; exceptions in either call are reported as problems.

**Allows:** the tests stub the WordPress functions the wiring calls, such as
`get_stylesheet_directory()`, and use stand-ins for WordPress classes such as
Expand All @@ -1337,8 +1337,15 @@ the same callback at the same priority replaces the entry; a fresh closure,
a different object or method, or a different priority adds an entry. Removing
an existing entry changes state; removing a missing entry leaves it unchanged.
Actions and filters share hook entries, and fired hooks retain their call
history. This does not check the plugin's own
`register_services()` orchestration (#26), which services activation,
history. Cron events are keyed by hook, timestamp and `md5( serialize( $args ) )`,
with recurrence stored per event. Scheduling the same event replaces it;
a different timestamp or argument array adds one. `wp_next_scheduled()` returns the
earliest timestamp matching the hook and arguments, or `false`.
`wp_clear_scheduled_hook()` removes events matching the hook and arguments
and returns the number removed; `wp_unschedule_event()` also matches the
timestamp and returns `true` even when nothing matched. Single events use the
same model without the ten-minute duplicate window. This does not check the
plugin's own `register_services()` orchestration (#26), which services activation,
deactivation and uninstall reach and in which sites
(`tests/php/Unit/ServiceBasedPluginTest.php`, `tests/php/Unit/ScopedLifecycleTest.php`
and `tests/php/Unit/UninstallLifecycleTest.php` cover that),
Expand Down
2 changes: 2 additions & 0 deletions tests/php/Architecture/CompositionRootContractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ public function test_it_reports_non_idempotent_lifecycle_steps(): void {
[ 'ChangingDeactivation', 'deactivate', 'changes the option "fixture_runs" again' ],
[ 'ChangingTransient', 'activate', 'changes the transient "fixture_runs" again' ],
[ 'ChangingTransient', 'activate', 'changes the user meta "fixture_runs" for object 42 again' ],
[ 'ChangingCronTimestamp', 'activate', 'changes the cron events of "fixture_event" again' ],
[ 'ChangingCronArguments', 'activate', 'changes the cron events of "fixture_event" again' ],
[
'AppendingScopedLifecycle',
'activate_network',
Expand Down
100 changes: 100 additions & 0 deletions tests/php/Architecture/LifecycleEffects.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ final class LifecycleEffects {
*/
private array $callbacks = [];

/**
* Recurrences keyed by hook, timestamp and md5( serialize( $args ) ).
* WordPress replaces events with the same identity, even if recurrence changes.
*
* @var array<string,array<int,array<string,string|false>>>
*/
private array $cron = [];

public function watch(): void {
foreach ( [ 'action', 'filter' ] as $type ) {
Functions\when( 'add_' . $type )->alias(
Expand Down Expand Up @@ -143,6 +151,89 @@ function ( string $name ) use ( $type ): bool {
foreach ( [ 'post', 'user', 'term', 'comment' ] as $type ) {
$this->watch_meta( $type );
}
$this->watch_cron();
}

private function watch_cron(): void {
Functions\when( 'wp_schedule_event' )->alias(
/** @param array<mixed> $args Event arguments, whose serialization identifies the event. */
function ( int $timestamp, string $recurrence, string $hook, array $args = [] ): bool {
$this->cron[ $hook ][ $timestamp ][ self::cron_key( $args ) ] = $recurrence;

return true;
}
);
Functions\when( 'wp_schedule_single_event' )->alias(
/** @param array<mixed> $args */
function ( int $timestamp, string $hook, array $args = [] ): bool {
$this->cron[ $hook ][ $timestamp ][ self::cron_key( $args ) ] = false;

return true;
}
);
Functions\when( 'wp_next_scheduled' )->alias(
/**
* @param array<mixed> $args
* @return int|false
*/
function ( string $hook, array $args = [] ) {
$key = self::cron_key( $args );
$next = false;
foreach ( $this->cron[ $hook ] ?? [] as $timestamp => $events ) {
if ( isset( $events[ $key ] ) && ( $next === false || $timestamp < $next ) ) {
$next = $timestamp;
}
}

return $next;
}
);
Functions\when( 'wp_unschedule_event' )->alias(
/** @param array<mixed> $args */
function ( int $timestamp, string $hook, array $args = [] ): bool {
$this->remove_cron_event( $hook, $timestamp, self::cron_key( $args ) );

// WordPress writes the cron array back even when no event matched.
return true;
}
);
Functions\when( 'wp_clear_scheduled_hook' )->alias(
/** @param array<mixed> $args */
function ( string $hook, array $args = [] ): int {
$key = self::cron_key( $args );
$removed = 0;
foreach ( $this->cron[ $hook ] ?? [] as $timestamp => $events ) {
if ( isset( $events[ $key ] ) ) {
$this->remove_cron_event( $hook, $timestamp, $key );
++$removed;
}
}

return $removed;
}
);
}

/**
* Preserve WordPress's distinction between argument types and array order.
*
* @param array<mixed> $args Event arguments.
*/
private static function cron_key( array $args ): string {
// WordPress identifies an event by this exact serialization; the arguments are test input.
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize -- Mirrors wp_schedule_event().
return md5( serialize( $args ) );
}

private function remove_cron_event( string $hook, int $timestamp, string $key ): void {
unset( $this->cron[ $hook ][ $timestamp ][ $key ] );
// Empty buckets must compare the same as a hook that never had events.
if ( ( $this->cron[ $hook ][ $timestamp ] ?? [] ) === [] ) {
unset( $this->cron[ $hook ][ $timestamp ] );
}
if ( ( $this->cron[ $hook ] ?? [] ) === [] ) {
unset( $this->cron[ $hook ] );
}
}

/**
Expand Down Expand Up @@ -238,6 +329,15 @@ public function snapshot(): array {
$hooks = $property->getValue( $storage );
$state = array_merge( $this->writes, $this->calls );

foreach ( $this->cron as $hook => $timestamps ) {
ksort( $timestamps );
foreach ( $timestamps as &$events ) {
ksort( $events );
}
unset( $events );
$state[ sprintf( 'changes the cron events of "%s" again', $hook ) ] = $timestamps;
}

foreach ( $this->hooks as $name => $priorities ) {
// Compare sets independently of the order entries were added in.
ksort( $priorities );
Expand Down
142 changes: 141 additions & 1 deletion tests/php/Architecture/LifecycleEffectsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Yoast\PHPUnitPolyfills\TestCases\TestCase;

/**
* Hook state follows WordPress identity rather than Brain Monkey's call log.
* Lifecycle state follows WordPress identity rather than Brain Monkey's call log.
*/
final class LifecycleEffectsTest extends TestCase {

Expand Down Expand Up @@ -150,6 +150,146 @@ public function test_it_still_records_fired_actions_and_filters(): void {
);
}

public function test_cron_scheduling_replaces_identical_events_and_retains_recurrence(): void {
$effects = new LifecycleEffects();
$effects->watch();
$this->assertTrue( wp_schedule_event( 100, 'daily', 'fixture_event', [ 42 ] ) );
$this->assertTrue( wp_schedule_single_event( 200, 'fixture_event', [ 42 ] ) );
$before = $effects->snapshot();

$this->assertSame(
[
100 => [ md5( 'a:1:{i:0;i:42;}' ) => 'daily' ],
200 => [ md5( 'a:1:{i:0;i:42;}' ) => false ],
],
$before['changes the cron events of "fixture_event" again']
);
$this->assertTrue( wp_schedule_event( 100, 'daily', 'fixture_event', [ 42 ] ) );
$this->assertTrue( wp_schedule_single_event( 200, 'fixture_event', [ 42 ] ) );
$this->assertSame( [], $effects->changes_since( $before ) );

wp_schedule_event( 100, 'hourly', 'fixture_event', [ 42 ] );
$this->assertSame(
[ 'changes the cron events of "fixture_event" again' ],
$effects->changes_since( $before )
);
}

public function test_cron_events_distinguish_timestamps_arguments_and_hooks(): void {
$effects = new LifecycleEffects();
$effects->watch();
wp_schedule_event( 100, 'daily', 'fixture_event', [ 42 ] );
$before = $effects->snapshot();
wp_schedule_event( 200, 'daily', 'fixture_event', [ 42 ] );
$this->assertSame(
[ 'changes the cron events of "fixture_event" again' ],
$effects->changes_since( $before )
);

$before = $effects->snapshot();
wp_schedule_event( 100, 'daily', 'fixture_event', [ '42' ] );
$this->assertSame(
[ 'changes the cron events of "fixture_event" again' ],
$effects->changes_since( $before )
);

$before = $effects->snapshot();
wp_schedule_single_event( 100, 'fixture_other', [ 42 ] );
$this->assertSame(
[ 'changes the cron events of "fixture_other" again' ],
$effects->changes_since( $before )
);
}

public function test_next_scheduled_returns_the_earliest_timestamp_for_the_hook_and_arguments(): void {
$effects = new LifecycleEffects();
$effects->watch();
$this->assertFalse( wp_next_scheduled( 'fixture_event' ) );
wp_schedule_event( 200, 'daily', 'fixture_event', [ 42 ] );
wp_schedule_single_event( 100, 'fixture_event', [ 42 ] );
wp_schedule_single_event( 50, 'fixture_event', [ 43 ] );
wp_schedule_single_event( 25, 'fixture_other', [ 42 ] );

$this->assertSame( 100, wp_next_scheduled( 'fixture_event', [ 42 ] ) );
$this->assertSame( 50, wp_next_scheduled( 'fixture_event', [ 43 ] ) );
$this->assertSame( 25, wp_next_scheduled( 'fixture_other', [ 42 ] ) );
$this->assertFalse( wp_next_scheduled( 'fixture_event', [ '42' ] ) );
$this->assertFalse( wp_next_scheduled( 'fixture_event' ) );
}

public function test_clearing_cron_events_matches_arguments_and_counts_removals(): void {
$effects = new LifecycleEffects();
$effects->watch();
wp_schedule_event( 100, 'daily', 'fixture_event', [ 42 ] );
wp_schedule_single_event( 200, 'fixture_event', [ 42 ] );
wp_schedule_single_event( 100, 'fixture_event', [ 43 ] );
wp_schedule_single_event( 100, 'fixture_other', [ 42 ] );
$before = $effects->snapshot();

$this->assertSame( 0, wp_clear_scheduled_hook( 'fixture_event' ) );
$this->assertSame( [], $effects->changes_since( $before ) );
$this->assertSame( 2, wp_clear_scheduled_hook( 'fixture_event', [ 42 ] ) );
$this->assertSame(
[ 'changes the cron events of "fixture_event" again' ],
$effects->changes_since( $before )
);
$this->assertFalse( wp_next_scheduled( 'fixture_event', [ 42 ] ) );
$this->assertSame( 100, wp_next_scheduled( 'fixture_event', [ 43 ] ) );
$this->assertSame( 100, wp_next_scheduled( 'fixture_other', [ 42 ] ) );
$before = $effects->snapshot();
$this->assertSame( 0, wp_clear_scheduled_hook( 'fixture_event', [ 42 ] ) );
$this->assertSame( [], $effects->changes_since( $before ) );
$this->assertSame( 1, wp_clear_scheduled_hook( 'fixture_event', [ 43 ] ) );
$this->assertFalse( wp_next_scheduled( 'fixture_event', [ 43 ] ) );
}

public function test_unscheduling_matches_timestamp_hook_and_arguments_even_when_missing(): void {
$effects = new LifecycleEffects();
$effects->watch();
wp_schedule_event( 100, 'daily', 'fixture_event', [ 42 ] );
wp_schedule_single_event( 200, 'fixture_event', [ 42 ] );
wp_schedule_single_event( 100, 'fixture_event', [ 43 ] );
wp_schedule_single_event( 100, 'fixture_other', [ 42 ] );
$before = $effects->snapshot();

$this->assertTrue( wp_unschedule_event( 100, 'fixture_event' ) );
$this->assertTrue( wp_unschedule_event( 300, 'fixture_event', [ 42 ] ) );
$this->assertTrue( wp_unschedule_event( 100, 'fixture_missing', [ 42 ] ) );
$this->assertSame( [], $effects->changes_since( $before ) );
$this->assertTrue( wp_unschedule_event( 100, 'fixture_event', [ 42 ] ) );
$this->assertSame(
[ 'changes the cron events of "fixture_event" again' ],
$effects->changes_since( $before )
);
$this->assertSame( 200, wp_next_scheduled( 'fixture_event', [ 42 ] ) );
$this->assertSame( 100, wp_next_scheduled( 'fixture_event', [ 43 ] ) );
$this->assertSame( 100, wp_next_scheduled( 'fixture_other', [ 42 ] ) );
$before = $effects->snapshot();
$this->assertTrue( wp_unschedule_event( 100, 'fixture_event', [ 42 ] ) );
$this->assertSame( [], $effects->changes_since( $before ) );
$this->assertTrue( wp_unschedule_event( 200, 'fixture_event', [ 42 ] ) );
$this->assertTrue( wp_unschedule_event( 100, 'fixture_event', [ 43 ] ) );
$this->assertFalse( wp_next_scheduled( 'fixture_event', [ 43 ] ) );
}

public function test_cron_snapshots_compare_events_independently_of_insertion_order(): void {
$effects = new LifecycleEffects();
$effects->watch();
$empty = $effects->snapshot();
wp_schedule_event( 200, 'daily', 'fixture_event', [ 42 ] );
wp_schedule_single_event( 100, 'fixture_event', [ 43 ] );
wp_schedule_single_event( 100, 'fixture_event', [ 42 ] );
$before = $effects->snapshot();

wp_clear_scheduled_hook( 'fixture_event', [ 42 ] );
wp_clear_scheduled_hook( 'fixture_event', [ 43 ] );
$this->assertSame( [], $effects->changes_since( $empty ) );
wp_schedule_single_event( 100, 'fixture_event', [ 42 ] );
wp_schedule_single_event( 100, 'fixture_event', [ 43 ] );
wp_schedule_event( 200, 'daily', 'fixture_event', [ 42 ] );
$this->assertSame( [], $effects->changes_since( $before ) );
}

public function __invoke( string $content ): string {
return $content;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/php/Fixture/CompositionRoot/BrokenLifecyclePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ protected function get_service_classes(): array {
'fixture.AppendingActivation' => AppendingActivation::class,
'fixture.ChangingDeactivation' => ChangingDeactivation::class,
'fixture.ChangingTransient' => ChangingTransient::class,
'fixture.ChangingCronTimestamp' => ChangingCronTimestamp::class,
'fixture.ChangingCronArguments' => ChangingCronArguments::class,
'fixture.AppendingScopedLifecycle' => AppendingScopedLifecycle::class,
'fixture.ThrowingLifecycle' => ThrowingLifecycle::class,
];
Expand Down
17 changes: 17 additions & 0 deletions tests/php/Fixture/CompositionRoot/ChangingCronArguments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare( strict_types=1 );

namespace MWPD\BasicScaffold\Tests\Fixture\CompositionRoot;

use MWPD\BasicScaffold\Infrastructure\Activateable;
use MWPD\BasicScaffold\Infrastructure\Service;

final class ChangingCronArguments implements Service, Activateable {

private int $runs = 0;

public function activate(): void {
wp_schedule_event( 100, 'daily', 'fixture_event', [ ++$this->runs ] );
}
}
18 changes: 18 additions & 0 deletions tests/php/Fixture/CompositionRoot/ChangingCronTimestamp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare( strict_types=1 );

namespace MWPD\BasicScaffold\Tests\Fixture\CompositionRoot;

use MWPD\BasicScaffold\Infrastructure\Activateable;
use MWPD\BasicScaffold\Infrastructure\Service;

final class ChangingCronTimestamp implements Service, Activateable {

private int $runs = 0;

public function activate(): void {
// A counter exposes duplicate scheduling without depending on the clock.
wp_schedule_event( ++$this->runs, 'daily', 'fixture_event' );
}
}
17 changes: 17 additions & 0 deletions tests/php/Fixture/CompositionRoot/GuardedCronActivation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare( strict_types=1 );

namespace MWPD\BasicScaffold\Tests\Fixture\CompositionRoot;

use MWPD\BasicScaffold\Infrastructure\Activateable;
use MWPD\BasicScaffold\Infrastructure\Service;

final class GuardedCronActivation implements Service, Activateable {

public function activate(): void {
if ( wp_next_scheduled( 'fixture_event', [ 42 ] ) === false ) {
wp_schedule_event( 100, 'daily', 'fixture_event', [ 42 ] );
}
}
}
12 changes: 7 additions & 5 deletions tests/php/Fixture/CompositionRoot/IdempotentLifecyclePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ final class IdempotentLifecyclePlugin extends ServiceBasedPlugin {

protected function get_service_classes(): array {
return [
'fixture.GuardedActivation' => GuardedActivation::class,
'fixture.SettingLifecycle' => SettingLifecycle::class,
'fixture.IdenticalRegistration' => IdenticalRegistration::class,
'fixture.RemovingDeactivation' => RemovingDeactivation::class,
'fixture.CleaningUninstall' => CleaningUninstall::class,
'fixture.GuardedActivation' => GuardedActivation::class,
'fixture.GuardedCronActivation' => GuardedCronActivation::class,
'fixture.IdenticalCronActivation' => IdenticalCronActivation::class,
'fixture.SettingLifecycle' => SettingLifecycle::class,
'fixture.IdenticalRegistration' => IdenticalRegistration::class,
'fixture.RemovingDeactivation' => RemovingDeactivation::class,
'fixture.CleaningUninstall' => CleaningUninstall::class,
];
}
}
Loading