5
5
* @package test_cases\logging
6
6
*/
7
7
class ActionScheduler_wpCommentLogger_Test extends ActionScheduler_UnitTestCase {
8
+
9
+ /** @var bool */
8
10
private $ use_comment_logger ;
9
11
10
12
public function test_default_logger () {
@@ -19,10 +21,10 @@ public function test_default_logger() {
19
21
20
22
public function test_add_log_entry () {
21
23
$ action_id = as_schedule_single_action ( time (), 'a hook ' );
22
- $ logger = ActionScheduler::logger ();
23
- $ message = 'Logging that something happened ' ;
24
- $ log_id = $ logger ->log ( $ action_id , $ message );
25
- $ entry = $ logger ->get_entry ( $ log_id );
24
+ $ logger = ActionScheduler::logger ();
25
+ $ message = 'Logging that something happened ' ;
26
+ $ log_id = $ logger ->log ( $ action_id , $ message );
27
+ $ entry = $ logger ->get_entry ( $ log_id );
26
28
27
29
$ this ->assertEquals ( $ action_id , $ entry ->get_action_id () );
28
30
$ this ->assertEquals ( $ message , $ entry ->get_message () );
@@ -39,109 +41,132 @@ public function test_add_log_datetime() {
39
41
$ this ->assertEquals ( $ action_id , $ entry ->get_action_id () );
40
42
$ this ->assertEquals ( $ message , $ entry ->get_message () );
41
43
42
- $ date = new ActionScheduler_DateTime ( 'now ' , new DateTimeZone ( 'UTC ' ) );
43
- $ log_id = $ logger ->log ( $ action_id , $ message , $ date );
44
- $ entry = $ logger ->get_entry ( $ log_id );
44
+ $ date = new ActionScheduler_DateTime ( 'now ' , new DateTimeZone ( 'UTC ' ) );
45
+ $ log_id = $ logger ->log ( $ action_id , $ message , $ date );
46
+ $ entry = $ logger ->get_entry ( $ log_id );
45
47
46
48
$ this ->assertEquals ( $ action_id , $ entry ->get_action_id () );
47
49
$ this ->assertEquals ( $ message , $ entry ->get_message () );
48
50
}
49
51
50
52
public function test_erroneous_entry_id () {
51
- $ comment = wp_insert_comment (array (
52
- 'comment_post_ID ' => 1 ,
53
- 'comment_author ' => 'test ' ,
54
- 'comment_content ' => 'this is not a log entry ' ,
55
- ));
53
+ $ comment = wp_insert_comment (
54
+ array (
55
+ 'comment_post_ID ' => 1 ,
56
+ 'comment_author ' => 'test ' ,
57
+ 'comment_content ' => 'this is not a log entry ' ,
58
+ )
59
+ );
60
+
56
61
$ logger = ActionScheduler::logger ();
57
- $ entry = $ logger ->get_entry ( $ comment );
62
+ $ entry = $ logger ->get_entry ( $ comment );
63
+
58
64
$ this ->assertEquals ( '' , $ entry ->get_action_id () );
59
65
$ this ->assertEquals ( '' , $ entry ->get_message () );
60
66
}
61
67
62
68
public function test_storage_comments () {
63
69
$ action_id = as_schedule_single_action ( time (), 'a hook ' );
64
- $ logger = ActionScheduler::logger ();
65
- $ logs = $ logger ->get_logs ( $ action_id );
66
- $ expected = new ActionScheduler_LogEntry ( $ action_id , 'action created ' );
67
- $ this ->assertTrue ( in_array ( $ this ->log_entry_to_array ( $ expected ) , $ this ->log_entry_to_array ( $ logs ) ) );
70
+ $ logger = ActionScheduler::logger ();
71
+ $ logs = $ logger ->get_logs ( $ action_id );
72
+ $ expected = new ActionScheduler_LogEntry ( $ action_id , 'action created ' );
73
+
74
+ // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
75
+ $ this ->assertTrue ( in_array ( $ this ->log_entry_to_array ( $ expected ), $ this ->log_entry_to_array ( $ logs ) ) );
68
76
}
69
77
70
78
protected function log_entry_to_array ( $ logs ) {
71
79
if ( $ logs instanceof ActionScheduler_LogEntry ) {
72
- return array ( 'action_id ' => $ logs ->get_action_id (), 'message ' => $ logs ->get_message () );
80
+ return array (
81
+ 'action_id ' => $ logs ->get_action_id (),
82
+ 'message ' => $ logs ->get_message (),
83
+ );
73
84
}
74
85
75
- foreach ( $ logs as $ id => $ log ) {
76
- $ logs [ $ id ] = array ( 'action_id ' => $ log ->get_action_id (), 'message ' => $ log ->get_message () );
86
+ foreach ( $ logs as $ id => $ log ) {
87
+ $ logs [ $ id ] = array (
88
+ 'action_id ' => $ log ->get_action_id (),
89
+ 'message ' => $ log ->get_message (),
90
+ );
77
91
}
78
92
79
93
return $ logs ;
80
94
}
81
95
82
96
public function test_execution_comments () {
83
97
$ action_id = as_schedule_single_action ( time (), ActionScheduler_Callbacks::HOOK_WITH_CALLBACK );
84
- $ logger = ActionScheduler::logger ();
85
- $ started = new ActionScheduler_LogEntry ( $ action_id , 'action started via Unit Tests ' );
86
- $ finished = new ActionScheduler_LogEntry ( $ action_id , 'action complete via Unit Tests ' );
98
+ $ logger = ActionScheduler::logger ();
99
+ $ started = new ActionScheduler_LogEntry ( $ action_id , 'action started via Unit Tests ' );
100
+ $ finished = new ActionScheduler_LogEntry ( $ action_id , 'action complete via Unit Tests ' );
87
101
88
102
$ runner = ActionScheduler_Mocker::get_queue_runner ();
89
103
$ runner ->run ( 'Unit Tests ' );
90
104
91
105
$ logs = $ logger ->get_logs ( $ action_id );
106
+
107
+ // phpcs:disable WordPress.PHP.StrictInArray.MissingTrueStrict
92
108
$ this ->assertTrue ( in_array ( $ this ->log_entry_to_array ( $ started ), $ this ->log_entry_to_array ( $ logs ) ) );
93
109
$ this ->assertTrue ( in_array ( $ this ->log_entry_to_array ( $ finished ), $ this ->log_entry_to_array ( $ logs ) ) );
110
+ // phpcs:enable
94
111
}
95
112
96
113
public function test_failed_execution_comments () {
97
- $ hook = md5 (rand ());
98
- add_action ( $ hook , array ( $ this , '_a_hook_callback_that_throws_an_exception ' ) );
114
+ $ hook = md5 ( wp_rand () );
115
+ add_action ( $ hook , array ( $ this , 'a_hook_callback_that_throws_an_exception ' ) );
116
+
99
117
$ action_id = as_schedule_single_action ( time (), $ hook );
100
- $ logger = ActionScheduler::logger ();
101
- $ started = new ActionScheduler_LogEntry ( $ action_id , 'action started via Unit Tests ' );
102
- $ finished = new ActionScheduler_LogEntry ( $ action_id , 'action complete via Unit Tests ' );
103
- $ failed = new ActionScheduler_LogEntry ( $ action_id , 'action failed via Unit Tests: Execution failed ' );
118
+ $ logger = ActionScheduler::logger ();
119
+ $ started = new ActionScheduler_LogEntry ( $ action_id , 'action started via Unit Tests ' );
120
+ $ finished = new ActionScheduler_LogEntry ( $ action_id , 'action complete via Unit Tests ' );
121
+ $ failed = new ActionScheduler_LogEntry ( $ action_id , 'action failed via Unit Tests: Execution failed ' );
104
122
105
123
$ runner = ActionScheduler_Mocker::get_queue_runner ();
106
124
$ runner ->run ( 'Unit Tests ' );
107
125
108
126
$ logs = $ logger ->get_logs ( $ action_id );
127
+
128
+ // phpcs:disable WordPress.PHP.StrictInArray.MissingTrueStrict
109
129
$ this ->assertTrue ( in_array ( $ this ->log_entry_to_array ( $ started ), $ this ->log_entry_to_array ( $ logs ) ) );
110
130
$ this ->assertFalse ( in_array ( $ this ->log_entry_to_array ( $ finished ), $ this ->log_entry_to_array ( $ logs ) ) );
111
131
$ this ->assertTrue ( in_array ( $ this ->log_entry_to_array ( $ failed ), $ this ->log_entry_to_array ( $ logs ) ) );
132
+ // phpcs:enable
112
133
}
113
134
114
135
public function test_failed_schedule_next_instance_comments () {
115
- $ action_id = rand ();
136
+ $ action_id = wp_rand ();
116
137
$ logger = ActionScheduler::logger ();
117
138
$ log_entry = new ActionScheduler_LogEntry ( $ action_id , 'There was a failure scheduling the next instance of this action: Execution failed ' );
118
139
119
140
try {
120
- $ this ->_a_hook_callback_that_throws_an_exception ();
141
+ $ this ->a_hook_callback_that_throws_an_exception ();
121
142
} catch ( Exception $ e ) {
122
143
do_action ( 'action_scheduler_failed_to_schedule_next_instance ' , $ action_id , $ e , new ActionScheduler_Action ( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK ) );
123
144
}
124
145
125
146
$ logs = $ logger ->get_logs ( $ action_id );
147
+
148
+ // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
126
149
$ this ->assertTrue ( in_array ( $ this ->log_entry_to_array ( $ log_entry ), $ this ->log_entry_to_array ( $ logs ) ) );
127
150
}
128
151
129
152
public function test_fatal_error_comments () {
130
- $ hook = md5 (rand () );
153
+ $ hook = md5 ( wp_rand () );
131
154
$ action_id = as_schedule_single_action ( time (), $ hook );
132
- $ logger = ActionScheduler::logger ();
133
- do_action ( ' action_scheduler_unexpected_shutdown ' , $ action_id , array (
134
- 'type ' => E_ERROR ,
155
+ $ logger = ActionScheduler::logger ();
156
+ $ args = array (
157
+ 'type ' => E_ERROR ,
135
158
'message ' => 'Test error ' ,
136
- 'file ' => __FILE__ ,
137
- 'line ' => __LINE__ ,
138
- )) ;
159
+ 'file ' => __FILE__ ,
160
+ 'line ' => __LINE__ ,
161
+ );
139
162
140
- $ logs = $ logger ->get_logs ( $ action_id );
141
- $ found_log = FALSE ;
163
+ do_action ( 'action_scheduler_unexpected_shutdown ' , $ action_id , $ args );
164
+
165
+ $ logs = $ logger ->get_logs ( $ action_id );
166
+ $ found_log = false ;
142
167
foreach ( $ logs as $ l ) {
143
168
if ( strpos ( $ l ->get_message (), 'unexpected shutdown ' ) === 0 ) {
144
- $ found_log = TRUE ;
169
+ $ found_log = true ;
145
170
}
146
171
}
147
172
$ this ->assertTrue ( $ found_log , 'Unexpected shutdown log not found ' );
@@ -150,14 +175,17 @@ public function test_fatal_error_comments() {
150
175
public function test_canceled_action_comments () {
151
176
$ action_id = as_schedule_single_action ( time (), 'a hook ' );
152
177
as_unschedule_action ( 'a hook ' );
153
- $ logger = ActionScheduler::logger ();
154
- $ logs = $ logger ->get_logs ( $ action_id );
178
+
179
+ $ logger = ActionScheduler::logger ();
180
+ $ logs = $ logger ->get_logs ( $ action_id );
155
181
$ expected = new ActionScheduler_LogEntry ( $ action_id , 'action canceled ' );
182
+
183
+ // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
156
184
$ this ->assertTrue ( in_array ( $ this ->log_entry_to_array ( $ expected ), $ this ->log_entry_to_array ( $ logs ) ) );
157
185
}
158
186
159
- public function _a_hook_callback_that_throws_an_exception () {
160
- throw new RuntimeException ('Execution failed ' );
187
+ public function a_hook_callback_that_throws_an_exception () {
188
+ throw new RuntimeException ( 'Execution failed ' );
161
189
}
162
190
163
191
public function test_filtering_of_get_comments () {
@@ -166,43 +194,44 @@ public function test_filtering_of_get_comments() {
166
194
return ;
167
195
}
168
196
169
- $ post_id = $ this ->factory ->post ->create_object (array (
170
- ' post_title ' => __FUNCTION__ ,
171
- ));
172
- $ comment_id = $ this -> factory -> comment -> create_object ( array (
173
- ' comment_post_ID ' => $ post_id ,
174
- ' comment_author ' => __CLASS__ ,
175
- ' comment_content ' => __FUNCTION__ ,
176
- )) ;
197
+ $ post_id = $ this ->factory ->post ->create_object ( array ( ' post_title ' => __FUNCTION__ ) );
198
+ $ comment_id = $ this -> factory -> comment -> create_object (
199
+ array (
200
+ ' comment_post_ID ' => $ post_id ,
201
+ ' comment_author ' => __CLASS__ ,
202
+ ' comment_content ' => __FUNCTION__ ,
203
+ )
204
+ );
177
205
178
- // Verify that we're getting the expected comment before we add logging comments
206
+ // Verify that we're getting the expected comment before we add logging comments.
179
207
$ comments = get_comments ();
180
208
$ this ->assertCount ( 1 , $ comments );
181
209
$ this ->assertEquals ( $ comment_id , $ comments [0 ]->comment_ID );
182
210
183
-
184
211
$ action_id = as_schedule_single_action ( time (), 'a hook ' );
185
- $ logger = ActionScheduler::logger ();
186
- $ message = 'Logging that something happened ' ;
187
- $ log_id = $ logger ->log ( $ action_id , $ message );
188
-
212
+ $ logger = ActionScheduler::logger ();
213
+ $ message = 'Logging that something happened ' ;
214
+ $ log_id = $ logger ->log ( $ action_id , $ message );
189
215
190
- // Verify that logging comments are excluded from general comment queries
216
+ // Verify that logging comments are excluded from general comment queries.
191
217
$ comments = get_comments ();
192
218
$ this ->assertCount ( 1 , $ comments );
193
219
$ this ->assertEquals ( $ comment_id , $ comments [0 ]->comment_ID );
194
220
195
- // Verify that logging comments are returned when asking for them specifically
196
- $ comments = get_comments (array (
197
- 'type ' => ActionScheduler_wpCommentLogger::TYPE ,
198
- ));
199
- // Expecting two: one when the action is created, another when we added our custom log
221
+ // Verify that logging comments are returned when asking for them specifically.
222
+ $ comments = get_comments (
223
+ array (
224
+ 'type ' => ActionScheduler_wpCommentLogger::TYPE ,
225
+ )
226
+ );
227
+
228
+ // Expecting two: one when the action is created, another when we added our custom log.
200
229
$ this ->assertCount ( 2 , $ comments );
201
- $ this ->assertContains ( $ log_id , wp_list_pluck ($ comments , 'comment_ID ' ) );
230
+ $ this ->assertContains ( $ log_id , wp_list_pluck ( $ comments , 'comment_ID ' ) );
202
231
}
203
232
204
233
private function using_comment_logger () {
205
- if ( null === $ this ->use_comment_logger ) {
234
+ if ( is_null ( $ this ->use_comment_logger ) ) {
206
235
$ this ->use_comment_logger = ! ActionScheduler_DataController::dependencies_met ();
207
236
}
208
237
0 commit comments