| | 1 | <?php |
| | 2 | /** |
| | 3 | * Test cases for the `_wp_privacy_send_erasure_fulfillment_notification()` function. |
| | 4 | * |
| | 5 | * @package WordPress |
| | 6 | * @subpackage UnitTests |
| | 7 | * @since 4.9.9 |
| | 8 | */ |
| | 9 | |
| | 10 | /** |
| | 11 | * Tests_Privacy_WpPrivacySendErasureFulfillmentNotification class. |
| | 12 | * |
| | 13 | * @group privacy |
| | 14 | * @covers ::_wp_privacy_send_erasure_fulfillment_notification |
| | 15 | * |
| | 16 | * @since 4.9.9 |
| | 17 | */ |
| | 18 | class Tests_Privacy_WpPrivacySendErasureFulfillmentNotification extends WP_UnitTestCase { |
| | 19 | /** |
| | 20 | * Request ID. |
| | 21 | * |
| | 22 | * @since 4.9.9 |
| | 23 | * |
| | 24 | * @var int $request_id |
| | 25 | */ |
| | 26 | protected static $request_id; |
| | 27 | |
| | 28 | /** |
| | 29 | * Requester Email. |
| | 30 | * |
| | 31 | * @since 4.9.9 |
| | 32 | * |
| | 33 | * @var string $requester_email |
| | 34 | */ |
| | 35 | protected static $requester_email; |
| | 36 | |
| | 37 | /** |
| | 38 | * Create user request fixtures shared by test methods. |
| | 39 | * |
| | 40 | * @since 4.9.9 |
| | 41 | * |
| | 42 | * @param WP_UnitTest_Factory $factory Factory. |
| | 43 | */ |
| | 44 | public static function wpSetUpBeforeClass( $factory ) { |
| | 45 | self::$requester_email = 'erase-my-data@local.test'; |
| | 46 | self::$request_id = wp_create_user_request( self::$requester_email, 'erase_personal_data' ); |
| | 47 | wp_update_post( |
| | 48 | array( |
| | 49 | 'ID' => self::$request_id, |
| | 50 | 'post_status' => 'request-completed', |
| | 51 | ) |
| | 52 | ); |
| | 53 | } |
| | 54 | |
| | 55 | /** |
| | 56 | * Reset the mocked PHPMailer instance before each test method. |
| | 57 | * |
| | 58 | * @since 4.9.9 |
| | 59 | */ |
| | 60 | public function setUp() { |
| | 61 | parent::setUp(); |
| | 62 | reset_phpmailer_instance(); |
| | 63 | } |
| | 64 | |
| | 65 | /** |
| | 66 | * Reset the mocked PHPMailer instance after each test method. |
| | 67 | * |
| | 68 | * @since 4.9.9 |
| | 69 | */ |
| | 70 | public function tearDown() { |
| | 71 | reset_phpmailer_instance(); |
| | 72 | parent::tearDown(); |
| | 73 | } |
| | 74 | |
| | 75 | /** |
| | 76 | * The function should send an email when a valid request ID is passed. |
| | 77 | * |
| | 78 | * @ticket 44234 |
| | 79 | */ |
| | 80 | public function test_should_send_email_no_privacy_policy() { |
| | 81 | |
| | 82 | _wp_privacy_send_erasure_fulfillment_notification( self::$request_id ); |
| | 83 | |
| | 84 | $mailer = tests_retrieve_phpmailer_instance(); |
| | 85 | $this->assertContains( self::$requester_email, $mailer->get_recipient( 'to' )->address ); |
| | 86 | $this->assertContains( 'Erasure Request Fulfilled', $mailer->get_sent()->subject ); |
| | 87 | $this->assertContains( 'Your request to erase your personal data', $mailer->get_sent()->body ); |
| | 88 | $this->assertContains( 'has been completed.', $mailer->get_sent()->body ); |
| | 89 | $this->assertContains( wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ), $mailer->get_sent()->body ); |
| | 90 | $this->assertContains( home_url(), $mailer->get_sent()->body ); |
| | 91 | |
| | 92 | $this->assertNotContains( 'you can also read our privacy policy', $mailer->get_sent()->body ); |
| | 93 | $this->assertTrue( (bool) get_post_meta( self::$request_id, '_wp_user_notified', true ) ); |
| | 94 | } |
| | 95 | |
| | 96 | /** |
| | 97 | * The email should include a link to the site's privacy policy when set. |
| | 98 | * |
| | 99 | * @ticket 44234 |
| | 100 | */ |
| | 101 | public function test_should_send_email_with_privacy_policy() { |
| | 102 | $privacy_policy = $this->factory->post->create( |
| | 103 | array( |
| | 104 | 'post_type' => 'page', |
| | 105 | 'title' => 'Site Privacy Policy', |
| | 106 | 'post_status' => 'publish', |
| | 107 | ) |
| | 108 | ); |
| | 109 | update_option( 'wp_page_for_privacy_policy', $privacy_policy ); |
| | 110 | |
| | 111 | _wp_privacy_send_erasure_fulfillment_notification( self::$request_id ); |
| | 112 | |
| | 113 | $mailer = tests_retrieve_phpmailer_instance(); |
| | 114 | |
| | 115 | $this->assertContains( self::$requester_email, $mailer->get_recipient( 'to' )->address ); |
| | 116 | $this->assertContains( 'you can also read our privacy policy', $mailer->get_sent()->body ); |
| | 117 | $this->assertContains( get_privacy_policy_url(), $mailer->get_sent()->body ); |
| | 118 | $this->assertTrue( (bool) get_post_meta( self::$request_id, '_wp_user_notified', true ) ); |
| | 119 | } |
| | 120 | |
| | 121 | /** |
| | 122 | * The function should send a fulfillment email only once. |
| | 123 | * |
| | 124 | * @ticket 44234 |
| | 125 | */ |
| | 126 | public function test_should_send_email_only_once() { |
| | 127 | // First function call. |
| | 128 | _wp_privacy_send_erasure_fulfillment_notification( self::$request_id ); |
| | 129 | |
| | 130 | $mailer = tests_retrieve_phpmailer_instance(); |
| | 131 | |
| | 132 | // Should send an email. |
| | 133 | $this->assertContains( self::$requester_email, $mailer->get_recipient( 'to' )->address ); |
| | 134 | $this->assertContains( 'Erasure Request Fulfilled', $mailer->get_sent()->subject ); |
| | 135 | $this->assertTrue( (bool) get_post_meta( self::$request_id, '_wp_user_notified', true ) ); |
| | 136 | |
| | 137 | reset_phpmailer_instance(); |
| | 138 | |
| | 139 | // Second function call. |
| | 140 | _wp_privacy_send_erasure_fulfillment_notification( self::$request_id ); |
| | 141 | |
| | 142 | $mailer = tests_retrieve_phpmailer_instance(); |
| | 143 | |
| | 144 | // Should not send an email. |
| | 145 | $this->assertEmpty( $mailer->mock_sent ); |
| | 146 | $this->assertTrue( metadata_exists( 'post', self::$request_id, '_wp_user_notified' ) ); |
| | 147 | } |
| | 148 | |
| | 149 | /** |
| | 150 | * The email address of the recipient of the fulfillment notification should be filterable. |
| | 151 | * |
| | 152 | * @ticket 44234 |
| | 153 | */ |
| | 154 | public function test_email_address_of_recipient_should_be_filterable() { |
| | 155 | add_filter( 'user_erasure_fulfillment_email_to', array( $this, 'filter_email_address' ) ); |
| | 156 | _wp_privacy_send_erasure_fulfillment_notification( self::$request_id ); |
| | 157 | |
| | 158 | $mailer = tests_retrieve_phpmailer_instance(); |
| | 159 | |
| | 160 | $this->assertSame( 'modified-' . self::$requester_email, $mailer->get_recipient( 'to' )->address ); |
| | 161 | } |
| | 162 | |
| | 163 | /** |
| | 164 | * Filter callback that modifies the email address of the recipient of the fulfillment notification. |
| | 165 | * |
| | 166 | * @since 4.9.9 |
| | 167 | * |
| | 168 | * @param string $user_email The email address of the notification recipient. |
| | 169 | * @return string $user_email The email address of the notification recipient. |
| | 170 | */ |
| | 171 | public function filter_email_address( $user_email ) { |
| | 172 | return 'modified-' . $user_email; |
| | 173 | } |
| | 174 | |
| | 175 | /** |
| | 176 | * The email subject of the fulfillment notification should be filterable. |
| | 177 | * |
| | 178 | * @ticket 44234 |
| | 179 | */ |
| | 180 | public function test_email_subject_should_be_filterable() { |
| | 181 | add_filter( 'user_erasure_complete_email_subject', array( $this, 'filter_email_subject' ) ); |
| | 182 | _wp_privacy_send_erasure_fulfillment_notification( self::$request_id ); |
| | 183 | |
| | 184 | $mailer = tests_retrieve_phpmailer_instance(); |
| | 185 | |
| | 186 | $this->assertSame( 'Modified subject', $mailer->get_sent()->subject ); |
| | 187 | } |
| | 188 | |
| | 189 | /** |
| | 190 | * Filter callback that modifies the email subject of the data erasure fulfillment notification. |
| | 191 | * |
| | 192 | * @since 4.9.9 |
| | 193 | * |
| | 194 | * @param string $subject The email subject. |
| | 195 | * @return string $subject The email subject. |
| | 196 | */ |
| | 197 | public function filter_email_subject( $subject ) { |
| | 198 | return 'Modified subject'; |
| | 199 | } |
| | 200 | |
| | 201 | /** |
| | 202 | * The email body text of the fulfillment notification should be filterable. |
| | 203 | * |
| | 204 | * @ticket 44234 |
| | 205 | */ |
| | 206 | public function test_email_body_text_should_be_filterable() { |
| | 207 | add_filter( 'user_confirmed_action_email_content', array( $this, 'filter_email_body_text' ) ); |
| | 208 | _wp_privacy_send_erasure_fulfillment_notification( self::$request_id ); |
| | 209 | |
| | 210 | $mailer = tests_retrieve_phpmailer_instance(); |
| | 211 | |
| | 212 | $this->assertSame( 'Modified text', trim( $mailer->get_sent()->body ) ); |
| | 213 | } |
| | 214 | |
| | 215 | /** |
| | 216 | * Filter callback that modifies the email body text of the data erasure fulfillment notification. |
| | 217 | * |
| | 218 | * @since 4.9.9 |
| | 219 | * |
| | 220 | * @param string $email_text Text in the email. |
| | 221 | * @return string $email_text Text in the email. |
| | 222 | */ |
| | 223 | public function filter_email_body_text( $email_text ) { |
| | 224 | return 'Modified text'; |
| | 225 | } |
| | 226 | |
| | 227 | /** |
| | 228 | * The function should not send an email when the request ID does not exist. |
| | 229 | * |
| | 230 | * @ticket 44234 |
| | 231 | */ |
| | 232 | public function test_should_not_send_email_when_passed_invalid_request_id() { |
| | 233 | _wp_privacy_send_erasure_fulfillment_notification( 1234567890 ); |
| | 234 | |
| | 235 | $mailer = tests_retrieve_phpmailer_instance(); |
| | 236 | |
| | 237 | $this->assertEmpty( $mailer->mock_sent ); |
| | 238 | } |
| | 239 | |
| | 240 | /** |
| | 241 | * The function should not send an email when the ID passed does not correspond to a user request. |
| | 242 | * |
| | 243 | * @ticket 44234 |
| | 244 | */ |
| | 245 | public function test_should_not_send_email_when_not_user_request() { |
| | 246 | $post_id = $this->factory->post->create( |
| | 247 | array( |
| | 248 | 'post_type' => 'post', // Should be 'user_request'. |
| | 249 | ) |
| | 250 | ); |
| | 251 | |
| | 252 | _wp_privacy_send_erasure_fulfillment_notification( $post_id ); |
| | 253 | $mailer = tests_retrieve_phpmailer_instance(); |
| | 254 | |
| | 255 | $this->assertEmpty( $mailer->mock_sent ); |
| | 256 | } |
| | 257 | |
| | 258 | /** |
| | 259 | * The function should not send an email when the request is not completed. |
| | 260 | * |
| | 261 | * @ticket 44234 |
| | 262 | */ |
| | 263 | public function test_should_not_send_email_when_request_not_completed() { |
| | 264 | wp_update_post( |
| | 265 | array( |
| | 266 | 'ID' => self::$request_id, |
| | 267 | 'post_status' => 'request-confirmed', // Should be 'request-completed'. |
| | 268 | ) |
| | 269 | ); |
| | 270 | |
| | 271 | _wp_privacy_send_erasure_fulfillment_notification( self::$request_id ); |
| | 272 | |
| | 273 | $mailer = tests_retrieve_phpmailer_instance(); |
| | 274 | |
| | 275 | $this->assertEmpty( $mailer->mock_sent ); |
| | 276 | $this->assertFalse( metadata_exists( 'post', self::$request_id, '_wp_user_notified' ) ); |
| | 277 | } |
| | 278 | |
| | 279 | } |