Make WordPress Core


Ignore:
Timestamp:
04/20/2018 12:18:35 PM (7 years ago)
Author:
azaozz
Message:

Privacy: add functionality to anonymize commenters.

Props xkon, fclaussen, allendav, birgire, azaozz.
See #43442.

File:
1 edited

Legend:

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

    r42987 r42994  
    813813    }
    814814
     815    /**
     816     * The `wp_comments_personal_data_eraser()` function should erase user's comments.
     817     *
     818     * @ticket 43442
     819     */
     820    public function test_wp_comments_personal_data_eraser() {
     821
     822        $post_id = self::factory()->post->create();
     823        $user_id = self::factory()->user->create();
     824
     825        $args       = array(
     826            'user_id'              => $user_id,
     827            'comment_post_ID'      => $post_id,
     828            'comment_author'       => 'Comment Author',
     829            'comment_author_email' => 'personal@local.host',
     830            'comment_author_url'   => 'https://local.host/',
     831            'comment_author_IP'    => '192.168.0.1',
     832            'comment_date'         => '2018-04-14 17:20:00',
     833            'comment_agent'        => 'COMMENT_AGENT',
     834            'comment_content'      => 'Comment Content',
     835        );
     836        $comment_id = self::factory()->comment->create( $args );
     837
     838        wp_comments_personal_data_eraser( $args['comment_author_email'] );
     839
     840        $comment = get_comment( $comment_id );
     841
     842        $actual = array(
     843            'comment_ID'           => $comment->comment_ID,
     844            'user_id'              => $comment->user_id,
     845            'comment_author'       => $comment->comment_author,
     846            'comment_author_email' => $comment->comment_author_email,
     847            'comment_author_url'   => $comment->comment_author_url,
     848            'comment_author_IP'    => $comment->comment_author_IP,
     849            'comment_date'         => $comment->comment_date,
     850            'comment_date_gmt'     => $comment->comment_date_gmt,
     851            'comment_agent'        => $comment->comment_agent,
     852            'comment_content'      => $comment->comment_content,
     853        );
     854
     855        $expected = array(
     856            'comment_ID'           => (string) $comment_id,
     857            'user_id'              => '0', // Anonymized.
     858            'comment_author'       => 'Anonymous', // Anonymized.
     859            'comment_author_email' => 'deleted@site.invalid', // Anonymized.
     860            'comment_author_url'   => 'https://site.invalid', // Anonymized.
     861            'comment_author_IP'    => '192.168.0.0', // Anonymized.
     862            'comment_date'         => '2018-04-14 17:20:00',
     863            'comment_date_gmt'     => '2018-04-14 17:20:00',
     864            'comment_agent'        => '', // Anonymized.
     865            'comment_content'      => 'Comment Content',
     866        );
     867
     868        $this->assertSame( $expected, $actual );
     869    }
     870
     871    /**
     872     * Testing the `wp_comments_personal_data_eraser()` function's output on an empty first page.
     873     *
     874     * @ticket 43442
     875     */
     876    public function test_wp_comments_personal_data_eraser_empty_first_page_output() {
     877
     878        $actual   = wp_comments_personal_data_eraser( 'nocommentsfound@local.host' );
     879        $expected = array(
     880            'num_items_removed'  => 0,
     881            'num_items_retained' => 0,
     882            'messages'           => array(),
     883            'done'               => true,
     884        );
     885
     886        $this->assertSame( $expected, $actual );
     887    }
     888
     889    /**
     890     * Testing the `wp_comments_personal_data_eraser()` function's output, for the non-empty first page.
     891     *
     892     * @ticket 43442
     893     */
     894    public function test_wp_comments_personal_data_eraser_non_empty_first_page_output() {
     895
     896        $post_id = self::factory()->post->create();
     897        $args    = array(
     898            'comment_post_ID'      => $post_id,
     899            'comment_author'       => 'Comment Author',
     900            'comment_author_email' => 'personal@local.host',
     901            'comment_author_url'   => 'https://local.host/',
     902            'comment_author_IP'    => '192.168.0.1',
     903            'comment_date'         => '2018-04-14 17:20:00',
     904            'comment_agent'        => 'COMMENT_AGENT',
     905            'comment_content'      => 'Comment Content',
     906        );
     907        self::factory()->comment->create( $args );
     908
     909        $actual   = wp_comments_personal_data_eraser( $args['comment_author_email'] );
     910        $expected = array(
     911            'num_items_removed'  => 1,
     912            'num_items_retained' => 0,
     913            'messages'           => array(),
     914            'done'               => true,
     915        );
     916
     917        $this->assertSame( $expected, $actual );
     918    }
     919
     920    /**
     921     * Testing the `wp_comments_personal_data_eraser()` function's output, for an empty second page.
     922     *
     923     * @ticket 43442
     924     */
     925    public function test_wp_comments_personal_data_eraser_empty_second_page_output() {
     926
     927        $post_id = self::factory()->post->create();
     928        $args    = array(
     929            'comment_post_ID'      => $post_id,
     930            'comment_author'       => 'Comment Author',
     931            'comment_author_email' => 'personal@local.host',
     932            'comment_author_url'   => 'https://local.host/',
     933            'comment_author_IP'    => '192.168.0.1',
     934            'comment_date'         => '2018-04-14 17:20:00',
     935            'comment_agent'        => 'COMMENT_AGENT',
     936            'comment_content'      => 'Comment Content',
     937        );
     938        self::factory()->comment->create( $args );
     939
     940        $actual   = wp_comments_personal_data_eraser( $args['comment_author_email'], 2 );
     941        $expected = array(
     942            'num_items_removed'  => 0,
     943            'num_items_retained' => 0,
     944            'messages'           => array(),
     945            'done'               => true,
     946        );
     947
     948        $this->assertSame( $expected, $actual );
     949    }
     950
     951    /**
     952     * Testing the `wp_anonymize_comment` filter, to prevent comment anonymization.
     953     *
     954     * @ticket 43442
     955     */
     956    public function test_wp_anonymize_comment_filter_to_prevent_comment_anonymization() {
     957
     958        $post_id    = self::factory()->post->create();
     959        $args       = array(
     960            'comment_post_ID'      => $post_id,
     961            'comment_author'       => 'Comment Author',
     962            'comment_author_email' => 'personal@local.host',
     963            'comment_author_url'   => 'https://local.host/',
     964            'comment_author_IP'    => '192.168.0.1',
     965            'comment_date'         => '2018-04-14 17:20:00',
     966            'comment_agent'        => 'COMMENT_AGENT',
     967            'comment_content'      => 'Comment Content',
     968        );
     969        $comment_id = self::factory()->comment->create( $args );
     970
     971        add_filter( 'wp_anonymize_comment', '__return_false' );
     972        $actual = wp_comments_personal_data_eraser( $args['comment_author_email'] );
     973        remove_filter( 'wp_anonymize_comment', '__return_false' );
     974
     975        $message = sprintf( 'Comment %d contains personal data but could not be anonymized.', $comment_id );
     976
     977        $expected = array(
     978            'num_items_removed'  => 0,
     979            'num_items_retained' => 1,
     980            'messages'           => array( $message ),
     981            'done'               => true,
     982        );
     983
     984        $this->assertSame( $expected, $actual );
     985    }
     986
     987    /**
     988     * Testing the `wp_anonymize_comment` filter, to prevent comment anonymization, with a custom message.
     989     *
     990     * @ticket 43442
     991     */
     992    public function test_wp_anonymize_comment_filter_to_prevent_comment_anonymization_with_custom_message() {
     993
     994        $post_id    = self::factory()->post->create();
     995        $args       = array(
     996            'comment_post_ID'      => $post_id,
     997            'comment_author'       => 'Comment Author',
     998            'comment_author_email' => 'personal@local.host',
     999            'comment_author_url'   => 'https://local.host/',
     1000            'comment_author_IP'    => '192.168.0.1',
     1001            'comment_date'         => '2018-04-14 17:20:00',
     1002            'comment_agent'        => 'COMMENT_AGENT',
     1003            'comment_content'      => 'Comment Content',
     1004        );
     1005        $comment_id = self::factory()->comment->create( $args );
     1006
     1007        add_filter( 'wp_anonymize_comment', array( $this, 'wp_anonymize_comment_custom_message' ), 10, 3 );
     1008        $actual = wp_comments_personal_data_eraser( $args['comment_author_email'] );
     1009        remove_filter( 'wp_anonymize_comment', array( $this, 'wp_anonymize_comment_custom_message' ) );
     1010
     1011        $message = sprintf( 'Some custom message for comment %d.', $comment_id );
     1012
     1013        $expected = array(
     1014            'num_items_removed'  => 0,
     1015            'num_items_retained' => 1,
     1016            'messages'           => array( $message ),
     1017            'done'               => true,
     1018        );
     1019
     1020        $this->assertSame( $expected, $actual );
     1021    }
     1022
     1023    /**
     1024     * Callback for the `wp_anonymize_comment` filter.
     1025     *
     1026     * @param  bool|string $anonymize          Whether to apply the comment anonymization (bool).
     1027     *                                         Custom prevention message (string). Default true.
     1028     * @param  WP_Comment  $comment            WP_Comment object.
     1029     * @param  array       $anonymized_comment Anonymized comment data.
     1030     * @return string
     1031     */
     1032    public function wp_anonymize_comment_custom_message( $anonymize, $comment, $anonymized_comment ) {
     1033        return sprintf( 'Some custom message for comment %d.', $comment->comment_ID );
     1034    }
     1035
    8151036    public function test_update_should_invalidate_comment_cache() {
    8161037        global $wpdb;
Note: See TracChangeset for help on using the changeset viewer.