Skip to content

Commit

Permalink
Tests: Bring some consistency to personal data email notification tests.
Browse files Browse the repository at this point in the history
Includes:
* Adding a test for `wp_privacy_send_personal_data_export_email()` to verify the `user_request` post type.
* Reordering some pre-existing tests to check the request ID and post type first.

Follow-up to [43291], [43499], [44535].

Props garrett-eclipse, berubenic.
See #46560.

git-svn-id: https://develop.svn.wordpress.org/trunk@58912 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
SergeyBiryukov committed Aug 18, 2024
1 parent 34aef31 commit be4fe64
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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 );
Expand All @@ -131,19 +111,57 @@ 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 );

$this->assertWPError( $email_sent );
$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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,26 @@ 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();

$this->assertEmpty( $mailer->mock_sent );
}

/**
* 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'.
)
);

Expand All @@ -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' );

Expand All @@ -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' );

Expand All @@ -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' );

Expand Down

0 comments on commit be4fe64

Please sign in to comment.