WordPress.org

Make WordPress Core

Ticket #43985: 43985.11.diff

File 43985.11.diff, 20.9 KB (added by desrosj, 16 months ago)
  • src/wp-admin/includes/file.php

     
    21962196        $request = wp_get_user_request_data( $request_id );
    21972197
    21982198        if ( ! $request || 'export_personal_data' !== $request->action_name ) {
    2199                 return new WP_Error( 'invalid', __( 'Invalid request ID when sending personal data export email.' ) );
     2199                return new WP_Error( 'invalid_request', __( 'Invalid request ID when sending personal data export email.' ) );
    22002200        }
    22012201
    22022202        /** This filter is documented in wp-includes/functions.php */
     
    22562256        );
    22572257
    22582258        if ( ! $mail_success ) {
    2259                 return new WP_Error( 'error', __( 'Unable to send personal data export email.' ) );
     2259                return new WP_Error( 'privacy_email_error', __( 'Unable to send personal data export email.' ) );
    22602260        }
    22612261
    22622262        return true;
  • src/wp-includes/user.php

     
    33573357 * @since 4.9.6
    33583358 *
    33593359 * @param string $request_id ID of the request created via wp_create_user_request().
    3360  * @return WP_Error|bool Will return true/false based on the success of sending the email, or a WP_Error object.
     3360 * @return bool|WP_Error true on success or `WP_Error` on failure.
    33613361 */
    33623362function wp_send_user_request( $request_id ) {
    33633363        $request_id = absint( $request_id );
     
    33643364        $request    = wp_get_user_request_data( $request_id );
    33653365
    33663366        if ( ! $request ) {
    3367                 return new WP_Error( 'user_request_error', __( 'Invalid request.' ) );
     3367                return new WP_Error( 'invalid_request', __( 'Invalid user request.' ) );
    33683368        }
    33693369
     3370        // Localize message content for user; fallback to site default for visitors.
     3371        if ( ! empty( $request->user_id ) ) {
     3372                $locale = get_user_locale( $request->user_id );
     3373        } else {
     3374                $locale = get_locale();
     3375        }
     3376
     3377        $switched_locale = switch_to_locale( $locale );
     3378
    33703379        $email_data = array(
    33713380                'request'     => $request,
    33723381                'email'       => $request->email,
     
    34543463         */
    34553464        $subject = apply_filters( 'user_request_action_email_subject', $subject, $email_data['sitename'], $email_data );
    34563465
    3457         return wp_mail( $email_data['email'], $subject, $content );
     3466        $email_sent = wp_mail( $email_data['email'], $subject, $content );
     3467
     3468        if ( $switched_locale ) {
     3469                restore_previous_locale();
     3470        }
     3471
     3472        if ( ! $email_sent ) {
     3473                return new WP_Error( 'privacy_email_error', __( 'Unable to send personal data export confirmation email.' ) );
     3474        }
     3475
     3476        return true;
    34583477}
    34593478
    34603479/**
     
    35043523        $request    = wp_get_user_request_data( $request_id );
    35053524
    35063525        if ( ! $request ) {
    3507                 return new WP_Error( 'user_request_error', __( 'Invalid request.' ) );
     3526                return new WP_Error( 'invalid_request', __( 'Invalid request.' ) );
    35083527        }
    35093528
    35103529        if ( ! in_array( $request->status, array( 'request-pending', 'request-failed' ), true ) ) {
     
    35913610         *
    35923611         * @var int
    35933612         */
    3594 
    35953613        public $user_id = 0;
    35963614
    35973615        /**
  • tests/phpunit/data/languages/de_DE.po

    Cannot display: file marked as a binary type.
    svn:mime-type = application/octet-stream
     
    1 # Translation of 4.6.x in German
    2 # This file is distributed under the same license as the 4.6.x package.
     1# Translation of 4.9.x in German
     2# This file is distributed under the same license as the 4.9.x package.
    33msgid ""
    44msgstr ""
    5 "PO-Revision-Date: 2016-10-25 18:27+0200\n"
     5"PO-Revision-Date: 2018-08-01 17:08-0400\n"
    66"MIME-Version: 1.0\n"
    77"Content-Type: text/plain; charset=UTF-8\n"
    88"Content-Transfer-Encoding: 8bit\n"
    99"Plural-Forms: nplurals=2; plural=n != 1;\n"
    10 "X-Generator: Poedit 1.8.10\n"
    11 "Project-Id-Version: 4.6.x\n"
     10"X-Generator: Poedit 2.1\n"
     11"Project-Id-Version: Development (4.9.x)\n"
    1212"Language: de_DE\n"
    1313
    1414#. translators: Translate this to the correct language tag for your locale, see
     
    4040#: wp-includes/script-loader.php:620
    4141msgid "Update %s now"
    4242msgstr "Jetzt %s aktualisieren"
     43
     44#. translators: Privacy data request subject. 1: Site name, 2: Name of the action
     45#: wp-includes/user.php:3445
     46msgid "[%1$s] Confirm Action: %2$s"
     47msgstr "[%1$s] Aktion bestätigen: %2$s"
  • tests/phpunit/data/languages/es_ES.po

    Cannot display: file marked as a binary type.
    svn:mime-type = application/octet-stream
     
    1 # Translation of Development (4.4.x) in Spanish (Spain)
    2 # This file is distributed under the same license as the Development (4.4.x) package.
     1# Translation of Development (4.9.x) in Spanish (Spain)
     2# This file is distributed under the same license as the Development (4.9.x) package.
    33msgid ""
    44msgstr ""
    5 "PO-Revision-Date: 2016-10-25 18:28+0200\n"
     5"PO-Revision-Date: 2018-08-01 17:08-0400\n"
    66"MIME-Version: 1.0\n"
    77"Content-Type: text/plain; charset=UTF-8\n"
    88"Content-Transfer-Encoding: 8bit\n"
    99"Plural-Forms: nplurals=2; plural=n != 1;\n"
    10 "X-Generator: Poedit 1.8.10\n"
    11 "Project-Id-Version: Development (4.4.x)\n"
     10"X-Generator: Poedit 2.1\n"
     11"Project-Id-Version: Development (4.9.x)\n"
    1212"Language: es_ES\n"
    1313
    1414#. translators: 'rtl' or 'ltr'. This sets the text direction for WordPress.
     
    3636msgctxt "menu"
    3737msgid "(Currently set to: %s)"
    3838msgstr "(Actualmente fijado en: %s)"
     39
     40#. translators: Privacy data request subject. 1: Site name, 2: Name of the action
     41#: wp-includes/user.php:3445
     42msgid "[%1$s] Confirm Action: %2$s"
     43msgstr "[%1$s] Confirma la acción: %2$s"
  • tests/phpunit/tests/privacy/wpPrivacySendPersonalDataExportEmail.php

     
    3939         *
    4040         * @since 4.9.6
    4141         */
    42         function setUp() {
     42        public function setUp() {
    4343                parent::setUp();
    4444                reset_phpmailer_instance();
    4545        }
     
    4949         *
    5050         * @since 4.9.6
    5151         */
    52         function tearDown() {
     52        public function tearDown() {
    5353                reset_phpmailer_instance();
    5454                parent::tearDown();
    5555        }
     
    9595                $request_id = 0;
    9696                $email_sent = wp_privacy_send_personal_data_export_email( $request_id );
    9797                $this->assertWPError( $email_sent );
    98                 $this->assertSame( 'invalid', $email_sent->get_error_code() );
     98                $this->assertSame( 'invalid_request', $email_sent->get_error_code() );
    9999
    100100                $request_id = PHP_INT_MAX;
    101101                $email_sent = wp_privacy_send_personal_data_export_email( $request_id );
    102102                $this->assertWPError( $email_sent );
    103                 $this->assertSame( 'invalid', $email_sent->get_error_code() );
     103                $this->assertSame( 'invalid_request', $email_sent->get_error_code() );
    104104        }
    105105
    106106        /**
     
    111111        public function test_return_wp_error_when_send_fails() {
    112112                add_filter( 'wp_mail_from', '__return_empty_string' ); // Cause `wp_mail()` to return false.
    113113                $email_sent = wp_privacy_send_personal_data_export_email( self::$request_id );
    114                 remove_filter( 'wp_mail_from', '__return_empty_string' );
    115114
    116115                $this->assertWPError( $email_sent );
    117                 $this->assertSame( 'error', $email_sent->get_error_code() );
     116                $this->assertSame( 'privacy_email_error', $email_sent->get_error_code() );
    118117        }
    119118
    120119        /**
     
    125124        public function test_export_expiration_should_be_filterable() {
    126125                add_filter( 'wp_privacy_export_expiration', array( $this, 'modify_export_expiration' ) );
    127126                wp_privacy_send_personal_data_export_email( self::$request_id );
    128                 remove_filter( 'wp_privacy_export_expiration', array( $this, 'modify_export_expiration' ) );
    129127
    130128                $mailer = tests_retrieve_phpmailer_instance();
    131129                $this->assertContains( 'we will automatically delete the file on December 18, 2017,', $mailer->get_sent()->body );
     
    152150        public function test_email_content_should_be_filterable() {
    153151                add_filter( 'wp_privacy_personal_data_email_content', array( $this, 'modify_email_content' ), 10, 2 );
    154152                wp_privacy_send_personal_data_export_email( self::$request_id );
    155                 remove_filter( 'wp_privacy_personal_data_email_content', array( $this, 'modify_email_content' ) );
    156153
    157154                $mailer = tests_retrieve_phpmailer_instance();
    158155                $this->assertContains( 'Custom content for request ID: ' . self::$request_id, $mailer->get_sent()->body );
  • tests/phpunit/tests/user/wpSendUserRequest.php

     
     1<?php
     2/**
     3 * Test cases for the `wp_send_user_request()` function.
     4 *
     5 * @package WordPress
     6 * @since 4.9.9
     7 */
     8
     9/**
     10 * Tests_User_WpSendUserRequest class.
     11 *
     12 * @since 4.9.9
     13 *
     14 * @group privacy
     15 * @group user
     16 * @covers wp_send_user_request
     17 */
     18class Tests_User_WpSendUserRequest extends WP_UnitTestCase {
     19
     20        /**
     21         * Test administrator user.
     22         *
     23         * @since 4.9.9
     24         *
     25         * @var WP_User $admin_user
     26         */
     27        protected static $admin_user;
     28
     29        /**
     30         * Test subscriber user.
     31         *
     32         * @since 4.9.9
     33         *
     34         * @var WP_User $test_user
     35         */
     36        protected static $test_user;
     37
     38        /**
     39         * Create users for tests.
     40         *
     41         * @since 4.9.9
     42         *
     43         * @param WP_UnitTest_Factory $factory Test fixture factory.
     44         */
     45        public static function wpSetUpBeforeClass( $factory ) {
     46                self::$admin_user = $factory->user->create_and_get(
     47                        array(
     48                                'user_email' => 'admin@local.dev',
     49                                'role'       => 'administrator',
     50                        )
     51                );
     52
     53                self::$test_user = $factory->user->create_and_get(
     54                        array(
     55                                'user_email' => 'export-user@local.dev',
     56                                'role'       => 'subscriber',
     57                        )
     58                );
     59        }
     60
     61        /**
     62         * Reset the mocked phpmailer instance before each test method.
     63         *
     64         * @since 4.9.9
     65         */
     66        public function setUp() {
     67                parent::setUp();
     68
     69                set_current_screen( 'dashboard' );
     70                reset_phpmailer_instance();
     71        }
     72
     73        /**
     74         * Reset the mocked phpmailer instance after each test method.
     75         *
     76         * @since 4.9.9
     77         */
     78        public function tearDown() {
     79                delete_option( 'WPLANG' );
     80                reset_phpmailer_instance();
     81
     82                unset( $GLOBALS['locale'] );
     83                unset( $GLOBALS['current_screen'] );
     84                unset( $GLOBALS['taxnow'] );
     85                unset( $GLOBALS['typenow'] );
     86
     87                parent::tearDown();
     88        }
     89
     90        /**
     91         * The function should error when the request ID is invalid.
     92         *
     93         * @ticket 43985
     94         */
     95        public function test_should_error_when_invalid_request_id() {
     96                $result = wp_send_user_request( null );
     97
     98                $this->assertWPError( $result );
     99                $this->assertSame( 'invalid_request', $result->get_error_code() );
     100        }
     101
     102        /**
     103         * The function should send a user request export email when the requester is a registered user.
     104         *
     105         * @ticket 43985
     106         */
     107        public function test_should_send_user_request_export_email_when_requester_registered_user() {
     108                $request_id = wp_create_user_request( self::$test_user->user_email, 'export_personal_data' );
     109
     110                $result = wp_send_user_request( $request_id );
     111                $mailer = tests_retrieve_phpmailer_instance();
     112
     113                $this->assertTrue( $result );
     114                $this->assertSame( self::$test_user->user_email, $mailer->get_recipient( 'to' )->address );
     115                $this->assertContains( 'Confirm Action: Export Personal Data', $mailer->get_sent()->subject );
     116                $this->assertContains( 'action=confirmaction&request_id=', $mailer->get_sent()->body );
     117                $this->assertContains( 'Export Personal Data', $mailer->get_sent()->body );
     118        }
     119
     120        /**
     121         * The function should send a user request erase email when the requester is a registered user.
     122         *
     123         * @ticket 43985
     124         */
     125        public function test_should_send_user_request_erase_email_when_requester_registered_user() {
     126                $request_id = wp_create_user_request( self::$test_user->user_email, 'remove_personal_data' );
     127
     128                $result = wp_send_user_request( $request_id );
     129                $mailer = tests_retrieve_phpmailer_instance();
     130
     131                $this->assertTrue( $result );
     132                $this->assertSame( self::$test_user->user_email, $mailer->get_recipient( 'to' )->address );
     133                $this->assertContains( 'Confirm Action: Erase Personal Data', $mailer->get_sent()->subject );
     134                $this->assertContains( 'action=confirmaction&request_id=', $mailer->get_sent()->body );
     135                $this->assertContains( 'Erase Personal Data', $mailer->get_sent()->body );
     136        }
     137
     138        /**
     139         * The function should send a user request export email when the requester is an un-registered user.
     140         *
     141         * @ticket 43985
     142         */
     143        public function test_should_send_user_request_export_email_when_user_not_registered() {
     144                $request_id = wp_create_user_request( self::$test_user->user_email, 'export_personal_data' );
     145
     146                $result = wp_send_user_request( $request_id );
     147                $mailer = tests_retrieve_phpmailer_instance();
     148
     149                $this->assertTrue( $result );
     150                $this->assertSame( self::$test_user->user_email, $mailer->get_recipient( 'to' )->address );
     151                $this->assertContains( 'Confirm Action: Export Personal Data', $mailer->get_sent()->subject );
     152                $this->assertContains( 'action=confirmaction&request_id=', $mailer->get_sent()->body );
     153                $this->assertContains( 'Export Personal Data', $mailer->get_sent()->body );
     154        }
     155
     156        /**
     157         * The function should send a user request erase email when the requester is an un-registered user.
     158         *
     159         * @ticket 43985
     160         */
     161        public function test_should_send_user_request_erase_email_when_user_not_registered() {
     162                $request_id = wp_create_user_request( self::$test_user->user_email, 'remove_personal_data' );
     163
     164                $result = wp_send_user_request( $request_id );
     165                $mailer = tests_retrieve_phpmailer_instance();
     166
     167                $this->assertTrue( $result );
     168                $this->assertSame( self::$test_user->user_email, $mailer->get_recipient( 'to' )->address );
     169                $this->assertContains( 'Confirm Action: Erase Personal Data', $mailer->get_sent()->subject );
     170                $this->assertContains( 'action=confirmaction&request_id=', $mailer->get_sent()->body );
     171                $this->assertContains( 'Erase Personal Data', $mailer->get_sent()->body );
     172        }
     173
     174        /**
     175         * The email subject should be filterable.
     176         *
     177         * @ticket 43985
     178         */
     179        public function test_email_subject_should_be_filterable() {
     180                $request_id = wp_create_user_request( self::$test_user->user_email, 'remove_personal_data' );
     181
     182                add_filter( 'user_request_action_email_subject', array( $this, 'modify_email_subject' ) );
     183                $result = wp_send_user_request( $request_id );
     184                $mailer = tests_retrieve_phpmailer_instance();
     185
     186                $this->assertTrue( $result );
     187                $this->assertSame( 'Custom Email Subject', $mailer->get_sent()->subject );
     188        }
     189
     190        /**
     191         * Filter callback to modify the subject of the email sent when an account action is attempted.
     192         *
     193         * @since 4.9.9
     194         *
     195         * @param string $subject The email subject.
     196         * @return string Filtered email subject.
     197         */
     198        public function modify_email_subject( $subject ) {
     199                return 'Custom Email Subject';
     200        }
     201
     202        /**
     203         * The email content should be filterable.
     204         *
     205         * @ticket 43985
     206         */
     207        public function test_email_content_should_be_filterable() {
     208                $request_id = wp_create_user_request( self::$test_user->user_email, 'remove_personal_data' );
     209
     210                add_filter( 'user_request_action_email_content', array( $this, 'modify_email_content' ), 10, 2 );
     211                $result = wp_send_user_request( $request_id );
     212                $mailer = tests_retrieve_phpmailer_instance();
     213
     214                $this->assertTrue( $result );
     215                $this->assertContains( 'Custom Email Content.', $mailer->get_sent()->body );
     216        }
     217
     218        /**
     219         * Filter callback to modify the content of the email sent when an account action is attempted.
     220         *
     221         * @since 4.9.9
     222         *
     223         * @param string $email_text Confirmation email text.
     224         * @return string $email_text Filtered email text.
     225         */
     226        public function modify_email_content( $email_text ) {
     227                return 'Custom Email Content.';
     228        }
     229
     230        /**
     231         * The function should error when the email was not sent.
     232         *
     233         * @since 4.9.9
     234         *
     235         * @ticket 43985
     236         */
     237        public function test_return_wp_error_when_sending_fails() {
     238                $request_id = wp_create_user_request( 'erase.request.from.unergistered.user@example.com', 'remove_personal_data' );
     239
     240                add_filter( 'wp_mail_from', '__return_empty_string' ); // Cause `wp_mail()` to return false.
     241                $result = wp_send_user_request( $request_id );
     242
     243                $this->assertWPError( $result );
     244                $this->assertSame( 'privacy_email_error', $result->get_error_code() );
     245        }
     246
     247        /**
     248         * The function should respect the user locale settings when the site uses the default locale.
     249         *
     250         * @ticket 43985
     251         */
     252        public function test_should_send_user_request_email_in_user_locale() {
     253                update_user_meta( self::$test_user->ID, 'locale', 'es_ES' );
     254
     255                wp_set_current_user( self::$admin_user->ID );
     256                $request_id = wp_create_user_request( self::$test_user->user_email, 'export_personal_data' );
     257
     258                wp_send_user_request( $request_id );
     259                $mailer = tests_retrieve_phpmailer_instance();
     260
     261                $this->assertContains( 'Confirma la', $mailer->get_sent()->subject );
     262        }
     263
     264        /**
     265         * The function should respect the user locale settings when the site has a default locale and the administrator
     266         * uses the site default.
     267         *
     268         * @ticket 43985
     269         */
     270        public function test_should_send_user_request_email_in_user_locale_when_site_has_non_default_locale() {
     271                update_option( 'WPLANG', 'es_ES' );
     272                switch_to_locale( 'es_ES' );
     273
     274                update_user_meta( self::$test_user->ID, 'locale', 'de_DE' );
     275
     276                wp_set_current_user( self::$admin_user->ID );
     277                $request_id = wp_create_user_request( self::$test_user->user_email, 'remove_personal_data' );
     278
     279                wp_send_user_request( $request_id );
     280                $mailer = tests_retrieve_phpmailer_instance();
     281
     282                $this->assertContains( 'Aktion bestätigen', $mailer->get_sent()->subject );
     283        }
     284
     285        /**
     286         * The function should respect the user locale settings when the site has a default locale, the administrator
     287         * has a different locale, and the user uses the site's default.
     288         *
     289         * @ticket 43985
     290         */
     291        public function test_should_send_user_request_email_in_user_locale_when_admin_has_different_locale_than_site() {
     292                update_option( 'WPLANG', 'es_ES' );
     293                switch_to_locale( 'es_ES' );
     294
     295                update_user_meta( self::$admin_user->ID, 'locale', 'de_DE' );
     296                wp_set_current_user( self::$admin_user->ID );
     297
     298                $request_id = wp_create_user_request( self::$test_user->user_email, 'remove_personal_data' );
     299
     300                wp_set_current_user( self::$admin_user->ID );
     301
     302                $request_id = wp_create_user_request( self::$test_user->user_email, 'export_personal_data' );
     303
     304                wp_send_user_request( $request_id );
     305                $mailer = tests_retrieve_phpmailer_instance();
     306
     307                $this->assertContains( 'Confirma la', $mailer->get_sent()->subject );
     308        }
     309
     310        /**
     311         * The function should respect the user locale settings when the site has a default locale and both the
     312         * administrator and the user use different locales.
     313         *
     314         * @ticket 43985
     315         */
     316        public function test_should_send_user_request_email_in_user_locale_when_admin_and_site_have_different_locales() {
     317                update_option( 'WPLANG', 'es_ES' );
     318                switch_to_locale( 'es_ES' );
     319
     320                update_user_meta( self::$admin_user->ID, 'locale', 'de_DE' );
     321                update_user_meta( self::$test_user->ID, 'locale', 'en_US' );
     322
     323                wp_set_current_user( self::$admin_user->ID );
     324
     325                $request_id = wp_create_user_request( self::$test_user->user_email, 'export_personal_data' );
     326
     327                wp_send_user_request( $request_id );
     328                $mailer = tests_retrieve_phpmailer_instance();
     329
     330                $this->assertContains( 'Confirm Action', $mailer->get_sent()->subject );
     331        }
     332
     333        /**
     334         * The function should respect the site's locale when the request is for an unregistered user and the
     335         * administrator does not use the site's locale.
     336         *
     337         * @ticket 43985
     338         */
     339        public function test_should_send_user_request_email_in_site_locale() {
     340                update_user_meta( self::$admin_user->ID, 'locale', 'es_ES' );
     341                wp_set_current_user( self::$admin_user->ID );
     342
     343                $request_id = wp_create_user_request( 'erase-user-not-registered@example.com', 'erase_personal_data' );
     344
     345                wp_send_user_request( $request_id );
     346                $mailer = tests_retrieve_phpmailer_instance();
     347
     348                $this->assertContains( 'Confirm Action', $mailer->get_sent()->subject );
     349        }
     350
     351        /**
     352         * The function should respect the site's locale when it is not the default (en_US), the request is for an
     353         * unregistered user and the administrator does not use the site's locale.
     354         *
     355         * @ticket 43985
     356         */
     357        public function test_should_send_user_request_email_in_site_locale_not_default() {
     358                update_option( 'WPLANG', 'es_ES' );
     359                switch_to_locale( 'es_ES' );
     360
     361                update_user_meta( self::$admin_user->ID, 'locale', 'de_DE' );
     362                wp_set_current_user( self::$admin_user->ID );
     363
     364                $request_id = wp_create_user_request( 'export-user-not-registered@example.com', 'erase_personal_data' );
     365
     366                wp_send_user_request( $request_id );
     367                $mailer = tests_retrieve_phpmailer_instance();
     368
     369                $this->assertContains( 'Confirma la', $mailer->get_sent()->subject );
     370        }
     371}