Make WordPress Core

Ticket #44141: 44141.2.diff

File 44141.2.diff, 7.1 KB (added by birgire, 6 years ago)
  • src/wp-includes/comment.php

    diff --git src/wp-includes/comment.php src/wp-includes/comment.php
    index 4c78eb9..9b09a1c 100644
    function wp_comments_personal_data_eraser( $email_address, $page = 1 ) { 
    34403440                )
    34413441        );
    34423442
     3443        /* translators: Name of a comment's author after being anonymized. */
    34433444        $anon_author = __( 'Anonymous' );
    34443445        $messages    = array();
    34453446
    function wp_comments_personal_data_eraser( $email_address, $page = 1 ) { 
    34863487                $updated = $wpdb->update( $wpdb->comments, $anonymized_comment, $args );
    34873488
    34883489                if ( $updated ) {
     3490                        update_comment_meta( $comment_id, '_wp_anonymized', true );
    34893491                        $items_removed = true;
    34903492                        clean_comment_cache( $comment_id );
    34913493                } else {
  • src/wp-includes/functions.php

    diff --git src/wp-includes/functions.php src/wp-includes/functions.php
    index 60229ee..d5682ed 100644
    function wp_privacy_anonymize_ip( $ip_addr, $ipv6_fallback = false ) { 
    62126212function wp_privacy_anonymize_data( $type, $data = '' ) {
    62136213
    62146214        switch ( $type ) {
    6215                 case 'email':
    6216                         $anonymous = 'deleted@site.invalid';
    6217                         break;
    6218                 case 'url':
    6219                         $anonymous = 'https://site.invalid';
    6220                         break;
    62216215                case 'ip':
    62226216                        $anonymous = wp_privacy_anonymize_ip( $data );
    62236217                        break;
  • tests/phpunit/tests/comment.php

    diff --git tests/phpunit/tests/comment.php tests/phpunit/tests/comment.php
    index 7f63c62..3eecf7e 100644
    class Tests_Comment extends WP_UnitTestCase { 
    815815        /**
    816816         * The `wp_comments_personal_data_eraser()` function should erase user's comments.
    817817         *
     818         * @group privacy
    818819         * @ticket 43442
    819820         */
    820821        public function test_wp_comments_personal_data_eraser() {
    class Tests_Comment extends WP_UnitTestCase { 
    856857                        'comment_ID'           => (string) $comment_id,
    857858                        'user_id'              => '0', // Anonymized.
    858859                        'comment_author'       => 'Anonymous', // Anonymized.
    859                         'comment_author_email' => 'deleted@site.invalid', // Anonymized.
    860                         'comment_author_url'   => 'https://site.invalid', // Anonymized.
     860                        'comment_author_email' => '', // Anonymized.
     861                        'comment_author_url'   => '', // Anonymized.
    861862                        'comment_author_IP'    => '192.168.0.0', // Anonymized.
    862863                        'comment_date'         => '2018-04-14 17:20:00',
    863864                        'comment_date_gmt'     => '2018-04-14 17:20:00',
    class Tests_Comment extends WP_UnitTestCase { 
    866867                );
    867868
    868869                $this->assertSame( $expected, $actual );
     870                // Ensure the comment was marked as anonymized in meta.
     871                $this->assertSame( '1', get_comment_meta( $comment_id, '_wp_anonymized', true ) );
    869872        }
    870873
    871874        /**
    872875         * Testing the `wp_comments_personal_data_eraser()` function's output on an empty first page.
    873876         *
     877         * @group privacy
    874878         * @ticket 43442
    875879         */
    876880        public function test_wp_comments_personal_data_eraser_empty_first_page_output() {
    class Tests_Comment extends WP_UnitTestCase { 
    889893        /**
    890894         * Testing the `wp_comments_personal_data_eraser()` function's output, for the non-empty first page.
    891895         *
     896         * @group privacy
    892897         * @ticket 43442
    893898         */
    894899        public function test_wp_comments_personal_data_eraser_non_empty_first_page_output() {
    class Tests_Comment extends WP_UnitTestCase { 
    920925        /**
    921926         * Testing the `wp_comments_personal_data_eraser()` function's output, for an empty second page.
    922927         *
     928         * @group privacy
    923929         * @ticket 43442
    924930         */
    925931        public function test_wp_comments_personal_data_eraser_empty_second_page_output() {
    class Tests_Comment extends WP_UnitTestCase { 
    951957        /**
    952958         * Testing the `wp_anonymize_comment` filter, to prevent comment anonymization.
    953959         *
     960         * @group privacy
    954961         * @ticket 43442
    955962         */
    956963        public function test_wp_anonymize_comment_filter_to_prevent_comment_anonymization() {
    class Tests_Comment extends WP_UnitTestCase { 
    987994        /**
    988995         * Testing the `wp_anonymize_comment` filter, to prevent comment anonymization, with a custom message.
    989996         *
     997         * @group privacy
    990998         * @ticket 43442
    991999         */
    9921000        public function test_wp_anonymize_comment_filter_to_prevent_comment_anonymization_with_custom_message() {
  • tests/phpunit/tests/functions/anonymization.php

    diff --git tests/phpunit/tests/functions/anonymization.php tests/phpunit/tests/functions/anonymization.php
    index a5c9c8a..65052c0 100644
     
    22/**
    33 * Test anonymization functions.
    44 *
    5  * @package WordPress
     5 * @package WordPress\UnitTests
    66 *
    7  * @since 5.0.0
     7 * @since 4.9.6
    88 */
    99
    1010/**
     
    1313 * @group functions.php
    1414 * @group privacy
    1515 *
    16  * @since 5.0.0
     16 * @since 4.9.6
    1717 */
    1818class Tests_Functions_Anonymization extends WP_UnitTestCase {
    1919        /**
    class Tests_Functions_Anonymization extends WP_UnitTestCase { 
    4242        /**
    4343         * Provide test cases for `test_wp_privacy_anonymize_ip()`.
    4444         *
    45          * @since 5.0.0 Moved from `Test_WP_Community_Events::data_get_unsafe_client_ip_anonymization()`.
     45         * @since 4.9.6 Moved from `Test_WP_Community_Events::data_get_unsafe_client_ip_anonymization()`.
    4646         *
    4747         * @return array {
    4848         *     @type array {
    class Tests_Functions_Anonymization extends WP_UnitTestCase { 
    212212         * Test email anonymization of `wp_privacy_anonymize_data()`.
    213213         */
    214214        public function test_anonymize_email() {
    215                 $this->assertEquals( 'deleted@site.invalid', wp_privacy_anonymize_data( 'email', 'bar@example.com' ) );
     215                $this->assertSame( '', wp_privacy_anonymize_data( 'email', 'bar@example.com' ) );
    216216        }
    217217
    218218        /**
    219219         * Test url anonymization of `wp_privacy_anonymize_data()`.
    220220         */
    221221        public function test_anonymize_url() {
    222                 $this->assertEquals( 'https://site.invalid', wp_privacy_anonymize_data( 'url', 'https://example.com/author/username' ) );
     222                $this->assertSame( '', wp_privacy_anonymize_data( 'url', 'https://example.com/author/username' ) );
    223223        }
    224224
    225225        /**
    class Tests_Functions_Anonymization extends WP_UnitTestCase { 
    244244                $text = __( 'Four score and seven years ago' );
    245245                $this->assertEquals( 'This content was deleted by the author.', wp_privacy_anonymize_data( 'longtext', $text ) );
    246246        }
     247
     248        /**
     249         * Test text anonymization when a filter is added.
     250         *
     251         * @ticket 44141
     252         */
     253        public function test_anonymize_with_filter() {
     254                add_filter( 'wp_privacy_anonymize_data', array( $this, 'filter_wp_privacy_anonymize_data' ), 10, 3 );
     255                $actual_email = wp_privacy_anonymize_data( 'email', 'bar@example.com' );
     256                $actual_url   = wp_privacy_anonymize_data( 'url', 'https://example.com/author/username' );
     257                remove_filter( 'wp_privacy_anonymize_data', array( $this, 'filter_wp_privacy_anonymize_data' ), 10 );
     258
     259                $this->assertSame( '', $actual_email );
     260                $this->assertSame( 'http://local.host/why-this-was-removed', $actual_url );
     261        }
     262
     263        /**
     264         * Change the anonymized value for URLs.
     265         *
     266         * @since 4.9.7
     267         *
     268         * @param string  $anonymous Anonymized data.
     269         * @param string  $type      Type of the data.
     270         * @param string  $data      Original data.
     271         * @return string $anonymous Anonymized data.
     272         */
     273        public function filter_wp_privacy_anonymize_data( $anonymous, $type, $data ) {
     274                if ( 'url' === $type && 'example.com' === parse_url( $data, PHP_URL_HOST ) ) {
     275                        return 'http://local.host/why-this-was-removed';
     276                }
     277                return $anonymous;
     278        }
     279
    247280}