Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/wp-admin/includes/privacy-tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,16 @@ function wp_privacy_process_personal_data_export_page( $response, $exporter_inde
$accumulated_data = get_post_meta( $request_id, '_export_data_raw', true );

if ( $accumulated_data ) {
$export_data = $accumulated_data;
if ( is_array( $accumulated_data ) ) {
$export_data = $accumulated_data;
} else {
wp_send_json_error(
sprintf(
'Warning: array_merge(): Expected parameter 1 to be an array, %1$s given.',
gettype( $accumulated_data )
)
);
Comment on lines +776 to +781
Copy link
Contributor Author

@hellofromtonya hellofromtonya Nov 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preserves the original warning when/if '_export_data_raw' exists but is not an array type.

< PHP 7.3

Warning: array_merge(): Argument #1 is not an array

PHP 7.3-7.4

Warning: array_merge(): Expected parameter 1 to be an array, string given

PHP 8

Fatal error: Uncaught TypeError: array_merge(): Argument #1 must be of type array, string given

See it in action here https://3v4l.org/3EbKY

}
}
}

Expand Down
138 changes: 125 additions & 13 deletions tests/phpunit/tests/privacy/wpPrivacyProcessPersonalDataExportPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,21 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$exporter_key_first = 'custom-exporter-first';
self::$exporter_key_last = 'custom-exporter-last';

$data = array(
$data = self::get_response_data();

self::$response_first_page = array(
'done' => false,
'data' => $data,
);

self::$response_last_page = array(
'done' => true,
'data' => $data,
);
}

private static function get_response_data() {
return array(
array(
'group_id' => 'custom-exporter-group-id',
'group_label' => 'Custom Exporter Group Label',
Expand All @@ -169,21 +183,11 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
'data' => array(
array(
'name' => 'Email',
'value' => self::$requester_email,
'value' => '[email protected]',
),
),
),
);

self::$response_first_page = array(
'done' => false,
'data' => $data,
);

self::$response_last_page = array(
'done' => true,
'data' => $data,
);
}

/**
Expand Down Expand Up @@ -412,6 +416,115 @@ public function test_send_error_when_invalid_request_action_name( $send_as_email
);
}

/**
* The function should store export raw data.
*
* @ticket 51423
*
* @dataProvider data_stores_raw_export_data
*
* @param string $response The response from the personal data exporter for the given page.
* @param int $exporter_index The index of the personal data exporter. Begins at 1.
* @param int $page The page of personal data for this exporter. Begins at 1.
* @param array $expected Expected '_export_data_raw' meta.
*/
public function test_should_stores_raw_export_data( $response, $exporter_index, $page, $expected, $export_data = null ) {
if ( null === $export_data ) {
$this->assertEmpty( get_post_meta( self::$request_id, '_export_data_raw', true ) );
} else {
update_post_meta( self::$request_id, '_export_data_raw', $export_data );
}

// Set to bail out to avoid post meta from being deleted.
$response['done'] = false;
$expect_error = ( null !== $export_data && ! is_array( $export_data ) );

if ( $expect_error ) {
$this->_setup_expected_failure( '{"success":false,"data":"Warning: array_merge(): Expected parameter 1 to be an array, ' . gettype( $export_data ) . ' given."}' );
} else {
$this->expectOutputString( '' );
}

wp_privacy_process_personal_data_export_page(
$response,
$exporter_index,
self::$requester_email,
$page,
self::$request_id,
true,
self::$exporter_key_first
);

$actual = get_post_meta( self::$request_id, '_export_data_raw', true );
$this->assertSame( $expected, $actual );
}

public function data_stores_raw_export_data() {
$data = self::get_response_data();
$export_data = array( 'test' => 'data' );
$merged_export_data = array_merge( $export_data, $data );

return array(
'1st exporter & page 1 with no export data stores response[data]' => array(
'response' => array(
'done' => false,
'data' => $data,
),
'exporter_index' => 1,
'page' => 1,
'expected' => $data,
),
'1st exporter & page 1 with (array) export data stores response[data]' => array(
'response' => array(
'done' => false,
'data' => $data,
),
'exporter_index' => 1,
'page' => 1,
'expected' => $data,
'export_data' => $export_data,
),
'not 1st exporter with no export data stores response[data]' => array(
'response' => array(
'done' => false,
'data' => $data,
),
'exporter_index' => 2,
'page' => 1,
'expected' => $data,
),
'not 1st exporter with (array) export data stores export data merged with response[data]' => array(
'response' => array(
'done' => false,
'data' => $data,
),
'exporter_index' => 2,
'page' => 1,
'expected' => $merged_export_data,
'export_data' => $export_data,
),
'1st exporter, page > 1, with (string) export data updates export data merged with response[data]' => array(
'response' => array(
'done' => false,
'data' => $data,
),
'exporter_index' => 1,
'page' => 2,
'expected' => $data,
'export_data' => 'not an array',
),
'not 1st exporter, page 1, no export data updates to response[data]' => array(
'response' => array(
'done' => false,
'data' => $data,
),
'exporter_index' => 2,
'page' => 1,
'expected' => $data,
),
);
}

/**
* The function should store export raw data until the export finishes. Then the meta key should be deleted.
*
Expand All @@ -420,7 +533,6 @@ public function test_send_error_when_invalid_request_action_name( $send_as_email
* @dataProvider data_send_as_email_options
*
* @param bool Whether the final results of the export should be emailed to the user.
*
*/
public function test_raw_data_post_meta( $send_as_email ) {
$this->assertEmpty( get_post_meta( self::$request_id, '_export_data_raw', true ) );
Expand Down