Skip to content

Commit 9f1575a

Browse files
committed
Default comment post id to 0 (WP 4.4 value). Check SERVER_NAME untrash/unspam also.
1 parent c7b54bc commit 9f1575a

File tree

2 files changed

+119
-47
lines changed

2 files changed

+119
-47
lines changed

features/comment.feature

Lines changed: 113 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -156,22 +156,31 @@ Feature: Manage WordPress comments
156156
Given I run `wp comment create --comment_post_ID=1 --comment_approved=0 --porcelain`
157157
And save STDOUT as {COMMENT_ID}
158158

159+
# With site url set.
159160
When I run `wp comment approve {COMMENT_ID} --url=www.example.com`
160-
Then STDOUT should contain:
161+
Then STDOUT should be:
161162
"""
162-
Approved comment {COMMENT_ID}
163+
Success: Approved comment {COMMENT_ID}.
163164
"""
164165

166+
When I try the previous command again
167+
Then STDERR should be:
168+
"""
169+
Error: Could not update comment status
170+
"""
171+
And STDOUT should be empty
172+
And the return code should be 1
173+
165174
When I run `wp comment get --field=comment_approved {COMMENT_ID}`
166175
Then STDOUT should be:
167176
"""
168177
1
169178
"""
170179

171180
When I run `wp comment unapprove {COMMENT_ID} --url=www.example.com`
172-
Then STDOUT should contain:
181+
Then STDOUT should be:
173182
"""
174-
Unapproved comment {COMMENT_ID}
183+
Success: Unapproved comment {COMMENT_ID}.
175184
"""
176185

177186
When I run `wp comment get --field=comment_approved {COMMENT_ID}`
@@ -180,50 +189,40 @@ Feature: Manage WordPress comments
180189
0
181190
"""
182191

192+
# Without site url set.
183193
When I try `wp comment approve {COMMENT_ID}`
184-
Then STDOUT should contain:
194+
Then STDOUT should be:
185195
"""
186-
Approved comment {COMMENT_ID}
196+
Success: Approved comment {COMMENT_ID}.
187197
"""
188-
And STDERR should contain:
198+
And STDERR should be:
189199
"""
190-
Warning: `$_SERVER['SERVER_NAME']` not set. Setting to 'example.com'. Notification email sent to post author may appear to come from 'example.com'.
200+
Warning: Site url not set - defaulting to 'example.com'. Any notification emails sent to post author may appear to come from 'example.com'.
191201
"""
192202
And the return code should be 0
193203

194-
When I run `wp comment get --field=comment_approved {COMMENT_ID}`
195-
Then STDOUT should be:
196-
"""
197-
1
198-
"""
199-
200204
When I try `wp comment unapprove {COMMENT_ID}`
201-
Then STDOUT should contain:
205+
Then STDOUT should be:
202206
"""
203-
Unapproved comment {COMMENT_ID}
207+
Success: Unapproved comment {COMMENT_ID}.
204208
"""
205-
And STDERR should contain:
209+
And STDERR should be:
206210
"""
207-
Warning: `$_SERVER['SERVER_NAME']` not set. Setting to 'example.com'. Notification email sent to post author may appear to come from 'example.com'.
211+
Warning: Site url not set - defaulting to 'example.com'. Any notification emails sent to post author may appear to come from 'example.com'.
208212
"""
209213
And the return code should be 0
210214

211-
When I run `wp comment get --field=comment_approved {COMMENT_ID}`
212-
Then STDOUT should be:
213-
"""
214-
0
215-
"""
216-
217215
Scenario: Approving/unapproving comments with multidigit comment ID
218216
Given I run `wp comment delete $(wp comment list --field=ID)`
219217
And I run `wp comment generate --count=10 --quiet`
220-
And I run `wp comment create --porcelain`
218+
And I run `wp comment create --comment_post_ID=1 --porcelain`
221219
And save STDOUT as {COMMENT_ID}
222220

221+
# With site url set.
223222
When I run `wp comment unapprove {COMMENT_ID} --url=www.example.com`
224-
Then STDOUT should contain:
223+
Then STDOUT should be:
225224
"""
226-
Unapproved comment {COMMENT_ID}
225+
Success: Unapproved comment {COMMENT_ID}.
227226
"""
228227

229228
When I run `wp comment list --format=count --status=approve`
@@ -233,9 +232,9 @@ Feature: Manage WordPress comments
233232
"""
234233

235234
When I run `wp comment approve {COMMENT_ID} --url=www.example.com`
236-
Then STDOUT should contain:
235+
Then STDOUT should be:
237236
"""
238-
Approved comment {COMMENT_ID}
237+
Success: Approved comment {COMMENT_ID}.
239238
"""
240239

241240
When I run `wp comment list --format=count --status=approve`
@@ -244,16 +243,40 @@ Feature: Manage WordPress comments
244243
11
245244
"""
246245

246+
# Without site url set.
247+
When I try `wp comment unapprove {COMMENT_ID}`
248+
Then STDOUT should be:
249+
"""
250+
Success: Unapproved comment {COMMENT_ID}.
251+
"""
252+
And STDERR should be:
253+
"""
254+
Warning: Site url not set - defaulting to 'example.com'. Any notification emails sent to post author may appear to come from 'example.com'.
255+
"""
256+
And the return code should be 0
257+
258+
When I try `wp comment approve {COMMENT_ID}`
259+
Then STDOUT should be:
260+
"""
261+
Success: Approved comment {COMMENT_ID}.
262+
"""
263+
And STDERR should be:
264+
"""
265+
Warning: Site url not set - defaulting to 'example.com'. Any notification emails sent to post author may appear to come from 'example.com'.
266+
"""
267+
And the return code should be 0
268+
247269
Scenario: Spam/unspam comments with multidigit comment ID
248270
Given I run `wp comment delete $(wp comment list --field=ID)`
249271
And I run `wp comment generate --count=10 --quiet`
250-
And I run `wp comment create --porcelain`
272+
And I run `wp comment create --comment_post_ID=1 --porcelain`
251273
And save STDOUT as {COMMENT_ID}
252274

275+
# With site url set.
253276
When I run `wp comment spam {COMMENT_ID}`
254-
Then STDOUT should contain:
277+
Then STDOUT should be:
255278
"""
256-
Marked as spam comment {COMMENT_ID}.
279+
Success: Marked as spam comment {COMMENT_ID}.
257280
"""
258281

259282
When I run `wp comment list --format=count --status=spam`
@@ -262,10 +285,10 @@ Feature: Manage WordPress comments
262285
1
263286
"""
264287

265-
When I run `wp comment unspam {COMMENT_ID}`
266-
Then STDOUT should contain:
288+
When I run `wp comment unspam {COMMENT_ID} --url=www.example.com`
289+
Then STDOUT should be:
267290
"""
268-
Unspammed comment {COMMENT_ID}.
291+
Success: Unspammed comment {COMMENT_ID}.
269292
"""
270293

271294
When I run `wp comment list --format=count --status=spam`
@@ -274,14 +297,33 @@ Feature: Manage WordPress comments
274297
0
275298
"""
276299

300+
# Without site url set.
301+
When I run `wp comment spam {COMMENT_ID}`
302+
Then STDOUT should be:
303+
"""
304+
Success: Marked as spam comment {COMMENT_ID}.
305+
"""
306+
307+
When I try `wp comment unspam {COMMENT_ID}`
308+
Then STDOUT should be:
309+
"""
310+
Success: Unspammed comment {COMMENT_ID}.
311+
"""
312+
And STDERR should be:
313+
"""
314+
Warning: Site url not set - defaulting to 'example.com'. Any notification emails sent to post author may appear to come from 'example.com'.
315+
"""
316+
And the return code should be 0
317+
277318
Scenario: Trash/untrash comments with multidigit comment ID
278319
Given I run `wp comment delete $(wp comment list --field=ID) --force`
279320
And I run `wp comment generate --count=10 --quiet`
280-
And I run `wp comment create --porcelain`
321+
And I run `wp comment create --comment_post_ID=1 --porcelain`
281322
And save STDOUT as {COMMENT_ID}
282323

324+
# With site url set.
283325
When I run `wp comment trash {COMMENT_ID}`
284-
Then STDOUT should contain:
326+
Then STDOUT should be:
285327
"""
286328
Success: Trashed comment {COMMENT_ID}.
287329
"""
@@ -292,10 +334,10 @@ Feature: Manage WordPress comments
292334
1
293335
"""
294336

295-
When I run `wp comment untrash {COMMENT_ID}`
296-
Then STDOUT should contain:
337+
When I run `wp comment untrash {COMMENT_ID} --url=www.example.com`
338+
Then STDOUT should be:
297339
"""
298-
Untrashed comment {COMMENT_ID}.
340+
Success: Untrashed comment {COMMENT_ID}.
299341
"""
300342

301343
When I run `wp comment list --format=count --status=trash`
@@ -304,6 +346,24 @@ Feature: Manage WordPress comments
304346
0
305347
"""
306348

349+
# Without site url set.
350+
When I run `wp comment trash {COMMENT_ID}`
351+
Then STDOUT should be:
352+
"""
353+
Success: Trashed comment {COMMENT_ID}.
354+
"""
355+
356+
When I try `wp comment untrash {COMMENT_ID}`
357+
Then STDOUT should be:
358+
"""
359+
Success: Untrashed comment {COMMENT_ID}.
360+
"""
361+
And STDERR should be:
362+
"""
363+
Warning: Site url not set - defaulting to 'example.com'. Any notification emails sent to post author may appear to come from 'example.com'.
364+
"""
365+
And the return code should be 0
366+
307367
Scenario: Make sure WordPress receives the slashed data it expects
308368
When I run `wp comment create --comment_content='My\Comment' --porcelain`
309369
Then save STDOUT as {COMMENT_ID}
@@ -322,3 +382,16 @@ Feature: Manage WordPress comments
322382
"""
323383
My\New\Comment
324384
"""
385+
386+
@require-wp-4.4
387+
Scenario: Approving/unapproving/unspamming/untrashing (approved) comments with no comment post id should not produce PHP notices for WP >= 4.4
388+
Given I run `wp comment create --comment_approved=0 --porcelain`
389+
And save STDOUT as {COMMENT_ID}
390+
When I run `wp comment approve {COMMENT_ID} --url=www.example.com`
391+
And I run `wp comment unapprove {COMMENT_ID} --url=www.example.com`
392+
393+
Given I run `wp comment approve {COMMENT_ID} --url=www.example.com`
394+
When I run `wp comment spam {COMMENT_ID} --url=www.example.com`
395+
And I run `wp comment unspam {COMMENT_ID} --url=www.example.com`
396+
And I run `wp comment trash {COMMENT_ID} --url=www.example.com`
397+
And I run `wp comment untrash {COMMENT_ID} --url=www.example.com`

src/Comment_Command.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public function update( $args, $assoc_args ) {
128128
* ---
129129
*
130130
* [--post_id=<post-id>]
131-
* : Assign comments to a specific post. Defaults to post_id 1.
131+
* : Assign comments to a specific post.
132132
*
133133
* [--format=<format>]
134134
* : Render output in a particular format.
@@ -155,7 +155,7 @@ public function generate( $args, $assoc_args ) {
155155

156156
$defaults = array(
157157
'count' => 100,
158-
'post_id' => null,
158+
'post_id' => 0,
159159
);
160160
$assoc_args = array_merge( $defaults, $assoc_args );
161161

@@ -166,17 +166,14 @@ public function generate( $args, $assoc_args ) {
166166
$notify = \WP_CLI\Utils\make_progress_bar( 'Generating comments', $assoc_args['count'] );
167167
}
168168

169-
// Make sure comment_post_ID set for older WP versions else get undefined PHP notice.
170-
$comment_post_ID = isset( $assoc_args['post_id'] ) ? $assoc_args['post_id'] : 1;
171-
172169
$comment_count = wp_count_comments();
173170
$total = (int )$comment_count->total_comments;
174171
$limit = $total + $assoc_args['count'];
175172

176173
for ( $i = $total; $i < $limit; $i++ ) {
177174
$comment_id = wp_insert_comment( array(
178175
'comment_content' => "Comment {$i}",
179-
'comment_post_ID' => $comment_post_ID,
176+
'comment_post_ID' => $assoc_args['post_id'],
180177
) );
181178
if ( 'progress' === $format ) {
182179
$notify->tick();
@@ -438,7 +435,7 @@ private function set_status( $args, $status, $success ) {
438435
*/
439436
private function check_server_name() {
440437
if ( empty( $_SERVER['SERVER_NAME'] ) ) {
441-
WP_CLI::warning( '`$_SERVER[\'SERVER_NAME\']` not set. Setting to \'example.com\'. Notification email sent to post author may appear to come from \'example.com\'.' );
438+
WP_CLI::warning( 'Site url not set - defaulting to \'example.com\'. Any notification emails sent to post author may appear to come from \'example.com\'.' );
442439
$_SERVER['SERVER_NAME'] = 'example.com';
443440
}
444441
}
@@ -478,6 +475,7 @@ public function trash( $args, $assoc_args ) {
478475
* Success: Untrashed comment 1337.
479476
*/
480477
public function untrash( $args, $assoc_args ) {
478+
$this->check_server_name();
481479
foreach( $args as $id ) {
482480
$this->call( $id, __FUNCTION__, 'Untrashed', 'Failed untrashing' );
483481
}
@@ -518,6 +516,7 @@ public function spam( $args, $assoc_args ) {
518516
* Success: Unspammed comment 1337.
519517
*/
520518
public function unspam( $args, $assoc_args ) {
519+
$this->check_server_name();
521520
foreach( $args as $id ) {
522521
$this->call( $id, __FUNCTION__, 'Unspammed', 'Failed unspamming' );
523522
}

0 commit comments

Comments
 (0)