Skip to content

Commit 861c0e8

Browse files
committed
WP/CronInterval: add tests for namespaced names
1 parent 4a34f38 commit 861c0e8

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

WordPress/Tests/WP/CronIntervalUnitTest.inc

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,9 @@ add_filter( 'cron_schedules', function ( $schedules ) {
165165
class FQNConstants {
166166
public function add_schedules() {
167167
add_filter( 'cron_schedules', array( $this, 'add_weekly_schedule' ) ); // Ok: > 15 min.
168-
add_filter( 'cron_schedules', array( $this, 'add_eight_minute_schedule' ) ); // Warning: 8 min.
169-
add_filter( 'cron_schedules', array( $this, 'add_hundred_minute_schedule' ) ); // Warning: time undetermined.
170-
add_filter( 'cron_schedules', array( $this, 'sneaky_fake_wp_constant_schedule' ) ); // Warning: time undetermined.
168+
\add_filter( 'cron_schedules', array( $this, 'add_eight_minute_schedule' ) ); // Warning: 8 min.
169+
ADD_FILTER( 'cron_schedules', array( $this, 'add_hundred_minute_schedule' ) ); // Warning: time undetermined.
170+
\Add_Filter( 'cron_schedules', array( $this, 'sneaky_fake_wp_constant_schedule' ) ); // Warning: time undetermined.
171171
}
172172

173173
public function add_weekly_schedule( $schedules ) {
@@ -316,6 +316,7 @@ function first_class_weekly_schedule( $schedules ) {
316316
}
317317
add_filter( 'cron_schedules', 'first_class_weekly_schedule'(...)); // Ok: > 15 min.
318318
add_filter( 'cron_schedules', first_class_weekly_schedule(...)); // Ok: > 15 min.
319+
add_filter( 'cron_schedules', namespace\first_class_weekly_schedule(...)); // Ok: > 15 min.
319320

320321
function first_class_six_min_schedule( $schedules ) {
321322
$schedules['every_6_mins'] = array(
@@ -326,3 +327,35 @@ function first_class_six_min_schedule( $schedules ) {
326327
}
327328
add_filter( 'cron_schedules', first_class_six_min_schedule(...)); // Warning: 6 min.
328329
add_filter( 'cron_schedules', 'first_class_six_min_schedule'(...)); // Warning: 6 min.
330+
add_filter( 'cron_schedules', \first_class_six_min_schedule(...)); // Warning: 6 min.
331+
add_filter( 'cron_schedules', namespace\first_class_six_min_schedule(...)); // Warning: 6 min.
332+
333+
/*
334+
* The tests below document the current behavior of the sniff, even though they are false negatives. The sniff treats
335+
* the first-class callable examples below as if referencing the global function first_class_six_min_schedule()
336+
* and not a namespaced function with the same name.
337+
*
338+
* Related to: https://github.com/WordPress/WordPress-Coding-Standards/issues/2644.
339+
*/
340+
add_filter( 'cron_schedules', MyNamespace\first_class_weekly_schedule(...)); // False negative - Ok: > 15 min, but should be marked `ChangeDetected`.
341+
add_filter( 'cron_schedules', \MyNamespace\first_class_weekly_schedule(...)); // False negative - Ok: > 15 min, but should be marked `ChangeDetected`.
342+
add_filter( 'cron_schedules', namespace\Sub\first_class_weekly_schedule(...)); // False negative - Ok: > 15 min, but should be marked `ChangeDetected`.
343+
344+
/*
345+
* The tests below document the current behavior of the sniff, even though they are false positives. The sniff treats
346+
* the first-class callable examples below as if referencing the global function first_class_six_min_schedule()
347+
* and not a namespaced function with the same name. Fixing this incorrect behavior is not trivial.
348+
*
349+
* Related to: https://github.com/WordPress/WordPress-Coding-Standards/issues/2644.
350+
*/
351+
add_filter( 'cron_schedules', MyNamespace\first_class_six_min_schedule(...)); // False positive - `CronSchedulesInterval` warning (6 min), but should be `ChangeDetected`.
352+
add_filter( 'cron_schedules', \MyNamespace\first_class_six_min_schedule(...)); // False positive - `CronSchedulesInterval` warning (6 min), but should be `ChangeDetected`.
353+
add_filter( 'cron_schedules', namespace\Sub\first_class_six_min_schedule(...)); // False positive - `CronSchedulesInterval` warning (6 min), but should be `ChangeDetected`.
354+
355+
/*
356+
* Safeguard correct handling of all types of namespaced function calls (except FQN global function call which is
357+
* handled above).
358+
*/
359+
MyNamespace\add_filter( 'cron_schedules', 'unknown_callback' ); // Ok.
360+
\MyNamespace\add_filter( 'cron_schedules', 'unknown_callback' ); // Ok.
361+
namespace\add_filter( 'cron_schedules', 'unknown_callback' ); // Ok. The sniff should start flagging this once it can resolve relative namespaces.

WordPress/Tests/WP/CronIntervalUnitTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,13 @@ public function getWarningList() {
6565
286 => 1,
6666
288 => 1,
6767
290 => 1,
68-
327 => 1,
6968
328 => 1,
69+
329 => 1,
70+
330 => 1,
71+
331 => 1,
72+
351 => 1,
73+
352 => 1,
74+
353 => 1,
7075
);
7176
}
7277
}

0 commit comments

Comments
 (0)