Make WordPress Core

Ticket #52892: 52892.diff

File 52892.diff, 2.3 KB (added by SergeyBiryukov, 5 years ago)
  • src/wp-admin/includes/privacy-tools.php

     
    413413        // Convert the groups to JSON format.
    414414        $groups_json = wp_json_encode( $groups );
    415415
     416        if ( false === $groups_json ) {
     417                $error_message = sprintf(
     418                        /* translators: %s: Error message. */
     419                        __( 'Unable to encode the personal data for export. Error: %s' ),
     420                        json_last_error_msg()
     421                );
     422
     423                wp_send_json_error( $error_message );
     424        }
     425
    416426        /*
    417427         * Handle the JSON export.
    418428         */
  • tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php

     
    631631                        ),
    632632                );
    633633        }
     634
     635        /**
     636         * Test should generate JSON error when JSON encoding fails.
     637         *
     638         * @ticket 52892
     639         */
     640        public function test_should_generate_json_error_when_json_encoding_fails() {
     641                add_filter( 'get_post_metadata', array( $this, 'filter_export_data_grouped_metadata' ), 10, 3 );
     642
     643                // Validate JSON encoding fails and returns `false`.
     644                $metadata = get_post_meta( self::$export_request_id, '_export_data_grouped', true );
     645                $this->assertFalse( wp_json_encode( $metadata ) );
     646
     647                $this->expectException( 'WPDieException' );
     648                $this->expectOutputString( '{"success":false,"data":"Unable to encode the personal data for export. Error: Type is not supported"}' );
     649                wp_privacy_generate_personal_data_export_file( self::$export_request_id );
     650        }
     651
     652        public function filter_export_data_grouped_metadata( $value, $object_id, $meta_key ) {
     653                if ( $object_id !== self::$export_request_id ) {
     654                        return $value;
     655                }
     656
     657                if ( '_export_data_grouped' !== $meta_key ) {
     658                        return $value;
     659                }
     660
     661                $file = fopen( __FILE__, 'r' );
     662
     663                $value = array(
     664                        'user' => array(
     665                                'group_label'       => 'User',
     666                                'group_description' => 'User’s profile data.',
     667                                'items'             => array(),
     668                                'resource'          => $file,
     669                        ),
     670                );
     671
     672                fclose( $file );
     673
     674                return array( $value );
     675        }
    634676}