<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[47278] trunk: Privacy: Add a table of contents to Personal Data Export report for easier navigation.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/47278">47278</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/47278","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>SergeyBiryukov</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2020-02-11 21:26:25 +0000 (Tue, 11 Feb 2020)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Privacy: Add a table of contents to Personal Data Export report for easier navigation.

Props xkon, garrett-eclipse, birgire, karmatosed.
Fixes <a href="https://core.trac.wordpress.org/ticket/46894">#46894</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminincludesprivacytoolsphp">trunk/src/wp-admin/includes/privacy-tools.php</a></li>
<li><a href="#trunktestsphpunittestsprivacywpPrivacyGeneratePersonalDataExportFilephp">trunk/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php</a></li>
<li><a href="#trunktestsphpunittestsprivacywpPrivacyGeneratePersonalDataExportGroupHtmlphp">trunk/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportGroupHtml.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminincludesprivacytoolsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/privacy-tools.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/privacy-tools.php     2020-02-11 20:53:40 UTC (rev 47277)
+++ trunk/src/wp-admin/includes/privacy-tools.php       2020-02-11 21:26:25 UTC (rev 47278)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -216,6 +216,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * Generate a single group for the personal data export report.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 4.9.6
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.4.0 Added the `$group_id` and `$groups_count` parameters.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @param array $group_data {
</span><span class="cx" style="display: block; padding: 0 10px">  *     The group data to render.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -232,10 +233,14 @@
</span><span class="cx" style="display: block; padding: 0 10px">  *         }
</span><span class="cx" style="display: block; padding: 0 10px">  *     }
</span><span class="cx" style="display: block; padding: 0 10px">  * }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @return string The HTML for this group and its items.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @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.
</ins><span class="cx" style="display: block; padding: 0 10px">  */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function wp_privacy_generate_personal_data_export_group_html( $group_data ) {
-       $group_html  = '<h2>';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+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  = '<h2 id="' . esc_attr( $group_id_attr ) . '">';
</ins><span class="cx" style="display: block; padding: 0 10px">         $group_html .= esc_html( $group_data['group_label'] );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        $items_count = count( (array) $group_data['items'] );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -272,6 +277,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $group_html .= '</table>';
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        if ( 1 < $groups_count ) {
+               $group_html .= '<div class="return_to_top">';
+               $group_html .= '<a href="#top">' . esc_html__( '&uarr; Return to top' ) . '</a>';
+               $group_html .= '</div>';
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         $group_html .= '</div>';
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        return $group_html;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -373,6 +384,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">        // Merge in the special about group.
</span><span class="cx" style="display: block; padding: 0 10px">        $groups = array_merge( array( 'about' => $about_group ), $groups );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        $groups_count = count( $groups );
+
</ins><span class="cx" style="display: block; padding: 0 10px">         // Convert the groups to JSON format.
</span><span class="cx" style="display: block; padding: 0 10px">        $groups_json = wp_json_encode( $groups );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -410,6 +423,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">        fwrite( $file, 'th { padding: 5px; text-align: left; width: 20%; }' );
</span><span class="cx" style="display: block; padding: 0 10px">        fwrite( $file, 'td { padding: 5px; }' );
</span><span class="cx" style="display: block; padding: 0 10px">        fwrite( $file, 'tr:nth-child(odd) { background-color: #fafafa; }' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        fwrite( $file, '.return_to_top { text-align:right; }' );
</ins><span class="cx" style="display: block; padding: 0 10px">         fwrite( $file, '</style>' );
</span><span class="cx" style="display: block; padding: 0 10px">        fwrite( $file, '<title>' );
</span><span class="cx" style="display: block; padding: 0 10px">        fwrite( $file, esc_html( $title ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -416,11 +430,31 @@
</span><span class="cx" style="display: block; padding: 0 10px">        fwrite( $file, '</title>' );
</span><span class="cx" style="display: block; padding: 0 10px">        fwrite( $file, "</head>\n" );
</span><span class="cx" style="display: block; padding: 0 10px">        fwrite( $file, "<body>\n" );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        fwrite( $file, '<h1>' . esc_html__( 'Personal Data Export' ) . '</h1>' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ fwrite( $file, '<h1 id="top">' . esc_html__( 'Personal Data Export' ) . '</h1>' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        // Create TOC.
+       if ( 1 < $groups_count ) {
+               fwrite( $file, '<div id="table_of_contents">' );
+               fwrite( $file, '<h2>' . esc_html__( 'Table of Contents' ) . '</h2>' );
+               fwrite( $file, '<ul>' );
+               foreach ( (array) $groups as $group_id => $group_data ) {
+                       $group_label       = esc_html( $group_data['group_label'] );
+                       $group_id_attr     = sanitize_title_with_dashes( $group_data['group_label'] . '-' . $group_id );
+                       $group_items_count = count( (array) $group_data['items'] );
+                       if ( $group_items_count > 1 ) {
+                               $group_label .= sprintf( ' <span class="count">(%d)</span>', $group_items_count );
+                       }
+                       fwrite( $file, '<li>' );
+                       fwrite( $file, '<a href="#' . esc_attr( $group_id_attr ) . '">' . $group_label . '</a>' );
+                       fwrite( $file, '</li>' );
+               }
+               fwrite( $file, '</ul>' );
+               fwrite( $file, '</div>' );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         // Now, iterate over every group in $groups and have the formatter render it in HTML.
</span><span class="cx" style="display: block; padding: 0 10px">        foreach ( (array) $groups as $group_id => $group_data ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                fwrite( $file, wp_privacy_generate_personal_data_export_group_html( $group_data ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         fwrite( $file, wp_privacy_generate_personal_data_export_group_html( $group_data, $group_id, $groups_count ) );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        fwrite( $file, "</body>\n" );
</span></span></pre></div>
<a id="trunktestsphpunittestsprivacywpPrivacyGeneratePersonalDataExportFilephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php     2020-02-11 20:53:40 UTC (rev 47277)
+++ trunk/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php       2020-02-11 21:26:25 UTC (rev 47278)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -260,8 +260,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $report_contents = file_get_contents( $report_dir . 'index.html' );
</span><span class="cx" style="display: block; padding: 0 10px">                $request         = wp_get_user_request( self::$export_request_id );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertContains( '<h1>Personal Data Export</h1>', $report_contents );
-               $this->assertContains( '<h2>About</h2>', $report_contents );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertContains( '<h1 id="top">Personal Data Export</h1>', $report_contents );
+               $this->assertContains( '<h2 id="about-about">About</h2>', $report_contents );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->assertContains( $request->email, $report_contents );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -294,4 +294,364 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertContains( '"Personal Data Export for ' . $request->email . '"', $report_contents_json );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertContains( '"about"', $report_contents_json );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * 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( '<div id="table_of_contents">', $report_contents );
+               $this->assertNotContains( '<div class="return_to_top">', $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&#8217;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( '<div id="table_of_contents">', $report_contents );
+               $this->assertContains( '<h2 id="user-user">User</h2>', $report_contents );
+               $this->assertContains( '<div class="return_to_top">', $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&#8217;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&#8217;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' => '<a href="http://localhost:8888/46894/2020/01/31/hello-world/#comment-2" target="_blank" rel="noreferrer noopener">http://localhost:8888/46894/2020/01/31/hello-world/#comment-2</a>',
+                                               ),
+                                       ),
+                                       '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' => '<a href="http://localhost:8888/46894/2020/01/31/hello-world/#comment-3" target="_blank" rel="noreferrer noopener">http://localhost:8888/46894/2020/01/31/hello-world/#comment-3</a>',
+                                               ),
+                                       ),
+                               ),
+                       ),
+               );
+               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( '<div id="table_of_contents">', $report_contents );
+               $this->assertContains( '<a href="#comments-comments">Comments <span class="count">(2)</span></a>', $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&#8217;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&#8217;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' => '<a href="http://localhost:8888/46894/2020/01/31/hello-world/#comment-2" target="_blank" rel="noreferrer noopener">http://localhost:8888/46894/2020/01/31/hello-world/#comment-2</a>',
+                                               ),
+                                       ),
+                               ),
+                       ),
+               );
+               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( '<div id="table_of_contents">', $report_contents );
+               $this->assertNotContains( '<span class="count">', $report_contents );
+               $this->assertContains( $request->email, $report_contents );
+
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsphpunittestsprivacywpPrivacyGeneratePersonalDataExportGroupHtmlphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportGroupHtml.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportGroupHtml.php        2020-02-11 20:53:40 UTC (rev 47277)
+++ trunk/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportGroupHtml.php  2020-02-11 21:26:25 UTC (rev 47278)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -39,10 +39,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        ),
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $actual                = wp_privacy_generate_personal_data_export_group_html( $data );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $actual                = wp_privacy_generate_personal_data_export_group_html( $data, 'test-data-group', 2 );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $expected_table_markup = '<table><tbody><tr><th>Field 1 Name</th><td>Field 1 Value</td></tr><tr><th>Field 2 Name</th><td>Field 2 Value</td></tr></tbody></table>';
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertContains( '<h2>Test Data Group</h2>', $actual );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertContains( '<h2 id="test-data-group-test-data-group">Test Data Group</h2>', $actual );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->assertContains( $expected_table_markup, $actual );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -79,9 +79,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        ),
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $actual = wp_privacy_generate_personal_data_export_group_html( $data );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $actual = wp_privacy_generate_personal_data_export_group_html( $data, 'test-data-group', 2 );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertContains( '<h2>Test Data Group', $actual );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertContains( '<h2 id="test-data-group-test-data-group">Test Data Group', $actual );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->assertContains( '<td>Field 1 Value', $actual );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertContains( '<td>Another Field 1 Value', $actual );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertContains( '<td>Field 2 Value', $actual );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -117,7 +117,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        ),
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $actual = wp_privacy_generate_personal_data_export_group_html( $data );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $actual = wp_privacy_generate_personal_data_export_group_html( $data, 'test-data-group', 2 );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertContains( '<a href="http://wordpress.org">http://wordpress.org</a>', $actual );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertContains( '<a href="https://wordpress.org">https://wordpress.org</a>', $actual );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -131,13 +131,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_group_labels_escaped() {
</span><span class="cx" style="display: block; padding: 0 10px">                $data = array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'group_label' => '<div>Escape HTML in group lavels</div>',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'group_label' => '<div>Escape HTML in group labels</div>',
</ins><span class="cx" style="display: block; padding: 0 10px">                         'items'       => array(),
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $actual = wp_privacy_generate_personal_data_export_group_html( $data );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $actual = wp_privacy_generate_personal_data_export_group_html( $data, 'escape-html-in-group-labels', 2 );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertContains( '<h2>&lt;div&gt;Escape HTML in group lavels&lt;/div&gt;</h2>', $actual );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertContains( '<h2 id="escape-html-in-group-labels-escape-html-in-group-labels">&lt;div&gt;Escape HTML in group labels&lt;/div&gt;</h2>', $actual );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -162,8 +162,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        ),
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $actual = wp_privacy_generate_personal_data_export_group_html( $data );
-
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $actual = wp_privacy_generate_personal_data_export_group_html( $data, 'test-data-group', 2 );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->assertContains( $data['items'][0]['links']['value'], $actual );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertContains( $data['items'][0]['formatting']['value'], $actual );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -190,7 +189,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        ),
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $actual = wp_privacy_generate_personal_data_export_group_html( $data );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $actual = wp_privacy_generate_personal_data_export_group_html( $data, 'test-data-group', 2 );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertNotContains( $data['items'][0]['scripts']['value'], $actual );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertContains( '<td>Testing that script tags are stripped.</td>', $actual );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -223,9 +222,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        ),
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $actual = wp_privacy_generate_personal_data_export_group_html( $data );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $actual = wp_privacy_generate_personal_data_export_group_html( $data, 'test-data-group', 2 );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertContains( '<h2>Test Data Group', $actual );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertContains( '<h2 id="test-data-group-test-data-group">Test Data Group', $actual );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->assertContains( '<span class="count">(2)</span></h2>', $actual );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSame( 2, substr_count( $actual, '<table>' ) );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -248,9 +247,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        ),
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $actual = wp_privacy_generate_personal_data_export_group_html( $data );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $actual = wp_privacy_generate_personal_data_export_group_html( $data, 'test-data-group', 2 );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertContains( '<h2>Test Data Group</h2>', $actual );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertContains( '<h2 id="test-data-group-test-data-group">Test Data Group</h2>', $actual );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->assertNotContains( '<span class="count">', $actual );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSame( 1, substr_count( $actual, '<table>' ) );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span></span></pre>
</div>
</div>

</body>
</html>