Make WordPress Core

Ticket #43546: 43546.wpPrivacyProcessPersonalDataExportPage.diff

File 43546.wpPrivacyProcessPersonalDataExportPage.diff, 16.4 KB (added by birgire, 7 years ago)

Unit-tests for wp_privacy_process_personal_data_export_page()

  • 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..ef9804b
    - +  
     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.6
     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.6
     17 */
     18class Tests_Privacy_WpPrivacyProcessPersonalDataExportPage extends WP_Ajax_UnitTestCase {
     19        /**
     20         * Request ID.
     21         *
     22         * @since 4.9.6
     23         *
     24         * @var int $request_id
     25         */
     26        protected static $request_id;
     27
     28        /**
     29         * Requester Email.
     30         *
     31         * @since 4.9.6
     32         *
     33         * @var int $request_email
     34         */
     35        protected static $request_email;
     36
     37        /**
     38         * Response for the First Page.
     39         *
     40         * @since 4.9.6
     41         *
     42         * @var array $response
     43         */
     44        protected static $response_first_page;
     45
     46        /**
     47         * Response for the Last Page.
     48         *
     49         * @since 4.9.6
     50         *
     51         * @var array $response_last_page
     52         */
     53        protected static $response_last_page;
     54
     55        /**
     56         * Export File Url.
     57         *
     58         * @since 4.9.6
     59         *
     60         * @var string $export_file_url
     61         */
     62        protected static $export_file_url;
     63
     64        /**
     65         * Requester Email.
     66         *
     67         * @since 4.9.6
     68         *
     69         * @var string $requester_email
     70         */
     71        protected static $requester_email;
     72
     73        /**
     74         * Send As Email.
     75         *
     76         * @since 4.9.6
     77         *
     78         * @var bool $send_as_email
     79         */
     80        protected static $send_as_email;
     81
     82        /**
     83         * Index Of The First Page.
     84         *
     85         * @since 4.9.6
     86         *
     87         * @var int $page
     88         */
     89        protected static $page_index_first;
     90
     91        /**
     92         * Index Of The Last Page.
     93         *
     94         * @since 4.9.6
     95         *
     96         * @var int $page_index_last
     97         */
     98        protected static $page_index_last;
     99
     100        /**
     101         * Index of the First Exporter.
     102         *
     103         * @since 4.9.6
     104         *
     105         * @var int $exporter_index_first
     106         */
     107        protected static $exporter_index_first;
     108
     109        /**
     110         * Index of the Last Exporter.
     111         *
     112         * @since 4.9.6
     113         *
     114         * @var int $exporter_index_last
     115         */
     116        protected static $exporter_index_last;
     117
     118        /**
     119         * Key of the First Exporter.
     120         *
     121         * @since 4.9.6
     122         *
     123         * @var int $exporter_key_first
     124         */
     125        protected static $exporter_key_first;
     126
     127        /**
     128         * Key of the Last Exporter.
     129         *
     130         * @since 4.9.6
     131         *
     132         * @var int $exporter_key_last
     133         */
     134        protected static $exporter_key_last;
     135
     136        /**
     137         * Before each test method.
     138         *
     139         * @since 4.9.6
     140         */
     141        function setUp() {
     142                parent::setUp();
     143                remove_all_filters( 'wp_privacy_personal_data_exporters' );
     144                remove_all_actions( 'wp_privacy_personal_data_export_file' );
     145                add_filter( 'wp_privacy_personal_data_exporters', array( $this, 'register_custom_personal_data_exporters' ) );
     146        }
     147
     148        /**
     149         * After each test method.
     150         *
     151         * @since 4.9.6
     152         */
     153        function tearDown() {
     154                remove_filter( 'wp_privacy_personal_data_exporters', array( $this, 'register_custom_personal_data_exporters' ) );
     155                parent::tearDown();
     156        }
     157
     158        /**
     159         * Create user request fixtures shared by test methods.
     160         *
     161         * @since 4.9.6
     162         *
     163         * @param WP_UnitTest_Factory $factory Factory.
     164         */
     165        public static function wpSetUpBeforeClass( $factory ) {
     166                self::$requester_email      = 'requester@example.com';
     167                self::$export_file_url      = wp_privacy_exports_url() . 'wp-personal-data-file-requester-at-example-com-Wv0RfMnGIkl4CFEDEEkSeIdfLmaUrLsl.zip';
     168                $data                       = array(
     169                        array(
     170                                'group_id'    => 'custom-exporter-group-id',
     171                                'group_label' => 'custom-exporter-group-label',
     172                                'item_id'     => 'custom-exporter-item-id',
     173                                'data'        => array(
     174                                        array(
     175                                                'name'  => 'Email',
     176                                                'value' => self::$requester_email,
     177                                        ),
     178                                ),
     179                        ),
     180                );
     181                self::$response_first_page  = array(
     182                        'done' => false,
     183                        'data' => $data,
     184                );
     185                self::$response_last_page   = array(
     186                        'done' => true,
     187                        'data' => $data,
     188                );
     189                self::$request_id           = wp_create_user_request( self::$requester_email, 'export_personal_data' );
     190                self::$send_as_email        = true;
     191                self::$page_index_first     = 1;
     192                self::$page_index_last      = 2;
     193                self::$exporter_index_first = 1;
     194                self::$exporter_index_last  = 2;
     195                self::$exporter_key_first   = 'custom-exporter-first';
     196                self::$exporter_key_last    = 'custom-exporter-last';
     197        }
     198
     199        /**
     200         * The function should return the response when it's not an array.
     201         *
     202         * @since 4.9.6
     203         */
     204        public function test_function_should_return_response_when_response_not_array() {
     205                $response          = 'not-an-array';
     206                $filtered_response = wp_privacy_process_personal_data_export_page(
     207                        $response,
     208                        self::$exporter_index_last,
     209                        self::$requester_email,
     210                        self::$page_index_last,
     211                        self::$request_id,
     212                        self::$send_as_email,
     213                        self::$exporter_key_last
     214                );
     215                $this->assertSame( $response, $filtered_response );
     216        }
     217
     218        /**
     219         * The function should return the response when it's missing the 'done' array key.
     220         *
     221         * @since 4.9.6
     222         */
     223        public function test_function_should_return_response_when_misisng_done_array_key() {
     224                $response          = array(
     225                        'missing-done-array-key' => true,
     226                );
     227                $filtered_response = wp_privacy_process_personal_data_export_page(
     228                        $response,
     229                        self::$exporter_index_last,
     230                        self::$requester_email,
     231                        self::$page_index_last,
     232                        self::$request_id,
     233                        self::$send_as_email,
     234                        self::$exporter_key_last
     235                );
     236                $this->assertSame( $response, $filtered_response );
     237        }
     238
     239        /**
     240         * The function should return the response when it's missing the 'data' array key.
     241         *
     242         * @since 4.9.6
     243         */
     244        public function test_function_should_return_response_when_missing_data_array_key() {
     245                $response          = array(
     246                        'done'                   => true,
     247                        'missing-data-array-key' => true,
     248                );
     249                $filtered_response = wp_privacy_process_personal_data_export_page(
     250                        $response,
     251                        self::$exporter_index_last,
     252                        self::$requester_email,
     253                        self::$page_index_last,
     254                        self::$request_id,
     255                        self::$send_as_email,
     256                        self::$exporter_key_last
     257                );
     258                $this->assertSame( $response, $filtered_response );
     259        }
     260
     261        /**
     262         * The function should return the response when data is not an array
     263         *
     264         * @since 4.9.6
     265         */
     266        public function test_function_should_return_response_when_data_not_array() {
     267                $response          = array(
     268                        'done' => true,
     269                        'data' => 'not-an-array',
     270                );
     271                $filtered_response = wp_privacy_process_personal_data_export_page(
     272                        $response,
     273                        self::$exporter_index_last,
     274                        self::$requester_email,
     275                        self::$page_index_last,
     276                        self::$request_id,
     277                        self::$send_as_email,
     278                        self::$exporter_key_last
     279                );
     280                $this->assertSame( $response, $filtered_response );
     281        }
     282
     283        /**
     284         * The function should error when invalid request ID.
     285         *
     286         * @since 4.9.6
     287         */
     288        public function test_function_should_error_when_invalid_request_id() {
     289                $response   = array(
     290                        'done' => true,
     291                        'data' => array(),
     292                );
     293                $request_id = 0; // Invalid request ID.
     294
     295                ob_start();
     296                try {
     297                        $filtered_response = wp_privacy_process_personal_data_export_page(
     298                                $response,
     299                                self::$exporter_index_last,
     300                                self::$requester_email,
     301                                self::$page_index_last,
     302                                $request_id,
     303                                ! self::$send_as_email,
     304                                self::$exporter_key_last
     305                        );
     306                } catch ( WPAjaxDieContinueException $e ) {
     307                        unset( $e );
     308                }
     309                $filtered_response = json_decode( $this->_last_response );
     310
     311                $this->assertFalse( $filtered_response->success );
     312                $this->assertSame( 'Invalid request ID when merging exporter data.', $filtered_response->data );
     313        }
     314
     315        /**
     316         * The function should error when mail not sent.
     317         *
     318         * @since 4.9.6
     319         */
     320        public function test_function_should_error_when_mail_not_sent() {
     321                ob_start();
     322                add_filter( 'wp_mail_from', '__return_empty_string' ); // Cause `wp_mail()` to return false.
     323                try {
     324                        $filtered_response = wp_privacy_process_personal_data_export_page(
     325                                self::$response_last_page,
     326                                self::$exporter_index_last,
     327                                self::$requester_email,
     328                                self::$page_index_last,
     329                                self::$request_id,
     330                                self::$send_as_email,
     331                                self::$exporter_key_last
     332                        );
     333                } catch ( WPAjaxDieContinueException $e ) {
     334                        unset( $e );
     335                }
     336                remove_filter( 'wp_mail_from', '__return_empty_string' );
     337                $filtered_response = json_decode( $this->_last_response );
     338
     339                $this->assertFalse( $filtered_response->success );
     340                $this->assertSame( 'Unable to send personal data export email.', $filtered_response->data );
     341        }
     342
     343        /**
     344         * The function should return the response containing the export file url when not send as email and last exporter and last page.
     345         *
     346         * @since 4.9.6
     347         */
     348        public function test_function_should_return_response_with_export_file_url_when_not_send_as_email_and_last_exporter_and_last_page() {
     349                update_post_meta( self::$request_id, '_export_file_url', self::$export_file_url );
     350
     351                $filtered_response = wp_privacy_process_personal_data_export_page(
     352                        self::$response_last_page,
     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
     361                $this->assertArrayHasKey( 'url', $filtered_response );
     362                $this->assertSame( self::$export_file_url, $filtered_response['url'] );
     363                $this->assertSame( self::$response_last_page['done'], $filtered_response['done'] );
     364                $this->assertSame( self::$response_last_page['data'], $filtered_response['data'] );
     365        }
     366
     367        /**
     368         * The function should return the response containing the export file url when send as email and last exporter and last page.
     369         *
     370         * @since 4.9.6
     371         */
     372        public function test_function_should_return_response_without_export_file_url_when_send_as_email_and_last_exporter_and_last_page() {
     373                update_post_meta( self::$request_id, '_export_file_url', self::$export_file_url );
     374
     375                $filtered_response = wp_privacy_process_personal_data_export_page(
     376                        self::$response_last_page,
     377                        self::$exporter_index_last,
     378                        self::$requester_email,
     379                        self::$page_index_last,
     380                        self::$request_id,
     381                        self::$send_as_email,
     382                        self::$exporter_key_last
     383                );
     384
     385                $this->assertArrayNotHasKey( 'url', $filtered_response );
     386                $this->assertSame( self::$response_last_page['done'], $filtered_response['done'] );
     387                $this->assertSame( self::$response_last_page['data'], $filtered_response['data'] );
     388        }
     389
     390        /**
     391         * The function should mark the request as completed when last exporter, last page.
     392         *
     393         * @since 4.9.6
     394         */
     395        public function test_function_should_mark_request_as_completed_when_last_exporter_and_last_page() {
     396                $filtered_response = wp_privacy_process_personal_data_export_page(
     397                        self::$response_last_page,
     398                        self::$exporter_index_last,
     399                        self::$requester_email,
     400                        self::$page_index_last,
     401                        self::$request_id,
     402                        self::$send_as_email,
     403                        self::$exporter_key_last
     404                );
     405                $this->assertSame( 'request-completed', get_post_status( self::$request_id ) );
     406
     407                $filtered_response = wp_privacy_process_personal_data_export_page(
     408                        self::$response_last_page,
     409                        self::$exporter_index_last,
     410                        self::$requester_email,
     411                        self::$page_index_last,
     412                        self::$request_id,
     413                        ! self::$send_as_email,
     414                        self::$exporter_key_last
     415                );
     416                $this->assertSame( 'request-completed', get_post_status( self::$request_id ) );
     417        }
     418
     419        /**
     420         * The function should leave the request as pending when not last exporter and not last page.
     421         *
     422         * @since 4.9.6
     423         */
     424        public function test_function_should_leave_request_as_pending_when_not_last_exporter_and_not_last_page() {
     425                $filtered_response = wp_privacy_process_personal_data_export_page(
     426                        self::$response_first_page,
     427                        self::$exporter_index_first,
     428                        self::$requester_email,
     429                        self::$page_index_first,
     430                        self::$request_id,
     431                        self::$send_as_email,
     432                        self::$exporter_key_first
     433                );
     434                $this->assertSame( 'request-pending', get_post_status( self::$request_id ) );
     435        }
     436
     437        /**
     438         * The function should leave the request as pending when last exporter and not last page.
     439         *
     440         * @since 4.9.6
     441         */
     442        public function test_function_should_leave_request_as_pending_when_last_exporter_and_not_last_page() {
     443                $filtered_response = wp_privacy_process_personal_data_export_page(
     444                        self::$response_first_page,
     445                        self::$exporter_index_last,
     446                        self::$requester_email,
     447                        self::$page_index_first,
     448                        self::$request_id,
     449                        self::$send_as_email,
     450                        self::$exporter_key_first
     451                );
     452                $this->assertSame( 'request-pending', get_post_status( self::$request_id ) );
     453        }
     454
     455        /**
     456         * The function should leave the request as pending when not last exporter and last page.
     457         *
     458         * @since 4.9.6
     459         */
     460        public function test_function_should_leave_request_as_pending_when_not_last_exporter_and_last_page() {
     461                $filtered_response = wp_privacy_process_personal_data_export_page(
     462                        self::$response_first_page,
     463                        self::$exporter_index_last,
     464                        self::$requester_email,
     465                        self::$page_index_first,
     466                        self::$request_id,
     467                        self::$send_as_email,
     468                        self::$exporter_key_first
     469                );
     470                $this->assertSame( 'request-pending', get_post_status( self::$request_id ) );
     471        }
     472
     473        /**
     474         * The function should add `_export_data_raw` post meta for the request, when first exporter and first page.
     475         *
     476         * @since 4.9.6
     477         */
     478        public function test_function_should_add_post_meta_with_raw_data_when_first_exporter_and_first_page() {
     479
     480                $this->assertEmpty( get_post_meta( self::$request_id, '_export_data_raw', true ) );     
     481
     482                // First exporter, first page.
     483                $filtered_response = wp_privacy_process_personal_data_export_page(
     484                        self::$response_first_page,
     485                        self::$exporter_index_first,
     486                        self::$requester_email,
     487                        self::$page_index_first,
     488                        self::$request_id,
     489                        self::$send_as_email,
     490                        self::$exporter_key_first
     491                );
     492
     493                $this->assertNotEmpty( get_post_meta( self::$request_id, '_export_data_raw', true ) ); 
     494
     495                // Last exporter, last page.
     496                $filtered_response = wp_privacy_process_personal_data_export_page(
     497                        self::$response_last_page,
     498                        self::$exporter_index_last,
     499                        self::$requester_email,
     500                        self::$page_index_last,
     501                        self::$request_id,
     502                        self::$send_as_email,
     503                        self::$exporter_key_last
     504                );
     505
     506                $this->assertEmpty( get_post_meta( self::$request_id, '_export_data_raw', true ) );     
     507        }
     508
     509        /**
     510         * The function should add `_export_data_grouped` post meta for the request, only available when personal data export file is generated.
     511         *
     512         * @since 4.9.6
     513         */
     514        public function test_function_should_add_post_meta_with_groups_data_only_available_when_export_file_generated() {
     515
     516                // First exporter, first page.
     517                $filtered_response = wp_privacy_process_personal_data_export_page(
     518                        self::$response_first_page,
     519                        self::$exporter_index_first,
     520                        self::$requester_email,
     521                        self::$page_index_first,
     522                        self::$request_id,
     523                        self::$send_as_email,
     524                        self::$exporter_key_first
     525                );
     526
     527                $this->assertEmpty( get_post_meta( self::$request_id, '_export_data_grouped', true ) );
     528
     529                // Last exporter, last page.
     530                add_action( 'wp_privacy_personal_data_export_file', array( $this, 'get_export_groups_data' ) );
     531                $filtered_response = wp_privacy_process_personal_data_export_page(
     532                        self::$response_last_page,
     533                        self::$exporter_index_last,
     534                        self::$requester_email,
     535                        self::$page_index_last,
     536                        self::$request_id,
     537                        self::$send_as_email,
     538                        self::$exporter_key_last
     539                );
     540                remove_action( 'wp_privacy_personal_data_export_file', array( $this, 'get_export_groups_data' ) );
     541
     542                $this->assertNotEmpty( $this->_export_data_grouped_fetched_within_callback );
     543                $this->assertEmpty( get_post_meta( self::$request_id, '_export_data_grouped', true ) );
     544        }
     545
     546        /**
     547         * A callback for the `wp_privacy_personal_data_export_file` action.
     548         *
     549         * @since 4.9.6
     550         *
     551         * @param int $request_id Request ID.
     552         */
     553        public function get_export_groups_data( $request_id ) {
     554                $this->_export_data_grouped_fetched_within_callback = get_post_meta( $request_id, '_export_data_grouped', true );
     555        }
     556
     557        /**
     558         * Register handler for a custom personal data exporter.
     559         *
     560         * @since 4.9.6
     561         *
     562         * @param  array $exporters An array of personal data exporters.
     563         * @return array $exporters An array of personal data exporters.
     564         */
     565        public function register_custom_personal_data_exporters( $exporters ) {
     566                $exporters[ self::$exporter_key_first ] = array(
     567                        'exporter_friendly_name' => __( 'Custom Exporter #1' ),
     568                        'callback'               => null,
     569                );
     570                $exporters[ self::$exporter_key_last ]  = array(
     571                        'exporter_friendly_name' => __( 'Custom Exporter #2' ),
     572                        'callback'               => null,
     573                );
     574                return $exporters;
     575        }
     576
     577}