Ticket #44500: 44500.2.diff
File 44500.2.diff, 10.9 KB (added by , 6 years ago) |
---|
-
src/wp-admin/includes/user.php
771 771 $request_ids = $requests_query->posts; 772 772 773 773 foreach ( $request_ids as $request_id ) { 774 wp_update_post( array( 775 'ID' => $request_id, 776 'post_status' => 'request-failed', 777 'post_password' => '', 778 ) ); 774 wp_mark_user_request_failed( $request_id ); 779 775 } 780 776 } 781 777 … … 1132 1128 * Get an associative array ( id => link ) with the list of views available on this table. 1133 1129 * 1134 1130 * @since 4.9.6 1135 * 1131 *he specified post is not a user_requ 1136 1132 * @return array Associative array of views in the format of $view_name => $view_markup. 1137 1133 */ 1138 1134 protected function get_views() { -
src/wp-includes/user.php
3491 3491 /** 3492 3492 * Validate a user request by comparing the key with the request's key. 3493 3493 * 3494 * If determined that the request is expired, it will be transitioned to the `request-failed` status. 3495 * 3494 3496 * @since 4.9.6 3495 3497 * 3496 3498 * @param string $request_id ID of the request being confirmed. … … 3508 3510 } 3509 3511 3510 3512 if ( ! in_array( $request->status, array( 'request-pending', 'request-failed' ), true ) ) { 3511 return __( 'This link has expired.');3513 return new WP_Error( 'expired_link', __( 'This link has expired.' ) ); 3512 3514 } 3513 3515 3514 3516 if ( empty( $key ) ) { … … 3546 3548 } 3547 3549 3548 3550 if ( ! $expiration_time || time() > $expiration_time ) { 3551 wp_mark_user_request_failed( $request_id ); 3549 3552 return new WP_Error( 'expired_key', __( 'The confirmation email has expired.' ) ); 3550 3553 } 3551 3554 … … 3572 3575 } 3573 3576 3574 3577 /** 3578 * Marks a user_request post as failed. 3579 * 3580 * @since 4.9.9 3581 * 3582 * @param int $request_id User request ID. 3583 * @return int|WP_Error Post ID when updated successfully, WP_Error on failure. 3584 */ 3585 function wp_mark_user_request_failed( $request_id ) { 3586 $post = get_post( $request_id ); 3587 3588 if ( ! $post ) { 3589 return new WP_Error( 'invalid_request_id', __( 'Invalid request ID.' ) ); 3590 } elseif ('user_request' !== $post->post_type ) { 3591 return new WP_Error( 'invalid_post_type', __( 'The specified post is not a user_request post.' ) ); 3592 } 3593 3594 if ( 'request-completed' === $post->post_status ) { 3595 return new WP_Error( 'already_completed', __( 'The request has already been completed.' ) ); 3596 } 3597 3598 return wp_update_post( 3599 array( 3600 'ID' => $request_id, 3601 'post_status' => 'request-failed', 3602 'post_password' => '', 3603 ), 3604 true 3605 ); 3606 } 3607 3608 /** 3575 3609 * WP_User_Request class. 3576 3610 * 3577 3611 * Represents user request data loaded from a WP_Post object. -
tests/phpunit/tests/privacy/wpPersonalDataCleanupRequests.php
1 <?php 2 /** 3 * Test the `_wp_personal_data_cleanup_requests()` function. 4 * 5 * @package WordPress 6 * @subpackage UnitTests 7 * @since 4.9.9 8 */ 9 10 /** 11 * Tests_WpPersonalDataCleanupRequests class. 12 * 13 * @group privacy 14 * @covers wp_mark_user_request_failed 15 * 16 * @since 4.9.9 17 */ 18 class Tests_WpPersonalDataCleanupRequests extends WP_UnitTestCase { 19 20 /** 21 * Utility method for creating user requests. 22 * 23 * @since 4.9.9 24 * 25 * @param int $number Number of posts to create. Default is 4. 26 * @param array $args Post arguments. Default is none. @see wp_insert_post(). 27 */ 28 public function _create_requests( $number = 4, $args = array() ) { 29 $defaults = array( 30 'post_type' => 'user_request', 31 'post_name' => 'export_personal_data', 32 'post_status' => 'request-pending', 33 ); 34 $args = wp_parse_args( $args, $defaults ); 35 $this->factory->post->create_many( $number, $args ); 36 } 37 38 /** 39 * Utility method for getting a list of request IDs with a given status. 40 * 41 * @param string $status Request status to query. Default is pending. 42 * @return array List of request post IDs. 43 */ 44 public function _get_request_ids( $status = 'request-pending' ) { 45 $failed_requests = new WP_Query( 46 array( 47 'post_type' => 'user_request', 48 'post_status' => $status, 49 'fields' => 'ids', 50 'posts_per_page' => 100, 51 ) 52 ); 53 54 return $failed_requests->posts; 55 } 56 57 /** 58 * The function should not mark unexpired requests as failed. 59 * 60 * @since 4.9.9 61 */ 62 public function test_function_does_not_move_pending_requests_with_time_remaining() { 63 $this->_create_requests(); 64 65 _wp_personal_data_cleanup_requests(); 66 67 $this->assertSame( 4, count( $this->_get_request_ids() ) ); 68 $this->assertSame( 0, count( $this->_get_request_ids( 'request-failed' ) ) ); 69 } 70 71 /** 72 * The function should mark all unexpired requests as failed. 73 * 74 * @since 4.9.9 75 */ 76 public function test_function_moves_all_expired_pending_requests() { 77 $this->_create_requests( 4, array( 78 'post_date' => date( 'Y-m-d H:i:s', strtotime( '4 days ago' ) ), 79 ) ); 80 81 _wp_personal_data_cleanup_requests(); 82 83 $this->assertSame( 0, count( $this->_get_request_ids() ) ); 84 $this->assertSame( 4, count( $this->_get_request_ids( 'request-failed' ) ) ); 85 } 86 87 /** 88 * The function should only mark expired requests as failed when there is a mix, even if requests with other 89 * statuses would be considered expired. 90 * 91 * @since 4.9.9 92 */ 93 public function test_function_udpates_correct_requests_with_mixed_status_requests() { 94 $this->_create_requests( 2 ); 95 $this->_create_requests( 2, array( 96 'post_date' => date( 'Y-m-d H:i:s', strtotime( '5 days ago' ) ), 97 'post_status' => 'request-completed', 98 ) ); 99 $this->_create_requests( 2, array( 100 'post_date' => date( 'Y-m-d H:i:s', strtotime( '4 days ago' ) ), 101 ) ); 102 103 _wp_personal_data_cleanup_requests(); 104 105 $this->assertSame( 2, count( $this->_get_request_ids() ) ); 106 $this->assertSame( 2, count( $this->_get_request_ids( 'request-completed' ) ) ); 107 $this->assertSame( 2, count( $this->_get_request_ids( 'request-failed' ) ) ); 108 } 109 110 /** 111 * The function should mark only expired requests as failed, even when there are export and erase requests mixed. 112 * 113 * @since 4.9.9 114 */ 115 public function test_function_updates_correct_requests_with_mixed_export_erase() { 116 $this->_create_requests( 2 ); 117 $this->_create_requests( 2, array( 118 'post_name' => 'remove_personal_data', 119 ) ); 120 $this->_create_requests( 2, array( 121 'post_date' => date( 'Y-m-d H:i:s', strtotime( '2 days ago' ) ), 122 ) ); 123 $this->_create_requests( 2, array( 124 'post_name' => 'remove_personal_data', 125 'post_date' => date( 'Y-m-d H:i:s', strtotime( '2 days ago' ) ), 126 ) ); 127 128 add_filter( 'user_request_key_expiration', array( $this, 'filter_request_expiration_time' ) ); 129 _wp_personal_data_cleanup_requests(); 130 131 $this->assertSame( 4, count( $this->_get_request_ids() ) ); 132 $this->assertSame( 4, count( $this->_get_request_ids( 'request-failed' ) ) ); 133 } 134 135 /** 136 * The function should mark only expired requests as failed when the expiration time is filtered. 137 * 138 * @since 4.9.9 139 */ 140 public function test_function_updates_correct_requests_with_filtered_expiration() { 141 $this->_create_requests( 2 ); 142 $this->_create_requests( 2, array( 143 'post_status' => 'request-completed', 144 'post_date' => date( 'Y-m-d H:i:s', strtotime( '2 days ago' ) ), 145 ) ); 146 $this->_create_requests( 2, array( 147 'post_date' => date( 'Y-m-d H:i:s', strtotime( '2 days ago' ) ), 148 ) ); 149 150 add_filter( 'user_request_key_expiration', array( $this, 'filter_request_expiration_time' ) ); 151 _wp_personal_data_cleanup_requests(); 152 153 $this->assertSame( 2, count( $this->_get_request_ids() ) ); 154 $this->assertSame( 2, count( $this->_get_request_ids( 'request-completed' ) ) ); 155 $this->assertSame( 2, count( $this->_get_request_ids( 'request-failed' ) ) ); 156 } 157 158 /** 159 * Filter the privacy data request expiration time. 160 * 161 * @since 4.9.9 162 * 163 * @return int Expiration time in seconds 164 */ 165 public function filter_request_expiration_time() { 166 return DAY_IN_SECONDS * 1; 167 } 168 } -
tests/phpunit/tests/user/wpMarkUserRequestFailed.php
1 <?php 2 /** 3 * Test the `wp_mark_user_request_failed()` function. 4 * 5 * @package WordPress 6 * @subpackage UnitTests 7 * @since 4.9.9 8 */ 9 10 /** 11 * Tests_WpMarkUserRequestFailed class. 12 * 13 * @group privacy 14 * @ticket 44500 15 16 * @covers wp_mark_user_request_failed 17 * 18 * @since 4.9.9 19 */ 20 class Tests_WpMarkUserRequestFailed extends WP_UnitTestCase { 21 /** 22 * Current test request ID. 23 * 24 * @since 4.9.9 25 * 26 * @var int $request_id 27 */ 28 protected static $request_id; 29 30 /** 31 * Create fixtures. 32 * 33 * @since 4.9.9 34 */ 35 public function setUp() { 36 parent::setUp(); 37 38 self::$request_id = wp_create_user_request( 'requester@example.com', 'export_personal_data' ); 39 } 40 41 /** 42 * The function should return an error when an invalid request post ID is passed. 43 * 44 * @since 4.9.9 45 */ 46 public function test_function_throws_error_when_invalid_request_id() { 47 $actual = wp_mark_user_request_failed( null ); 48 49 $this->assertWPError( $actual ); 50 $this->assertSame( 'Invalid request ID.', $actual->get_error_message() ); 51 } 52 53 /** 54 * The function should return an error when an invalid request post ID is passed. 55 * 56 * @since 4.9.9 57 */ 58 public function test_function_throws_error_when_invalid_request_type() { 59 $non_request_post_id = $this->factory->post->create( 60 array( 61 'post_type' => 'post', 62 ) 63 ); 64 65 $actual = wp_mark_user_request_failed( $non_request_post_id ); 66 67 $this->assertWPError( $actual ); 68 $this->assertSame( 'The specified post is not a user_request post.', $actual->get_error_message() ); 69 } 70 71 /** 72 * The function should return an error when the request has already been completed. 73 * 74 * @since 4.9.9 75 */ 76 public function test_function_throws_error_when_request_already_completed() { 77 wp_update_post( 78 array( 79 'ID' => self::$request_id, 80 'post_status' => 'request-completed', 81 ) 82 ); 83 84 $actual = wp_mark_user_request_failed( self::$request_id ); 85 86 $this->assertWPError( $actual ); 87 $this->assertSame( 'The request has already been completed.', $actual->get_error_message() ); 88 } 89 90 /** 91 * The function should return an error when an invalid request post ID is passed. 92 * 93 * @since 4.9.9 94 */ 95 public function test_function_succeeds() { 96 $actual = wp_mark_user_request_failed( self::$request_id ); 97 98 $this->assertSame( self::$request_id, $actual ); 99 $this->assertSame( 'request-failed', get_post_status( self::$request_id ) ); 100 } 101 }