Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/wp-admin/includes/privacy-tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,17 @@ function _wp_personal_data_handle_actions() {
}

if ( 'pending' === $status ) {
wp_send_user_request( $request_id );
$send_request_result = wp_send_user_request( $request_id );

if ( is_wp_error( $send_request_result ) ) {
add_settings_error(
'username_or_email_for_privacy_request',
'username_or_email_for_privacy_request',
$send_request_result->get_error_message(),
'error'
);
break;
}

$message = __( 'Confirmation request initiated successfully.' );
} elseif ( 'confirmed' === $status ) {
Expand Down
95 changes: 95 additions & 0 deletions tests/phpunit/tests/privacy/wpPersonalDataHandleActions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
/**
* Test cases for the `_wp_personal_data_handle_actions()` function.
*
* @package WordPress
* @subpackage UnitTests
*
* @group privacy
* @group admin
* @covers ::_wp_personal_data_handle_actions
*/
class Tests_Privacy_wpPersonalDataHandleActions extends WP_UnitTestCase {

/**
* Reset the mocked phpmailer instance before each test.
*/
public function set_up() {
parent::set_up();

reset_phpmailer_instance();
}

/**
* Clean up superglobals and settings errors after each test.
*/
public function tear_down() {
unset(
$_POST['action'],
$_POST['_wpnonce'],
$_POST['type_of_action'],
$_POST['username_or_email_for_privacy_request'],
$_POST['send_confirmation_email'],
$_REQUEST['_wpnonce']
);

$GLOBALS['wp_settings_errors'] = array();

reset_phpmailer_instance();

parent::tear_down();
}

/**
* Populates $_POST with a valid "add personal data request" submission.
*
* @param string $email Email address for the request.
*/
private function set_up_add_request_post_data( $email ) {
$_POST['action'] = 'add_export_personal_data_request';
$_POST['_wpnonce'] = wp_create_nonce( 'personal-data-request' );

Check warning on line 50 in tests/phpunit/tests/privacy/wpPersonalDataHandleActions.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Equals sign not aligned with surrounding assignments; expected 30 spaces but found 31 spaces
$_POST['type_of_action'] = 'export_personal_data';

Check warning on line 51 in tests/phpunit/tests/privacy/wpPersonalDataHandleActions.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Equals sign not aligned with surrounding assignments; expected 24 spaces but found 25 spaces
$_POST['username_or_email_for_privacy_request'] = $email;

Check warning on line 52 in tests/phpunit/tests/privacy/wpPersonalDataHandleActions.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Equals sign not aligned with surrounding assignments; expected 1 space but found 2 spaces
$_POST['send_confirmation_email'] = '1';

Check warning on line 53 in tests/phpunit/tests/privacy/wpPersonalDataHandleActions.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Equals sign not aligned with surrounding assignments; expected 15 spaces but found 16 spaces

// check_admin_referer() reads the nonce from $_REQUEST, which the test bootstrap resets per test.
$_REQUEST['_wpnonce'] = $_POST['_wpnonce'];
}

/**
* A confirmation email failing to send should surface as an error, not a false success message.
*
* @ticket 54442
*/
public function test_should_add_error_when_confirmation_email_fails_to_send() {
$this->set_up_add_request_post_data( 'requester@example.com' );

// Cause `wp_mail()` to return false.
add_filter( 'wp_mail_from', '__return_empty_string' );

_wp_personal_data_handle_actions();

$errors = get_settings_errors( 'username_or_email_for_privacy_request' );

$this->assertNotEmpty( $errors, 'An error should be recorded when the confirmation email fails to send.' );
$this->assertSame( 'error', $errors[0]['type'] );
$this->assertSame( 'Unable to send personal data export confirmation email.', $errors[0]['message'] );
}

/**
* A successfully sent confirmation email should still report success.
*
* @ticket 54442
*/
public function test_should_add_success_message_when_confirmation_email_sends() {
$this->set_up_add_request_post_data( 'requester@example.com' );

_wp_personal_data_handle_actions();

$errors = get_settings_errors( 'username_or_email_for_privacy_request' );

$this->assertNotEmpty( $errors );
$this->assertSame( 'success', $errors[0]['type'] );
$this->assertSame( 'Confirmation request initiated successfully.', $errors[0]['message'] );
}
}
Loading