Make WordPress Core

Ticket #46894: 46894.4.diff

File 46894.4.diff, 5.9 KB (added by garrett-eclipse, 5 years ago)

Refreshed patch to esc_attr, and fix unit tests

  • src/wp-admin/includes/privacy-tools.php

     
    234234 * @return string The HTML for this group and its items.
    235235 */
    236236function wp_privacy_generate_personal_data_export_group_html( $group_data ) {
    237         $group_html  = '<h2>';
     237        $group_html  = '<h2 id="' . esc_attr( sanitize_title_with_dashes( $group_data['group_label'] ) ) . '">';
    238238        $group_html .= esc_html( $group_data['group_label'] );
    239239
    240240        $items_count = count( (array) $group_data['items'] );
     
    271271                $group_html .= '</table>';
    272272        }
    273273
     274        $group_html .= '<div class="return_to_top">';
     275        $group_html .= '<a href="#top">' . esc_html__( '&uarr; Return to top' ) . '</a>';
    274276        $group_html .= '</div>';
    275277
     278        $group_html .= '</div>';
     279
    276280        return $group_html;
    277281}
    278282
     
    350354        fwrite( $file, 'th { padding: 5px; text-align: left; width: 20%; }' );
    351355        fwrite( $file, 'td { padding: 5px; }' );
    352356        fwrite( $file, 'tr:nth-child(odd) { background-color: #fafafa; }' );
     357        fwrite( $file, '.return_to_top { text-align:right; }' );
    353358        fwrite( $file, '</style>' );
    354359        fwrite( $file, '<title>' );
    355360        fwrite( $file, esc_html( $title ) );
     
    360365        fwrite( $file, "<body>\n" );
    361366
    362367        // Heading.
    363         fwrite( $file, '<h1>' . esc_html__( 'Personal Data Export' ) . '</h1>' );
     368        fwrite( $file, '<h1 id="top">' . esc_html__( 'Personal Data Export' ) . '</h1>' );
    364369
    365370        // And now, all the Groups.
    366371        $groups = get_post_meta( $request_id, '_export_data_grouped', true );
     
    396401        // Merge in the special about group.
    397402        $groups = array_merge( array( 'about' => $about_group ), $groups );
    398403
     404        // Create TOC.
     405        if ( count( $groups ) > 1 ) {
     406                fwrite( $file, '<div id="table_of_contents">' );
     407                fwrite( $file, '<h2>' . esc_html__( 'Table of Contents' ) . '</h2>' );
     408                fwrite( $file, '<ul>' );
     409
     410                foreach ( (array) $groups as $group_id => $group_data ) {
     411                        fwrite( $file, '<li>' );
     412                        fwrite( $file, '<a href="#' . esc_attr( sanitize_title_with_dashes( $group_data['group_label'] ) ) . '">' . esc_html( $group_data['group_label'] ) . '</a>' );
     413                        fwrite( $file, '</li>' );
     414                }
     415
     416                fwrite( $file, '</ul>' );
     417                fwrite( $file, '</div>' );
     418        }
     419
    399420        // Now, iterate over every group in $groups and have the formatter render it in HTML.
    400421        foreach ( (array) $groups as $group_id => $group_data ) {
    401422                fwrite( $file, wp_privacy_generate_personal_data_export_group_html( $group_data ) );
  • tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php

     
    260260                $report_contents = file_get_contents( $report_dir . 'index.html' );
    261261                $request         = wp_get_user_request_data( self::$export_request_id );
    262262
    263                 $this->assertContains( '<h1>Personal Data Export</h1>', $report_contents );
    264                 $this->assertContains( '<h2>About</h2>', $report_contents );
     263                $this->assertContains( '<h1 id="top">Personal Data Export</h1>', $report_contents );
     264                $this->assertContains( '<h2 id="about">About</h2>', $report_contents );
    265265                $this->assertContains( $request->email, $report_contents );
    266266        }
    267267}
  • tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportGroupHtml.php

     
    4242                $actual                = wp_privacy_generate_personal_data_export_group_html( $data );
    4343                $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>';
    4444
    45                 $this->assertContains( '<h2>Test Data Group</h2>', $actual );
     45                $this->assertContains( '<h2 id="test-data-group">Test Data Group</h2>', $actual );
    4646                $this->assertContains( $expected_table_markup, $actual );
    4747        }
    4848
     
    8181
    8282                $actual = wp_privacy_generate_personal_data_export_group_html( $data );
    8383
    84                 $this->assertContains( '<h2>Test Data Group', $actual );
     84                $this->assertContains( '<h2 id="test-data-group">Test Data Group', $actual );
    8585                $this->assertContains( '<td>Field 1 Value', $actual );
    8686                $this->assertContains( '<td>Another Field 1 Value', $actual );
    8787                $this->assertContains( '<td>Field 2 Value', $actual );
     
    131131         */
    132132        public function test_group_labels_escaped() {
    133133                $data = array(
    134                         'group_label' => '<div>Escape HTML in group lavels</div>',
     134                        'group_label' => '<div>Escape HTML in group labels</div>',
    135135                        'items'       => array(),
    136136                );
    137137
    138138                $actual = wp_privacy_generate_personal_data_export_group_html( $data );
    139139
    140                 $this->assertContains( '<h2>&lt;div&gt;Escape HTML in group lavels&lt;/div&gt;</h2>', $actual );
     140                $this->assertContains( '<h2 id="escape-html-in-group-labels">&lt;div&gt;Escape HTML in group labels&lt;/div&gt;</h2>', $actual );
    141141        }
    142142
    143143        /**
     
    225225
    226226                $actual = wp_privacy_generate_personal_data_export_group_html( $data );
    227227
    228                 $this->assertContains( '<h2>Test Data Group', $actual );
     228                $this->assertContains( '<h2 id="test-data-group">Test Data Group', $actual );
    229229                $this->assertContains( '<span class="count">(2)</span></h2>', $actual );
    230230                $this->assertSame( 2, substr_count( $actual, '<table>' ) );
    231231        }
     
    250250
    251251                $actual = wp_privacy_generate_personal_data_export_group_html( $data );
    252252
    253                 $this->assertContains( '<h2>Test Data Group</h2>', $actual );
     253                $this->assertContains( '<h2 id="test-data-group">Test Data Group</h2>', $actual );
    254254                $this->assertNotContains( '<span class="count">', $actual );
    255255                $this->assertSame( 1, substr_count( $actual, '<table>' ) );
    256256        }