Make WordPress Core

Ticket #44233: 44233.3.diff

File 44233.3.diff, 37.3 KB (added by birgire, 7 years ago)
  • tests/phpunit/tests/privacy/wpPrivacyDeleteOldExportFiles.php

    diff --git tests/phpunit/tests/privacy/wpPrivacyDeleteOldExportFiles.php tests/phpunit/tests/privacy/wpPrivacyDeleteOldExportFiles.php
    index 6162951..a85e2e3 100644
    class Tests_Privacy_WpPrivacyDeleteOldExportFiles extends WP_UnitTestCase { 
    143143
    144144                $this->assertTrue( file_exists( self::$index_path ) );
    145145        }
     146
     147        /**
     148         * Test the correct files are deleted when the expiration time is filtered.
     149         */
     150        public function test_filtered_expiration_time() {
     151                add_filter( 'wp_privacy_export_expiration', array( $this, 'filter_export_file_expiration_time' ) );
     152                wp_privacy_delete_old_export_files();
     153
     154                $this->assertTrue( file_exists( self::$active_export_file ) );
     155                $this->assertTrue( file_exists( self::$expired_export_file ) );
     156                remove_filter( 'wp_privacy_export_expiration', array( $this, 'filter_export_file_expiration_time' ) );
     157        }
     158
     159        /**
     160         * Filters the expiration time for export files.
     161         *
     162         * @return int New, longer expiration time.
     163         */
     164        public function filter_export_file_expiration_time() {
     165                return 6 * DAY_IN_SECONDS;
     166        }
    146167}
  • new file tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php

    diff --git tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php
    new file mode 100644
    index 0000000..342e457
    - +  
     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.7
     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.7
     17 */
     18class Tests_Privacy_WpPrivacyGeneratePersonalDataExportFile extends WP_UnitTestCase {
     19
     20        /**
     21         * An Export Request ID
     22         *
     23         * @since 4.9.7
     24         *
     25         * @var int $export_request_id
     26         */
     27        protected static $export_request_id;
     28
     29        /**
     30         * Whether an exception has been thrown for the current test method.
     31         *
     32         * Will always be set to false in setUp().
     33         *
     34         * @since 4.9.7
     35         *
     36         * @var bool $exception_was_thrown
     37         */
     38        public $exception_was_thrown = false;
     39
     40        /**
     41         * The full path to the export file for the current test method.
     42         *
     43         * @since 4.9.7
     44         *
     45         * @var string $export_file_name
     46         */
     47        public $export_file_name = '';
     48
     49        /**
     50         * The full path to the exports directory.
     51         *
     52         * @since 4.9.7
     53         *
     54         * @var string $exports_dir
     55         */
     56        public static $exports_dir;
     57
     58        /**
     59         * Create fixtures that are shared by multiple test cases.
     60         *
     61         * @since 4.9.7
     62         *
     63         * @param WP_UnitTest_Factory $factory The base factory object.
     64         */
     65        public static function wpSetUpBeforeClass( $factory ) {
     66                self::$export_request_id = wp_create_user_request( 'export-requester@example.com', 'export_personal_data' );
     67                update_post_meta( self::$export_request_id, '_export_data_grouped', array() );
     68                self::$exports_dir = wp_privacy_exports_dir();
     69        }
     70
     71        /**
     72         * Set up the test fixture.
     73         * Override wp_die(), pretend to be ajax, and suppress E_WARNINGs
     74         *
     75         * @since 4.9.7
     76         */
     77        public function setUp() {
     78                parent::setUp();
     79
     80                $this->exception_was_thrown = false;
     81                $this->export_file_name     = '';
     82
     83                // Make sure the ZipArchive class exists, else skip test.
     84                if ( ! class_exists( 'ZipArchive' ) ) {
     85                        $this->markTestSkipped( 'The ZipArchive class is missing.' );
     86                }
     87
     88                // Make sure the privacy exports directory is not corrupted by any filters,
     89                // because of it's removal, else skip test.
     90                if ( false === strpos( self::$exports_dir, 'wp-personal-data-exports/' ) ) {
     91                        $this->markTestSkipped( 'Exports directory unclear. Skipping test to avoid loss of data.' );
     92                }
     93
     94                // Remove existing privacy exports directory, else skip test.
     95                if ( ! $this->remove_exports_dir() ) {
     96                        $this->markTestSkipped( 'Existing exports directory could not be removed. Skipping test.' );
     97                }
     98
     99                // We need to override the die handler. Otherwise, the unit tests will die too.
     100                add_filter( 'wp_die_ajax_handler', array( $this, 'get_die_handler' ), 1, 1 );
     101                add_filter( 'wp_doing_ajax', '__return_true' );
     102                add_action( 'wp_privacy_personal_data_export_file_created', array( $this, 'action_wp_privacy_personal_data_export_file_created' ) );
     103
     104                // Suppress warnings from "Cannot modify header information - headers already sent by".
     105                $this->_error_level = error_reporting();
     106                error_reporting( $this->_error_level & ~E_WARNING );
     107        }
     108
     109        /**
     110         * Tear down the test fixture.
     111         *
     112         * Remove the wp_die() override, restore error reporting.
     113         *
     114         * @since 4.9.7
     115         */
     116        public function tearDown() {
     117                remove_filter( 'wp_die_ajax_handler', array( $this, 'get_die_handler' ), 1, 1 );
     118                remove_filter( 'wp_doing_ajax', '__return_true' );
     119                remove_action( 'wp_privacy_personal_data_export_file_created', array( $this, 'action_wp_privacy_personal_data_export_file_created' ) );
     120
     121                $this->remove_exports_dir();
     122
     123                error_reporting( $this->_error_level );
     124                parent::tearDown();
     125        }
     126
     127        /**
     128         * Return our callback handler.
     129         *
     130         * @since 4.9.7
     131         *
     132         * @return callback
     133         */
     134        public function get_die_handler() {
     135                return array( $this, 'die_handler' );
     136        }
     137
     138        /**
     139         * Handler for wp_die()
     140         * Don't die, throw.
     141         *
     142         * @since 4.9.7
     143         *
     144         * @param string $message (Unfortunately, always empty).
     145         * @throws Exception Throws exception when called.
     146         */
     147        public function die_handler( $message ) {
     148                throw new Exception();
     149        }
     150
     151        /**
     152         * Stores the name of the export zip file to check the file is actually created.
     153         *
     154         * @since 4.9.7
     155         *
     156         * @param string $archive_name Created export zip file path.
     157         */
     158        public function action_wp_privacy_personal_data_export_file_created( $archive_name ) {
     159                $this->export_file_name = $archive_name;
     160        }
     161
     162        /**
     163         * Removes the privacy exports directory, including files and subdirectories.
     164         *
     165         * Ignores hidden files and has upper limit of nested levels, because of `list_files()`.
     166         *
     167         * @return bool Whether the privacy exports directory was removed.
     168         */
     169        private function remove_exports_dir() {
     170                // Make sure the export directory path is not corrupted by any filter.
     171                if ( false === strpos( self::$exports_dir, 'wp-personal-data-exports/' ) ) {
     172                        return false;
     173                }
     174
     175                // Remove if the file exists.
     176                if ( is_file( untrailingslashit( self::$exports_dir ) ) ) {
     177                        wp_delete_file( untrailingslashit( self::$exports_dir ) );
     178                        return ! is_file( untrailingslashit( self::$exports_dir ) );
     179                }
     180
     181                // Directory already removed.
     182                if ( ! is_dir( self::$exports_dir ) ) {
     183                        return true;
     184                }
     185
     186                chmod( self::$exports_dir, 0755 );
     187
     188                // Get files and subdirectories (mixed order).
     189                $files = list_files( self::$exports_dir );
     190
     191                // Delete files, before deleting subdirectories.
     192                foreach ( $files as $file ) {
     193                        if ( is_file( $file ) ) {
     194                                wp_delete_file( $file );
     195                        }
     196                }
     197
     198                // Delete subdirectories.
     199                foreach ( $files as $file ) {
     200                        if ( is_dir( $file ) ) {
     201                                rmdir( $file );
     202                        }
     203                }
     204
     205                // Delete main directory.
     206                rmdir( self::$exports_dir );
     207
     208                return ! is_dir( self::$exports_dir );
     209        }
     210
     211        /**
     212         * When a remove request ID is passed to the export function an error should be displayed.
     213         *
     214         * @since 4.9.7
     215         */
     216        public function test_function_rejects_remove_requests() {
     217                $request_id = wp_create_user_request( 'removal-requester@example.com', 'remove_personal_data' );
     218
     219                try {
     220                        $this->expectOutputString( '{"success":false,"data":"Invalid request ID when generating export file."}' );
     221                        wp_privacy_generate_personal_data_export_file( $request_id );
     222                } catch ( Exception $e ) {
     223                        $this->exception_was_thrown = true;
     224                }
     225
     226                $this->assertTrue( $this->exception_was_thrown );
     227        }
     228
     229        /**
     230         * When an invalid request ID is passed an error should be displayed.
     231         *
     232         * @since 4.9.7
     233         */
     234        public function test_function_invalid_request_id() {
     235                try {
     236                        $this->expectOutputString( '{"success":false,"data":"Invalid request ID when generating export file."}' );
     237                        wp_privacy_generate_personal_data_export_file( 123456789 );
     238                } catch ( Exception $e ) {
     239                        $this->exception_was_thrown = true;
     240                }
     241
     242                $this->assertTrue( $this->exception_was_thrown );
     243        }
     244
     245        /**
     246         * When the request post title is not a valid email an error should be displayed.
     247         *
     248         * @since 4.9.7
     249         */
     250        public function test_function_rejects_requests_with_bad_email_addresses() {
     251                $request_id = wp_create_user_request( 'bad-email-requester@example.com', 'export_personal_data' );
     252
     253                wp_update_post(
     254                        array(
     255                                'ID'         => $request_id,
     256                                'post_title' => 'not-a-valid-email-address',
     257                        )
     258                );
     259
     260                try {
     261                        $this->expectOutputString( '{"success":false,"data":"Invalid email address when generating export file."}' );
     262                        wp_privacy_generate_personal_data_export_file( $request_id );
     263                } catch ( Exception $e ) {
     264                        $this->exception_was_thrown = true;
     265                }
     266
     267                $this->assertTrue( $this->exception_was_thrown );
     268        }
     269
     270        /**
     271         * When the export directory fails to be created an error should be displayed.
     272         *
     273         * @since 4.9.7
     274         */
     275        public function test_function_detects_cannot_create_folder() {
     276                $index_html_path = self::$exports_dir;
     277                // Create a file with the folder name to ensure the function cannot create a folder.
     278                touch( untrailingslashit( $index_html_path ) );
     279
     280                try {
     281                        $this->expectOutputString( '{"success":false,"data":"Unable to create export folder."}' );
     282                        wp_privacy_generate_personal_data_export_file( self::$export_request_id );
     283                } catch ( Exception $e ) {
     284                        $this->exception_was_thrown = true;
     285                }
     286
     287                $this->assertTrue( $this->exception_was_thrown );
     288        }
     289
     290        /**
     291         * When the index.html file cannot be created an error should be displayed.
     292         *
     293         * @since 4.9.7
     294         */
     295        public function test_function_detects_cannot_create_index() {
     296                // Create and make the export directory read only so protection will fail.
     297                mkdir( self::$exports_dir, 0444 );
     298
     299                try {
     300                        $this->expectOutputString( '{"success":false,"data":"Unable to protect export folder from browsing."}' );
     301                        wp_privacy_generate_personal_data_export_file( self::$export_request_id );
     302                } catch ( Exception $e ) {
     303                        $this->exception_was_thrown = true;
     304                }
     305
     306                $this->assertTrue( $this->exception_was_thrown );
     307        }
     308
     309        /**
     310         * Test that an index.html file can be added to the export directory.
     311         *
     312         * @since 4.9.7
     313         */
     314        public function test_function_creates_index_in_export_folder() {
     315                try {
     316                        $this->expectOutputString( '' );
     317                        wp_privacy_generate_personal_data_export_file( self::$export_request_id );
     318                } catch ( Exception $e ) {
     319                        $this->exception_was_thrown = true;
     320                }
     321                $index_html_path = self::$exports_dir . 'index.html';
     322
     323                $this->assertFalse( $this->exception_was_thrown );
     324                $this->assertTrue( file_exists( $index_html_path ) );
     325        }
     326
     327        /**
     328         * When the export directory is not writable the report should fail to write.
     329         *
     330         * @since 4.9.7
     331         */
     332        public function test_function_detects_cannot_write_html() {
     333                mkdir( self::$exports_dir );
     334                $index_html_path = self::$exports_dir . 'index.html';
     335                touch( $index_html_path );
     336                chmod( self::$exports_dir, 0555 ); // Make the folder read only so html writing will fail.
     337
     338                try {
     339                        $this->expectOutputString( '{"success":false,"data":"Unable to open export file (HTML report) for writing."}' );
     340                        wp_privacy_generate_personal_data_export_file( self::$export_request_id );
     341                } catch ( Exception $e ) {
     342                        $this->exception_was_thrown = true;
     343                }
     344
     345                $this->assertTrue( $this->exception_was_thrown );
     346                $this->assertEmpty( $this->export_file_name );
     347        }
     348
     349
     350        /**
     351         * Test that an export file is successfully created.
     352         *
     353         * @since 4.9.7
     354         */
     355        public function test_function_can_succeed() {
     356                try {
     357                        wp_privacy_generate_personal_data_export_file( self::$export_request_id );
     358                } catch ( Exception $e ) {
     359                        $this->exception_was_thrown = true;
     360                }
     361
     362                $this->assertFalse( $this->exception_was_thrown );
     363                $this->assertTrue( file_exists( $this->export_file_name ) );
     364        }
     365
     366        /**
     367         * Test the export file has all the expected parts.
     368         *
     369         * @since 4.9.7
     370         */
     371        public function test_function_contents() {
     372                try {
     373                        $this->expectOutputString( '' );
     374                        wp_privacy_generate_personal_data_export_file( self::$export_request_id );
     375                } catch ( Exception $e ) {
     376                        $this->exception_was_thrown = true;
     377                }
     378
     379                $this->assertFalse( $this->exception_was_thrown );
     380                $this->assertTrue( file_exists( $this->export_file_name ) );
     381
     382                $temp_dir = trailingslashit( self::$exports_dir . 'tempdir' );
     383                mkdir( $temp_dir );
     384                $zip = new ZipArchive;
     385                $res = $zip->open( $this->export_file_name );
     386
     387                $this->assertTrue( $res );
     388
     389                $zip->extractTo( $temp_dir );
     390                $zip->close();
     391
     392                $this->assertTrue( file_exists( $temp_dir . 'index.html' ) );
     393
     394                $report_contents = file_get_contents( $temp_dir . 'index.html' );
     395
     396                $this->assertContains( '<h1>Personal Data Export</h1>', $report_contents );
     397                $this->assertContains( '<h2>About</h2>', $report_contents );
     398
     399                $request = wp_get_user_request_data( self::$export_request_id );
     400                $this->assertContains( $request->email, $report_contents );
     401        }
     402}
  • new file tests/phpunit/tests/privacy/wpPrivacyProcessPersonalDataExportPage.php

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