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