|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Test cases for the `wp_privacy_send_personal_data_export_email()` function. |
| 4 | + * |
| 5 | + * @package WordPress |
| 6 | + * @subpackage UnitTests |
| 7 | + * @since 4.9.6 |
| 8 | + */ |
| 9 | + |
| 10 | +/** |
| 11 | + * Tests_Privacy_WpPrivacySendPersonalDataExportEmail class. |
| 12 | + * |
| 13 | + * @group privacy |
| 14 | + * @covers wp_privacy_send_personal_data_export_email |
| 15 | + * |
| 16 | + * @since 4.9.6 |
| 17 | + */ |
| 18 | +class Tests_Privacy_WpPrivacySendPersonalDataExportEmail extends WP_UnitTestCase { |
| 19 | + /** |
| 20 | + * Request ID. |
| 21 | + * |
| 22 | + * @since 4.9.6 |
| 23 | + * |
| 24 | + * @var int $request_id |
| 25 | + */ |
| 26 | + protected static $request_id; |
| 27 | + |
| 28 | + /** |
| 29 | + * Requester Email. |
| 30 | + * |
| 31 | + * @since 4.9.6 |
| 32 | + * |
| 33 | + * @var string $requester_email |
| 34 | + */ |
| 35 | + protected static $requester_email; |
| 36 | + |
| 37 | + /** |
| 38 | + * Reset the mocked phpmailer instance before each test method. |
| 39 | + * |
| 40 | + * @since 4.9.6 |
| 41 | + */ |
| 42 | + function setUp() { |
| 43 | + parent::setUp(); |
| 44 | + reset_phpmailer_instance(); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Reset the mocked phpmailer instance after each test method. |
| 49 | + * |
| 50 | + * @since 4.9.6 |
| 51 | + */ |
| 52 | + function tearDown() { |
| 53 | + reset_phpmailer_instance(); |
| 54 | + parent::tearDown(); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Create user request fixtures shared by test methods. |
| 59 | + * |
| 60 | + * @since 4.9.6 |
| 61 | + * |
| 62 | + * @param WP_UnitTest_Factory $factory Factory. |
| 63 | + */ |
| 64 | + public static function wpSetUpBeforeClass( $factory ) { |
| 65 | + self::$requester_email = 'requester@example.com'; |
| 66 | + self::$request_id = wp_create_user_request( self::$requester_email, 'export_personal_data' ); |
| 67 | + |
| 68 | + _wp_privacy_account_request_confirmed( self::$request_id ); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * The function should send an export link to the requester when the user request is confirmed. |
| 73 | + */ |
| 74 | + public function test_function_should_send_export_link_to_requester() { |
| 75 | + $archive_url = wp_privacy_exports_url() . 'wp-personal-data-file-requester-at-example-com-Wv0RfMnGIkl4CFEDEEkSeIdfLmaUrLsl.zip'; |
| 76 | + update_post_meta( self::$request_id, '_export_file_url', $archive_url ); |
| 77 | + |
| 78 | + $email_sent = wp_privacy_send_personal_data_export_email( self::$request_id ); |
| 79 | + $mailer = tests_retrieve_phpmailer_instance(); |
| 80 | + |
| 81 | + $this->assertSame( 'request-confirmed', get_post_status( self::$request_id ) ); |
| 82 | + $this->assertSame( self::$requester_email, $mailer->get_recipient( 'to' )->address ); |
| 83 | + $this->assertContains( 'Personal Data Export', $mailer->get_sent()->subject ); |
| 84 | + $this->assertContains( $archive_url, $mailer->get_sent()->body ); |
| 85 | + $this->assertContains( 'please download it', $mailer->get_sent()->body ); |
| 86 | + $this->assertTrue( $email_sent ); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * The function should error when the request ID is invalid. |
| 91 | + * |
| 92 | + * @since 4.9.6 |
| 93 | + */ |
| 94 | + public function test_function_should_error_when_request_id_invalid() { |
| 95 | + $request_id = 0; |
| 96 | + $email_sent = wp_privacy_send_personal_data_export_email( $request_id ); |
| 97 | + $this->assertWPError( $email_sent ); |
| 98 | + $this->assertSame( 'invalid', $email_sent->get_error_code() ); |
| 99 | + |
| 100 | + $request_id = PHP_INT_MAX; |
| 101 | + $email_sent = wp_privacy_send_personal_data_export_email( $request_id ); |
| 102 | + $this->assertWPError( $email_sent ); |
| 103 | + $this->assertSame( 'invalid', $email_sent->get_error_code() ); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * The function should error when the email was not sent. |
| 108 | + * |
| 109 | + * @since 4.9.6 |
| 110 | + */ |
| 111 | + public function test_return_wp_error_when_send_fails() { |
| 112 | + add_filter( 'wp_mail_from', '__return_empty_string' ); // Cause `wp_mail()` to return false. |
| 113 | + $email_sent = wp_privacy_send_personal_data_export_email( self::$request_id ); |
| 114 | + remove_filter( 'wp_mail_from', '__return_empty_string' ); |
| 115 | + |
| 116 | + $this->assertWPError( $email_sent ); |
| 117 | + $this->assertSame( 'error', $email_sent->get_error_code() ); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * The export expiration should be filterable. |
| 122 | + * |
| 123 | + * @since 4.9.6 |
| 124 | + */ |
| 125 | + public function test_export_expiration_should_be_filterable() { |
| 126 | + add_filter( 'wp_privacy_export_expiration', array( $this, 'modify_export_expiration' ) ); |
| 127 | + wp_privacy_send_personal_data_export_email( self::$request_id ); |
| 128 | + remove_filter( 'wp_privacy_export_expiration', array( $this, 'modify_export_expiration' ) ); |
| 129 | + |
| 130 | + $mailer = tests_retrieve_phpmailer_instance(); |
| 131 | + $this->assertContains( 'we will automatically delete the file on December 18, 2017,', $mailer->get_sent()->body ); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Filter callback that modifies the lifetime, in seconds, of a personal data export file. |
| 136 | + * |
| 137 | + * @since 4.9.6 |
| 138 | + * |
| 139 | + * @param int $expiration The expiration age of the export, in seconds. |
| 140 | + * @return int $expiration The expiration age of the export, in seconds. |
| 141 | + */ |
| 142 | + public function modify_export_expiration( $expiration ) { |
| 143 | + // Set date to always be "Mon, 18 Dec 2017 21:30:00 GMT", so can assert a fixed date. |
| 144 | + return 1513632600 - time(); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * The email content should be filterable. |
| 149 | + * |
| 150 | + * @since 4.9.6 |
| 151 | + */ |
| 152 | + public function test_email_content_should_be_filterable() { |
| 153 | + add_filter( 'wp_privacy_personal_data_email_content', array( $this, 'modify_email_content' ), 10, 2 ); |
| 154 | + wp_privacy_send_personal_data_export_email( self::$request_id ); |
| 155 | + remove_filter( 'wp_privacy_personal_data_email_content', array( $this, 'modify_email_content' ) ); |
| 156 | + |
| 157 | + $mailer = tests_retrieve_phpmailer_instance(); |
| 158 | + $this->assertContains( 'Custom content for request ID: ' . self::$request_id, $mailer->get_sent()->body ); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Filter callback that modifies the text of the email sent with a personal data export file. |
| 163 | + * |
| 164 | + * @since 4.9.6 |
| 165 | + * |
| 166 | + * @param string $email_text Text in the email. |
| 167 | + * @param int $request_id The request ID for this personal data export. |
| 168 | + * @return string $email_text Text in the email. |
| 169 | + */ |
| 170 | + public function modify_email_content( $email_text, $request_id ) { |
| 171 | + return 'Custom content for request ID: ' . $request_id; |
| 172 | + } |
| 173 | +} |
0 commit comments