| 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 | * @since 4.9.6 |
| 14 | * |
| 15 | * @group privacy |
| 16 | * @covers wp_privacy_send_personal_data_export_email |
| 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 int $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 | * @param WP_UnitTest_Factory $factory Factory. |
| 61 | */ |
| 62 | public static function wpSetUpBeforeClass( $factory ) { |
| 63 | self::$requester_email = 'requester@example.com'; |
| 64 | self::$request_id = wp_create_user_request( self::$requester_email, 'export_personal_data' ); |
| 65 | $confirmed = _wp_privacy_account_request_confirmed( self::$request_id ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * The function should send an export link to the requester when the user request is confirmed. |
| 70 | * |
| 71 | * @ticket 43546 |
| 72 | */ |
| 73 | public function test_function_should_send_export_link_to_requester() { |
| 74 | $archive_url = wp_privacy_exports_url() . 'wp-personal-data-file-requester-at-example-com-Wv0RfMnGIkl4CFEDEEkSeIdfLmaUrLsl.zip'; |
| 75 | update_post_meta( self::$request_id, '_export_file_url', $archive_url ); |
| 76 | |
| 77 | wp_privacy_send_personal_data_export_email( self::$request_id ); |
| 78 | |
| 79 | $mailer = tests_retrieve_phpmailer_instance(); |
| 80 | $this->assertSame( 'request-confirmed', get_post_status( self::$request_id ) ); |
| 81 | $this->assertSame( self::$requester_email, $mailer->get_recipient( 'to' )->address ); |
| 82 | $this->assertContains( 'Personal Data Export', $mailer->get_sent()->subject ); |
| 83 | $this->assertContains( $archive_url, $mailer->get_sent()->body ); |
| 84 | $this->assertContains( 'please download it', $mailer->get_sent()->body ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * The function should error when the request ID is invalid. |
| 89 | * |
| 90 | * @ticket 43546 |
| 91 | */ |
| 92 | public function test_function_should_error_when_request_id_invalid() { |
| 93 | $request_id = 0; // Invalid request ID. |
| 94 | $email_sent = wp_privacy_send_personal_data_export_email( $request_id ); |
| 95 | $this->assertWPError( $email_sent ); |
| 96 | $this->assertSame( 'Invalid request ID when sending personal data export email.', $email_sent->get_error_message() ); |
| 97 | |
| 98 | $request_id = PHP_INT_MAX; // Invalid request ID. |
| 99 | $email_sent = wp_privacy_send_personal_data_export_email( $request_id ); |
| 100 | $this->assertWPError( $email_sent ); |
| 101 | $this->assertSame( 'Invalid request ID when sending personal data export email.', $email_sent->get_error_message() ); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * The export expiration should be filterable. |
| 106 | * |
| 107 | * @ticket 43546 |
| 108 | */ |
| 109 | public function test_export_expiration_should_be_filterable() { |
| 110 | add_filter( 'wp_privacy_export_expiration', array( $this, 'modify_export_expiration' ) ); |
| 111 | wp_privacy_send_personal_data_export_email( self::$request_id ); |
| 112 | remove_filter( 'wp_privacy_export_expiration', array( $this, 'modify_export_expiration' ) ); |
| 113 | |
| 114 | $mailer = tests_retrieve_phpmailer_instance(); |
| 115 | $this->assertContains( 'we will automatically delete the file on December 18, 2017,', $mailer->get_sent()->body ); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Filter callback that modifies the lifetime, in seconds, of a personal data export file. |
| 120 | * |
| 121 | * @since 4.9.6 |
| 122 | * |
| 123 | * @param int $expiration The expiration age of the export, in seconds. |
| 124 | * @return int $expiration The expiration age of the export, in seconds. |
| 125 | */ |
| 126 | public function modify_export_expiration( $expiration ) { |
| 127 | // Adjust the expiration date to "Mon, 18 Dec 2017 21:30:00 GMT" with Unix Timestamp 1513632600. |
| 128 | $expiration = 1513632600 - time(); |
| 129 | return $expiration; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * The email content should be filterable. |
| 134 | * |
| 135 | * @ticket 43546 |
| 136 | */ |
| 137 | public function test_email_content_should_be_filterable() { |
| 138 | add_filter( 'wp_privacy_personal_data_email_content', array( $this, 'modify_email_content' ), 10, 2 ); |
| 139 | wp_privacy_send_personal_data_export_email( self::$request_id ); |
| 140 | remove_filter( 'wp_privacy_personal_data_email_content', array( $this, 'modify_email_content' ) ); |
| 141 | |
| 142 | $mailer = tests_retrieve_phpmailer_instance(); |
| 143 | $this->assertContains( 'Custom content for request ID:' . self::$request_id, $mailer->get_sent()->body ); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Filter callback that modifies the text of the email sent with a personal data export file. |
| 148 | * |
| 149 | * @since 4.9.6 |
| 150 | * |
| 151 | * @param string $email_text Text in the email. |
| 152 | * @param int $request_id The request ID for this personal data export. |
| 153 | * @return string $email_text Text in the email. |
| 154 | */ |
| 155 | public function modify_email_content( $email_text, $request_id ) { |
| 156 | $email_text = 'Custom content for request ID:' . $request_id; |
| 157 | return $email_text; |
| 158 | } |
| 159 | |
| 160 | } |