Make WordPress Core

Ticket #43440: 43440.7.diff

File 43440.7.diff, 7.6 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..158c1b4 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..4193473 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                $this->assertTrue( $actual['done'] );
     918
     919                // Number of exported comments.
     920                $this->assertSame( 1, count( $actual['data'] ) );
     921
     922                // Number of exported comment properties.
     923                $this->assertSame( 8, count( $actual['data'][0]['data'] ) );
     924
     925                // Exported group.
     926                $this->assertSame( 'comments', $actual['data'][0]['group_id'] );
     927                $this->assertSame( 'Comments', $actual['data'][0]['group_label'] );
     928
     929                // Exported comment properties.
     930                $this->assertSame( $expected['comment_author'], $actual['data'][0]['data'][0]['value'] );
     931                $this->assertSame( $expected['comment_author_email'], $actual['data'][0]['data'][1]['value'] );
     932                $this->assertSame( $expected['comment_author_url'], $actual['data'][0]['data'][2]['value'] );
     933                $this->assertSame( $expected['comment_author_IP'], $actual['data'][0]['data'][3]['value'] );
     934                $this->assertSame( $expected['comment_agent'], $actual['data'][0]['data'][4]['value'] );
     935                $this->assertSame( $expected['comment_date'], $actual['data'][0]['data'][5]['value'] );
     936                $this->assertSame( $expected['comment_content'], $actual['data'][0]['data'][6]['value'] );
     937                $this->assertSame( get_comment_link( $c ), $actual['data'][0]['data'][7]['value'] );
     938        }
     939
     940        /**
     941         * Testing the `wp_comments_personal_data_exporter()` function for no comments found.
     942         *
     943         * @ticket 43440
     944         */
     945        public function test_wp_comments_personal_data_exporter_no_comments_found() {
     946
     947                $actual = wp_comments_personal_data_exporter( 'nocommentsfound@local.host' );
     948
     949                $expected = array(
     950                        'data' => array(),
     951                        'done' => true,
     952                );
     953
     954                $this->assertSame( $expected, $actual );
     955        }
     956
     957        /**
     958         * Testing the `wp_comments_personal_data_exporter()` function for an empty comment property.
     959         *
     960         * @ticket 43440
     961         */
     962        public function test_wp_comments_personal_data_exporter_empty_comment_prop() {
     963                $args = array(
     964                        'comment_post_ID'      => self::$post_id,
     965                        'comment_author'       => 'Comment Author',
     966                        'comment_author_email' => 'personal@local.host',
     967                        'comment_author_url'   => 'https://local.host/',
     968                        'comment_author_IP'    => '192.168.0.1',
     969                        'comment_date'         => '2018-03-28 20:05:00',
     970                        'comment_agent'        => '',
     971                        'comment_content'      => 'Comment',
     972                );
     973
     974                $c = self::factory()->comment->create( $args );
     975
     976                $actual = wp_comments_personal_data_exporter( $args['comment_author_email'] );
     977
     978                $this->assertTrue( $actual['done'] );
     979
     980                // Number of exported comments.
     981                $this->assertSame( 1, count( $actual['data'] ) );
     982
     983                // Number of exported comment properties.
     984                $this->assertSame( 7, count( $actual['data'][0]['data'] ) );
     985        }
     986
     987        /**
     988         * Testing the `wp_comments_personal_data_exporter()` function with an empty second page.
     989         *
     990         * @ticket 43440
     991         */
     992        public function test_wp_comments_personal_data_exporter_empty_second_page() {
     993                $args = array(
     994                        'comment_post_ID'      => self::$post_id,
     995                        'comment_author'       => 'Comment Author',
     996                        'comment_author_email' => 'personal@local.host',
     997                        'comment_author_url'   => 'https://local.host/',
     998                        'comment_author_IP'    => '192.168.0.1',
     999                        'comment_date'         => '2018-03-28 20:05:00',
     1000                        'comment_agent'        => 'SOME_AGENT',
     1001                        'comment_content'      => 'Comment',
     1002                );
     1003
     1004                $c = self::factory()->comment->create( $args );
     1005
     1006                $actual = wp_comments_personal_data_exporter( $args['comment_author_email'], 2 );
     1007
     1008                $this->assertTrue( $actual['done'] );
     1009
     1010                // Number of exported comments.
     1011                $this->assertSame( 0, count( $actual['data'] ) );
     1012        }
    8941013}