From 9f1575a5c8157e2fabb94e74fa9567f777b1587d Mon Sep 17 00:00:00 2001 From: gitlost Date: Sat, 18 Nov 2017 14:13:22 +0000 Subject: [PATCH] Default comment post id to 0 (WP 4.4 value). Check SERVER_NAME untrash/unspam also. --- features/comment.feature | 153 +++++++++++++++++++++++++++++---------- src/Comment_Command.php | 13 ++-- 2 files changed, 119 insertions(+), 47 deletions(-) diff --git a/features/comment.feature b/features/comment.feature index 5b32354af..e09c3feb2 100644 --- a/features/comment.feature +++ b/features/comment.feature @@ -156,12 +156,21 @@ Feature: Manage WordPress comments Given I run `wp comment create --comment_post_ID=1 --comment_approved=0 --porcelain` And save STDOUT as {COMMENT_ID} + # With site url set. When I run `wp comment approve {COMMENT_ID} --url=www.example.com` - Then STDOUT should contain: + Then STDOUT should be: """ - Approved comment {COMMENT_ID} + Success: Approved comment {COMMENT_ID}. """ + When I try the previous command again + Then STDERR should be: + """ + Error: Could not update comment status + """ + And STDOUT should be empty + And the return code should be 1 + When I run `wp comment get --field=comment_approved {COMMENT_ID}` Then STDOUT should be: """ @@ -169,9 +178,9 @@ Feature: Manage WordPress comments """ When I run `wp comment unapprove {COMMENT_ID} --url=www.example.com` - Then STDOUT should contain: + Then STDOUT should be: """ - Unapproved comment {COMMENT_ID} + Success: Unapproved comment {COMMENT_ID}. """ When I run `wp comment get --field=comment_approved {COMMENT_ID}` @@ -180,50 +189,40 @@ Feature: Manage WordPress comments 0 """ + # Without site url set. When I try `wp comment approve {COMMENT_ID}` - Then STDOUT should contain: + Then STDOUT should be: """ - Approved comment {COMMENT_ID} + Success: Approved comment {COMMENT_ID}. """ - And STDERR should contain: + And STDERR should be: """ - Warning: `$_SERVER['SERVER_NAME']` not set. Setting to 'example.com'. Notification email sent to post author may appear to come from 'example.com'. + Warning: Site url not set - defaulting to 'example.com'. Any notification emails sent to post author may appear to come from 'example.com'. """ And the return code should be 0 - When I run `wp comment get --field=comment_approved {COMMENT_ID}` - Then STDOUT should be: - """ - 1 - """ - When I try `wp comment unapprove {COMMENT_ID}` - Then STDOUT should contain: + Then STDOUT should be: """ - Unapproved comment {COMMENT_ID} + Success: Unapproved comment {COMMENT_ID}. """ - And STDERR should contain: + And STDERR should be: """ - Warning: `$_SERVER['SERVER_NAME']` not set. Setting to 'example.com'. Notification email sent to post author may appear to come from 'example.com'. + Warning: Site url not set - defaulting to 'example.com'. Any notification emails sent to post author may appear to come from 'example.com'. """ And the return code should be 0 - When I run `wp comment get --field=comment_approved {COMMENT_ID}` - Then STDOUT should be: - """ - 0 - """ - Scenario: Approving/unapproving comments with multidigit comment ID Given I run `wp comment delete $(wp comment list --field=ID)` And I run `wp comment generate --count=10 --quiet` - And I run `wp comment create --porcelain` + And I run `wp comment create --comment_post_ID=1 --porcelain` And save STDOUT as {COMMENT_ID} + # With site url set. When I run `wp comment unapprove {COMMENT_ID} --url=www.example.com` - Then STDOUT should contain: + Then STDOUT should be: """ - Unapproved comment {COMMENT_ID} + Success: Unapproved comment {COMMENT_ID}. """ When I run `wp comment list --format=count --status=approve` @@ -233,9 +232,9 @@ Feature: Manage WordPress comments """ When I run `wp comment approve {COMMENT_ID} --url=www.example.com` - Then STDOUT should contain: + Then STDOUT should be: """ - Approved comment {COMMENT_ID} + Success: Approved comment {COMMENT_ID}. """ When I run `wp comment list --format=count --status=approve` @@ -244,16 +243,40 @@ Feature: Manage WordPress comments 11 """ + # Without site url set. + When I try `wp comment unapprove {COMMENT_ID}` + Then STDOUT should be: + """ + Success: Unapproved comment {COMMENT_ID}. + """ + And STDERR should be: + """ + Warning: Site url not set - defaulting to 'example.com'. Any notification emails sent to post author may appear to come from 'example.com'. + """ + And the return code should be 0 + + When I try `wp comment approve {COMMENT_ID}` + Then STDOUT should be: + """ + Success: Approved comment {COMMENT_ID}. + """ + And STDERR should be: + """ + Warning: Site url not set - defaulting to 'example.com'. Any notification emails sent to post author may appear to come from 'example.com'. + """ + And the return code should be 0 + Scenario: Spam/unspam comments with multidigit comment ID Given I run `wp comment delete $(wp comment list --field=ID)` And I run `wp comment generate --count=10 --quiet` - And I run `wp comment create --porcelain` + And I run `wp comment create --comment_post_ID=1 --porcelain` And save STDOUT as {COMMENT_ID} + # With site url set. When I run `wp comment spam {COMMENT_ID}` - Then STDOUT should contain: + Then STDOUT should be: """ - Marked as spam comment {COMMENT_ID}. + Success: Marked as spam comment {COMMENT_ID}. """ When I run `wp comment list --format=count --status=spam` @@ -262,10 +285,10 @@ Feature: Manage WordPress comments 1 """ - When I run `wp comment unspam {COMMENT_ID}` - Then STDOUT should contain: + When I run `wp comment unspam {COMMENT_ID} --url=www.example.com` + Then STDOUT should be: """ - Unspammed comment {COMMENT_ID}. + Success: Unspammed comment {COMMENT_ID}. """ When I run `wp comment list --format=count --status=spam` @@ -274,14 +297,33 @@ Feature: Manage WordPress comments 0 """ + # Without site url set. + When I run `wp comment spam {COMMENT_ID}` + Then STDOUT should be: + """ + Success: Marked as spam comment {COMMENT_ID}. + """ + + When I try `wp comment unspam {COMMENT_ID}` + Then STDOUT should be: + """ + Success: Unspammed comment {COMMENT_ID}. + """ + And STDERR should be: + """ + Warning: Site url not set - defaulting to 'example.com'. Any notification emails sent to post author may appear to come from 'example.com'. + """ + And the return code should be 0 + Scenario: Trash/untrash comments with multidigit comment ID Given I run `wp comment delete $(wp comment list --field=ID) --force` And I run `wp comment generate --count=10 --quiet` - And I run `wp comment create --porcelain` + And I run `wp comment create --comment_post_ID=1 --porcelain` And save STDOUT as {COMMENT_ID} + # With site url set. When I run `wp comment trash {COMMENT_ID}` - Then STDOUT should contain: + Then STDOUT should be: """ Success: Trashed comment {COMMENT_ID}. """ @@ -292,10 +334,10 @@ Feature: Manage WordPress comments 1 """ - When I run `wp comment untrash {COMMENT_ID}` - Then STDOUT should contain: + When I run `wp comment untrash {COMMENT_ID} --url=www.example.com` + Then STDOUT should be: """ - Untrashed comment {COMMENT_ID}. + Success: Untrashed comment {COMMENT_ID}. """ When I run `wp comment list --format=count --status=trash` @@ -304,6 +346,24 @@ Feature: Manage WordPress comments 0 """ + # Without site url set. + When I run `wp comment trash {COMMENT_ID}` + Then STDOUT should be: + """ + Success: Trashed comment {COMMENT_ID}. + """ + + When I try `wp comment untrash {COMMENT_ID}` + Then STDOUT should be: + """ + Success: Untrashed comment {COMMENT_ID}. + """ + And STDERR should be: + """ + Warning: Site url not set - defaulting to 'example.com'. Any notification emails sent to post author may appear to come from 'example.com'. + """ + And the return code should be 0 + Scenario: Make sure WordPress receives the slashed data it expects When I run `wp comment create --comment_content='My\Comment' --porcelain` Then save STDOUT as {COMMENT_ID} @@ -322,3 +382,16 @@ Feature: Manage WordPress comments """ My\New\Comment """ + + @require-wp-4.4 + Scenario: Approving/unapproving/unspamming/untrashing (approved) comments with no comment post id should not produce PHP notices for WP >= 4.4 + Given I run `wp comment create --comment_approved=0 --porcelain` + And save STDOUT as {COMMENT_ID} + When I run `wp comment approve {COMMENT_ID} --url=www.example.com` + And I run `wp comment unapprove {COMMENT_ID} --url=www.example.com` + + Given I run `wp comment approve {COMMENT_ID} --url=www.example.com` + When I run `wp comment spam {COMMENT_ID} --url=www.example.com` + And I run `wp comment unspam {COMMENT_ID} --url=www.example.com` + And I run `wp comment trash {COMMENT_ID} --url=www.example.com` + And I run `wp comment untrash {COMMENT_ID} --url=www.example.com` diff --git a/src/Comment_Command.php b/src/Comment_Command.php index 8c1b9fae1..5625ad43f 100644 --- a/src/Comment_Command.php +++ b/src/Comment_Command.php @@ -128,7 +128,7 @@ public function update( $args, $assoc_args ) { * --- * * [--post_id=] - * : Assign comments to a specific post. Defaults to post_id 1. + * : Assign comments to a specific post. * * [--format=] * : Render output in a particular format. @@ -155,7 +155,7 @@ public function generate( $args, $assoc_args ) { $defaults = array( 'count' => 100, - 'post_id' => null, + 'post_id' => 0, ); $assoc_args = array_merge( $defaults, $assoc_args ); @@ -166,9 +166,6 @@ public function generate( $args, $assoc_args ) { $notify = \WP_CLI\Utils\make_progress_bar( 'Generating comments', $assoc_args['count'] ); } - // Make sure comment_post_ID set for older WP versions else get undefined PHP notice. - $comment_post_ID = isset( $assoc_args['post_id'] ) ? $assoc_args['post_id'] : 1; - $comment_count = wp_count_comments(); $total = (int )$comment_count->total_comments; $limit = $total + $assoc_args['count']; @@ -176,7 +173,7 @@ public function generate( $args, $assoc_args ) { for ( $i = $total; $i < $limit; $i++ ) { $comment_id = wp_insert_comment( array( 'comment_content' => "Comment {$i}", - 'comment_post_ID' => $comment_post_ID, + 'comment_post_ID' => $assoc_args['post_id'], ) ); if ( 'progress' === $format ) { $notify->tick(); @@ -438,7 +435,7 @@ private function set_status( $args, $status, $success ) { */ private function check_server_name() { if ( empty( $_SERVER['SERVER_NAME'] ) ) { - 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\'.' ); + 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\'.' ); $_SERVER['SERVER_NAME'] = 'example.com'; } } @@ -478,6 +475,7 @@ public function trash( $args, $assoc_args ) { * Success: Untrashed comment 1337. */ public function untrash( $args, $assoc_args ) { + $this->check_server_name(); foreach( $args as $id ) { $this->call( $id, __FUNCTION__, 'Untrashed', 'Failed untrashing' ); } @@ -518,6 +516,7 @@ public function spam( $args, $assoc_args ) { * Success: Unspammed comment 1337. */ public function unspam( $args, $assoc_args ) { + $this->check_server_name(); foreach( $args as $id ) { $this->call( $id, __FUNCTION__, 'Unspammed', 'Failed unspamming' ); }