Make WordPress Core


Ignore:
Timestamp:
01/13/2016 01:24:46 AM (10 years ago)
Author:
rachelbaker
Message:

Comments: Restrict the maximum characters for input fields within the comments template.

Added hardcoded maxlength attributes on the author, author_email, author_url, and comment_field input markup. These can be modified via the comment_form_defaults filter. Added logic in wp_handle_comment_submission() to return a WP_Error when the comment_author, comment_author_url, or comment_content values exceed the max length of their columns. Introduces wp_get_comment_column_max_length() which returns the max column length for a given column name, and is filterable. Unit tests included for the error conditions in wp_handle_comment_submission()

Fixes #10377.

Props westonruter rachelbaker.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/comment-submission.php

    r36038 r36272  
    591591        $this->assertSame( $error, $comment->get_error_code() );
    592592
     593    }
     594
     595    /**
     596     * @ticket 10377
     597     */
     598    public function test_submitting_comment_with_content_too_long_returns_error() {
     599        $error = 'comment_content_column_length';
     600
     601        $post = self::factory()->post->create_and_get();
     602
     603        $data = array(
     604            'comment_post_ID' => $post->ID,
     605            'comment'         => rand_long_str( 65536 ),
     606            'author'          => 'Comment Author',
     607            'email'           => 'comment@example.org',
     608        );
     609        $comment = wp_handle_comment_submission( $data );
     610
     611        $this->assertWPError( $comment );
     612        $this->assertSame( $error, $comment->get_error_code() );
     613    }
     614
     615    /**
     616     * @ticket 10377
     617     */
     618    public function test_submitting_comment_with_author_too_long_returns_error() {
     619        $error = 'comment_author_column_length';
     620
     621        $post = self::factory()->post->create_and_get();
     622
     623        $data = array(
     624            'comment_post_ID' => $post->ID,
     625            'comment'         => rand_str(),
     626            'author'          => rand_long_str( 255 ),
     627            'email'           => 'comment@example.org',
     628        );
     629        $comment = wp_handle_comment_submission( $data );
     630
     631        $this->assertWPError( $comment );
     632        $this->assertSame( $error, $comment->get_error_code() );
     633    }
     634
     635    /**
     636     * @ticket 10377
     637     */
     638    public function test_submitting_comment_with_email_too_long_returns_error() {
     639        $error = 'comment_author_email_column_length';
     640
     641        $post = self::factory()->post->create_and_get();
     642
     643        $data = array(
     644            'comment_post_ID' => $post->ID,
     645            'comment'         => rand_str(),
     646            'author'          => 'Comment Author',
     647            'email'           => rand_long_str( 90 ) . '@example.com',
     648        );
     649        $comment = wp_handle_comment_submission( $data );
     650
     651        $this->assertWPError( $comment );
     652        $this->assertSame( $error, $comment->get_error_code() );
     653    }
     654
     655    /**
     656     * @ticket 10377
     657     */
     658    public function test_submitting_comment_with_url_too_long_returns_error() {
     659        $error = 'comment_author_url_column_length';
     660
     661        $post = self::factory()->post->create_and_get();
     662        $data = array(
     663            'comment_post_ID' => $post->ID,
     664            'comment'         => rand_str(),
     665            'author'          => 'Comment Author',
     666            'email'           => 'comment@example.org',
     667            'url'             => rand_long_str( 201 ),
     668        );
     669        $comment = wp_handle_comment_submission( $data );
     670
     671        $this->assertWPError( $comment );
     672        $this->assertSame( $error, $comment->get_error_code() );
    593673    }
    594674
Note: See TracChangeset for help on using the changeset viewer.