Privacy: Ensure the personal data export email is sent in the locale of the user whose data is being exported (or the site's default locale if they are not a registered user) when the administrator fulfilling the request uses a different locale.

Props garrett-eclipse.
Fixes #46056.

git-svn-id: https://develop.svn.wordpress.org/trunk@45062 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2019-03-28 21:28:37 +00:00
parent beb4a31c36
commit 87d4d0ae92
6 changed files with 191 additions and 6 deletions

View File

@ -2353,6 +2353,15 @@ function wp_privacy_send_personal_data_export_email( $request_id ) {
return new WP_Error( 'invalid_request', __( 'Invalid request ID when sending personal data export email.' ) );
}
// Localize message content for user; fallback to site default for visitors.
if ( ! empty( $request->user_id ) ) {
$locale = get_user_locale( $request->user_id );
} else {
$locale = get_locale();
}
$switched_locale = switch_to_locale( $locale );
/** This filter is documented in wp-includes/functions.php */
$expiration = apply_filters( 'wp_privacy_export_expiration', 3 * DAY_IN_SECONDS );
$expiration_date = date_i18n( get_option( 'date_format' ), time() + $expiration );
@ -2409,6 +2418,10 @@ All at ###SITENAME###
$content
);
if ( $switched_locale ) {
restore_previous_locale();
}
if ( ! $mail_success ) {
return new WP_Error( 'privacy_email_error', __( 'Unable to send personal data export email.' ) );
}

View File

@ -1,8 +1,8 @@
# Translation of 4.9.x in German
# This file is distributed under the same license as the 4.9.x package.
# Translation of 5.2.x in German
# This file is distributed under the same license as the 5.2.x package.
msgid ""
msgstr ""
"PO-Revision-Date: 2019-03-27 22:27+0300\n"
"PO-Revision-Date: 2019-03-28 19:42+0300\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
@ -53,3 +53,7 @@ msgstr "[%1$s] Aktion bestätigen: %2$s"
#: wp-includes/user.php:3175
msgid "[%s] Erasure Request Fulfilled"
msgstr "[%s] Löschauftrag ausgeführt"
#: wp-admin/includes/file.php:2415
msgid "[%s] Personal Data Export"
msgstr "[%s] Export personenbezogener Daten"

View File

@ -1,8 +1,8 @@
# Translation of Development (4.9.x) in Spanish (Spain)
# This file is distributed under the same license as the Development (4.9.x) package.
# Translation of Development (5.2.x) in Spanish (Spain)
# This file is distributed under the same license as the Development (5.2.x) package.
msgid ""
msgstr ""
"PO-Revision-Date: 2019-03-27 22:27+0300\n"
"PO-Revision-Date: 2019-03-28 19:43+0300\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
@ -49,3 +49,7 @@ msgstr "[%1$s] Confirma la acción: %2$s"
#: wp-includes/user.php:3175
msgid "[%s] Erasure Request Fulfilled"
msgstr "[%s] Solicitud de borrado completada"
#: wp-admin/includes/file.php:2415
msgid "[%s] Personal Data Export"
msgstr "[%s] Exportación de datos personales"

View File

@ -34,6 +34,24 @@ class Tests_Privacy_WpPrivacySendPersonalDataExportEmail extends WP_UnitTestCase
*/
protected static $requester_email;
/**
* Request user.
*
* @since 5.2.0
*
* @var WP_User $request_user
*/
protected static $request_user;
/**
* Test administrator user.
*
* @since 5.2.0
*
* @var WP_User $admin_user
*/
protected static $admin_user;
/**
* Reset the mocked phpmailer instance before each test method.
*
@ -51,6 +69,7 @@ class Tests_Privacy_WpPrivacySendPersonalDataExportEmail extends WP_UnitTestCase
*/
public function tearDown() {
reset_phpmailer_instance();
restore_previous_locale();
parent::tearDown();
}
@ -63,6 +82,19 @@ class Tests_Privacy_WpPrivacySendPersonalDataExportEmail extends WP_UnitTestCase
*/
public static function wpSetUpBeforeClass( $factory ) {
self::$requester_email = 'requester@example.com';
self::$request_user = $factory->user->create_and_get(
array(
'user_email' => self::$requester_email,
'role' => 'subscriber',
)
);
self::$admin_user = $factory->user->create_and_get(
array(
'user_email' => 'admin@local.dev',
'role' => 'administrator',
)
);
self::$request_id = wp_create_user_request( self::$requester_email, 'export_personal_data' );
_wp_privacy_account_request_confirmed( self::$request_id );
@ -167,4 +199,136 @@ class Tests_Privacy_WpPrivacySendPersonalDataExportEmail extends WP_UnitTestCase
public function modify_email_content( $email_text, $request_id ) {
return 'Custom content for request ID: ' . $request_id;
}
/**
* The function should respect the user locale settings when the site uses the default locale.
*
* @since 5.2.0
* @ticket 46056
* @group l10n
*/
public function test_should_send_personal_data_export_email_in_user_locale() {
update_user_meta( self::$request_user->ID, 'locale', 'es_ES' );
wp_privacy_send_personal_data_export_email( self::$request_id );
$mailer = tests_retrieve_phpmailer_instance();
$this->assertContains( 'Exportación de datos personales', $mailer->get_sent()->subject );
}
/**
* The function should respect the user locale settings when the site does not use en_US, the administrator
* uses the site's default locale, and the user has a different locale.
*
* @since 5.2.0
* @ticket 46056
* @group l10n
*/
public function test_should_send_personal_data_export_email_in_user_locale_when_site_is_not_en_us() {
update_option( 'WPLANG', 'es_ES' );
switch_to_locale( 'es_ES' );
update_user_meta( self::$request_user->ID, 'locale', 'de_DE' );
wp_set_current_user( self::$admin_user->ID );
wp_privacy_send_personal_data_export_email( self::$request_id );
$mailer = tests_retrieve_phpmailer_instance();
$this->assertContains( 'Export personenbezogener Daten', $mailer->get_sent()->subject );
}
/**
* The function should respect the user locale settings when the site is not en_US, the administrator
* has a different selected locale, and the user uses the site's default locale.
*
* @since 5.2.0
* @ticket 46056
* @group l10n
*/
public function test_should_send_personal_data_export_email_in_user_locale_when_admin_and_site_have_different_locales() {
update_option( 'WPLANG', 'es_ES' );
switch_to_locale( 'es_ES' );
update_user_meta( self::$admin_user->ID, 'locale', 'de_DE' );
wp_set_current_user( self::$admin_user->ID );
wp_privacy_send_personal_data_export_email( self::$request_id );
$mailer = tests_retrieve_phpmailer_instance();
$this->assertContains( 'Exportación de datos personales', $mailer->get_sent()->subject );
}
/**
* The function should respect the user locale settings when the site is not en_US and both the
* administrator and the user use different locales.
*
* @since 5.2.0
* @ticket 46056
* @group l10n
*/
public function test_should_send_personal_data_export_email_in_user_locale_when_both_have_different_locales_than_site() {
update_option( 'WPLANG', 'es_ES' );
switch_to_locale( 'es_ES' );
update_user_meta( self::$admin_user->ID, 'locale', 'en_US' );
update_user_meta( self::$request_user->ID, 'locale', 'de_DE' );
wp_set_current_user( self::$admin_user->ID );
wp_privacy_send_personal_data_export_email( self::$request_id );
$mailer = tests_retrieve_phpmailer_instance();
$this->assertContains( 'Export personenbezogener Daten', $mailer->get_sent()->subject );
}
/**
* The function should respect the site's locale when the request is for an unregistered user and the
* administrator does not use the site's locale.
*
* @since 5.2.0
* @ticket 46056
* @group l10n
*/
public function test_should_send_personal_data_export_email_in_site_locale() {
update_user_meta( self::$admin_user->ID, 'locale', 'es_ES' );
wp_set_current_user( self::$admin_user->ID );
$request_id = wp_create_user_request( 'export-user-not-registered@example.com', 'export_personal_data' );
_wp_privacy_account_request_confirmed( self::$request_id );
wp_privacy_send_personal_data_export_email( $request_id );
$mailer = tests_retrieve_phpmailer_instance();
$this->assertContains( 'Personal Data Export', $mailer->get_sent()->subject );
}
/**
* The function should respect the site's locale when it is not en_US, the request is for an
* unregistered user, and the administrator does not use the site's default locale.
*
* @since 5.2.0
* @ticket 46056
* @group l10n
*/
public function test_should_send_personal_data_export_email_in_site_locale_when_not_en_us_and_admin_has_different_locale() {
update_option( 'WPLANG', 'es_ES' );
switch_to_locale( 'es_ES' );
update_user_meta( self::$admin_user->ID, 'locale', 'de_DE' );
wp_set_current_user( self::$admin_user->ID );
$request_id = wp_create_user_request( 'export-user-not-registered@example.com', 'export_personal_data' );
_wp_privacy_account_request_confirmed( self::$request_id );
wp_privacy_send_personal_data_export_email( $request_id );
$mailer = tests_retrieve_phpmailer_instance();
$this->assertContains( 'Exportación de datos personales', $mailer->get_sent()->subject );
}
}