test for wp_schedule_delete_old_privacy_export_files - #5550
Conversation
…Files.php Co-authored-by: John Parris <public@johnparris.com>
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @pbearne@git.wordpress.org. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
|
Thanks @pbearne for the patch. I've prepared a refreshed version of the test that resolves the outstanding review feedback from @mindctrl on this PR:
Verified locally against |
|
For visibility: I've also opened #12563 as an alternative PR carrying these same tests, in case that's an easier path to land than updating this branch. Happy to close it and fold the changes in here instead — whichever maintainers prefer. See #59707. |
…Files.php Co-authored-by: John Parris <public@johnparris.com>
…Files.php Co-authored-by: John Parris <public@johnparris.com>
…Files.php Co-authored-by: John Parris <public@johnparris.com>
There was a problem hiding this comment.
Pull request overview
Adds PHPUnit coverage for wp_schedule_delete_old_privacy_export_files() (Trac #59707) to ensure the cron event is scheduled appropriately and not duplicated.
Changes:
- Introduces a new test class covering initial scheduling behavior.
- Adds a test to ensure no scheduling occurs during installation mode.
- Adds a test to ensure re-calling the function does not create duplicate cron events.
Comments suppressed due to low confidence (1)
tests/phpunit/tests/functions/wpScheduleDeleteOldPrivacyExportFiles.php:47
- Calling parent::tear_down() inside an individual test method is incorrect; tear_down() should be handled by PHPUnit, and this call can run cleanup at the wrong time. Once tear_down() correctly calls parent::tear_down(), this line should be removed.
wp_schedule_delete_old_privacy_export_files();
$this->assertFalse( wp_next_scheduled( 'wp_privacy_delete_old_export_files' ) );
parent::tear_down();
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@pbearne, Can you address the feedback from Copilot? Both seem like reasonable suggestions. |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
| wp_schedule_delete_old_privacy_export_files(); | ||
| $first = wp_next_scheduled( 'wp_privacy_delete_old_export_files' ); | ||
|
|
||
| wp_schedule_delete_old_privacy_export_files(); | ||
| $this->assertSame( $first, wp_next_scheduled( 'wp_privacy_delete_old_export_files' ) ); |
There was a problem hiding this comment.
| wp_schedule_delete_old_privacy_export_files(); | |
| $first = wp_next_scheduled( 'wp_privacy_delete_old_export_files' ); | |
| wp_schedule_delete_old_privacy_export_files(); | |
| $this->assertSame( $first, wp_next_scheduled( 'wp_privacy_delete_old_export_files' ) ); | |
| // Schedule ahead of time() so that a duplicate would land on a different timestamp. | |
| wp_schedule_event( strtotime( '+1 hour' ), 'hourly', 'wp_privacy_delete_old_export_files' ); | |
| // Take a snapshot of the cron option while the event is scheduled. | |
| $expected = _get_cron_array(); | |
| // The event is already scheduled, so this call should be a no-op. | |
| wp_schedule_delete_old_privacy_export_files(); | |
| // Check cron option is unchanged. | |
| $this->assertSame( $expected, _get_cron_array(), 'the event should not be scheduled again' ); |
This assertion doesn't currently catch a regression — removing the ! wp_next_scheduled() guard leaves this test green. Two reasons:
wp_schedule_event()stores events as$crons[ $timestamp ][ $hook ][ md5( serialize( $args ) ) ]. This function always schedules with no args, so both calls collide on all three keys when they happen within the same second — the second one silently overwrites the first, and no duplicate is ever observable.- Even if the two calls land on different seconds,
wp_next_scheduled()only returns the soonest timestamp, so it still returns$firstand the assertion passes.
Seeding the event at a timestamp other than time() and comparing the whole cron option would cover both cases:
This follows the same pattern as test_pre_schedule_event_filter().
| // set to installing mode | ||
| $prior = wp_installing(); | ||
| wp_installing( true ); | ||
|
|
||
| $this->assertFalse( wp_next_scheduled( 'wp_privacy_delete_old_export_files' ), 'no export should be scheduled' ); | ||
| wp_schedule_delete_old_privacy_export_files(); | ||
| $this->assertFalse( wp_next_scheduled( 'wp_privacy_delete_old_export_files' ), 'export should be scheduled' ); | ||
|
|
||
| wp_installing( $prior ); |
There was a problem hiding this comment.
| // set to installing mode | |
| $prior = wp_installing(); | |
| wp_installing( true ); | |
| $this->assertFalse( wp_next_scheduled( 'wp_privacy_delete_old_export_files' ), 'no export should be scheduled' ); | |
| wp_schedule_delete_old_privacy_export_files(); | |
| $this->assertFalse( wp_next_scheduled( 'wp_privacy_delete_old_export_files' ), 'export should be scheduled' ); | |
| wp_installing( $prior ); | |
| $this->assertFalse( wp_next_scheduled( 'wp_privacy_delete_old_export_files' ), 'no export should be scheduled' ); | |
| // set to installing mode | |
| $prior = wp_installing(); | |
| wp_installing( true ); | |
| wp_schedule_delete_old_privacy_export_files(); | |
| wp_installing( $prior ); | |
| $this->assertFalse( wp_next_scheduled( 'wp_privacy_delete_old_export_files' ), 'no export should be scheduled while installing' ); |
Since the installing mode restoration occurs after the assertion, if the assertion fails, the installing mode is not restored, which affects subsequent tests. I believe we need to be careful about the order to ensure that the installing mode is always restored regardless of whether the assertion succeeds or fails.
Also, please note that the message has been changed from export should be scheduled to no export should be scheduled while installing. The previous message indicated the opposite of the assertion.
Trac ticket: https://core.trac.wordpress.org/ticket/59707