Skip to content

Commit

Permalink
Default comment post id to 0 (WP 4.4 value). Check SERVER_NAME untras…
Browse files Browse the repository at this point in the history
…h/unspam also.
  • Loading branch information
gitlost committed Nov 18, 2017
1 parent c7b54bc commit 9f1575a
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 47 deletions.
153 changes: 113 additions & 40 deletions features/comment.feature
Original file line number Diff line number Diff line change
Expand Up @@ -156,22 +156,31 @@ 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:
"""
1
"""

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}`
Expand All @@ -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`
Expand All @@ -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`
Expand All @@ -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`
Expand All @@ -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`
Expand All @@ -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}.
"""
Expand All @@ -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`
Expand All @@ -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}
Expand All @@ -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`
13 changes: 6 additions & 7 deletions src/Comment_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function update( $args, $assoc_args ) {
* ---
*
* [--post_id=<post-id>]
* : Assign comments to a specific post. Defaults to post_id 1.
* : Assign comments to a specific post.
*
* [--format=<format>]
* : Render output in a particular format.
Expand All @@ -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 );

Expand All @@ -166,17 +166,14 @@ 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'];

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();
Expand Down Expand Up @@ -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';
}
}
Expand Down Expand Up @@ -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' );
}
Expand Down Expand Up @@ -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' );
}
Expand Down

0 comments on commit 9f1575a

Please sign in to comment.