Index: /trunk/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php
===================================================================
--- /trunk/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php	(revision 44786)
+++ /trunk/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php	(revision 44786)
@@ -0,0 +1,308 @@
+<?php
+/**
+ * Define a class to test `wp_privacy_generate_personal_data_export_file()`.
+ *
+ * @package WordPress
+ * @subpackage UnitTests
+ * @since 5.2.0
+ */
+
+/**
+ * Test cases for `wp_privacy_generate_personal_data_export_file()`.
+ *
+ * @group privacy
+ * @covers ::wp_privacy_generate_personal_data_export_file
+ *
+ * @since 5.2.0
+ */
+class Tests_Privacy_WpPrivacyGeneratePersonalDataExportFile extends WP_UnitTestCase {
+	/**
+	 * An Export Request ID
+	 *
+	 * @since 5.2.0
+	 *
+	 * @var int $export_request_id
+	 */
+	protected static $export_request_id;
+
+	/**
+	 * The full path to the export file for the current test method.
+	 *
+	 * @since 5.2.0
+	 *
+	 * @var string $export_file_name
+	 */
+	public $export_file_name = '';
+
+	/**
+	 * The full path to the exports directory.
+	 *
+	 * @since 5.2.0
+	 *
+	 * @var string $exports_dir
+	 */
+	public static $exports_dir;
+
+	/**
+	 * Create fixtures that are shared by multiple test cases.
+	 *
+	 * @since 5.2.0
+	 *
+	 * @param WP_UnitTest_Factory $factory The base factory object.
+	 */
+	public static function wpSetUpBeforeClass( $factory ) {
+		self::$export_request_id = wp_create_user_request( 'export-requester@example.com', 'export_personal_data' );
+		update_post_meta( self::$export_request_id, '_export_data_grouped', array() );
+		self::$exports_dir = wp_privacy_exports_dir();
+	}
+
+	/**
+	 * Set up the test fixture.
+	 *
+	 * Override `wp_die()`, pretend to be Ajax, and suppress `E_WARNING`s.
+	 *
+	 * @since 5.2.0
+	 */
+	public function setUp() {
+		parent::setUp();
+
+		$this->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( '<h1>Personal Data Export</h1>', $report_contents );
+		$this->assertContains( '<h2>About</h2>', $report_contents );
+		$this->assertContains( $request->email, $report_contents );
+	}
+}
Index: /trunk/tests/phpunit/tests/privacy/wpPrivacyProcessPersonalDataExportPage.php
===================================================================
--- /trunk/tests/phpunit/tests/privacy/wpPrivacyProcessPersonalDataExportPage.php	(revision 44786)
+++ /trunk/tests/phpunit/tests/privacy/wpPrivacyProcessPersonalDataExportPage.php	(revision 44786)
@@ -0,0 +1,700 @@
+<?php
+/**
+ * Test cases for the `wp_privacy_process_personal_data_export_page()` function.
+ *
+ * @package WordPress
+ * @subpackage UnitTests
+ * @since 5.2.0
+ */
+
+/**
+ * Tests_Privacy_WpPrivacyProcessPersonalDataExportPage class.
+ *
+ * @group privacy
+ * @covers ::wp_privacy_process_personal_data_export_page
+ *
+ * @since 5.2.0
+ */
+class Tests_Privacy_WpPrivacyProcessPersonalDataExportPage extends WP_UnitTestCase {
+	/**
+	 * Request ID.
+	 *
+	 * @since 5.2.0
+	 *
+	 * @var int $request_id
+	 */
+	protected static $request_id;
+
+	/**
+	 * Response for the First Page.
+	 *
+	 * @since 5.2.0
+	 *
+	 * @var array $response
+	 */
+	protected static $response_first_page;
+
+	/**
+	 * Response for the Last Page.
+	 *
+	 * @since 5.2.0
+	 *
+	 * @var array $response_last_page
+	 */
+	protected static $response_last_page;
+
+	/**
+	 * Export File Url.
+	 *
+	 * @since 5.2.0
+	 *
+	 * @var string $export_file_url
+	 */
+	protected static $export_file_url;
+
+	/**
+	 * Requester Email.
+	 *
+	 * @since 5.2.0
+	 *
+	 * @var string $requester_email
+	 */
+	protected static $requester_email;
+
+	/**
+	 * Index Of The First Page.
+	 *
+	 * @since 5.2.0
+	 *
+	 * @var int $page
+	 */
+	protected static $page_index_first;
+
+	/**
+	 * Index Of The Last Page.
+	 *
+	 * @since 5.2.0
+	 *
+	 * @var int $page_index_last
+	 */
+	protected static $page_index_last;
+
+	/**
+	 * Index of the First Exporter.
+	 *
+	 * @since 5.2.0
+	 *
+	 * @var int $exporter_index_first
+	 */
+	protected static $exporter_index_first;
+
+	/**
+	 * Index of the Last Exporter.
+	 *
+	 * @since 5.2.0
+	 *
+	 * @var int $exporter_index_last
+	 */
+	protected static $exporter_index_last;
+
+	/**
+	 * Key of the First Exporter.
+	 *
+	 * @since 5.2.0
+	 *
+	 * @var int $exporter_key_first
+	 */
+	protected static $exporter_key_first;
+
+	/**
+	 * Key of the Last Exporter.
+	 *
+	 * @since 5.2.0
+	 *
+	 * @var int $exporter_key_last
+	 */
+	protected static $exporter_key_last;
+
+	/**
+	 * Export data stored on the `wp_privacy_personal_data_export_file` action hook.
+	 *
+	 * @var string $_export_data_grouped_fetched_within_callback
+	 */
+	public $_export_data_grouped_fetched_within_callback;
+
+	/**
+	 * Create user request fixtures shared by test methods.
+	 *
+	 * @since 5.2.0
+	 *
+	 * @param WP_UnitTest_Factory $factory Factory.
+	 */
+	public static function wpSetUpBeforeClass( $factory ) {
+		self::$requester_email      = 'requester@example.com';
+		self::$export_file_url      = wp_privacy_exports_url() . 'wp-personal-data-file-requester-at-example-com-Wv0RfMnGIkl4CFEDEEkSeIdfLmaUrLsl.zip';
+		self::$request_id           = wp_create_user_request( self::$requester_email, 'export_personal_data' );
+		self::$page_index_first     = 1;
+		self::$page_index_last      = 2;
+		self::$exporter_index_first = 1;
+		self::$exporter_index_last  = 2;
+		self::$exporter_key_first   = 'custom-exporter-first';
+		self::$exporter_key_last    = 'custom-exporter-last';
+
+		$data = array(
+			array(
+				'group_id'    => '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 );
+	}
+}
