diff --git a/src/wp-admin/includes/privacy-tools.php b/src/wp-admin/includes/privacy-tools.php
index 33611c197546f..6afdb7e9612d3 100644
--- a/src/wp-admin/includes/privacy-tools.php
+++ b/src/wp-admin/includes/privacy-tools.php
@@ -357,9 +357,6 @@ function wp_privacy_generate_personal_data_export_file( $request_id ) {
$email_address
);
- // And now, all the Groups.
- $groups = get_post_meta( $request_id, '_export_data_grouped', true );
-
// First, build an "About" group on the fly for this report.
$about_group = array(
/* translators: Header for the About section in a personal data export. */
@@ -388,10 +385,24 @@ function wp_privacy_generate_personal_data_export_file( $request_id ) {
),
);
- // Merge in the special about group.
- $groups = array_merge( array( 'about' => $about_group ), $groups );
+ // And now, all the Groups.
+ $groups = get_post_meta( $request_id, '_export_data_grouped', true );
+ if ( is_array( $groups ) ) {
+ $groups = array_merge( array( 'about' => $about_group ), $groups );
+ $groups_count = count( $groups );
+ } else {
+ if ( false !== $groups ) {
+ _doing_it_wrong(
+ __FUNCTION__,
+ /* translators: %s: post meta key. */
+ sprintf( __( 'The %s post meta must be an array.' ), "'_export_data_grouped'" ),
+ '5.8.0'
+ );
+ }
- $groups_count = count( $groups );
+ $groups = null;
+ $groups_count = 0;
+ }
// Convert the groups to JSON format.
$groups_json = wp_json_encode( $groups );
diff --git a/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php b/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php
index 68d1136cc09ef..541da49c4c173 100644
--- a/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php
+++ b/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php
@@ -213,6 +213,75 @@ public function test_detect_cannot_create_folder() {
wp_privacy_generate_personal_data_export_file( self::$export_request_id );
}
+ /**
+ * @dataProvider data_export_data_grouped_invalid_type
+ *
+ * @ticket 51423
+ *
+ * @param mixed $groups '_export_data_grouped' post meta value.
+ */
+ public function test_doing_it_wrong_for_export_data_grouped_invalid_type( $groups ) {
+ update_post_meta( self::$export_request_id, '_export_data_grouped', $groups );
+
+ $this->setExpectedIncorrectUsage( 'wp_privacy_generate_personal_data_export_file' );
+
+ wp_privacy_generate_personal_data_export_file( self::$export_request_id );
+ }
+
+ public function data_export_data_grouped_invalid_type() {
+ return array(
+ array( 10 ),
+ array( 'WordPress' ),
+ array( null ),
+ array( true ),
+ array( false ),
+ array( new stdClass() ),
+ array( serialize( array( 10, 'WordPress', null, true, false ) ) ),
+ array(
+ json_encode(
+ array(
+ 'user' => array(
+ 'group_label' => 'User',
+ 'group_description' => 'User’s profile data.',
+ 'items' => array(
+ 'user-1' => array(
+ array(
+ 'name' => 'User ID',
+ 'value' => 1,
+ ),
+ array(
+ 'name' => 'User Login Name',
+ 'value' => 'user_login',
+ ),
+ array(
+ 'name' => 'User Nice Name',
+ 'value' => 'User Name',
+ ),
+ array(
+ 'name' => 'User Email',
+ 'value' => 'export-requester@example.com',
+ ),
+ array(
+ 'name' => 'User Registration Date',
+ 'value' => '2020-01-31 19:29:29',
+ ),
+ array(
+ 'name' => 'User Display Name',
+ 'value' => 'User Name',
+ ),
+ array(
+ 'name' => 'User Nickname',
+ 'value' => 'User',
+ ),
+ ),
+ ),
+ ),
+ )
+ ),
+ ),
+ );
+ }
+
/**
* Test that an index.php file can be added to the export directory.
*
@@ -240,401 +309,301 @@ public function test_can_succeed() {
* Test the export HTML file has all the expected parts.
*
* @ticket 44233
+ * @ticket 51423
+ *
+ * @dataProvider data_contents
+ *
+ * @param mixed $groups '_export_data_grouped' post meta value.
+ * @param string[] $expected_content Optional. Expected content. Use "html" key for this test.
*/
- public function test_html_contents() {
- $this->expectOutputString( '' );
- wp_privacy_generate_personal_data_export_file( self::$export_request_id );
- $this->assertTrue( file_exists( $this->export_file_name ) );
+ public function test_html_contents( $groups, array $expected_content = array() ) {
+ // Set the _doing_it_wrong assertion.
+ if ( ! is_array( $groups ) ) {
+ $this->setExpectedIncorrectUsage( 'wp_privacy_generate_personal_data_export_file' );
+ }
- $report_dir = trailingslashit( self::$exports_dir . 'test_contents' );
- mkdir( $report_dir );
+ $request = wp_get_user_request( self::$export_request_id );
+ $report_dir = $this->setup_export_contents_test( $groups );
- $zip = new ZipArchive();
- $opened_zip = $zip->open( $this->export_file_name );
- $this->assertTrue( $opened_zip );
+ $this->assertFileExists( $report_dir . 'index.html' );
+ $actual_contents = file_get_contents( $report_dir . 'index.html' );
- $zip->extractTo( $report_dir );
- $zip->close();
- $this->assertTrue( file_exists( $report_dir . 'index.html' ) );
+ $expected = "\n";
+ $expected .= "\n";
+ $expected .= "
Overview of export report.
| Report generated for | export-requester@example.com |
|---|---|
| For site | Test Blog |
| At URL | http://example.org |
| On | {{TIMESTAMP}} |
Overview of export report.
| Report generated for | export-requester@example.com |
|---|---|
| For site | Test Blog |
| At URL | http://example.org |
| On | {{TIMESTAMP}} |
User’s profile data.
| User ID | 1 |
|---|---|
| User Login Name | user_login |
| User Nice Name | User Name |
| User Email | export-requester@example.com |
| User Registration Date | 2020-01-31 19:29:29 |
| User Display Name | User Name |
| User Nickname | User |
Overview of export report.
| Report generated for | export-requester@example.com |
|---|---|
| For site | Test Blog |
| At URL | http://example.org |
| On | {{TIMESTAMP}} |
User’s profile data.
| User ID | 1 |
|---|---|
| User Login Name | user_login |
| User Nice Name | User Name |
| User Email | export-requester@example.com |
| User Registration Date | 2020-01-31 19:29:29 |
| User Display Name | User Name |
| User Nickname | User |
User’s comment data.
| Comment Author | User Name |
|---|---|
| Comment Author Email | export-requester@example.com |
| Comment Author IP | ::1 |
| Comment Author User Agent | Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36 |
| Comment Date | 2020-01-31 19:55:19 |
| Comment Content | Test |
| Comment URL | http://localhost:8888/46894/2020/01/31/hello-world/#comment-2 |
| Comment Author | User Name |
|---|---|
| Comment Author Email | export-requester@example.com |
| Comment Author IP | ::1 |
| Comment Author User Agent | Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36 |
| Comment Date | 2020-01-31 20:55:19 |
| Comment Content | Test #2 |
| Comment URL | http://localhost:8888/46894/2020/01/31/hello-world/#comment-3 |