@@ -38,3 +38,86 @@ wp_cache_replace( 'test', $data, $group, 100 ); // Lower than 300.
38
38
wp_cache_replace ( 'test ' , $ data , $ group , 2 *MINUTE_IN_SECONDS ); // Lower than 300.
39
39
wp_cache_replace ( 123 , $ data , null , 1.5 * MINUTE_IN_SECONDS ); // Lower than 300.
40
40
wp_cache_replace ( $ testing , $ data , '' , 1.5 * MINUTE_IN_SECONDS ); // Lower than 300.
41
+
42
+ // Test error being reported on the line containing the parameter.
43
+ wp_cache_replace (
44
+ $ testing ,
45
+ $ data ,
46
+ '' ,
47
+ 1.5 * MINUTE_IN_SECONDS // Lower than 300.
48
+ );
49
+
50
+ // Test calculations with floats.
51
+ wp_cache_replace ( $ testing , $ data , '' , 7.5 * MINUTE_IN_SECONDS ); // OK.
52
+ wp_cache_replace ( $ testing , $ data , '' , 500 * 0.1 ); // Bad.
53
+
54
+ // Test comment handling.
55
+ wp_cache_add ( 'test ' , $ data , $ group , /* Deliberately left empty */ ); // OK.
56
+ wp_cache_add ( 'test ' , $ data , $ group , 600 * 0.1 /* = 1 minute */ ); // Bad.
57
+ wp_cache_add (
58
+ 'test ' ,
59
+ $ data ,
60
+ $ group ,
61
+ // Cache for 10 minutes.
62
+ 600
63
+ ); // OK.
64
+
65
+ wp_cache_add (
66
+ 'test ' ,
67
+ $ data ,
68
+ $ group ,
69
+ // phpcs:ignore Stnd.Cat.Sniff -- Just here for testing purposes.
70
+ 600
71
+ ); // OK.
72
+
73
+ // Test variable/constant with or without calculation being passed.
74
+ wp_cache_set ( $ key , $ data , '' , $ time ); // Manual inspection warning.
75
+ wp_cache_set ( $ key , $ data , '' , PREFIX_FIVE_MINUTES ); // Manual inspection warning.
76
+ wp_cache_set ( $ key , $ data , '' , 20 * $ time ); // Manual inspection warning.
77
+ wp_cache_set ( $ key , $ data , '' , $ base + $ extra ); // Manual inspection warning.
78
+ wp_cache_set ( $ key , $ data , '' , 300 + $ extra ); // Manual inspection warning.
79
+ wp_cache_set ( $ key , $ data , '' , PREFIX_CUSTOM_TIME * 5 ); // Manual inspection warning.
80
+
81
+ // Test calculations with additional aritmetic operators.
82
+ wp_cache_replace ( 'test ' , $ data , $ group , +5 ** MINUTE_IN_SECONDS ); // OK.
83
+ wp_cache_add ( 'test ' , $ data , $ group , WEEK_IN_SECONDS / 3 + HOUR_IN_SECONDS ); // OK.
84
+
85
+ // Test calculations grouped with parentheses.
86
+ wp_cache_set ( $ key , $ data , '' , (24 * 60 * 60 ) ); // OK.
87
+ wp_cache_set ( $ key , $ data , '' , (-(2 * 60 ) + 600 ) ); // OK.
88
+ wp_cache_set ( $ key , $ data , '' , (2 * 60 ) ); // Bad.
89
+ wp_cache_set ( $ key , $ data , '' , (-(2 * 60 ) + 600 ); // OK - includes parse error, close parenthesis missing.
90
+
91
+ // Test handling of numbers passed as strings.
92
+ wp_cache_set ( 'test ' , $ data , $ group , '300 ' ); // OK - type cast to integer within the function.
93
+ wp_cache_set ( 'test ' , $ data , $ group , '100 ' * 3 ); // OK - type cast to integer by PHP during the calculation.
94
+ wp_cache_set ( 'test ' , $ data , $ group , '-10 ' ); // Bad - type cast to integer within the function.
95
+ wp_cache_replace ( $ testing , $ data , '' , '1.5 ' * MINUTE_IN_SECONDS ); // Bad - type cast to integer by PHP during the calculation.
96
+
97
+ // Test handling of 0 values. `0` is the default value for the parameter and translates internally to "no expiration".
98
+ wp_cache_add ( 'test ' , $ data , $ group , 0 ); // OK.
99
+ wp_cache_add ( 'test ' , $ data , $ group , 0.0 ); // OK.
100
+ wp_cache_add ( 'test ' , $ data , $ group , '0 ' ); // OK.
101
+ wp_cache_add ( 'test ' , $ data , $ group , false ); // OK.
102
+ wp_cache_add ( 'test ' , $ data , $ group , null ); // OK.
103
+
104
+ // Test handling of other scalar values.
105
+ wp_cache_add ( 'test ' , $ data , $ group , true ); // Bad - becomes integer 1.
106
+
107
+ // Test passing just and only one of the time constants, including passing it as an FQN.
108
+ wp_cache_set ( 'test ' , $ data , $ group , HOUR_IN_SECONDS ); // OK.
109
+ wp_cache_set ( 'test ' , $ data , $ group , \MONTH_IN_SECONDS ); // OK.
110
+
111
+ // Test passing something which may look like one of the time constants, but isn't.
112
+ wp_cache_set ( 'test ' , $ data , $ group , month_in_seconds ); // Bad - constants are case-sensitive.
113
+ wp_cache_set ( 'test ' , $ data , $ group , HOUR_IN_SECONDS ::methodName () ); // Bad - not a constant.
114
+ wp_cache_set ( 'test ' , $ data , $ group , $ obj ->MONTH_IN_SECONDS ); // Bad - not a constant.
115
+ wp_cache_set ( 'test ' , $ data , $ group , $ obj ::MONTH_IN_SECONDS ); // Bad - not the WP constant.
116
+ wp_cache_set ( 'test ' , $ data , $ group , PluginNamespace \SubLevel \DAY_IN_SECONDS ); // Bad - not the WP constant.
117
+
118
+ // Test passing negative number as cache time.
119
+ wp_cache_set ( 'test ' , $ data , $ group , -300 ); // Bad.
120
+ wp_cache_add ( $ testing , $ data , 'test_group ' , -6 * MINUTE_IN_SECONDS ); // Bad.
121
+
122
+ // Test more complex logic in the parameter.
123
+ wp_cache_add ( $ key , $ data , '' , ($ toggle ? 200 : 400 ) ); // Manual inspection warning.
0 commit comments