Make WordPress Core

Ticket #43440: 43440.6.diff

File 43440.6.diff, 7.7 KB (added by birgire, 7 years ago)
  • src/wp-includes/comment.php

    diff --git src/wp-includes/comment.php src/wp-includes/comment.php
    index 595f825..6c484fa 100644
    function wp_handle_comment_submission( $comment_data ) { 
    32763276}
    32773277
    32783278/**
    3279  * Registers the personal data exporter for comments
     3279 * Registers the personal data exporter for comments.
    32803280 *
    3281  * @param array   $exporters   An array of personal data exporters.
    3282  * @return array  An array of personal data exporters.
     3281 * @since 5.0.0
     3282 *
     3283 * @param  array $exporters An array of personal data exporters.
     3284 * @return array $exporters An array of personal data exporters.
    32833285 */
    32843286function wp_register_comment_personal_data_exporter( $exporters ) {
    32853287        $exporters[] = array(
    function wp_register_comment_personal_data_exporter( $exporters ) { 
    32933295/**
    32943296 * Finds and exports personal data associated with an email address from the comments table.
    32953297 *
    3296  * @param string  $email_address The comment author email address.
    3297  * @param int     $page          Comment page.
    3298  * @return array  An array of personal data.
     3298 * @since 5.0.0
     3299 *
     3300 * @param string $email_address The comment author email address.
     3301 * @param int    $page          Comment page.
     3302 * @return array $return        An array of personal data.
    32993303 */
    33003304function wp_comments_personal_data_exporter( $email_address, $page = 1 ) {
    33013305
    3302         // Technically, strtolower isn't necessary since get_comments will match email insensitive
    3303         // But it is a good example for plugin developers whose targets might not be as generous
    3304         $email_address = trim( strtolower( $email_address ) );
     3306        /**
     3307         * Technically, strtolower isn't necessary since get_comments will match email insensitive.
     3308         * But it is a good example for plugin developers whose targets might not be as generous.
     3309         */
     3310        $email_address = strtolower( trim( $email_address ) );
    33053311
    3306         // Limit us to 500 comments at a time to avoid timing out
     3312        // Limit us to 500 comments at a time to avoid timing out.
    33073313        $number = 500;
    3308         $page = (int) $page;
     3314        $page   = (int) $page;
    33093315
    33103316        $data_to_export = array();
    33113317
    function wp_comments_personal_data_exporter( $email_address, $page = 1 ) { 
    33243330                'comment_author_email' => __( 'Comment Author Email' ),
    33253331                'comment_author_url'   => __( 'Comment Author URL' ),
    33263332                'comment_author_IP'    => __( 'Comment Author IP' ),
    3327                 'comment_agent'        => __( 'Comment Agent' ),
     3333                'comment_agent'        => __( 'Comment Author User Agent' ),
    33283334                'comment_date'         => __( 'Comment Date' ),
    33293335                'comment_content'      => __( 'Comment Content' ),
    33303336                'comment_link'         => __( 'Comment URL' ),
    function wp_comments_personal_data_exporter( $email_address, $page = 1 ) { 
    33363342                foreach ( $comment_prop_to_export as $key => $name ) {
    33373343                        $value = '';
    33383344
    3339                         switch( $key ) {
     3345                        switch ( $key ) {
    33403346                                case 'comment_author':
    33413347                                case 'comment_author_email':
    33423348                                case 'comment_author_url':
    function wp_comments_personal_data_exporter( $email_address, $page = 1 ) { 
    33563362                        }
    33573363
    33583364                        if ( ! empty( $value ) ) {
    3359                                 $comment_data_to_export[] = array( 'name' => $name, 'value' => $value );
     3365                                $comment_data_to_export[] = array(
     3366                                        'name'  => $name,
     3367                                        'value' => $value,
     3368                                );
    33603369                        }
    33613370                }
    33623371
  • tests/phpunit/tests/comment.php

    diff --git tests/phpunit/tests/comment.php tests/phpunit/tests/comment.php
    index 43dacdb..1efb8d9 100644
    class Tests_Comment extends WP_UnitTestCase { 
    891891
    892892                $this->assertSame( '1', $comment->comment_approved );
    893893        }
     894
     895        /**
     896         * Testing the `wp_comments_personal_data_exporter()` function.
     897         *
     898         * @ticket 43440
     899         */
     900        public function test_wp_comments_personal_data_exporter() {
     901                $args = array(
     902                        'comment_post_ID'      => self::$post_id,
     903                        'comment_author'       => 'Comment Author',
     904                        'comment_author_email' => 'personal@local.host',
     905                        'comment_author_url'   => 'https://local.host/',
     906                        'comment_author_IP'    => '192.168.0.1',
     907                        'comment_agent'        => 'SOME_AGENT',
     908                        'comment_date'         => '2018-03-28 20:05:00',
     909                        'comment_content'      => 'Comment',
     910                );
     911
     912                $c = self::factory()->comment->create( $args );
     913
     914                $actual   = wp_comments_personal_data_exporter( $args['comment_author_email'] );
     915                $expected = $args;
     916
     917                // Done.
     918                $this->assertSame( true, $actual['done'] );
     919
     920                // Number of exported comments.
     921                $this->assertSame( 1, count( $actual['data'] ) );
     922
     923                // Number of exported comment properties.
     924                $this->assertSame( 8, count( $actual['data'][0]['data'] ) );
     925
     926                // Exported group.
     927                $this->assertSame( 'comments', $actual['data'][0]['group_id'] );
     928                $this->assertSame( 'Comments', $actual['data'][0]['group_label'] );
     929
     930                // Exported comment properties.
     931                $this->assertSame( $expected['comment_author'], $actual['data'][0]['data'][0]['value'] );
     932                $this->assertSame( $expected['comment_author_email'], $actual['data'][0]['data'][1]['value'] );
     933                $this->assertSame( $expected['comment_author_url'], $actual['data'][0]['data'][2]['value'] );
     934                $this->assertSame( $expected['comment_author_IP'], $actual['data'][0]['data'][3]['value'] );
     935                $this->assertSame( $expected['comment_agent'], $actual['data'][0]['data'][4]['value'] );
     936                $this->assertSame( $expected['comment_date'], $actual['data'][0]['data'][5]['value'] );
     937                $this->assertSame( $expected['comment_content'], $actual['data'][0]['data'][6]['value'] );
     938                $this->assertSame( get_comment_link( $c ), $actual['data'][0]['data'][7]['value'] );
     939        }
     940
     941        /**
     942         * Testing the `wp_comments_personal_data_exporter()` function with no comments found.
     943         *
     944         * @ticket 43440
     945         */
     946        public function test_wp_comments_personal_data_exporter_with_no_comments_found() {
     947
     948                $actual = wp_comments_personal_data_exporter( 'nocommentsfound@local.host' );
     949
     950                $expected = array(
     951                        'data' => array(),
     952                        'done' => true,
     953                );
     954
     955                $this->assertSame( $expected, $actual );
     956        }
     957
     958        /**
     959         * Testing the `wp_comments_personal_data_exporter()` function for an empty comment property.
     960         *
     961         * @ticket 43440
     962         */
     963        public function test_wp_comments_personal_data_exporter_with_empty_comment_prop() {
     964                $args = array(
     965                        'comment_post_ID'      => self::$post_id,
     966                        'comment_author'       => 'Comment Author',
     967                        'comment_author_email' => 'personal@local.host',
     968                        'comment_author_url'   => 'https://local.host/',
     969                        'comment_author_IP'    => '192.168.0.1',
     970                        'comment_date'         => '2018-03-28 20:05:00',
     971                        'comment_agent'        => '',
     972                        'comment_content'      => 'Comment',
     973                );
     974
     975                $c = self::factory()->comment->create( $args );
     976
     977                $actual = wp_comments_personal_data_exporter( $args['comment_author_email'] );
     978
     979                $this->assertSame( true, $actual['done'] );
     980                // Number of exported comments.
     981                $this->assertSame( 1, count( $actual['data'] ) );
     982                // Number of exported comment properties.
     983                $this->assertSame( 7, count( $actual['data'][0]['data'] ) );
     984        }
     985
     986        /**
     987         * Testing the `wp_comments_personal_data_exporter()` function for the second page with less than 500 comments.
     988         *
     989         * @ticket 43440
     990         */
     991        public function test_wp_comments_personal_data_exporter_second_page() {
     992                $args = array(
     993                        'comment_post_ID'      => self::$post_id,
     994                        'comment_author'       => 'Comment Author',
     995                        'comment_author_email' => 'personal@local.host',
     996                        'comment_author_url'   => 'https://local.host/',
     997                        'comment_author_IP'    => '192.168.0.1',
     998                        'comment_date'         => '2018-03-28 20:05:00',
     999                        'comment_agent'        => 'SOME_AGENT',
     1000                        'comment_content'      => 'Comment',
     1001                );
     1002
     1003                $c = self::factory()->comment->create( $args );
     1004
     1005                $actual = wp_comments_personal_data_exporter( $args['comment_author_email'], 2 );
     1006
     1007                $this->assertSame( true, $actual['done'] );
     1008                // Number of exported comments.
     1009                $this->assertSame( 0, count( $actual['data'] ) );
     1010        }
    8941011}