Make WordPress Core

Ticket #43602: 43602.7.diff

File 43602.7.diff, 8.5 KB (added by ericdaams, 5 years ago)

Update comment erasing tests. Avoid counting number of comments erased.

  • src/wp-admin/includes/ajax-actions.php

    diff --git src/wp-admin/includes/ajax-actions.php src/wp-admin/includes/ajax-actions.php
    index d3416c6126..adfc5bf513 100644
    function wp_ajax_wp_privacy_erase_personal_data() { 
    44864486         *     An array of callable erasers of personal data. Default empty array.
    44874487         *     [
    44884488         *         callback             string Callable eraser that accepts an email address and
    4489          *                                     a page and returns an array with the number of items
    4490          *                                     removed, the number of items retained and any messages
     4489         *                                     a page and returns an array with boolean values for
     4490         *                                     whether items were removed or retained and any messages
    44914491         *                                     from the eraser, as well as if additional pages are
    44924492         *                                     available.
    44934493         *         eraser_friendly_name string Translated user facing friendly name for the eraser.
    function wp_ajax_wp_privacy_erase_personal_data() { 
    45624562                                )
    45634563                        );
    45644564                }
    4565                 if ( ! array_key_exists( 'num_items_removed', $response ) ) {
     4565                if ( ! array_key_exists( 'items_removed', $response ) ) {
    45664566                        wp_send_json_error(
    45674567                                sprintf(
    4568                                         __( 'Error: Expected num_items_removed key in response array from %s eraser (index %d).' ),
     4568                                        __( 'Error: Expected items_removed key in response array from %s eraser (index %d).' ),
    45694569                                        $eraser_friendly_name,
    45704570                                        $eraser_index
    45714571                                )
    45724572                        );
    45734573                }
    4574                 if ( ! array_key_exists( 'num_items_retained', $response ) ) {
     4574                if ( ! array_key_exists( 'items_retained', $response ) ) {
    45754575                        wp_send_json_error(
    45764576                                sprintf(
    4577                                         __( 'Error: Expected num_items_retained key in response array from %s eraser (index %d).' ),
     4577                                        __( 'Error: Expected items_retained key in response array from %s eraser (index %d).' ),
    45784578                                        $eraser_friendly_name,
    45794579                                        $eraser_index
    45804580                                )
    function wp_ajax_wp_privacy_erase_personal_data() { 
    46104610        } else {
    46114611                // No erasers, so we're done
    46124612                $response = array(
    4613                         'num_items_removed' => 0,
    4614                         'num_items_retained' => 0,
    4615                         'messages' => array(),
    4616                         'done' => true,
     4613                        'items_removed'  => false,
     4614                        'items_retained' => false,
     4615                        'messages'       => array(),
     4616                        'done'           => true,
    46174617                );
    46184618        }
    46194619
  • src/wp-admin/js/xfn.js

    diff --git src/wp-admin/js/xfn.js src/wp-admin/js/xfn.js
    index 7881c76159..9274c573c4 100644
    jQuery( document ).ready( function( $ ) { 
    6161                var nonce         = $action.data( 'nonce' );
    6262                var erasersCount  = $action.data( 'erasers-count' );
    6363
    64                 var removedCount  = 0;
    65                 var retainedCount = 0;
     64                var hasRemoved    = false;
     65                var hasRetained   = false;
    6666                var messages      = [];
    6767
    6868                $action.blur();
    jQuery( document ).ready( function( $ ) { 
    7272                        set_action_state( $action, 'remove_personal_data_idle' );
    7373                        var summaryMessage = strings.noDataFound;
    7474                        var classes = 'notice-success';
    75                         if ( 0 === removedCount ) {
    76                                 if ( 0 === retainedCount ) {
     75                        if ( false === hasRemoved ) {
     76                                if ( false === hasRetained ) {
    7777                                        summaryMessage = strings.noDataFound;
    7878                                } else {
    7979                                        summaryMessage = strings.noneRemoved;
    8080                                        classes = 'notice-warning';
    8181                                }
    8282                        } else {
    83                                 if ( 0 === retainedCount ) {
     83                                if ( false === hasRetained ) {
    8484                                        summaryMessage = strings.foundAndRemoved;
    8585                                } else {
    8686                                        summaryMessage = strings.someNotRemoved;
    jQuery( document ).ready( function( $ ) { 
    112112                                        return;
    113113                                }
    114114                                var responseData = response.data;
    115                                 if ( responseData.num_items_removed ) {
    116                                         removedCount += responseData.num_items_removed;
     115                                if ( responseData.items_removed ) {
     116                                        hasRemoved = hasRemoved || responseData.items_removed;
    117117                                }
    118                                 if ( responseData.num_items_retained ) {
    119                                         retainedCount += responseData.num_items_removed;
     118                                if ( responseData.items_retained ) {
     119                                        hasRetained = hasRetained || responseData.items_retained;
    120120                                }
    121121                                if ( responseData.messages ) {
    122122                                        messages = messages.concat( responseData.messages );
  • src/wp-includes/comment.php

    diff --git src/wp-includes/comment.php src/wp-includes/comment.php
    index 6b6e7b45af..87e0cbac3a 100644
    function wp_comments_personal_data_eraser( $email_address, $page = 1 ) { 
    34103410
    34113411        if ( empty( $email_address ) ) {
    34123412                return array(
    3413                         'num_items_removed'  => 0,
    3414                         'num_items_retained' => 0,
    3415                         'messages'           => array(),
    3416                         'done'               => true,
     3413                        'items_removed'  => false,
     3414                        'items_retained' => false,
     3415                        'messages'       => array(),
     3416                        'done'           => true,
    34173417                );
    34183418        }
    34193419
    34203420        // Limit us to 500 comments at a time to avoid timing out.
    3421         $number            = 500;
    3422         $page              = (int) $page;
    3423         $num_items_removed = 0;
     3421        $number         = 500;
     3422        $page           = (int) $page;
     3423        $items_removed  = false;
     3424        $items_retained = false;
    34243425
    34253426        $comments = get_comments(
    34263427                array(
    function wp_comments_personal_data_eraser( $email_address, $page = 1 ) { 
    34673468                                $messages[] = sprintf( __( 'Comment %d contains personal data but could not be anonymized.' ), $comment_id );
    34683469                        }
    34693470
     3471                        $items_retained = true;
     3472
    34703473                        continue;
    34713474                }
    34723475
    function wp_comments_personal_data_eraser( $email_address, $page = 1 ) { 
    34773480                $updated = $wpdb->update( $wpdb->comments, $anonymized_comment, $args );
    34783481
    34793482                if ( $updated ) {
    3480                         $num_items_removed++;
     3483                        $items_removed = true;
    34813484                        clean_comment_cache( $comment_id );
     3485                } else {
     3486                        $items_retained = true;
    34823487                }
    34833488        }
    34843489
    34853490        $done = count( $comments ) < $number;
    34863491
    34873492        return array(
    3488                 'num_items_removed'  => $num_items_removed,
    3489                 'num_items_retained' => count( $comments ) - $num_items_removed,
    3490                 'messages'           => $messages,
    3491                 'done'               => $done,
     3493                'items_removed'  => $items_removed,
     3494                'items_retained' => $items_retained,
     3495                'messages'       => $messages,
     3496                'done'           => $done,
    34923497        );
    34933498}
     3499
  • tests/phpunit/tests/comment.php

    diff --git tests/phpunit/tests/comment.php tests/phpunit/tests/comment.php
    index 29453179e4..6e9fd60712 100644
    class Tests_Comment extends WP_UnitTestCase { 
    877877
    878878                $actual   = wp_comments_personal_data_eraser( 'nocommentsfound@local.host' );
    879879                $expected = array(
    880                         'num_items_removed'  => 0,
    881                         'num_items_retained' => 0,
    882                         'messages'           => array(),
    883                         'done'               => true,
     880                        'items_removed'  => false,
     881                        'items_retained' => false,
     882                        'messages'       => array(),
     883                        'done'           => true,
    884884                );
    885885
    886886                $this->assertSame( $expected, $actual );
    class Tests_Comment extends WP_UnitTestCase { 
    908908
    909909                $actual   = wp_comments_personal_data_eraser( $args['comment_author_email'] );
    910910                $expected = array(
    911                         'num_items_removed'  => 1,
    912                         'num_items_retained' => 0,
    913                         'messages'           => array(),
    914                         'done'               => true,
     911                        'items_removed'  => true,
     912                        'items_retained' => false,
     913                        'messages'       => array(),
     914                        'done'           => true,
    915915                );
    916916
    917917                $this->assertSame( $expected, $actual );
    class Tests_Comment extends WP_UnitTestCase { 
    939939
    940940                $actual   = wp_comments_personal_data_eraser( $args['comment_author_email'], 2 );
    941941                $expected = array(
    942                         'num_items_removed'  => 0,
    943                         'num_items_retained' => 0,
    944                         'messages'           => array(),
    945                         'done'               => true,
     942                        'items_removed'  => false,
     943                        'items_retained' => false,
     944                        'messages'       => array(),
     945                        'done'           => true,
    946946                );
    947947
    948948                $this->assertSame( $expected, $actual );
    class Tests_Comment extends WP_UnitTestCase { 
    975975                $message = sprintf( 'Comment %d contains personal data but could not be anonymized.', $comment_id );
    976976
    977977                $expected = array(
    978                         'num_items_removed'  => 0,
    979                         'num_items_retained' => 1,
    980                         'messages'           => array( $message ),
    981                         'done'               => true,
     978                        'items_removed'  => false,
     979                        'items_retained' => true,
     980                        'messages'       => array( $message ),
     981                        'done'           => true,
    982982                );
    983983
    984984                $this->assertSame( $expected, $actual );
    class Tests_Comment extends WP_UnitTestCase { 
    10111011                $message = sprintf( 'Some custom message for comment %d.', $comment_id );
    10121012
    10131013                $expected = array(
    1014                         'num_items_removed'  => 0,
    1015                         'num_items_retained' => 1,
    1016                         'messages'           => array( $message ),
    1017                         'done'               => true,
     1014                        'items_removed'  => false,
     1015                        'items_retained' => true,
     1016                        'messages'       => array( $message ),
     1017                        'done'           => true,
    10181018                );
    10191019
    10201020                $this->assertSame( $expected, $actual );