Make WordPress Core

Ticket #44038: 44038.6.diff

File 44038.6.diff, 8.7 KB (added by davidbaumwald, 5 years ago)

Patch with unit tests updates

  • src/wp-admin/includes/privacy-tools.php

    diff --git a/src/wp-admin/includes/privacy-tools.php b/src/wp-admin/includes/privacy-tools.php
    index 2d35984499..78e513ab01 100644
    a b function wp_privacy_generate_personal_data_export_file( $request_id ) { 
    468468         * filename, to avoid breaking any URLs that may have been previously sent
    469469         * via email.
    470470         */
    471         $error            = false;
     471        $error = false;
     472
     473        // This postmeta is used from version 5.4.
     474        $archive_filename = get_post_meta( $request_id, '_export_file_name', true );
     475
     476        // These are used for backwards compatibility.
    472477        $archive_url      = get_post_meta( $request_id, '_export_file_url', true );
    473478        $archive_pathname = get_post_meta( $request_id, '_export_file_path', true );
    474479
    475         if ( empty( $archive_pathname ) || empty( $archive_url ) ) {
    476                 $archive_filename = $file_basename . '.zip';
     480        // If archive_filename exists, make sure to remove deprecated postmeta.
     481        if ( ! empty( $archive_filename ) ) {
    477482                $archive_pathname = $exports_dir . $archive_filename;
    478483                $archive_url      = $exports_url . $archive_filename;
    479484
    480                 update_post_meta( $request_id, '_export_file_url', $archive_url );
    481                 update_post_meta( $request_id, '_export_file_path', wp_normalize_path( $archive_pathname ) );
     485                // Remove the deprecated postmeta.
     486                delete_post_meta( $request_id, '_export_file_url' );
     487                delete_post_meta( $request_id, '_export_file_path' );
     488        } elseif ( ! empty( $archive_pathname ) ) {
     489                // Check if archive_pathname exists. If not, create the new postmeta and remove the deprecated.
     490                $archive_filename = basename( $archive_pathname );
     491                $archive_url      = $exports_url . $archive_filename;
     492
     493                // Add the new postmeta that is used since version 5.4.
     494                update_post_meta( $request_id, '_export_file_name', wp_normalize_path( $archive_filename ) );
     495
     496                // Remove the deprecated postmeta.
     497                delete_post_meta( $request_id, '_export_file_url' );
     498                delete_post_meta( $request_id, '_export_file_path' );
     499        } else {
     500                // If there's no archive_filename or archive_pathname create a new one.
     501                $archive_filename = $file_basename . '.zip';
     502                $archive_url      = $exports_url . $archive_filename;
     503                $archive_pathname = $exports_dir . $archive_filename;
     504
     505                // Add the new postmeta that is used since version 5.4.
     506                update_post_meta( $request_id, '_export_file_name', wp_normalize_path( $archive_filename ) );
     507
     508                // Remove the deprecated postmeta.
     509                delete_post_meta( $request_id, '_export_file_url' );
     510                delete_post_meta( $request_id, '_export_file_path' );
    482511        }
    483512
    484513        if ( ! empty( $archive_pathname ) && file_exists( $archive_pathname ) ) {
    function wp_privacy_send_personal_data_export_email( $request_id ) { 
    539568        // Get the request.
    540569        $request = wp_get_user_request( $request_id );
    541570
     571        // Get the export file URL.
     572        $exports_url      = wp_privacy_exports_url();
     573        $export_file_name = get_post_meta( $request_id, '_export_file_name', true );
     574
    542575        if ( ! $request || 'export_personal_data' !== $request->action_name ) {
    543576                return new WP_Error( 'invalid_request', __( 'Invalid request ID when sending personal data export email.' ) );
    544577        }
    function wp_privacy_send_personal_data_export_email( $request_id ) { 
    556589        $expiration      = apply_filters( 'wp_privacy_export_expiration', 3 * DAY_IN_SECONDS );
    557590        $expiration_date = date_i18n( get_option( 'date_format' ), time() + $expiration );
    558591
    559         $export_file_url = get_post_meta( $request_id, '_export_file_url', true );
     592        $export_file_url = $exports_url . $export_file_name;
    560593        $site_name       = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
    561594        $site_url        = home_url();
    562595
    function wp_privacy_process_personal_data_export_page( $response, $exporter_inde 
    820853                _wp_privacy_completed_request( $request_id );
    821854        } else {
    822855                // Modify the response to include the URL of the export file so the browser can fetch it.
    823                 $export_file_url = get_post_meta( $request_id, '_export_file_url', true );
     856                $exports_url      = wp_privacy_exports_url();
     857                $export_file_name = get_post_meta( $request_id, '_export_file_name', true );
     858                $export_file_url  = $exports_url . $export_file_name;
     859
    824860                if ( ! empty( $export_file_url ) ) {
    825861                        $response['url'] = $export_file_url;
    826862                }
  • tests/phpunit/tests/privacy/wpPrivacyProcessPersonalDataExportPage.php

    diff --git a/tests/phpunit/tests/privacy/wpPrivacyProcessPersonalDataExportPage.php b/tests/phpunit/tests/privacy/wpPrivacyProcessPersonalDataExportPage.php
    index 736f0ad7f4..ab5c633b60 100644
    a b class Tests_Privacy_WpPrivacyProcessPersonalDataExportPage extends WP_UnitTestCa 
    4444        protected static $response_last_page;
    4545
    4646        /**
    47          * Export File Url.
     47         * Export Url.
    4848         *
    49          * @since 5.2.0
     49         * @since 5.5.0
     50         *
     51         * @var string $export_url
     52         */
     53        protected static $export_url;
     54
     55        /**
     56         * Export File Name.
     57         *
     58         * @since 5.5.0
     59         *
     60         * @var string $export_file_name
     61         */
     62        protected static $export_file_name;
     63
     64        /**
     65         * Export File URL.
     66         *
     67         * @since 5.5.0
    5068         *
    5169         * @var string $export_file_url
    5270         */
    class Tests_Privacy_WpPrivacyProcessPersonalDataExportPage extends WP_UnitTestCa 
    131149         */
    132150        public static function wpSetUpBeforeClass( $factory ) {
    133151                self::$requester_email      = 'requester@example.com';
    134                 self::$export_file_url      = wp_privacy_exports_url() . 'wp-personal-data-file-Wv0RfMnGIkl4CFEDEEkSeIdfLmaUrLsl.zip';
     152                self::$export_url           = wp_privacy_exports_url();
     153                self::$export_file_name     = 'wp-personal-data-file-Wv0RfMnGIkl4CFEDEEkSeIdfLmaUrLsl.zip';
     154                self::$export_file_url      = self::$export_url . self::$export_file_name;
    135155                self::$request_id           = wp_create_user_request( self::$requester_email, 'export_personal_data' );
    136156                self::$page_index_first     = 1;
    137157                self::$page_index_last      = 2;
    class Tests_Privacy_WpPrivacyProcessPersonalDataExportPage extends WP_UnitTestCa 
    502522         * @ticket 44233
    503523         */
    504524        public function test_return_response_with_export_file_url_when_not_sent_as_email_for_last_exporter_on_last_page() {
    505                 update_post_meta( self::$request_id, '_export_file_url', self::$export_file_url );
     525                update_post_meta( self::$request_id, '_export_file_name', self::$export_file_name );
    506526
    507527                // Process data, given the last exporter, on the last page and not send as email.
    508528                $actual_response = wp_privacy_process_personal_data_export_page(
    class Tests_Privacy_WpPrivacyProcessPersonalDataExportPage extends WP_UnitTestCa 
    528548         * @ticket 44233
    529549         */
    530550        public function test_return_response_without_export_file_url_when_sent_as_email_for_last_exporter_on_last_page() {
    531                 update_post_meta( self::$request_id, '_export_file_url', self::$export_file_url );
     551                update_post_meta( self::$request_id, '_export_file_name', self::$export_file_name );
    532552
    533553                // Process data, given the last exporter, on the last page and send as email.
    534554                $actual_response = wp_privacy_process_personal_data_export_page(
  • tests/phpunit/tests/privacy/wpPrivacySendPersonalDataExportEmail.php

    diff --git a/tests/phpunit/tests/privacy/wpPrivacySendPersonalDataExportEmail.php b/tests/phpunit/tests/privacy/wpPrivacySendPersonalDataExportEmail.php
    index 06863187c9..ab99cc7698 100644
    a b class Tests_Privacy_WpPrivacySendPersonalDataExportEmail extends WP_UnitTestCase 
    104104         * The function should send an export link to the requester when the user request is confirmed.
    105105         */
    106106        public function test_function_should_send_export_link_to_requester() {
    107                 $archive_url = wp_privacy_exports_url() . 'wp-personal-data-file-Wv0RfMnGIkl4CFEDEEkSeIdfLmaUrLsl.zip';
    108                 update_post_meta( self::$request_id, '_export_file_url', $archive_url );
     107                $archive_url       = wp_privacy_exports_url();
     108                $archive_file_name = 'wp-personal-data-file-Wv0RfMnGIkl4CFEDEEkSeIdfLmaUrLsl.zip';
     109                $archive_file_url  = $archive_url . $archive_file_name;
     110                update_post_meta( self::$request_id, '_export_file_name', $archive_file_name );
    109111
    110112                $email_sent = wp_privacy_send_personal_data_export_email( self::$request_id );
    111113                $mailer     = tests_retrieve_phpmailer_instance();
    class Tests_Privacy_WpPrivacySendPersonalDataExportEmail extends WP_UnitTestCase 
    113115                $this->assertSame( 'request-confirmed', get_post_status( self::$request_id ) );
    114116                $this->assertSame( self::$requester_email, $mailer->get_recipient( 'to' )->address );
    115117                $this->assertContains( 'Personal Data Export', $mailer->get_sent()->subject );
    116                 $this->assertContains( $archive_url, $mailer->get_sent()->body );
     118                $this->assertContains( $archive_file_url, $mailer->get_sent()->body );
    117119                $this->assertContains( 'please download it', $mailer->get_sent()->body );
    118120                $this->assertTrue( $email_sent );
    119121        }