Make WordPress Core

Changeset 58912


Ignore:
Timestamp:
08/18/2024 11:43:37 PM (2 years ago)
Author:
SergeyBiryukov
Message:

Tests: Bring some consistency to personal data email notification tests.

Includes:

  • Adding a test for wp_privacy_send_personal_data_export_email() to verify the user_request post type.
  • Reordering some pre-existing tests to check the request ID and post type first.

Follow-up to [43291], [43499], [44535].

Props garrett-eclipse, berubenic.
See #46560.

Location:
trunk/tests/phpunit/tests/privacy
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/privacy/wpPrivacySendErasureFulfillmentNotification.php

    r55337 r58912  
    100100
    101101        /**
     102         * The function should not send an email when the request ID does not exist.
     103         *
     104         * @ticket 44234
     105         */
     106        public function test_should_not_send_email_when_not_a_valid_request_id() {
     107                _wp_privacy_send_erasure_fulfillment_notification( 1234567890 );
     108
     109                $mailer = tests_retrieve_phpmailer_instance();
     110
     111                $this->assertEmpty( $mailer->mock_sent );
     112        }
     113
     114        /**
     115         * The function should not send an email when the ID passed does not correspond to a user request.
     116         *
     117         * @ticket 44234
     118         */
     119        public function test_should_not_send_email_when_not_a_user_request() {
     120                $post_id = self::factory()->post->create(
     121                        array(
     122                                'post_type' => 'post', // Should be 'user_request'.
     123                        )
     124                );
     125
     126                _wp_privacy_send_erasure_fulfillment_notification( $post_id );
     127                $mailer = tests_retrieve_phpmailer_instance();
     128
     129                $this->assertEmpty( $mailer->mock_sent );
     130        }
     131
     132        /**
     133         * The function should not send an email when the request is not completed.
     134         *
     135         * @ticket 44234
     136         */
     137        public function test_should_not_send_email_when_request_not_completed() {
     138                wp_update_post(
     139                        array(
     140                                'ID'          => self::$request_id,
     141                                'post_status' => 'request-confirmed', // Should be 'request-completed'.
     142                        )
     143                );
     144
     145                _wp_privacy_send_erasure_fulfillment_notification( self::$request_id );
     146
     147                $mailer = tests_retrieve_phpmailer_instance();
     148
     149                $this->assertEmpty( $mailer->mock_sent );
     150                $this->assertFalse( metadata_exists( 'post', self::$request_id, '_wp_user_notified' ) );
     151        }
     152
     153        /**
    102154         * The function should send an email when a valid request ID is passed.
    103155         *
     
    284336
    285337        /**
    286          * The function should not send an email when the request ID does not exist.
    287          *
    288          * @ticket 44234
    289          */
    290         public function test_should_not_send_email_when_passed_invalid_request_id() {
    291                 _wp_privacy_send_erasure_fulfillment_notification( 1234567890 );
    292 
    293                 $mailer = tests_retrieve_phpmailer_instance();
    294 
    295                 $this->assertEmpty( $mailer->mock_sent );
    296         }
    297 
    298         /**
    299          * The function should not send an email when the ID passed does not correspond to a user request.
    300          *
    301          * @ticket 44234
    302          */
    303         public function test_should_not_send_email_when_not_user_request() {
    304                 $post_id = self::factory()->post->create(
    305                         array(
    306                                 'post_type' => 'post', // Should be 'user_request'.
    307                         )
    308                 );
    309 
    310                 _wp_privacy_send_erasure_fulfillment_notification( $post_id );
    311                 $mailer = tests_retrieve_phpmailer_instance();
    312 
    313                 $this->assertEmpty( $mailer->mock_sent );
    314         }
    315 
    316         /**
    317          * The function should not send an email when the request is not completed.
    318          *
    319          * @ticket 44234
    320          */
    321         public function test_should_not_send_email_when_request_not_completed() {
    322                 wp_update_post(
    323                         array(
    324                                 'ID'          => self::$request_id,
    325                                 'post_status' => 'request-confirmed', // Should be 'request-completed'.
    326                         )
    327                 );
    328 
    329                 _wp_privacy_send_erasure_fulfillment_notification( self::$request_id );
    330 
    331                 $mailer = tests_retrieve_phpmailer_instance();
    332 
    333                 $this->assertEmpty( $mailer->mock_sent );
    334                 $this->assertFalse( metadata_exists( 'post', self::$request_id, '_wp_user_notified' ) );
    335         }
    336 
    337         /**
    338338         * The function should respect the user locale settings when the site uses the default locale.
    339339         *
  • trunk/tests/phpunit/tests/privacy/wpPrivacySendPersonalDataExportEmail.php

    r55337 r58912  
    4646         */
    4747        protected static $admin_user;
    48 
    49         /**
    50          * Reset the mocked phpmailer instance before each test method.
    51          *
    52          * @since 4.9.6
    53          */
    54         public function set_up() {
    55                 parent::set_up();
    56                 reset_phpmailer_instance();
    57         }
    58 
    59         /**
    60          * Reset the mocked phpmailer instance after each test method.
    61          *
    62          * @since 4.9.6
    63          */
    64         public function tear_down() {
    65                 reset_phpmailer_instance();
    66                 restore_previous_locale();
    67                 parent::tear_down();
    68         }
    6948
    7049        /**
     
    9675
    9776        /**
     77         * Reset the mocked phpmailer instance before each test method.
     78         *
     79         * @since 4.9.6
     80         */
     81        public function set_up() {
     82                parent::set_up();
     83                reset_phpmailer_instance();
     84        }
     85
     86        /**
     87         * Reset the mocked phpmailer instance after each test method.
     88         *
     89         * @since 4.9.6
     90         */
     91        public function tear_down() {
     92                reset_phpmailer_instance();
     93                restore_previous_locale();
     94                parent::tear_down();
     95        }
     96
     97        /**
     98         * The function should error when the request ID does not exist.
     99         *
     100         * @since 4.9.6
     101         */
     102        public function test_should_return_wp_error_when_not_a_valid_request_id() {
     103                $request_id = 0;
     104                $email_sent = wp_privacy_send_personal_data_export_email( $request_id );
     105                $this->assertWPError( $email_sent );
     106                $this->assertSame( 'invalid_request', $email_sent->get_error_code() );
     107
     108                $request_id = PHP_INT_MAX;
     109                $email_sent = wp_privacy_send_personal_data_export_email( $request_id );
     110                $this->assertWPError( $email_sent );
     111                $this->assertSame( 'invalid_request', $email_sent->get_error_code() );
     112        }
     113
     114        /**
     115         * The function should error when the ID passed does not correspond to a user request.
     116         *
     117         * @since 6.7.0
     118         * @ticket 46560
     119         */
     120        public function test_should_return_wp_error_when_not_a_user_request() {
     121                $post_id = self::factory()->post->create(
     122                        array(
     123                                'post_type' => 'post', // Should be 'user_request'.
     124                        )
     125                );
     126
     127                $email_sent = wp_privacy_send_personal_data_export_email( $post_id );
     128                $this->assertWPError( $email_sent );
     129                $this->assertSame( 'invalid_request', $email_sent->get_error_code() );
     130        }
     131
     132        /**
     133         * The function should error when the email was not sent.
     134         *
     135         * @since 4.9.6
     136         */
     137        public function test_should_return_wp_error_when_sending_fails() {
     138                add_filter( 'wp_mail_from', '__return_empty_string' ); // Cause `wp_mail()` to return false.
     139                $email_sent = wp_privacy_send_personal_data_export_email( self::$request_id );
     140
     141                $this->assertWPError( $email_sent );
     142                $this->assertSame( 'privacy_email_error', $email_sent->get_error_code() );
     143        }
     144
     145        /**
    98146         * The function should send an export link to the requester when the user request is confirmed.
    99147         */
    100         public function test_function_should_send_export_link_to_requester() {
     148        public function test_should_send_export_link_to_requester() {
    101149                $exports_url      = wp_privacy_exports_url();
    102150                $export_file_name = 'wp-personal-data-file-Wv0RfMnGIkl4CFEDEEkSeIdfLmaUrLsl.zip';
     
    113161                $this->assertStringContainsString( 'please download it', $mailer->get_sent()->body );
    114162                $this->assertTrue( $email_sent );
    115         }
    116 
    117         /**
    118          * The function should error when the request ID is invalid.
    119          *
    120          * @since 4.9.6
    121          */
    122         public function test_function_should_error_when_request_id_invalid() {
    123                 $request_id = 0;
    124                 $email_sent = wp_privacy_send_personal_data_export_email( $request_id );
    125                 $this->assertWPError( $email_sent );
    126                 $this->assertSame( 'invalid_request', $email_sent->get_error_code() );
    127 
    128                 $request_id = PHP_INT_MAX;
    129                 $email_sent = wp_privacy_send_personal_data_export_email( $request_id );
    130                 $this->assertWPError( $email_sent );
    131                 $this->assertSame( 'invalid_request', $email_sent->get_error_code() );
    132         }
    133 
    134         /**
    135          * The function should error when the email was not sent.
    136          *
    137          * @since 4.9.6
    138          */
    139         public function test_return_wp_error_when_send_fails() {
    140                 add_filter( 'wp_mail_from', '__return_empty_string' ); // Cause `wp_mail()` to return false.
    141                 $email_sent = wp_privacy_send_personal_data_export_email( self::$request_id );
    142 
    143                 $this->assertWPError( $email_sent );
    144                 $this->assertSame( 'privacy_email_error', $email_sent->get_error_code() );
    145163        }
    146164
  • trunk/tests/phpunit/tests/privacy/wpPrivacySendRequestConfirmationNotification.php

    r56547 r58912  
    3333
    3434        /**
    35          * The function should not send emails when the request ID does not exist.
    36          *
    37          * @ticket 43967
    38          */
    39         public function test_function_should_not_send_email_when_not_a_valid_request_id() {
     35         * The function should not send an email when the request ID does not exist.
     36         *
     37         * @ticket 43967
     38         */
     39        public function test_should_not_send_email_when_not_a_valid_request_id() {
    4040                _wp_privacy_send_request_confirmation_notification( 1234567890 );
    4141                $mailer = tests_retrieve_phpmailer_instance();
     
    4545
    4646        /**
    47          * The function should not send emails when the ID passed is not a WP_User_Request.
    48          *
    49          * @ticket 43967
    50          */
    51         public function test_function_should_not_send_email_when_not_a_wp_user_request() {
     47         * The function should not send an email when the ID passed does not correspond to a user request.
     48         *
     49         * @ticket 43967
     50         */
     51        public function test_should_not_send_email_when_not_a_user_request() {
    5252                $post_id = self::factory()->post->create(
    5353                        array(
    54                                 'post_type' => 'post',
     54                                'post_type' => 'post', // Should be 'user_request'.
    5555                        )
    5656                );
     
    6767         * @ticket 43967
    6868         */
    69         public function test_function_should_send_email_to_site_admin_when_user_request_confirmed() {
     69        public function test_should_send_email_to_site_admin_when_user_request_confirmed() {
    7070                $email      = 'export.request.from.unregistered.user@example.com';
    7171                $request_id = wp_create_user_request( $email, 'export_personal_data' );
     
    9090         * @ticket 43967
    9191         */
    92         public function test_function_should_only_send_email_to_site_admin_when_user_request_is_confirmed() {
     92        public function test_should_only_send_email_to_site_admin_when_user_request_is_confirmed() {
    9393                $email      = 'export.request.from.unregistered.user@example.com';
    9494                $request_id = wp_create_user_request( $email, 'export_personal_data' );
     
    110110         * @ticket 43967
    111111         */
    112         public function test_function_should_only_send_email_once_to_admin_when_user_request_is_confirmed() {
     112        public function test_should_only_send_email_once_to_admin_when_user_request_is_confirmed() {
    113113                $email      = 'export.request.from.unregistered.user@example.com';
    114114                $request_id = wp_create_user_request( $email, 'export_personal_data' );
Note: See TracChangeset for help on using the changeset viewer.