Make WordPress Core

Ticket #44233: 44233.7.diff

File 44233.7.diff, 29.4 KB (added by desrosj, 6 years ago)
  • tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php

     
     1<?php
     2/**
     3 * Define a class to test `wp_privacy_generate_personal_data_export_file()`.
     4 *
     5 * @package WordPress
     6 * @subpackage UnitTests
     7 * @since 4.9.9
     8 */
     9
     10/**
     11 * Test cases for `wp_privacy_generate_personal_data_export_file()`.
     12 *
     13 * @group privacy
     14 * @covers ::wp_privacy_generate_personal_data_export_file
     15 *
     16 * @since 4.9.9
     17 */
     18class Tests_Privacy_WpPrivacyGeneratePersonalDataExportFile extends WP_UnitTestCase {
     19        /**
     20         * An Export Request ID
     21         *
     22         * @since 4.9.9
     23         *
     24         * @var int $export_request_id
     25         */
     26        protected static $export_request_id;
     27
     28        /**
     29         * The full path to the export file for the current test method.
     30         *
     31         * @since 4.9.9
     32         *
     33         * @var string $export_file_name
     34         */
     35        public $export_file_name = '';
     36
     37        /**
     38         * The full path to the exports directory.
     39         *
     40         * @since 4.9.9
     41         *
     42         * @var string $exports_dir
     43         */
     44        public static $exports_dir;
     45
     46        /**
     47         * Create fixtures that are shared by multiple test cases.
     48         *
     49         * @since 4.9.9
     50         *
     51         * @param WP_UnitTest_Factory $factory The base factory object.
     52         */
     53        public static function wpSetUpBeforeClass( $factory ) {
     54                self::$export_request_id = wp_create_user_request( 'export-requester@example.com', 'export_personal_data' );
     55                update_post_meta( self::$export_request_id, '_export_data_grouped', array() );
     56                self::$exports_dir = wp_privacy_exports_dir();
     57        }
     58
     59        /**
     60         * Set up the test fixture.
     61         *
     62         * Override `wp_die()`, pretend to be Ajax, and suppress `E_WARNING`s.
     63         *
     64         * @since 4.9.9
     65         */
     66        public function setUp() {
     67                parent::setUp();
     68
     69                $this->export_file_name = '';
     70
     71                if ( ! class_exists( 'ZipArchive' ) ) {
     72                        $this->markTestSkipped( 'The ZipArchive class is missing.' );
     73                }
     74
     75                if ( ! $this->remove_exports_dir() ) {
     76                        $this->markTestSkipped( 'Existing exports directory could not be removed. Skipping test.' );
     77                }
     78
     79                // We need to override the die handler. Otherwise, the unit tests will die too.
     80                add_filter( 'wp_die_ajax_handler', array( $this, 'get_wp_die_handler' ), 1, 1 );
     81                add_filter( 'wp_doing_ajax', '__return_true' );
     82                add_action( 'wp_privacy_personal_data_export_file_created', array( $this, 'action_wp_privacy_personal_data_export_file_created' ) );
     83
     84                // Suppress warnings from "Cannot modify header information - headers already sent by".
     85                $this->_error_level = error_reporting();
     86                error_reporting( $this->_error_level & ~E_WARNING );
     87        }
     88
     89        /**
     90         * Tear down the test fixture.
     91         *
     92         * Remove the `wp_die()` override, restore error reporting.
     93         *
     94         * @since 4.9.9
     95         */
     96        public function tearDown() {
     97                $this->remove_exports_dir();
     98                error_reporting( $this->_error_level );
     99                parent::tearDown();
     100        }
     101
     102        /**
     103         * Stores the name of the export zip file to check the file is actually created.
     104         *
     105         * @since 4.9.9
     106         *
     107         * @param string $archive_name Created export zip file path.
     108         */
     109        public function action_wp_privacy_personal_data_export_file_created( $archive_name ) {
     110                $this->export_file_name = $archive_name;
     111        }
     112
     113        /**
     114         * Removes the privacy exports directory, including files and subdirectories.
     115         *
     116         * Ignores hidden files and has upper limit of nested levels, because of `list_files()`.
     117         *
     118         * @since 4.9.9
     119         *
     120         * @return bool Whether the privacy exports directory was removed.
     121         */
     122        private function remove_exports_dir() {
     123                /**
     124                 * The `$exports_dir` will be a file after the `test_detect_cannot_create_folder()` test method, or,
     125                 * if an incorrect value is returned to the `wp_privacy_exports_dir` filter.
     126                 */
     127                if ( is_file( untrailingslashit( self::$exports_dir ) ) ) {
     128                        wp_delete_file( untrailingslashit( self::$exports_dir ) );
     129                        return ! is_file( untrailingslashit( self::$exports_dir ) );
     130                }
     131
     132                if ( ! is_dir( self::$exports_dir ) ) {
     133                        return true;
     134                }
     135
     136                chmod( self::$exports_dir, 0755 );
     137
     138                $files = list_files( self::$exports_dir );
     139
     140                // Delete files first, then delete subdirectories.
     141                foreach ( $files as $file ) {
     142                        if ( is_file( $file ) ) {
     143                                wp_delete_file( $file );
     144                        }
     145                }
     146
     147                foreach ( $files as $file ) {
     148                        if ( is_dir( $file ) ) {
     149                                rmdir( $file );
     150                        }
     151                }
     152
     153                rmdir( self::$exports_dir );
     154
     155                return ! is_dir( self::$exports_dir );
     156        }
     157
     158        /**
     159         * When a remove request ID is passed to the export function an error should be displayed.
     160         *
     161         * @ticket 44233
     162         */
     163        public function test_rejects_remove_requests() {
     164                $request_id = wp_create_user_request( 'removal-requester@example.com', 'remove_personal_data' );
     165
     166                $this->setExpectedException( 'WPDieException' );
     167                $this->expectOutputString( '{"success":false,"data":"Invalid request ID when generating export file."}' );
     168                wp_privacy_generate_personal_data_export_file( $request_id );
     169        }
     170
     171        /**
     172         * When an invalid request ID is passed an error should be displayed.
     173         *
     174         * @ticket 44233
     175         */
     176        public function test_invalid_request_id() {
     177                $this->setExpectedException( 'WPDieException' );
     178                $this->expectOutputString( '{"success":false,"data":"Invalid request ID when generating export file."}' );
     179                wp_privacy_generate_personal_data_export_file( 123456789 );
     180        }
     181
     182        /**
     183         * When the request post title is not a valid email an error should be displayed.
     184         *
     185         * @ticket 44233
     186         */
     187        public function test_rejects_requests_with_bad_email_addresses() {
     188                $request_id = wp_create_user_request( 'bad-email-requester@example.com', 'export_personal_data' );
     189
     190                wp_update_post(
     191                        array(
     192                                'ID'         => $request_id,
     193                                'post_title' => 'not-a-valid-email-address',
     194                        )
     195                );
     196
     197                $this->setExpectedException( 'WPDieException' );
     198                $this->expectOutputString( '{"success":false,"data":"Invalid email address when generating export file."}' );
     199                wp_privacy_generate_personal_data_export_file( $request_id );
     200        }
     201
     202        /**
     203         * When the export directory fails to be created an error should be displayed.
     204         *
     205         * @ticket 44233
     206         */
     207        public function test_detect_cannot_create_folder() {
     208                // Create a file with the folder name to ensure the function cannot create a folder.
     209                touch( untrailingslashit( self::$exports_dir ) );
     210
     211                $this->setExpectedException( 'WPDieException' );
     212                $this->expectOutputString( '{"success":false,"data":"Unable to create export folder."}' );
     213                wp_privacy_generate_personal_data_export_file( self::$export_request_id );
     214        }
     215
     216        /**
     217         * When the index.html file cannot be created an error should be displayed.
     218         *
     219         * @ticket 44233
     220         */
     221        public function test_detects_cannot_create_index() {
     222                // Make the export directory read only so the index.html file can't be created.
     223                mkdir( self::$exports_dir );
     224                chmod( self::$exports_dir, 0444 );
     225
     226                if ( '444' !== substr( decoct( fileperms( self::$exports_dir ) ), -3 ) ) {
     227                        $this->markTestSkipped( 'Data export directory permissions were not changed correctly.' );
     228                }
     229
     230                $this->setExpectedException( 'WPDieException' );
     231                $this->expectOutputString( '{"success":false,"data":"Unable to protect export folder from browsing."}' );
     232                wp_privacy_generate_personal_data_export_file( self::$export_request_id );
     233        }
     234
     235        /**
     236         * Test that an index.html file can be added to the export directory.
     237         *
     238         * @ticket 44233
     239         */
     240        public function test_creates_index_in_export_folder() {
     241                $this->expectOutputString( '' );
     242                wp_privacy_generate_personal_data_export_file( self::$export_request_id );
     243
     244                $this->assertTrue( file_exists( self::$exports_dir . 'index.html' ) );
     245        }
     246
     247        /**
     248         * When the export directory is not writable the report should fail to write.
     249         *
     250         * @ticket 44233
     251         */
     252        public function test_detects_cannot_write_html() {
     253                // Make the folder read only so HTML writing will fail.
     254                mkdir( self::$exports_dir );
     255                touch( self::$exports_dir . 'index.html' );
     256                chmod( self::$exports_dir, 0555 );
     257
     258                if ( '555' !== substr( decoct( fileperms( self::$exports_dir ) ), -3 ) ) {
     259                        $this->markTestSkipped( 'Data export directory permissions were not changed correctly.' );
     260                }
     261
     262                $this->setExpectedException( 'WPDieException' );
     263                $this->expectOutputString( '{"success":false,"data":"Unable to open export file (HTML report) for writing."}' );
     264                wp_privacy_generate_personal_data_export_file( self::$export_request_id );
     265
     266                $this->assertEmpty( $this->export_file_name );
     267        }
     268
     269        /**
     270         * Test that an export file is successfully created.
     271         *
     272         * @ticket 44233
     273         */
     274        public function test_can_succeed() {
     275                wp_privacy_generate_personal_data_export_file( self::$export_request_id );
     276
     277                $this->assertTrue( file_exists( $this->export_file_name ) );
     278        }
     279
     280        /**
     281         * Test the export file has all the expected parts.
     282         *
     283         * @ticket 44233
     284         */
     285        public function test_contents() {
     286                $this->expectOutputString( '' );
     287                wp_privacy_generate_personal_data_export_file( self::$export_request_id );
     288                $this->assertTrue( file_exists( $this->export_file_name ) );
     289
     290                $report_dir = trailingslashit( self::$exports_dir . 'test_contents' );
     291                mkdir( $report_dir );
     292
     293                $zip        = new ZipArchive();
     294                $opened_zip = $zip->open( $this->export_file_name );
     295                $this->assertTrue( $opened_zip );
     296
     297                $zip->extractTo( $report_dir );
     298                $zip->close();
     299                $this->assertTrue( file_exists( $report_dir . 'index.html' ) );
     300
     301                $report_contents = file_get_contents( $report_dir . 'index.html' );
     302                $request         = wp_get_user_request_data( self::$export_request_id );
     303
     304                $this->assertContains( '<h1>Personal Data Export</h1>', $report_contents );
     305                $this->assertContains( '<h2>About</h2>', $report_contents );
     306                $this->assertContains( $request->email, $report_contents );
     307        }
     308}
  • tests/phpunit/tests/privacy/wpPrivacyProcessPersonalDataExportPage.php

     
     1<?php
     2/**
     3 * Test cases for the `wp_privacy_process_personal_data_export_page()` function.
     4 *
     5 * @package WordPress
     6 * @subpackage UnitTests
     7 * @since 4.9.9
     8 */
     9
     10/**
     11 * Tests_Privacy_WpPrivacyProcessPersonalDataExportPage class.
     12 *
     13 * @group privacy
     14 * @covers ::wp_privacy_process_personal_data_export_page
     15 *
     16 * @since 4.9.9
     17 */
     18class Tests_Privacy_WpPrivacyProcessPersonalDataExportPage extends WP_UnitTestCase {
     19        /**
     20         * Request ID.
     21         *
     22         * @since 4.9.9
     23         *
     24         * @var int $request_id
     25         */
     26        protected static $request_id;
     27
     28        /**
     29         * Response for the First Page.
     30         *
     31         * @since 4.9.9
     32         *
     33         * @var array $response
     34         */
     35        protected static $response_first_page;
     36
     37        /**
     38         * Response for the Last Page.
     39         *
     40         * @since 4.9.9
     41         *
     42         * @var array $response_last_page
     43         */
     44        protected static $response_last_page;
     45
     46        /**
     47         * Export File Url.
     48         *
     49         * @since 4.9.9
     50         *
     51         * @var string $export_file_url
     52         */
     53        protected static $export_file_url;
     54
     55        /**
     56         * Requester Email.
     57         *
     58         * @since 4.9.9
     59         *
     60         * @var string $requester_email
     61         */
     62        protected static $requester_email;
     63
     64        /**
     65         * Index Of The First Page.
     66         *
     67         * @since 4.9.9
     68         *
     69         * @var int $page
     70         */
     71        protected static $page_index_first;
     72
     73        /**
     74         * Index Of The Last Page.
     75         *
     76         * @since 4.9.9
     77         *
     78         * @var int $page_index_last
     79         */
     80        protected static $page_index_last;
     81
     82        /**
     83         * Index of the First Exporter.
     84         *
     85         * @since 4.9.9
     86         *
     87         * @var int $exporter_index_first
     88         */
     89        protected static $exporter_index_first;
     90
     91        /**
     92         * Index of the Last Exporter.
     93         *
     94         * @since 4.9.9
     95         *
     96         * @var int $exporter_index_last
     97         */
     98        protected static $exporter_index_last;
     99
     100        /**
     101         * Key of the First Exporter.
     102         *
     103         * @since 4.9.9
     104         *
     105         * @var int $exporter_key_first
     106         */
     107        protected static $exporter_key_first;
     108
     109        /**
     110         * Key of the Last Exporter.
     111         *
     112         * @since 4.9.9
     113         *
     114         * @var int $exporter_key_last
     115         */
     116        protected static $exporter_key_last;
     117
     118        /**
     119         * Export data stored on the `wp_privacy_personal_data_export_file` action hook.
     120         *
     121         * @var string $_export_data_grouped_fetched_within_callback
     122         */
     123        public $_export_data_grouped_fetched_within_callback;
     124
     125        /**
     126         * Create user request fixtures shared by test methods.
     127         *
     128         * @since 4.9.9
     129         *
     130         * @param WP_UnitTest_Factory $factory Factory.
     131         */
     132        public static function wpSetUpBeforeClass( $factory ) {
     133                self::$requester_email      = 'requester@example.com';
     134                self::$export_file_url      = wp_privacy_exports_url() . 'wp-personal-data-file-requester-at-example-com-Wv0RfMnGIkl4CFEDEEkSeIdfLmaUrLsl.zip';
     135                self::$request_id           = wp_create_user_request( self::$requester_email, 'export_personal_data' );
     136                self::$page_index_first     = 1;
     137                self::$page_index_last      = 2;
     138                self::$exporter_index_first = 1;
     139                self::$exporter_index_last  = 2;
     140                self::$exporter_key_first   = 'custom-exporter-first';
     141                self::$exporter_key_last    = 'custom-exporter-last';
     142
     143                $data = array(
     144                        array(
     145                                'group_id'    => 'custom-exporter-group-id',
     146                                'group_label' => 'custom-exporter-group-label',
     147                                'item_id'     => 'custom-exporter-item-id',
     148                                'data'        => array(
     149                                        array(
     150                                                'name'  => 'Email',
     151                                                'value' => self::$requester_email,
     152                                        ),
     153                                ),
     154                        ),
     155                );
     156
     157                self::$response_first_page = array(
     158                        'done' => false,
     159                        'data' => $data,
     160                );
     161
     162                self::$response_last_page = array(
     163                        'done' => true,
     164                        'data' => $data,
     165                );
     166        }
     167
     168        /**
     169         * Setup before each test method.
     170         *
     171         * @since 4.9.9
     172         */
     173        public function setUp() {
     174                parent::setUp();
     175
     176                // Avoid writing export files to disk. Using `WP_Filesystem_MockFS` is blocked by #44204.
     177                remove_action( 'wp_privacy_personal_data_export_file', 'wp_privacy_generate_personal_data_export_file', 10 );
     178
     179                // Register our custom data exporters, very late, so we can override other unrelated exporters.
     180                add_filter( 'wp_privacy_personal_data_exporters', array( $this, 'filter_register_custom_personal_data_exporters' ), 9999 );
     181
     182                // Set Ajax context for `wp_send_json()` and `wp_die()`.
     183                add_filter( 'wp_doing_ajax', '__return_true' );
     184
     185                // Set up a `wp_die()` ajax handler that throws an exception, to be able to get
     186                // the error message from `wp_send_json_error( 'some message here' )`,
     187                // called by `wp_privacy_process_personal_data_export_page()`.
     188                add_filter( 'wp_die_ajax_handler', array( $this, 'get_wp_die_handler' ), 1, 1 );
     189
     190                // Suppress warnings from "Cannot modify header information - headers already sent by".
     191                $this->_error_level = error_reporting();
     192                error_reporting( $this->_error_level & ~E_WARNING );
     193        }
     194
     195        /**
     196         * Clean up after each test method.
     197         *
     198         * @since 4.9.9
     199         */
     200        public function tearDown() {
     201                error_reporting( $this->_error_level );
     202
     203                parent::tearDown();
     204        }
     205
     206        /**
     207         * Filter to register custom personal data exporters.
     208         *
     209         * @since 4.9.9
     210         *
     211         * @param  array $exporters An array of personal data exporters.
     212         * @return array $exporters An array of personal data exporters.
     213         */
     214        public function filter_register_custom_personal_data_exporters( $exporters ) {
     215                // Let's override other unrelated exporters.
     216                $exporters = array();
     217
     218                $exporters[ self::$exporter_key_first ] = array(
     219                        'exporter_friendly_name' => __( 'Custom Exporter #1' ),
     220                        'callback'               => null,
     221                );
     222                $exporters[ self::$exporter_key_last ]  = array(
     223                        'exporter_friendly_name' => __( 'Custom Exporter #2' ),
     224                        'callback'               => null,
     225                );
     226
     227                return $exporters;
     228        }
     229
     230        /**
     231         * Set up a test method to properly assert an exception.
     232         *
     233         * @since 4.9.9
     234         *
     235         * @param string $expected_output The expected string exception output.
     236         */
     237        private function _setup_expected_failure( $expected_output ) {
     238                $this->setExpectedException( 'WPDieException' );
     239                $this->expectOutputString( $expected_output );
     240        }
     241
     242        /**
     243         * Ensure the correct errors are returned when exporter responses are incorrect.
     244         *
     245         * @ticket 44233
     246         *
     247         * @dataProvider data_wp_privacy_process_personal_data_export_page
     248         *
     249         * @param string|array $expected_response The response from the personal data exporter for the given test.
     250         */
     251        public function test_wp_privacy_process_personal_data_export_page( $expected_response ) {
     252                $actual_response = wp_privacy_process_personal_data_export_page(
     253                        $expected_response,
     254                        self::$exporter_index_last,
     255                        self::$requester_email,
     256                        self::$page_index_last,
     257                        self::$request_id,
     258                        true,
     259                        self::$exporter_key_last
     260                );
     261
     262                $this->assertSame( $expected_response, $actual_response );
     263        }
     264
     265        /**
     266         * Provide test cases for `test_wp_privacy_process_personal_data_export_page()`.
     267         *
     268         * @since 4.9.9
     269         *
     270         * @return array {
     271         *     @type array {
     272         *         @type string|array $response The response from the personal data exporter to test. Can be a string or an array.
     273         *     }
     274         * }
     275         */
     276        public function data_wp_privacy_process_personal_data_export_page() {
     277                return array(
     278                        // Response is not an array.
     279                        array(
     280                                'not-an-array',
     281                        ),
     282                        // Missing `done` array key.
     283                        array(
     284                                array(
     285                                        'missing-done-array-key' => true,
     286                                ),
     287                        ),
     288                        // Missing `data` array key.
     289                        array(
     290                                array(
     291                                        'done' => true,
     292                                ),
     293                        ),
     294                        // `data` key is not an array.
     295                        array(
     296                                array(
     297                                        'done' => true,
     298                                        'data' => 'not-an-array',
     299                                ),
     300                        ),
     301                        array(
     302                                array(
     303                                        'done' => true,
     304                                        'data' => array(),
     305                                ),
     306                        ),
     307                );
     308        }
     309
     310        /**
     311         * Provide test scenarios for both sending and not sending an email.
     312         *
     313         * @since 4.9.9
     314         *
     315         * @return array {
     316         *     @type array {
     317         *         @type bool $send_as_email Whether the final results of the export should be emailed to the user.
     318         *     }
     319         * }
     320         */
     321        public function data_send_as_email_options() {
     322                return array(
     323                        array(
     324                                true,
     325                        ),
     326                        array(
     327                                false,
     328                        ),
     329                );
     330        }
     331
     332        /**
     333         * The function should send a JSON error when receiving an invalid request ID.
     334         *
     335         * @ticket 44233
     336         *
     337         * @dataProvider data_send_as_email_options
     338         *
     339         * @param bool Whether the final results of the export should be emailed to the user.
     340         */
     341        public function test_send_error_when_invalid_request_id( $send_as_email ) {
     342                $response           = array(
     343                        'done' => true,
     344                        'data' => array(),
     345                );
     346                $invalid_request_id = 0;
     347
     348                // Process data, given the last exporter, on the last page and send as email.
     349                $this->_setup_expected_failure( '{"success":false,"data":"Invalid request ID when merging exporter data."}' );
     350
     351                wp_privacy_process_personal_data_export_page(
     352                        $response,
     353                        self::$exporter_index_last,
     354                        self::$requester_email,
     355                        self::$page_index_last,
     356                        $invalid_request_id,
     357                        $send_as_email,
     358                        self::$exporter_key_last
     359                );
     360        }
     361
     362        /**
     363         * The function should send a JSON error when the request has an invalid action name.
     364         *
     365         * @ticket 44233
     366         *
     367         * @dataProvider data_send_as_email_options
     368         *
     369         * @param bool Whether the final results of the export should be emailed to the user.
     370         */
     371        public function test_send_error_when_invalid_request_action_name( $send_as_email ) {
     372                $response = array(
     373                        'done' => true,
     374                        'data' => array(),
     375                );
     376
     377                // Create a valid request ID, but for a different action than the function expects.
     378                $request_id = wp_create_user_request( self::$requester_email, 'remove_personal_data' );
     379
     380                // Process data, given the last exporter, on the last page and send as email.
     381                $this->_setup_expected_failure( '{"success":false,"data":"Invalid request ID when merging exporter data."}' );
     382
     383                wp_privacy_process_personal_data_export_page(
     384                        $response,
     385                        self::$exporter_index_last,
     386                        self::$requester_email,
     387                        self::$page_index_last,
     388                        $request_id,
     389                        $send_as_email,
     390                        self::$exporter_key_last
     391                );
     392        }
     393
     394        /**
     395         * The function should store export raw data until the export finishes. Then the meta key should be deleted.
     396         *
     397         * @ticket 44233
     398         *
     399         * @dataProvider data_send_as_email_options
     400         *
     401         * @param bool Whether the final results of the export should be emailed to the user.
     402         *
     403         */
     404        public function test_raw_data_post_meta( $send_as_email ) {
     405                $this->assertEmpty( get_post_meta( self::$request_id, '_export_data_raw', true ) );
     406
     407                // Adds post meta when processing data, given the first exporter on the first page and send as email.
     408                wp_privacy_process_personal_data_export_page(
     409                        self::$response_first_page,
     410                        self::$exporter_index_first,
     411                        self::$requester_email,
     412                        self::$page_index_first,
     413                        self::$request_id,
     414                        $send_as_email,
     415                        self::$exporter_key_first
     416                );
     417
     418                $this->assertNotEmpty( get_post_meta( self::$request_id, '_export_data_raw', true ) );
     419
     420                // Deletes post meta when processing data, given the last exporter on the last page and send as email.
     421                wp_privacy_process_personal_data_export_page(
     422                        self::$response_last_page,
     423                        self::$exporter_index_last,
     424                        self::$requester_email,
     425                        self::$page_index_last,
     426                        self::$request_id,
     427                        $send_as_email,
     428                        self::$exporter_key_last
     429                );
     430
     431                $this->assertEmpty( get_post_meta( self::$request_id, '_export_data_raw', true ) );
     432        }
     433
     434        /**
     435         * The function should add `_export_data_grouped` post meta for the request, only available
     436         * when personal data export file is generated.
     437         *
     438         * @ticket 44233
     439         *
     440         * @dataProvider data_send_as_email_options
     441         *
     442         * @param bool Whether the final results of the export should be emailed to the user.
     443         */
     444        public function test_add_post_meta_with_groups_data_only_available_when_export_file_generated( $send_as_email ) {
     445                // Adds post meta when processing data, given the first exporter on the first page and send as email.
     446                wp_privacy_process_personal_data_export_page(
     447                        self::$response_first_page,
     448                        self::$exporter_index_first,
     449                        self::$requester_email,
     450                        self::$page_index_first,
     451                        self::$request_id,
     452                        true,
     453                        self::$exporter_key_first
     454                );
     455                $this->assertEmpty( get_post_meta( self::$request_id, '_export_data_grouped', true ) );
     456
     457                add_action( 'wp_privacy_personal_data_export_file', array( $this, 'action_callback_to_get_export_groups_data' ) );
     458
     459                // Process data, given the last exporter on the last page and send as email.
     460                wp_privacy_process_personal_data_export_page(
     461                        self::$response_last_page,
     462                        self::$exporter_index_last,
     463                        self::$requester_email,
     464                        self::$page_index_last,
     465                        self::$request_id,
     466                        true,
     467                        self::$exporter_key_last
     468                );
     469
     470                $this->assertNotEmpty( $this->_export_data_grouped_fetched_within_callback );
     471                $this->assertEmpty( get_post_meta( self::$request_id, '_export_data_grouped', true ) );
     472        }
     473
     474        /**
     475         * When mail delivery fails, the function should send a JSON error on the last page of the last exporter.
     476         *
     477         * @ticket 44233
     478         */
     479        public function test_send_error_on_last_page_of_last_exporter_when_mail_delivery_fails() {
     480                // Cause `wp_mail()` to return false, to simulate mail delivery failure. Filter removed in tearDown.
     481                add_filter( 'wp_mail_from', '__return_empty_string' );
     482
     483                // Process data, given the last exporter, on the last page and send as email.
     484                $this->_setup_expected_failure( '{"success":false,"data":"Unable to send personal data export email."}' );
     485
     486                wp_privacy_process_personal_data_export_page(
     487                        self::$response_last_page,
     488                        self::$exporter_index_last,
     489                        self::$requester_email,
     490                        self::$page_index_last,
     491                        self::$request_id,
     492                        true,
     493                        self::$exporter_key_last
     494                );
     495        }
     496
     497        /**
     498         * The function should return the response, containing the export file URL, when not sent as email
     499         * for the last exporter on the last page.
     500         *
     501         * @ticket 44233
     502         */
     503        public function test_return_response_with_export_file_url_when_not_sent_as_email_for_last_exporter_on_last_page() {
     504                update_post_meta( self::$request_id, '_export_file_url', self::$export_file_url );
     505
     506                // Process data, given the last exporter, on the last page and not send as email.
     507                $actual_response = wp_privacy_process_personal_data_export_page(
     508                        self::$response_last_page,
     509                        self::$exporter_index_last,
     510                        self::$requester_email,
     511                        self::$page_index_last,
     512                        self::$request_id,
     513                        false,
     514                        self::$exporter_key_last
     515                );
     516
     517                $this->assertArrayHasKey( 'url', $actual_response );
     518                $this->assertSame( self::$export_file_url, $actual_response['url'] );
     519                $this->assertSame( self::$response_last_page['done'], $actual_response['done'] );
     520                $this->assertSame( self::$response_last_page['data'], $actual_response['data'] );
     521        }
     522
     523        /**
     524         * The function should return the response, not containing the export file URL, when sent as email
     525         * for the last exporter on the last page.
     526         *
     527         * @ticket 44233
     528         */
     529        public function test_return_response_without_export_file_url_when_sent_as_email_for_last_exporter_on_last_page() {
     530                update_post_meta( self::$request_id, '_export_file_url', self::$export_file_url );
     531
     532                // Process data, given the last exporter, on the last page and send as email.
     533                $actual_response = wp_privacy_process_personal_data_export_page(
     534                        self::$response_last_page,
     535                        self::$exporter_index_last,
     536                        self::$requester_email,
     537                        self::$page_index_last,
     538                        self::$request_id,
     539                        true,
     540                        self::$exporter_key_last
     541                );
     542
     543                $this->assertArrayNotHasKey( 'url', $actual_response );
     544                $this->assertSame( self::$response_last_page['done'], $actual_response['done'] );
     545                $this->assertSame( self::$response_last_page['data'], $actual_response['data'] );
     546        }
     547
     548        /**
     549         * Test that request statuses are properly transitioned.
     550         *
     551         * @ticket 44233
     552         *
     553         * @dataProvider data_export_page_status_transitions
     554         *
     555         * @param string $expected_status The expected post status after calling the function.
     556         * @param string $response_page   The exporter page to pass. Options are 'first' and 'last'. Default 'first'.
     557         * @param string $exporter_index  The exporter index to pass. Options are 'first' and 'last'. Default 'first'.
     558         * @param string $page_index      The page index to pass. Options are 'first' and 'last'. Default 'first'.
     559         * @param bool   $send_as_email   If the response should be sent as an email.
     560         * @param string $exporter_key    The slug (key) of the exporter to pass.
     561         */
     562        public function test_request_status_transitions_correctly( $expected_status, $response_page, $exporter_index, $page_index, $send_as_email, $exporter_key ) {
     563                if ( 'first' === $response_page ) {
     564                        $response_page = self::$response_first_page;
     565                } else {
     566                        $response_page = self::$response_last_page;
     567                }
     568
     569                if ( 'first' === $exporter_index ) {
     570                        $exporter_index = self::$exporter_index_first;
     571                } else {
     572                        $exporter_index = self::$exporter_index_last;
     573                }
     574
     575                if ( 'first' === $page_index ) {
     576                        $page_index = self::$page_index_first;
     577                } else {
     578                        $page_index = self::$page_index_last;
     579                }
     580
     581                if ( 'first' === $exporter_key ) {
     582                        $exporter_key = self::$exporter_key_first;
     583                } else {
     584                        $exporter_key = self::$exporter_key_last;
     585                }
     586
     587                wp_privacy_process_personal_data_export_page(
     588                        $response_page,
     589                        $exporter_index,
     590                        self::$requester_email,
     591                        $page_index,
     592                        self::$request_id,
     593                        $send_as_email,
     594                        $exporter_key
     595                );
     596
     597                $this->assertSame( $expected_status, get_post_status( self::$request_id ) );
     598        }
     599
     600        /**
     601         * Provide test cases for `test_wp_privacy_process_personal_data_export_page()`.
     602         *
     603         * @since 4.9.9
     604         *
     605         * @return array {
     606         *     @type array {
     607         *         @string string $expected_status The expected post status after calling the function.
     608         *         @string string $response_page   The exporter page to pass. Options are 'first' and 'last'. Default 'first'.
     609         *         @string string $exporter_index  The exporter index to pass. Options are 'first' and 'last'. Default 'first'.
     610         *         @string string $page_index      The page index to pass. Options are 'first' and 'last'. Default 'first'.
     611         *         @bool   bool   $send_as_email   If the response should be sent as an email.
     612         *         @string string $exporter_key    The slug (key) of the exporter to pass.
     613         *     }
     614         * }
     615         */
     616        public function data_export_page_status_transitions() {
     617                return array(
     618                        // Mark the request as completed for the last exporter on the last page, with and without email.
     619                        array(
     620                                'request-completed',
     621                                'last',
     622                                'last',
     623                                'last',
     624                                true,
     625                                'last',
     626                        ),
     627                        array(
     628                                'request-completed',
     629                                'last',
     630                                'last',
     631                                'last',
     632                                false,
     633                                'last',
     634                        ),
     635                        // Leave the request as pending when not the last exporter and not on the last page.
     636                        array(
     637                                'request-pending',
     638                                'first',
     639                                'first',
     640                                'first',
     641                                true,
     642                                'first',
     643                        ),
     644                        array(
     645                                'request-pending',
     646                                'first',
     647                                'first',
     648                                'first',
     649                                false,
     650                                'first',
     651                        ),
     652                        // Leave the request as pending when last exporter and not on the last page.
     653                        array(
     654                                'request-pending',
     655                                'first',
     656                                'last',
     657                                'first',
     658                                true,
     659                                'last',
     660                        ),
     661                        array(
     662                                'request-pending',
     663                                'first',
     664                                'last',
     665                                'first',
     666                                false,
     667                                'last',
     668                        ),
     669                        // Leave the request as pending when not last exporter on the last page.
     670                        array(
     671                                'request-pending',
     672                                'last',
     673                                'first',
     674                                'last',
     675                                true,
     676                                'last',
     677                        ),
     678                        array(
     679                                'request-pending',
     680                                'last',
     681                                'first',
     682                                'last',
     683                                false,
     684                                'first',
     685                        ),
     686                );
     687        }
     688
     689        /**
     690         * A callback for the `wp_privacy_personal_data_export_file` action that stores the
     691         * `_export_data_grouped` meta data locally for testing.
     692         *
     693         * @since 4.9.9
     694         *
     695         * @param int $request_id Request ID.
     696         */
     697        public function action_callback_to_get_export_groups_data( $request_id ) {
     698                $this->_export_data_grouped_fetched_within_callback = get_post_meta( $request_id, '_export_data_grouped', true );
     699        }
     700}