WordPress.org

Make WordPress Core

Ticket #43967: 43967.4.diff

File 43967.4.diff, 6.6 KB (added by birgire, 5 weeks ago)
  • new file tests/phpunit/tests/user/wpPrivacySendRequestConfirmationNotification.php

    diff --git tests/phpunit/tests/user/wpPrivacySendRequestConfirmationNotification.php tests/phpunit/tests/user/wpPrivacySendRequestConfirmationNotification.php
    new file mode 100644
    index 0000000..4f2505e
    - +  
     1<?php
     2/**
     3 * Test cases for the `_wp_privacy_send_request_confirmation_notification()` function.
     4 *
     5 * @since 4.9.7
     6 */
     7
     8/**
     9 * Tests_User_WpPrivacySendRequestConfirmationNotification class.
     10 *
     11 * @since 4.9.7
     12 *
     13 * @group privacy
     14 * @group user
     15 * @covers _wp_privacy_send_request_confirmation_notification()
     16 */
     17class Tests_User_WpPrivacySendRequestConfirmationNotification extends WP_UnitTestCase {
     18        /**
     19         * Reset the mocked phpmailer instance before each test method.
     20         *
     21         * @since 4.9.7
     22         */
     23        function setUp() {
     24                parent::setUp();
     25                reset_phpmailer_instance();
     26        }
     27
     28        /**
     29         * Reset the mocked phpmailer instance after each test method.
     30         *
     31         * @since 4.9.7
     32         */
     33        function tearDown() {
     34                reset_phpmailer_instance();
     35                parent::tearDown();
     36        }
     37
     38        /**
     39         * The function should send an email to the site admin when a user request is confirmed.
     40         *
     41         * @ticket 43211
     42         */
     43        public function test_function_should_send_email_to_site_admin_when_user_request_confirmed() {
     44                $email      = 'export.request.from.unregistered.user@example.com';
     45                $request_id = wp_create_user_request( $email, 'export_personal_data' );
     46                $result     = _wp_privacy_account_request_confirmed( $request_id );
     47
     48                _wp_privacy_send_request_confirmation_notification( $request_id );
     49                $mailer = tests_retrieve_phpmailer_instance();
     50
     51                $this->assertSame( 'request-confirmed', get_post_status( $request_id ) );
     52                $this->assertSame( get_site_option( 'admin_email' ), $mailer->get_recipient( 'to' )->address );
     53                $this->assertContains( 'Action Confirmed', $mailer->get_sent()->subject );
     54                $this->assertContains( 'Request: Export Personal Data', $mailer->get_sent()->body );
     55                $this->assertContains( 'A user data privacy request has been confirmed', $mailer->get_sent()->body );
     56        }
     57
     58        /**
     59         * The function should only send an email to the site admin when a user request is confirmed.
     60         *
     61         * @ticket 43211
     62         */
     63        public function test_function_should_only_send_email_to_site_admin_when_user_request_is_confirmed() {
     64                $email      = 'export.request.from.unregistered.user@example.com';
     65                $request_id = wp_create_user_request( $email, 'export_personal_data' );
     66
     67                _wp_privacy_send_request_confirmation_notification( $request_id );
     68
     69                // Non-confirmed request.
     70                $this->assertSame( 'request-pending', get_post_status( $request_id ) );
     71
     72                $mailer = tests_retrieve_phpmailer_instance();
     73                // Email not sent.
     74                $this->assertEmpty( $mailer->mock_sent );
     75        }
     76
     77        /**
     78         * The function should only send an email once to the site admin when a user request is confirmed.
     79         *
     80         * @ticket 43211
     81         */
     82        public function test_function_should_only_send_email_once_to_admin_when_user_request_is_confirmed() {
     83                $email      = 'export.request.from.unregistered.user@example.com';
     84                $request_id = wp_create_user_request( $email, 'export_personal_data' );
     85                $result     = _wp_privacy_account_request_confirmed( $request_id );
     86
     87                _wp_privacy_send_request_confirmation_notification( $request_id );
     88                $first_mailer = tests_retrieve_phpmailer_instance();
     89                reset_phpmailer_instance();
     90                _wp_privacy_send_request_confirmation_notification( $request_id );
     91                $second_mailer = tests_retrieve_phpmailer_instance();
     92
     93                $this->assertSame( 'request-confirmed', get_post_status( $request_id ) );
     94                // First email sent.
     95                $this->assertNotEmpty( $first_mailer->mock_sent );
     96                // Second email not sent.
     97                $this->assertEmpty( $second_mailer->mock_sent );
     98        }
     99
     100        /**
     101         * The email address should be filterable.
     102         *
     103         * @ticket 43211
     104         */
     105        public function test_email_address_should_be_filterable() {
     106                $email      = 'export.request.from.unregistered.user@example.com';
     107                $request_id = wp_create_user_request( $email, 'export_personal_data' );
     108                $result     = _wp_privacy_account_request_confirmed( $request_id );
     109
     110                add_filter( 'user_request_confirmed_email_to', array( $this, 'modify_email_address' ), 10, 2 );
     111                _wp_privacy_send_request_confirmation_notification( $request_id );
     112                remove_filter( 'user_request_confirmed_email_to', array( $this, 'modify_email_address' ), 10 );
     113
     114                $mailer = tests_retrieve_phpmailer_instance();
     115                $this->assertSame( $email, $mailer->get_recipient( 'to' )->address );
     116        }
     117
     118        /**
     119         * Filter callback that modifies the recipient of the data request confirmation notification.
     120         *
     121         * @since 4.9.7
     122         *
     123         * @param string          $admin_email  The email address of the notification recipient.
     124         * @param WP_User_Request $request_data The request that is initiating the notification.
     125         */
     126        public function modify_email_address( $admin_email, $request_data ) {
     127                $admin_email = $request_data->email;
     128                return $admin_email;
     129        }
     130
     131        /**
     132         * The email content should be filterable.
     133         *
     134         * @ticket 43211
     135         */
     136        public function test_email_content_should_be_filterable() {
     137                $email      = 'export.request.from.unregistered.user@example.com';
     138                $request_id = wp_create_user_request( $email, 'export_personal_data' );
     139                $result     = _wp_privacy_account_request_confirmed( $request_id );
     140
     141                add_filter( 'user_confirmed_action_email_content', array( $this, 'modify_email_content' ), 10, 2 );
     142                _wp_privacy_send_request_confirmation_notification( $request_id );
     143                remove_filter( 'user_confirmed_action_email_content', array( $this, 'modify_email_content' ), 10 );
     144
     145                $mailer = tests_retrieve_phpmailer_instance();
     146                $this->assertContains( 'Custom content containing email address:' . $email, $mailer->get_sent()->body );
     147        }
     148
     149        /**
     150         * Filter callback that modifies the body of the user request confirmation email.
     151         *
     152         * @since 4.9.7
     153         *
     154         * @param string $email_text Text in the email.
     155         * @param array  $email_data {
     156         *     Data relating to the account action email.
     157         *
     158         *     @type WP_User_Request $request     User request object.
     159         *     @type string          $user_email  The email address confirming a request
     160         *     @type string          $description Description of the action being performed so the user knows what the email is for.
     161         *     @type string          $manage_url  The link to click manage privacy requests of this type.
     162         *     @type string          $sitename    The site name sending the mail.
     163         *     @type string          $siteurl     The site URL sending the mail.
     164         * }
     165         */
     166        public function modify_email_content( $email_text, $email_data ) {
     167                $email_text = 'Custom content containing email address:' . $email_data['user_email'];
     168                return $email_text;
     169        }
     170
     171}