From b8e935b2f22a6b3d1e08252b42de1a008342db76 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 11 Feb 2020 21:26:25 +0000 Subject: [PATCH] Privacy: Add a table of contents to Personal Data Export report for easier navigation. Props xkon, garrett-eclipse, birgire, karmatosed. Fixes #46894. git-svn-id: https://develop.svn.wordpress.org/trunk@47278 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/privacy-tools.php | 44 ++- ...pPrivacyGeneratePersonalDataExportFile.php | 364 +++++++++++++++++- ...acyGeneratePersonalDataExportGroupHtml.php | 29 +- 3 files changed, 415 insertions(+), 22 deletions(-) diff --git a/src/wp-admin/includes/privacy-tools.php b/src/wp-admin/includes/privacy-tools.php index f246545b0e..faa5d8b9ac 100644 --- a/src/wp-admin/includes/privacy-tools.php +++ b/src/wp-admin/includes/privacy-tools.php @@ -216,6 +216,7 @@ function _wp_personal_data_cleanup_requests() { * Generate a single group for the personal data export report. * * @since 4.9.6 + * @since 5.4.0 Added the `$group_id` and `$groups_count` parameters. * * @param array $group_data { * The group data to render. @@ -232,10 +233,14 @@ function _wp_personal_data_cleanup_requests() { * } * } * } - * @return string The HTML for this group and its items. + * @param string $group_id The group identifier. + * @param int $groups_count The number of all groups + * @return string $group_html The HTML for this group and its items. */ -function wp_privacy_generate_personal_data_export_group_html( $group_data ) { - $group_html = '

'; +function wp_privacy_generate_personal_data_export_group_html( $group_data, $group_id = '', $groups_count = 1 ) { + $group_id_attr = sanitize_title_with_dashes( $group_data['group_label'] . '-' . $group_id ); + + $group_html = '

'; $group_html .= esc_html( $group_data['group_label'] ); $items_count = count( (array) $group_data['items'] ); @@ -272,6 +277,12 @@ function wp_privacy_generate_personal_data_export_group_html( $group_data ) { $group_html .= ''; } + if ( 1 < $groups_count ) { + $group_html .= '
'; + $group_html .= '' . esc_html__( '↑ Return to top' ) . ''; + $group_html .= '
'; + } + $group_html .= ''; return $group_html; @@ -373,6 +384,8 @@ function wp_privacy_generate_personal_data_export_file( $request_id ) { // Merge in the special about group. $groups = array_merge( array( 'about' => $about_group ), $groups ); + $groups_count = count( $groups ); + // Convert the groups to JSON format. $groups_json = wp_json_encode( $groups ); @@ -410,17 +423,38 @@ function wp_privacy_generate_personal_data_export_file( $request_id ) { fwrite( $file, 'th { padding: 5px; text-align: left; width: 20%; }' ); fwrite( $file, 'td { padding: 5px; }' ); fwrite( $file, 'tr:nth-child(odd) { background-color: #fafafa; }' ); + fwrite( $file, '.return_to_top { text-align:right; }' ); fwrite( $file, '' ); fwrite( $file, '' ); fwrite( $file, esc_html( $title ) ); fwrite( $file, '' ); fwrite( $file, "\n" ); fwrite( $file, "\n" ); - fwrite( $file, '

' . esc_html__( 'Personal Data Export' ) . '

' ); + fwrite( $file, '

' . esc_html__( 'Personal Data Export' ) . '

' ); + + // Create TOC. + if ( 1 < $groups_count ) { + fwrite( $file, '
' ); + fwrite( $file, '

' . esc_html__( 'Table of Contents' ) . '

' ); + fwrite( $file, '' ); + fwrite( $file, '
' ); + } // Now, iterate over every group in $groups and have the formatter render it in HTML. foreach ( (array) $groups as $group_id => $group_data ) { - fwrite( $file, wp_privacy_generate_personal_data_export_group_html( $group_data ) ); + fwrite( $file, wp_privacy_generate_personal_data_export_group_html( $group_data, $group_id, $groups_count ) ); } fwrite( $file, "\n" ); diff --git a/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php b/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php index 7a4f79ed80..c65c993bed 100644 --- a/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php +++ b/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php @@ -260,8 +260,8 @@ class Tests_Privacy_WpPrivacyGeneratePersonalDataExportFile extends WP_UnitTestC $report_contents = file_get_contents( $report_dir . 'index.html' ); $request = wp_get_user_request( self::$export_request_id ); - $this->assertContains( '

Personal Data Export

', $report_contents ); - $this->assertContains( '

About

', $report_contents ); + $this->assertContains( '

Personal Data Export

', $report_contents ); + $this->assertContains( '

About

', $report_contents ); $this->assertContains( $request->email, $report_contents ); } @@ -294,4 +294,364 @@ class Tests_Privacy_WpPrivacyGeneratePersonalDataExportFile extends WP_UnitTestC $this->assertContains( '"Personal Data Export for ' . $request->email . '"', $report_contents_json ); $this->assertContains( '"about"', $report_contents_json ); } + + /** + * Test the export HTML file containing one export group has no table of contents. + * + * @ticket 46894 + */ + public function test_single_group_export_no_toc_or_return_to_top() { + $this->expectOutputString( '' ); + wp_privacy_generate_personal_data_export_file( self::$export_request_id ); + $this->assertTrue( file_exists( $this->export_file_name ) ); + + $report_dir = trailingslashit( self::$exports_dir . 'test_contents' ); + mkdir( $report_dir ); + + $zip = new ZipArchive(); + $opened_zip = $zip->open( $this->export_file_name ); + $this->assertTrue( $opened_zip ); + + $zip->extractTo( $report_dir ); + $zip->close(); + $this->assertTrue( file_exists( $report_dir . 'index.html' ) ); + + $report_contents = file_get_contents( $report_dir . 'index.html' ); + $request = wp_get_user_request( self::$export_request_id ); + + $this->assertNotContains( '
', $report_contents ); + $this->assertNotContains( '
', $report_contents ); + $this->assertContains( $request->email, $report_contents ); + } + + /** + * Test the export HTML file containing ore than one export group has a table of contents. + * + * @ticket 46894 + */ + public function test_multiple_group_export_has_toc_and_return_to_top() { + $this->expectOutputString( '' ); + + // Setup Export Data to contain multiple groups + $export_data_grouped = 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', + ), + ), + ), + ), + ); + update_post_meta( self::$export_request_id, '_export_data_grouped', $export_data_grouped ); + + // Generate Export File + wp_privacy_generate_personal_data_export_file( self::$export_request_id ); + $this->assertTrue( file_exists( $this->export_file_name ) ); + + // Cleam-up for subsequent tests + update_post_meta( self::$export_request_id, '_export_data_grouped', array() ); + + $report_dir = trailingslashit( self::$exports_dir . 'test_contents' ); + mkdir( $report_dir ); + + $zip = new ZipArchive(); + $opened_zip = $zip->open( $this->export_file_name ); + $this->assertTrue( $opened_zip ); + + $zip->extractTo( $report_dir ); + $zip->close(); + $this->assertTrue( file_exists( $report_dir . 'index.html' ) ); + + $report_contents = file_get_contents( $report_dir . 'index.html' ); + $request = wp_get_user_request( self::$export_request_id ); + + $this->assertContains( '
', $report_contents ); + $this->assertContains( '

User

', $report_contents ); + $this->assertContains( '
', $report_contents ); + $this->assertContains( $request->email, $report_contents ); + } + + /** + * Test the export HTML file containing multiple export groups with multiple group items + * has a table of contents with group count. + * + * @ticket 46894 + */ + public function test_multiple_group_export_multiple_items_group_count_in_toc() { + $this->expectOutputString( '' ); + + // Setup Export Data to contain multiple groups + $export_data_grouped = 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', + ), + ), + ), + ), + 'comments' => array( + 'group_label' => 'Comments', + 'group_description' => 'User’s comment data.', + 'items' => array( + 'comment-2' => array( + array( + 'name' => 'Comment Author', + 'value' => 'User Name', + ), + array( + 'name' => 'Comment Author Email', + 'value' => 'export-requester@example.com', + ), + array( + 'name' => 'Comment Author IP', + 'value' => '::1', + ), + array( + 'name' => 'Comment Author User Agent', + 'value' => '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', + ), + array( + 'name' => 'Comment Date', + 'value' => '2020-01-31 19:55:19', + ), + array( + 'name' => 'Comment Content', + 'value' => 'Test', + ), + array( + 'name' => 'Comment URL', + 'value' => 'http://localhost:8888/46894/2020/01/31/hello-world/#comment-2', + ), + ), + 'comment-3' => array( + array( + 'name' => 'Comment Author', + 'value' => 'User Name', + ), + array( + 'name' => 'Comment Author Email', + 'value' => 'export-requester@example.com', + ), + array( + 'name' => 'Comment Author IP', + 'value' => '::1', + ), + array( + 'name' => 'Comment Author User Agent', + 'value' => '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', + ), + array( + 'name' => 'Comment Date', + 'value' => '2020-01-31 20:55:19', + ), + array( + 'name' => 'Comment Content', + 'value' => 'Test #2', + ), + array( + 'name' => 'Comment URL', + 'value' => 'http://localhost:8888/46894/2020/01/31/hello-world/#comment-3', + ), + ), + ), + ), + ); + update_post_meta( self::$export_request_id, '_export_data_grouped', $export_data_grouped ); + + // Generate Export File + wp_privacy_generate_personal_data_export_file( self::$export_request_id ); + $this->assertTrue( file_exists( $this->export_file_name ) ); + + // Cleam-up for subsequent tests + update_post_meta( self::$export_request_id, '_export_data_grouped', array() ); + + $report_dir = trailingslashit( self::$exports_dir . 'test_contents' ); + mkdir( $report_dir ); + + $zip = new ZipArchive(); + $opened_zip = $zip->open( $this->export_file_name ); + $this->assertTrue( $opened_zip ); + + $zip->extractTo( $report_dir ); + $zip->close(); + $this->assertTrue( file_exists( $report_dir . 'index.html' ) ); + + $report_contents = file_get_contents( $report_dir . 'index.html' ); + $request = wp_get_user_request( self::$export_request_id ); + + $this->assertContains( '
', $report_contents ); + $this->assertContains( 'Comments (2)', $report_contents ); + $this->assertContains( $request->email, $report_contents ); + } + + /** + * Test the export HTML file containing multiple export groups with no multiple group items + * has a table of contents without group count. + * + * @ticket 46894 + */ + public function test_multiple_group_export_single_items_no_group_count_in_toc() { + $this->expectOutputString( '' ); + + // Setup Export Data to contain multiple groups + $export_data_grouped = 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', + ), + ), + ), + ), + 'comments' => array( + 'group_label' => 'Comments', + 'group_description' => 'User’s comment data.', + 'items' => array( + 'comment-2' => array( + array( + 'name' => 'Comment Author', + 'value' => 'User Name', + ), + array( + 'name' => 'Comment Author Email', + 'value' => 'export-requester@example.com', + ), + array( + 'name' => 'Comment Author IP', + 'value' => '::1', + ), + array( + 'name' => 'Comment Author User Agent', + 'value' => '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', + ), + array( + 'name' => 'Comment Date', + 'value' => '2020-01-31 19:55:19', + ), + array( + 'name' => 'Comment Content', + 'value' => 'Test', + ), + array( + 'name' => 'Comment URL', + 'value' => 'http://localhost:8888/46894/2020/01/31/hello-world/#comment-2', + ), + ), + ), + ), + ); + update_post_meta( self::$export_request_id, '_export_data_grouped', $export_data_grouped ); + + // Generate Export File + wp_privacy_generate_personal_data_export_file( self::$export_request_id ); + $this->assertTrue( file_exists( $this->export_file_name ) ); + + // Cleam-up for subsequent tests + update_post_meta( self::$export_request_id, '_export_data_grouped', array() ); + + $report_dir = trailingslashit( self::$exports_dir . 'test_contents' ); + mkdir( $report_dir ); + + $zip = new ZipArchive(); + $opened_zip = $zip->open( $this->export_file_name ); + $this->assertTrue( $opened_zip ); + + $zip->extractTo( $report_dir ); + $zip->close(); + $this->assertTrue( file_exists( $report_dir . 'index.html' ) ); + + $report_contents = file_get_contents( $report_dir . 'index.html' ); + $request = wp_get_user_request( self::$export_request_id ); + + $this->assertContains( '
', $report_contents ); + $this->assertNotContains( '', $report_contents ); + $this->assertContains( $request->email, $report_contents ); + + } } diff --git a/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportGroupHtml.php b/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportGroupHtml.php index 688f790e22..de721ab451 100644 --- a/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportGroupHtml.php +++ b/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportGroupHtml.php @@ -39,10 +39,10 @@ class Tests_Privacy_WpPrivacyGeneratePersonalDataExportGroupHtml extends WP_Unit ), ); - $actual = wp_privacy_generate_personal_data_export_group_html( $data ); + $actual = wp_privacy_generate_personal_data_export_group_html( $data, 'test-data-group', 2 ); $expected_table_markup = '
Field 1 NameField 1 Value
Field 2 NameField 2 Value
'; - $this->assertContains( '

Test Data Group

', $actual ); + $this->assertContains( '

Test Data Group

', $actual ); $this->assertContains( $expected_table_markup, $actual ); } @@ -79,9 +79,9 @@ class Tests_Privacy_WpPrivacyGeneratePersonalDataExportGroupHtml extends WP_Unit ), ); - $actual = wp_privacy_generate_personal_data_export_group_html( $data ); + $actual = wp_privacy_generate_personal_data_export_group_html( $data, 'test-data-group', 2 ); - $this->assertContains( '

Test Data Group', $actual ); + $this->assertContains( '

Test Data Group', $actual ); $this->assertContains( 'Field 1 Value', $actual ); $this->assertContains( 'Another Field 1 Value', $actual ); $this->assertContains( 'Field 2 Value', $actual ); @@ -117,7 +117,7 @@ class Tests_Privacy_WpPrivacyGeneratePersonalDataExportGroupHtml extends WP_Unit ), ); - $actual = wp_privacy_generate_personal_data_export_group_html( $data ); + $actual = wp_privacy_generate_personal_data_export_group_html( $data, 'test-data-group', 2 ); $this->assertContains( 'http://wordpress.org', $actual ); $this->assertContains( 'https://wordpress.org', $actual ); @@ -131,13 +131,13 @@ class Tests_Privacy_WpPrivacyGeneratePersonalDataExportGroupHtml extends WP_Unit */ public function test_group_labels_escaped() { $data = array( - 'group_label' => '
Escape HTML in group lavels
', + 'group_label' => '
Escape HTML in group labels
', 'items' => array(), ); - $actual = wp_privacy_generate_personal_data_export_group_html( $data ); + $actual = wp_privacy_generate_personal_data_export_group_html( $data, 'escape-html-in-group-labels', 2 ); - $this->assertContains( '

<div>Escape HTML in group lavels</div>

', $actual ); + $this->assertContains( '

<div>Escape HTML in group labels</div>

', $actual ); } /** @@ -162,8 +162,7 @@ class Tests_Privacy_WpPrivacyGeneratePersonalDataExportGroupHtml extends WP_Unit ), ); - $actual = wp_privacy_generate_personal_data_export_group_html( $data ); - + $actual = wp_privacy_generate_personal_data_export_group_html( $data, 'test-data-group', 2 ); $this->assertContains( $data['items'][0]['links']['value'], $actual ); $this->assertContains( $data['items'][0]['formatting']['value'], $actual ); } @@ -190,7 +189,7 @@ class Tests_Privacy_WpPrivacyGeneratePersonalDataExportGroupHtml extends WP_Unit ), ); - $actual = wp_privacy_generate_personal_data_export_group_html( $data ); + $actual = wp_privacy_generate_personal_data_export_group_html( $data, 'test-data-group', 2 ); $this->assertNotContains( $data['items'][0]['scripts']['value'], $actual ); $this->assertContains( 'Testing that script tags are stripped.', $actual ); @@ -223,9 +222,9 @@ class Tests_Privacy_WpPrivacyGeneratePersonalDataExportGroupHtml extends WP_Unit ), ); - $actual = wp_privacy_generate_personal_data_export_group_html( $data ); + $actual = wp_privacy_generate_personal_data_export_group_html( $data, 'test-data-group', 2 ); - $this->assertContains( '

Test Data Group', $actual ); + $this->assertContains( '

Test Data Group', $actual ); $this->assertContains( '(2)

', $actual ); $this->assertSame( 2, substr_count( $actual, '' ) ); } @@ -248,9 +247,9 @@ class Tests_Privacy_WpPrivacyGeneratePersonalDataExportGroupHtml extends WP_Unit ), ); - $actual = wp_privacy_generate_personal_data_export_group_html( $data ); + $actual = wp_privacy_generate_personal_data_export_group_html( $data, 'test-data-group', 2 ); - $this->assertContains( '

Test Data Group

', $actual ); + $this->assertContains( '

Test Data Group

', $actual ); $this->assertNotContains( '', $actual ); $this->assertSame( 1, substr_count( $actual, '
' ) ); }