diff --git a/tests/phpunit/tests/privacy/wpPrivacySendErasureFulfillmentNotification.php b/tests/phpunit/tests/privacy/wpPrivacySendErasureFulfillmentNotification.php index 401528a158da4..bb6195912e233 100644 --- a/tests/phpunit/tests/privacy/wpPrivacySendErasureFulfillmentNotification.php +++ b/tests/phpunit/tests/privacy/wpPrivacySendErasureFulfillmentNotification.php @@ -98,6 +98,58 @@ public function tear_down() { parent::tear_down(); } + /** + * The function should not send an email when the request ID does not exist. + * + * @ticket 44234 + */ + public function test_should_not_send_email_when_not_a_valid_request_id() { + _wp_privacy_send_erasure_fulfillment_notification( 1234567890 ); + + $mailer = tests_retrieve_phpmailer_instance(); + + $this->assertEmpty( $mailer->mock_sent ); + } + + /** + * The function should not send an email when the ID passed does not correspond to a user request. + * + * @ticket 44234 + */ + public function test_should_not_send_email_when_not_a_user_request() { + $post_id = self::factory()->post->create( + array( + 'post_type' => 'post', // Should be 'user_request'. + ) + ); + + _wp_privacy_send_erasure_fulfillment_notification( $post_id ); + $mailer = tests_retrieve_phpmailer_instance(); + + $this->assertEmpty( $mailer->mock_sent ); + } + + /** + * The function should not send an email when the request is not completed. + * + * @ticket 44234 + */ + public function test_should_not_send_email_when_request_not_completed() { + wp_update_post( + array( + 'ID' => self::$request_id, + 'post_status' => 'request-confirmed', // Should be 'request-completed'. + ) + ); + + _wp_privacy_send_erasure_fulfillment_notification( self::$request_id ); + + $mailer = tests_retrieve_phpmailer_instance(); + + $this->assertEmpty( $mailer->mock_sent ); + $this->assertFalse( metadata_exists( 'post', self::$request_id, '_wp_user_notified' ) ); + } + /** * The function should send an email when a valid request ID is passed. * @@ -282,58 +334,6 @@ public function modify_email_headers( $headers ) { return $headers; } - /** - * The function should not send an email when the request ID does not exist. - * - * @ticket 44234 - */ - public function test_should_not_send_email_when_passed_invalid_request_id() { - _wp_privacy_send_erasure_fulfillment_notification( 1234567890 ); - - $mailer = tests_retrieve_phpmailer_instance(); - - $this->assertEmpty( $mailer->mock_sent ); - } - - /** - * The function should not send an email when the ID passed does not correspond to a user request. - * - * @ticket 44234 - */ - public function test_should_not_send_email_when_not_user_request() { - $post_id = self::factory()->post->create( - array( - 'post_type' => 'post', // Should be 'user_request'. - ) - ); - - _wp_privacy_send_erasure_fulfillment_notification( $post_id ); - $mailer = tests_retrieve_phpmailer_instance(); - - $this->assertEmpty( $mailer->mock_sent ); - } - - /** - * The function should not send an email when the request is not completed. - * - * @ticket 44234 - */ - public function test_should_not_send_email_when_request_not_completed() { - wp_update_post( - array( - 'ID' => self::$request_id, - 'post_status' => 'request-confirmed', // Should be 'request-completed'. - ) - ); - - _wp_privacy_send_erasure_fulfillment_notification( self::$request_id ); - - $mailer = tests_retrieve_phpmailer_instance(); - - $this->assertEmpty( $mailer->mock_sent ); - $this->assertFalse( metadata_exists( 'post', self::$request_id, '_wp_user_notified' ) ); - } - /** * The function should respect the user locale settings when the site uses the default locale. * diff --git a/tests/phpunit/tests/privacy/wpPrivacySendPersonalDataExportEmail.php b/tests/phpunit/tests/privacy/wpPrivacySendPersonalDataExportEmail.php index a0f4c96d298b2..37e2e4c282bd5 100644 --- a/tests/phpunit/tests/privacy/wpPrivacySendPersonalDataExportEmail.php +++ b/tests/phpunit/tests/privacy/wpPrivacySendPersonalDataExportEmail.php @@ -46,27 +46,6 @@ class Tests_Privacy_wpPrivacySendPersonalDataExportEmail extends WP_UnitTestCase */ protected static $admin_user; - /** - * Reset the mocked phpmailer instance before each test method. - * - * @since 4.9.6 - */ - public function set_up() { - parent::set_up(); - reset_phpmailer_instance(); - } - - /** - * Reset the mocked phpmailer instance after each test method. - * - * @since 4.9.6 - */ - public function tear_down() { - reset_phpmailer_instance(); - restore_previous_locale(); - parent::tear_down(); - } - /** * Create user request fixtures shared by test methods. * @@ -95,31 +74,32 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { } /** - * The function should send an export link to the requester when the user request is confirmed. + * Reset the mocked phpmailer instance before each test method. + * + * @since 4.9.6 */ - public function test_function_should_send_export_link_to_requester() { - $exports_url = wp_privacy_exports_url(); - $export_file_name = 'wp-personal-data-file-Wv0RfMnGIkl4CFEDEEkSeIdfLmaUrLsl.zip'; - $export_file_url = $exports_url . $export_file_name; - update_post_meta( self::$request_id, '_export_file_name', $export_file_name ); - - $email_sent = wp_privacy_send_personal_data_export_email( self::$request_id ); - $mailer = tests_retrieve_phpmailer_instance(); + public function set_up() { + parent::set_up(); + reset_phpmailer_instance(); + } - $this->assertSame( 'request-confirmed', get_post_status( self::$request_id ) ); - $this->assertSame( self::$requester_email, $mailer->get_recipient( 'to' )->address ); - $this->assertStringContainsString( 'Personal Data Export', $mailer->get_sent()->subject ); - $this->assertStringContainsString( $export_file_url, $mailer->get_sent()->body ); - $this->assertStringContainsString( 'please download it', $mailer->get_sent()->body ); - $this->assertTrue( $email_sent ); + /** + * Reset the mocked phpmailer instance after each test method. + * + * @since 4.9.6 + */ + public function tear_down() { + reset_phpmailer_instance(); + restore_previous_locale(); + parent::tear_down(); } /** - * The function should error when the request ID is invalid. + * The function should error when the request ID does not exist. * * @since 4.9.6 */ - public function test_function_should_error_when_request_id_invalid() { + public function test_should_return_wp_error_when_not_a_valid_request_id() { $request_id = 0; $email_sent = wp_privacy_send_personal_data_export_email( $request_id ); $this->assertWPError( $email_sent ); @@ -131,12 +111,30 @@ public function test_function_should_error_when_request_id_invalid() { $this->assertSame( 'invalid_request', $email_sent->get_error_code() ); } + /** + * The function should error when the ID passed does not correspond to a user request. + * + * @since 6.7.0 + * @ticket 46560 + */ + public function test_should_return_wp_error_when_not_a_user_request() { + $post_id = self::factory()->post->create( + array( + 'post_type' => 'post', // Should be 'user_request'. + ) + ); + + $email_sent = wp_privacy_send_personal_data_export_email( $post_id ); + $this->assertWPError( $email_sent ); + $this->assertSame( 'invalid_request', $email_sent->get_error_code() ); + } + /** * The function should error when the email was not sent. * * @since 4.9.6 */ - public function test_return_wp_error_when_send_fails() { + public function test_should_return_wp_error_when_sending_fails() { add_filter( 'wp_mail_from', '__return_empty_string' ); // Cause `wp_mail()` to return false. $email_sent = wp_privacy_send_personal_data_export_email( self::$request_id ); @@ -144,6 +142,26 @@ public function test_return_wp_error_when_send_fails() { $this->assertSame( 'privacy_email_error', $email_sent->get_error_code() ); } + /** + * The function should send an export link to the requester when the user request is confirmed. + */ + public function test_should_send_export_link_to_requester() { + $exports_url = wp_privacy_exports_url(); + $export_file_name = 'wp-personal-data-file-Wv0RfMnGIkl4CFEDEEkSeIdfLmaUrLsl.zip'; + $export_file_url = $exports_url . $export_file_name; + update_post_meta( self::$request_id, '_export_file_name', $export_file_name ); + + $email_sent = wp_privacy_send_personal_data_export_email( self::$request_id ); + $mailer = tests_retrieve_phpmailer_instance(); + + $this->assertSame( 'request-confirmed', get_post_status( self::$request_id ) ); + $this->assertSame( self::$requester_email, $mailer->get_recipient( 'to' )->address ); + $this->assertStringContainsString( 'Personal Data Export', $mailer->get_sent()->subject ); + $this->assertStringContainsString( $export_file_url, $mailer->get_sent()->body ); + $this->assertStringContainsString( 'please download it', $mailer->get_sent()->body ); + $this->assertTrue( $email_sent ); + } + /** * The export expiration should be filterable. * diff --git a/tests/phpunit/tests/privacy/wpPrivacySendRequestConfirmationNotification.php b/tests/phpunit/tests/privacy/wpPrivacySendRequestConfirmationNotification.php index 951039cb086d1..59d56c0642f35 100644 --- a/tests/phpunit/tests/privacy/wpPrivacySendRequestConfirmationNotification.php +++ b/tests/phpunit/tests/privacy/wpPrivacySendRequestConfirmationNotification.php @@ -32,11 +32,11 @@ public function tear_down() { } /** - * The function should not send emails when the request ID does not exist. + * The function should not send an email when the request ID does not exist. * * @ticket 43967 */ - public function test_function_should_not_send_email_when_not_a_valid_request_id() { + public function test_should_not_send_email_when_not_a_valid_request_id() { _wp_privacy_send_request_confirmation_notification( 1234567890 ); $mailer = tests_retrieve_phpmailer_instance(); @@ -44,14 +44,14 @@ public function test_function_should_not_send_email_when_not_a_valid_request_id( } /** - * The function should not send emails when the ID passed is not a WP_User_Request. + * The function should not send an email when the ID passed does not correspond to a user request. * * @ticket 43967 */ - public function test_function_should_not_send_email_when_not_a_wp_user_request() { + public function test_should_not_send_email_when_not_a_user_request() { $post_id = self::factory()->post->create( array( - 'post_type' => 'post', + 'post_type' => 'post', // Should be 'user_request'. ) ); @@ -66,7 +66,7 @@ public function test_function_should_not_send_email_when_not_a_wp_user_request() * * @ticket 43967 */ - public function test_function_should_send_email_to_site_admin_when_user_request_confirmed() { + public function test_should_send_email_to_site_admin_when_user_request_confirmed() { $email = 'export.request.from.unregistered.user@example.com'; $request_id = wp_create_user_request( $email, 'export_personal_data' ); @@ -89,7 +89,7 @@ public function test_function_should_send_email_to_site_admin_when_user_request_ * * @ticket 43967 */ - public function test_function_should_only_send_email_to_site_admin_when_user_request_is_confirmed() { + public function test_should_only_send_email_to_site_admin_when_user_request_is_confirmed() { $email = 'export.request.from.unregistered.user@example.com'; $request_id = wp_create_user_request( $email, 'export_personal_data' ); @@ -109,7 +109,7 @@ public function test_function_should_only_send_email_to_site_admin_when_user_req * * @ticket 43967 */ - public function test_function_should_only_send_email_once_to_admin_when_user_request_is_confirmed() { + public function test_should_only_send_email_once_to_admin_when_user_request_is_confirmed() { $email = 'export.request.from.unregistered.user@example.com'; $request_id = wp_create_user_request( $email, 'export_personal_data' );