Make WordPress Core

Ticket #30946: wp_update_comment.patch

File wp_update_comment.patch, 2.4 KB (added by tyxla, 10 years ago)

Allowing the comment_post_ID to be updated, and if passed, make sure that it corresponds to a valid post ID. Adding a unit test assertion in Tests_Comment::test_wp_update_comment() for that functionality.

  • src/wp-includes/comment.php

     
    22022202        if ( empty( $comment ) ) {
    22032203                return 0;
    22042204        }
     2205
     2206        // Make sure that the comment post ID is valid (if specified).
     2207        if ( isset( $commentarr['comment_post_ID'] ) && !get_post($commentarr['comment_post_ID']) ) {
     2208                return 0;
     2209        }
     2210
    22052211        // Escape data pulled from DB.
    22062212        $comment = wp_slash($comment);
    22072213
     
    22362242
    22372243        $comment_ID = $data['comment_ID'];
    22382244        $comment_post_ID = $data['comment_post_ID'];
    2239         $keys = array( 'comment_content', 'comment_author', 'comment_author_email', 'comment_approved', 'comment_karma', 'comment_author_url', 'comment_date', 'comment_date_gmt', 'comment_parent' );
     2245        $keys = array( 'comment_post_ID', 'comment_content', 'comment_author', 'comment_author_email', 'comment_approved', 'comment_karma', 'comment_author_url', 'comment_date', 'comment_date_gmt', 'comment_parent' );
    22402246        $data = wp_array_slice_assoc( $data, $keys );
    22412247        $rval = $wpdb->update( $wpdb->comments, $data, compact( 'comment_ID' ) );
    22422248
  • tests/phpunit/tests/comment.php

     
    66class Tests_Comment extends WP_UnitTestCase {
    77        function test_wp_update_comment() {
    88                $post = $this->factory->post->create_and_get( array( 'post_title' => 'some-post', 'post_type' => 'post' ) );
     9                $post2 = $this->factory->post->create_and_get( array( 'post_title' => 'some-post-2', 'post_type' => 'post' ) );
    910                $comments = $this->factory->comment->create_post_comments( $post->ID, 5 );
    1011                $result = wp_update_comment( array( 'comment_ID' => $comments[0], 'comment_parent' => $comments[1] ) );
    1112                $this->assertEquals( 1, $result );
     
    1314                $this->assertEquals( $comments[1], $comment->comment_parent );
    1415                $result = wp_update_comment( array( 'comment_ID' => $comments[0], 'comment_parent' => $comments[1] ) );
    1516                $this->assertEquals( 0, $result );
     17                $result = wp_update_comment( array( 'comment_ID' => $comments[0], 'comment_post_ID' => $post2->ID ) );
     18                $comment = get_comment( $comments[0] );
     19                $this->assertEquals( $post2->ID, $comment->comment_post_ID );
    1620        }
    1721
    1822        public function test_get_approved_comments() {