Make WordPress Core

Ticket #43967: 43967.7.diff

File 43967.7.diff, 7.8 KB (added by desrosj, 7 years ago)
  • tests/phpunit/tests/user/wpPrivacySendRequestConfirmationNotification.php

     
     1<?php
     2/**
     3 * Test cases for the `_wp_privacy_send_request_confirmation_notification()` function.
     4 *
     5 * @since 4.9.8
     6 */
     7
     8/**
     9 * Tests_User_WpPrivacySendRequestConfirmationNotification class.
     10 *
     11 * @since 4.9.8
     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.8
     22         */
     23        public 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.8
     32         */
     33        public function tearDown() {
     34                reset_phpmailer_instance();
     35                parent::tearDown();
     36        }
     37
     38        /**
     39         * The function should not send emails when the request ID does not exist.
     40         *
     41         * @ticket 43967
     42         */
     43        public function test_function_should_not_send_email_when_not_a_valid_request_id() {
     44                _wp_privacy_send_request_confirmation_notification( 1234567890 );
     45                $mailer = tests_retrieve_phpmailer_instance();
     46
     47                $this->assertEmpty( $mailer->mock_sent );
     48        }
     49
     50        /**
     51         * The function should not send emails when the ID passed is not a WP_User_Request.
     52         *
     53         * @ticket 43967
     54         */
     55        public function test_function_should_not_send_email_when_not_a_wp_user_request() {
     56                $post_id = $this->factory->post->create( array(
     57                        'post_type' => 'post',
     58                ) );
     59
     60                _wp_privacy_send_request_confirmation_notification( $post_id );
     61                $mailer = tests_retrieve_phpmailer_instance();
     62
     63                $this->assertEmpty( $mailer->mock_sent );
     64        }
     65
     66        /**
     67         * The function should send an email to the site admin when a user request is confirmed.
     68         *
     69         * @ticket 43967
     70         */
     71        public function test_function_should_send_email_to_site_admin_when_user_request_confirmed() {
     72                $email      = 'export.request.from.unregistered.user@example.com';
     73                $request_id = wp_create_user_request( $email, 'export_personal_data' );
     74
     75                _wp_privacy_account_request_confirmed( $request_id );
     76
     77                _wp_privacy_send_request_confirmation_notification( $request_id );
     78                $mailer = tests_retrieve_phpmailer_instance();
     79
     80                $this->assertSame( 'request-confirmed', get_post_status( $request_id ) );
     81                $this->assertTrue( (bool) get_post_meta( $request_id, '_wp_user_request_confirmed_timestamp', true ) );
     82                $this->assertTrue( (bool) get_post_meta( $request_id, '_wp_admin_notified', true ) );
     83                $this->assertSame( get_site_option( 'admin_email' ), $mailer->get_recipient( 'to' )->address );
     84                $this->assertContains( 'Action Confirmed', $mailer->get_sent()->subject );
     85                $this->assertContains( 'Request: Export Personal Data', $mailer->get_sent()->body );
     86                $this->assertContains( 'A user data privacy request has been confirmed', $mailer->get_sent()->body );
     87        }
     88
     89        /**
     90         * The function should only send an email to the site admin when a user request is confirmed.
     91         *
     92         * @ticket 43967
     93         */
     94        public function test_function_should_only_send_email_to_site_admin_when_user_request_is_confirmed() {
     95                $email      = 'export.request.from.unregistered.user@example.com';
     96                $request_id = wp_create_user_request( $email, 'export_personal_data' );
     97
     98                _wp_privacy_send_request_confirmation_notification( $request_id );
     99
     100                // Non-confirmed request.
     101                $this->assertSame( 'request-pending', get_post_status( $request_id ) );
     102
     103                $mailer = tests_retrieve_phpmailer_instance();
     104                // Email not sent.
     105                $this->assertEmpty( $mailer->mock_sent );
     106                $this->assertEmpty( get_post_meta( $request_id, '_wp_admin_notified', true ) );
     107        }
     108
     109        /**
     110         * The function should only send an email once to the site admin when a user request is confirmed.
     111         *
     112         * @ticket 43967
     113         */
     114        public function test_function_should_only_send_email_once_to_admin_when_user_request_is_confirmed() {
     115                $email      = 'export.request.from.unregistered.user@example.com';
     116                $request_id = wp_create_user_request( $email, 'export_personal_data' );
     117
     118                _wp_privacy_account_request_confirmed( $request_id );
     119
     120                _wp_privacy_send_request_confirmation_notification( $request_id );
     121                $first_mailer = tests_retrieve_phpmailer_instance();
     122                reset_phpmailer_instance();
     123                _wp_privacy_send_request_confirmation_notification( $request_id );
     124                $second_mailer = tests_retrieve_phpmailer_instance();
     125
     126                $this->assertSame( 'request-confirmed', get_post_status( $request_id ) );
     127                // First email sent.
     128                $this->assertNotEmpty( $first_mailer->mock_sent );
     129                // Second email not sent.
     130                $this->assertEmpty( $second_mailer->mock_sent );
     131        }
     132
     133        /**
     134         * The email address should be filterable.
     135         *
     136         * @ticket 43967
     137         */
     138        public function test_email_address_should_be_filterable() {
     139                $email      = 'export.request.from.unregistered.user@example.com';
     140                $request_id = wp_create_user_request( $email, 'export_personal_data' );
     141
     142                _wp_privacy_account_request_confirmed( $request_id );
     143
     144                add_filter( 'user_request_confirmed_email_to', array( $this, 'modify_email_address' ), 10, 2 );
     145                _wp_privacy_send_request_confirmation_notification( $request_id );
     146                remove_all_filters( 'user_request_confirmed_email_to' );
     147
     148                $mailer = tests_retrieve_phpmailer_instance();
     149                $this->assertSame( $email, $mailer->get_recipient( 'to' )->address );
     150        }
     151
     152        /**
     153         * Filter callback that modifies the recipient of the data request confirmation notification.
     154         *
     155         * @since 4.9.8
     156         *
     157         * @param string          $admin_email  The email address of the notification recipient.
     158         * @param WP_User_Request $request_data The request that is initiating the notification.
     159         * @return string Admin email address.
     160         */
     161        public function modify_email_address( $admin_email, $request_data ) {
     162                $admin_email = $request_data->email;
     163                return $admin_email;
     164        }
     165
     166        /**
     167         * The email content should be filterable.
     168         *
     169         * @ticket 43967
     170         */
     171        public function test_email_content_should_be_filterable() {
     172                $email      = 'export.request.from.unregistered.user@example.com';
     173                $request_id = wp_create_user_request( $email, 'export_personal_data' );
     174
     175                _wp_privacy_account_request_confirmed( $request_id );
     176
     177                add_filter( 'user_confirmed_action_email_content', array( $this, 'modify_email_content' ), 10, 2 );
     178                _wp_privacy_send_request_confirmation_notification( $request_id );
     179                remove_filter( 'user_confirmed_action_email_content', array( $this, 'modify_email_content' ), 10 );
     180
     181                $mailer = tests_retrieve_phpmailer_instance();
     182                $this->assertContains( 'Custom content containing email address:' . $email, $mailer->get_sent()->body );
     183        }
     184
     185        /**
     186         * Filter callback that modifies the body of the user request confirmation email.
     187         *
     188         * @since 4.9.8
     189         *
     190         * @param string $email_text Email text.
     191         * @param array  $email_data {
     192         *     Data relating to the account action email.
     193         *
     194         *     @type WP_User_Request $request     User request object.
     195         *     @type string          $user_email  The email address confirming a request
     196         *     @type string          $description Description of the action being performed so the user knows what the email is for.
     197         *     @type string          $manage_url  The link to click manage privacy requests of this type.
     198         *     @type string          $sitename    The site name sending the mail.
     199         *     @type string          $siteurl     The site URL sending the mail.
     200         * }
     201         * @return string Email text.
     202         */
     203        public function modify_email_content( $email_text, $email_data ) {
     204                $email_text = 'Custom content containing email address:' . $email_data['user_email'];
     205                return $email_text;
     206        }
     207
     208}