diff --git a/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php b/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php new file mode 100755 index 0000000000..941229f2aa --- /dev/null +++ b/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php @@ -0,0 +1,308 @@ +export_file_name = ''; + + if ( ! class_exists( 'ZipArchive' ) ) { + $this->markTestSkipped( 'The ZipArchive class is missing.' ); + } + + if ( ! $this->remove_exports_dir() ) { + $this->markTestSkipped( 'Existing exports directory could not be removed. Skipping test.' ); + } + + // We need to override the die handler. Otherwise, the unit tests will die too. + add_filter( 'wp_die_ajax_handler', array( $this, 'get_wp_die_handler' ), 1, 1 ); + add_filter( 'wp_doing_ajax', '__return_true' ); + add_action( 'wp_privacy_personal_data_export_file_created', array( $this, 'action_wp_privacy_personal_data_export_file_created' ) ); + + // Suppress warnings from "Cannot modify header information - headers already sent by". + $this->_error_level = error_reporting(); + error_reporting( $this->_error_level & ~E_WARNING ); + } + + /** + * Tear down the test fixture. + * + * Remove the `wp_die()` override, restore error reporting. + * + * @since 5.2.0 + */ + public function tearDown() { + $this->remove_exports_dir(); + error_reporting( $this->_error_level ); + parent::tearDown(); + } + + /** + * Stores the name of the export zip file to check the file is actually created. + * + * @since 5.2.0 + * + * @param string $archive_name Created export zip file path. + */ + public function action_wp_privacy_personal_data_export_file_created( $archive_name ) { + $this->export_file_name = $archive_name; + } + + /** + * Removes the privacy exports directory, including files and subdirectories. + * + * Ignores hidden files and has upper limit of nested levels, because of `list_files()`. + * + * @since 5.2.0 + * + * @return bool Whether the privacy exports directory was removed. + */ + private function remove_exports_dir() { + /** + * The `$exports_dir` will be a file after the `test_detect_cannot_create_folder()` test method, or, + * if an incorrect value is returned to the `wp_privacy_exports_dir` filter. + */ + if ( is_file( untrailingslashit( self::$exports_dir ) ) ) { + wp_delete_file( untrailingslashit( self::$exports_dir ) ); + return ! is_file( untrailingslashit( self::$exports_dir ) ); + } + + if ( ! is_dir( self::$exports_dir ) ) { + return true; + } + + chmod( self::$exports_dir, 0755 ); + + $files = list_files( self::$exports_dir ); + + // Delete files first, then delete subdirectories. + foreach ( $files as $file ) { + if ( is_file( $file ) ) { + wp_delete_file( $file ); + } + } + + foreach ( $files as $file ) { + if ( is_dir( $file ) ) { + rmdir( $file ); + } + } + + rmdir( self::$exports_dir ); + + return ! is_dir( self::$exports_dir ); + } + + /** + * When a remove request ID is passed to the export function an error should be displayed. + * + * @ticket 44233 + */ + public function test_rejects_remove_requests() { + $request_id = wp_create_user_request( 'removal-requester@example.com', 'remove_personal_data' ); + + $this->setExpectedException( 'WPDieException' ); + $this->expectOutputString( '{"success":false,"data":"Invalid request ID when generating export file."}' ); + wp_privacy_generate_personal_data_export_file( $request_id ); + } + + /** + * When an invalid request ID is passed an error should be displayed. + * + * @ticket 44233 + */ + public function test_invalid_request_id() { + $this->setExpectedException( 'WPDieException' ); + $this->expectOutputString( '{"success":false,"data":"Invalid request ID when generating export file."}' ); + wp_privacy_generate_personal_data_export_file( 123456789 ); + } + + /** + * When the request post title is not a valid email an error should be displayed. + * + * @ticket 44233 + */ + public function test_rejects_requests_with_bad_email_addresses() { + $request_id = wp_create_user_request( 'bad-email-requester@example.com', 'export_personal_data' ); + + wp_update_post( + array( + 'ID' => $request_id, + 'post_title' => 'not-a-valid-email-address', + ) + ); + + $this->setExpectedException( 'WPDieException' ); + $this->expectOutputString( '{"success":false,"data":"Invalid email address when generating export file."}' ); + wp_privacy_generate_personal_data_export_file( $request_id ); + } + + /** + * When the export directory fails to be created an error should be displayed. + * + * @ticket 44233 + */ + public function test_detect_cannot_create_folder() { + // Create a file with the folder name to ensure the function cannot create a folder. + touch( untrailingslashit( self::$exports_dir ) ); + + $this->setExpectedException( 'WPDieException' ); + $this->expectOutputString( '{"success":false,"data":"Unable to create export folder."}' ); + wp_privacy_generate_personal_data_export_file( self::$export_request_id ); + } + + /** + * When the index.html file cannot be created an error should be displayed. + * + * @ticket 44233 + */ + public function test_detects_cannot_create_index() { + // Make the export directory read only so the index.html file can't be created. + mkdir( self::$exports_dir ); + chmod( self::$exports_dir, 0444 ); + + if ( '444' !== substr( decoct( fileperms( self::$exports_dir ) ), -3 ) ) { + $this->markTestSkipped( 'Data export directory permissions were not changed correctly.' ); + } + + $this->setExpectedException( 'WPDieException' ); + $this->expectOutputString( '{"success":false,"data":"Unable to protect export folder from browsing."}' ); + wp_privacy_generate_personal_data_export_file( self::$export_request_id ); + } + + /** + * Test that an index.html file can be added to the export directory. + * + * @ticket 44233 + */ + public function test_creates_index_in_export_folder() { + $this->expectOutputString( '' ); + wp_privacy_generate_personal_data_export_file( self::$export_request_id ); + + $this->assertTrue( file_exists( self::$exports_dir . 'index.html' ) ); + } + + /** + * When the export directory is not writable the report should fail to write. + * + * @ticket 44233 + */ + public function test_detects_cannot_write_html() { + // Make the folder read only so HTML writing will fail. + mkdir( self::$exports_dir ); + touch( self::$exports_dir . 'index.html' ); + chmod( self::$exports_dir, 0555 ); + + if ( '555' !== substr( decoct( fileperms( self::$exports_dir ) ), -3 ) ) { + $this->markTestSkipped( 'Data export directory permissions were not changed correctly.' ); + } + + $this->setExpectedException( 'WPDieException' ); + $this->expectOutputString( '{"success":false,"data":"Unable to open export file (HTML report) for writing."}' ); + wp_privacy_generate_personal_data_export_file( self::$export_request_id ); + + $this->assertEmpty( $this->export_file_name ); + } + + /** + * Test that an export file is successfully created. + * + * @ticket 44233 + */ + public function test_can_succeed() { + wp_privacy_generate_personal_data_export_file( self::$export_request_id ); + + $this->assertTrue( file_exists( $this->export_file_name ) ); + } + + /** + * Test the export file has all the expected parts. + * + * @ticket 44233 + */ + public function test_contents() { + $this->expectOutputString( '' ); + wp_privacy_generate_personal_data_export_file( self::$export_request_id ); + $this->assertTrue( file_exists( $this->export_file_name ) ); + + $report_dir = trailingslashit( self::$exports_dir . 'test_contents' ); + mkdir( $report_dir ); + + $zip = new ZipArchive(); + $opened_zip = $zip->open( $this->export_file_name ); + $this->assertTrue( $opened_zip ); + + $zip->extractTo( $report_dir ); + $zip->close(); + $this->assertTrue( file_exists( $report_dir . 'index.html' ) ); + + $report_contents = file_get_contents( $report_dir . 'index.html' ); + $request = wp_get_user_request_data( self::$export_request_id ); + + $this->assertContains( '

Personal Data Export

', $report_contents ); + $this->assertContains( '

About

', $report_contents ); + $this->assertContains( $request->email, $report_contents ); + } +} diff --git a/tests/phpunit/tests/privacy/wpPrivacyProcessPersonalDataExportPage.php b/tests/phpunit/tests/privacy/wpPrivacyProcessPersonalDataExportPage.php new file mode 100755 index 0000000000..ab576547c9 --- /dev/null +++ b/tests/phpunit/tests/privacy/wpPrivacyProcessPersonalDataExportPage.php @@ -0,0 +1,700 @@ + 'custom-exporter-group-id', + 'group_label' => 'custom-exporter-group-label', + 'item_id' => 'custom-exporter-item-id', + 'data' => array( + array( + 'name' => 'Email', + 'value' => self::$requester_email, + ), + ), + ), + ); + + self::$response_first_page = array( + 'done' => false, + 'data' => $data, + ); + + self::$response_last_page = array( + 'done' => true, + 'data' => $data, + ); + } + + /** + * Setup before each test method. + * + * @since 5.2.0 + */ + public function setUp() { + parent::setUp(); + + // Avoid writing export files to disk. Using `WP_Filesystem_MockFS` is blocked by #44204. + remove_action( 'wp_privacy_personal_data_export_file', 'wp_privacy_generate_personal_data_export_file', 10 ); + + // Register our custom data exporters, very late, so we can override other unrelated exporters. + add_filter( 'wp_privacy_personal_data_exporters', array( $this, 'filter_register_custom_personal_data_exporters' ), 9999 ); + + // Set Ajax context for `wp_send_json()` and `wp_die()`. + add_filter( 'wp_doing_ajax', '__return_true' ); + + // Set up a `wp_die()` ajax handler that throws an exception, to be able to get + // the error message from `wp_send_json_error( 'some message here' )`, + // called by `wp_privacy_process_personal_data_export_page()`. + add_filter( 'wp_die_ajax_handler', array( $this, 'get_wp_die_handler' ), 1, 1 ); + + // Suppress warnings from "Cannot modify header information - headers already sent by". + $this->_error_level = error_reporting(); + error_reporting( $this->_error_level & ~E_WARNING ); + } + + /** + * Clean up after each test method. + * + * @since 5.2.0 + */ + public function tearDown() { + error_reporting( $this->_error_level ); + + parent::tearDown(); + } + + /** + * Filter to register custom personal data exporters. + * + * @since 5.2.0 + * + * @param array $exporters An array of personal data exporters. + * @return array $exporters An array of personal data exporters. + */ + public function filter_register_custom_personal_data_exporters( $exporters ) { + // Let's override other unrelated exporters. + $exporters = array(); + + $exporters[ self::$exporter_key_first ] = array( + 'exporter_friendly_name' => __( 'Custom Exporter #1' ), + 'callback' => null, + ); + $exporters[ self::$exporter_key_last ] = array( + 'exporter_friendly_name' => __( 'Custom Exporter #2' ), + 'callback' => null, + ); + + return $exporters; + } + + /** + * Set up a test method to properly assert an exception. + * + * @since 5.2.0 + * + * @param string $expected_output The expected string exception output. + */ + private function _setup_expected_failure( $expected_output ) { + $this->setExpectedException( 'WPDieException' ); + $this->expectOutputString( $expected_output ); + } + + /** + * Ensure the correct errors are returned when exporter responses are incorrect. + * + * @ticket 44233 + * + * @dataProvider data_wp_privacy_process_personal_data_export_page + * + * @param string|array $expected_response The response from the personal data exporter for the given test. + */ + public function test_wp_privacy_process_personal_data_export_page( $expected_response ) { + $actual_response = wp_privacy_process_personal_data_export_page( + $expected_response, + self::$exporter_index_last, + self::$requester_email, + self::$page_index_last, + self::$request_id, + true, + self::$exporter_key_last + ); + + $this->assertSame( $expected_response, $actual_response ); + } + + /** + * Provide test cases for `test_wp_privacy_process_personal_data_export_page()`. + * + * @since 5.2.0 + * + * @return array { + * @type array { + * @type string|array $response The response from the personal data exporter to test. Can be a string or an array. + * } + * } + */ + public function data_wp_privacy_process_personal_data_export_page() { + return array( + // Response is not an array. + array( + 'not-an-array', + ), + // Missing `done` array key. + array( + array( + 'missing-done-array-key' => true, + ), + ), + // Missing `data` array key. + array( + array( + 'done' => true, + ), + ), + // `data` key is not an array. + array( + array( + 'done' => true, + 'data' => 'not-an-array', + ), + ), + array( + array( + 'done' => true, + 'data' => array(), + ), + ), + ); + } + + /** + * Provide test scenarios for both sending and not sending an email. + * + * @since 5.2.0 + * + * @return array { + * @type array { + * @type bool $send_as_email Whether the final results of the export should be emailed to the user. + * } + * } + */ + public function data_send_as_email_options() { + return array( + array( + true, + ), + array( + false, + ), + ); + } + + /** + * The function should send a JSON error when receiving an invalid request ID. + * + * @ticket 44233 + * + * @dataProvider data_send_as_email_options + * + * @param bool Whether the final results of the export should be emailed to the user. + */ + public function test_send_error_when_invalid_request_id( $send_as_email ) { + $response = array( + 'done' => true, + 'data' => array(), + ); + $invalid_request_id = 0; + + // Process data, given the last exporter, on the last page and send as email. + $this->_setup_expected_failure( '{"success":false,"data":"Invalid request ID when merging exporter data."}' ); + + wp_privacy_process_personal_data_export_page( + $response, + self::$exporter_index_last, + self::$requester_email, + self::$page_index_last, + $invalid_request_id, + $send_as_email, + self::$exporter_key_last + ); + } + + /** + * The function should send a JSON error when the request has an invalid action name. + * + * @ticket 44233 + * + * @dataProvider data_send_as_email_options + * + * @param bool Whether the final results of the export should be emailed to the user. + */ + public function test_send_error_when_invalid_request_action_name( $send_as_email ) { + $response = array( + 'done' => true, + 'data' => array(), + ); + + // Create a valid request ID, but for a different action than the function expects. + $request_id = wp_create_user_request( self::$requester_email, 'remove_personal_data' ); + + // Process data, given the last exporter, on the last page and send as email. + $this->_setup_expected_failure( '{"success":false,"data":"Invalid request ID when merging exporter data."}' ); + + wp_privacy_process_personal_data_export_page( + $response, + self::$exporter_index_last, + self::$requester_email, + self::$page_index_last, + $request_id, + $send_as_email, + self::$exporter_key_last + ); + } + + /** + * The function should store export raw data until the export finishes. Then the meta key should be deleted. + * + * @ticket 44233 + * + * @dataProvider data_send_as_email_options + * + * @param bool Whether the final results of the export should be emailed to the user. + * + */ + public function test_raw_data_post_meta( $send_as_email ) { + $this->assertEmpty( get_post_meta( self::$request_id, '_export_data_raw', true ) ); + + // Adds post meta when processing data, given the first exporter on the first page and send as email. + wp_privacy_process_personal_data_export_page( + self::$response_first_page, + self::$exporter_index_first, + self::$requester_email, + self::$page_index_first, + self::$request_id, + $send_as_email, + self::$exporter_key_first + ); + + $this->assertNotEmpty( get_post_meta( self::$request_id, '_export_data_raw', true ) ); + + // Deletes post meta when processing data, given the last exporter on the last page and send as email. + wp_privacy_process_personal_data_export_page( + self::$response_last_page, + self::$exporter_index_last, + self::$requester_email, + self::$page_index_last, + self::$request_id, + $send_as_email, + self::$exporter_key_last + ); + + $this->assertEmpty( get_post_meta( self::$request_id, '_export_data_raw', true ) ); + } + + /** + * The function should add `_export_data_grouped` post meta for the request, only available + * when personal data export file is generated. + * + * @ticket 44233 + * + * @dataProvider data_send_as_email_options + * + * @param bool Whether the final results of the export should be emailed to the user. + */ + public function test_add_post_meta_with_groups_data_only_available_when_export_file_generated( $send_as_email ) { + // Adds post meta when processing data, given the first exporter on the first page and send as email. + wp_privacy_process_personal_data_export_page( + self::$response_first_page, + self::$exporter_index_first, + self::$requester_email, + self::$page_index_first, + self::$request_id, + true, + self::$exporter_key_first + ); + $this->assertEmpty( get_post_meta( self::$request_id, '_export_data_grouped', true ) ); + + add_action( 'wp_privacy_personal_data_export_file', array( $this, 'action_callback_to_get_export_groups_data' ) ); + + // Process data, given the last exporter on the last page and send as email. + wp_privacy_process_personal_data_export_page( + self::$response_last_page, + self::$exporter_index_last, + self::$requester_email, + self::$page_index_last, + self::$request_id, + true, + self::$exporter_key_last + ); + + $this->assertNotEmpty( $this->_export_data_grouped_fetched_within_callback ); + $this->assertEmpty( get_post_meta( self::$request_id, '_export_data_grouped', true ) ); + } + + /** + * When mail delivery fails, the function should send a JSON error on the last page of the last exporter. + * + * @ticket 44233 + */ + public function test_send_error_on_last_page_of_last_exporter_when_mail_delivery_fails() { + // Cause `wp_mail()` to return false, to simulate mail delivery failure. Filter removed in tearDown. + add_filter( 'wp_mail_from', '__return_empty_string' ); + + // Process data, given the last exporter, on the last page and send as email. + $this->_setup_expected_failure( '{"success":false,"data":"Unable to send personal data export email."}' ); + + wp_privacy_process_personal_data_export_page( + self::$response_last_page, + self::$exporter_index_last, + self::$requester_email, + self::$page_index_last, + self::$request_id, + true, + self::$exporter_key_last + ); + } + + /** + * The function should return the response, containing the export file URL, when not sent as email + * for the last exporter on the last page. + * + * @ticket 44233 + */ + public function test_return_response_with_export_file_url_when_not_sent_as_email_for_last_exporter_on_last_page() { + update_post_meta( self::$request_id, '_export_file_url', self::$export_file_url ); + + // Process data, given the last exporter, on the last page and not send as email. + $actual_response = wp_privacy_process_personal_data_export_page( + self::$response_last_page, + self::$exporter_index_last, + self::$requester_email, + self::$page_index_last, + self::$request_id, + false, + self::$exporter_key_last + ); + + $this->assertArrayHasKey( 'url', $actual_response ); + $this->assertSame( self::$export_file_url, $actual_response['url'] ); + $this->assertSame( self::$response_last_page['done'], $actual_response['done'] ); + $this->assertSame( self::$response_last_page['data'], $actual_response['data'] ); + } + + /** + * The function should return the response, not containing the export file URL, when sent as email + * for the last exporter on the last page. + * + * @ticket 44233 + */ + public function test_return_response_without_export_file_url_when_sent_as_email_for_last_exporter_on_last_page() { + update_post_meta( self::$request_id, '_export_file_url', self::$export_file_url ); + + // Process data, given the last exporter, on the last page and send as email. + $actual_response = wp_privacy_process_personal_data_export_page( + self::$response_last_page, + self::$exporter_index_last, + self::$requester_email, + self::$page_index_last, + self::$request_id, + true, + self::$exporter_key_last + ); + + $this->assertArrayNotHasKey( 'url', $actual_response ); + $this->assertSame( self::$response_last_page['done'], $actual_response['done'] ); + $this->assertSame( self::$response_last_page['data'], $actual_response['data'] ); + } + + /** + * Test that request statuses are properly transitioned. + * + * @ticket 44233 + * + * @dataProvider data_export_page_status_transitions + * + * @param string $expected_status The expected post status after calling the function. + * @param string $response_page The exporter page to pass. Options are 'first' and 'last'. Default 'first'. + * @param string $exporter_index The exporter index to pass. Options are 'first' and 'last'. Default 'first'. + * @param string $page_index The page index to pass. Options are 'first' and 'last'. Default 'first'. + * @param bool $send_as_email If the response should be sent as an email. + * @param string $exporter_key The slug (key) of the exporter to pass. + */ + public function test_request_status_transitions_correctly( $expected_status, $response_page, $exporter_index, $page_index, $send_as_email, $exporter_key ) { + if ( 'first' === $response_page ) { + $response_page = self::$response_first_page; + } else { + $response_page = self::$response_last_page; + } + + if ( 'first' === $exporter_index ) { + $exporter_index = self::$exporter_index_first; + } else { + $exporter_index = self::$exporter_index_last; + } + + if ( 'first' === $page_index ) { + $page_index = self::$page_index_first; + } else { + $page_index = self::$page_index_last; + } + + if ( 'first' === $exporter_key ) { + $exporter_key = self::$exporter_key_first; + } else { + $exporter_key = self::$exporter_key_last; + } + + wp_privacy_process_personal_data_export_page( + $response_page, + $exporter_index, + self::$requester_email, + $page_index, + self::$request_id, + $send_as_email, + $exporter_key + ); + + $this->assertSame( $expected_status, get_post_status( self::$request_id ) ); + } + + /** + * Provide test cases for `test_wp_privacy_process_personal_data_export_page()`. + * + * @since 5.2.0 + * + * @return array { + * @type array { + * @string string $expected_status The expected post status after calling the function. + * @string string $response_page The exporter page to pass. Options are 'first' and 'last'. Default 'first'. + * @string string $exporter_index The exporter index to pass. Options are 'first' and 'last'. Default 'first'. + * @string string $page_index The page index to pass. Options are 'first' and 'last'. Default 'first'. + * @bool bool $send_as_email If the response should be sent as an email. + * @string string $exporter_key The slug (key) of the exporter to pass. + * } + * } + */ + public function data_export_page_status_transitions() { + return array( + // Mark the request as completed for the last exporter on the last page, with and without email. + array( + 'request-completed', + 'last', + 'last', + 'last', + true, + 'last', + ), + array( + 'request-completed', + 'last', + 'last', + 'last', + false, + 'last', + ), + // Leave the request as pending when not the last exporter and not on the last page. + array( + 'request-pending', + 'first', + 'first', + 'first', + true, + 'first', + ), + array( + 'request-pending', + 'first', + 'first', + 'first', + false, + 'first', + ), + // Leave the request as pending when last exporter and not on the last page. + array( + 'request-pending', + 'first', + 'last', + 'first', + true, + 'last', + ), + array( + 'request-pending', + 'first', + 'last', + 'first', + false, + 'last', + ), + // Leave the request as pending when not last exporter on the last page. + array( + 'request-pending', + 'last', + 'first', + 'last', + true, + 'last', + ), + array( + 'request-pending', + 'last', + 'first', + 'last', + false, + 'first', + ), + ); + } + + /** + * A callback for the `wp_privacy_personal_data_export_file` action that stores the + * `_export_data_grouped` meta data locally for testing. + * + * @since 5.2.0 + * + * @param int $request_id Request ID. + */ + public function action_callback_to_get_export_groups_data( $request_id ) { + $this->_export_data_grouped_fetched_within_callback = get_post_meta( $request_id, '_export_data_grouped', true ); + } +}