Make WordPress Core


Ignore:
Timestamp:
02/11/2020 06:57:51 PM (5 years ago)
Author:
SergeyBiryukov
Message:

Privacy: Introduce wp_privacy_additional_user_data filter to make it easier to include additional user meta in a personal data export.

Props pbiron, xkon, garrett-eclipse, azaozz.
Fixes #47509.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/user.php

    r47237 r47270  
    17951795        $this->assertEquals( 'January 29, 2020 09:13 AM', $actual['data'][1]['data'][3]['value'] );
    17961796    }
     1797
     1798    /**
     1799     * Testing the `wp_privacy_additional_user_profile_data` filter works.
     1800     *
     1801     * @ticket 47509
     1802     */
     1803    function test_filter_wp_privacy_additional_user_profile_data() {
     1804        $test_user = new WP_User( self::$contrib_id );
     1805
     1806        add_filter( 'wp_privacy_additional_user_profile_data', array( $this, 'export_additional_user_profile_data' ) );
     1807
     1808        $actual = wp_user_personal_data_exporter( $test_user->user_email );
     1809
     1810        remove_filter( 'wp_privacy_additional_user_profile_data', array( $this, 'export_additional_user_profile_data' ) );
     1811
     1812        $this->assertTrue( $actual['done'] );
     1813
     1814        // Number of exported users.
     1815        $this->assertSame( 1, count( $actual['data'] ) );
     1816
     1817        // Number of exported user properties (the 11 core properties,
     1818        // plus 1 additional from the filter).
     1819        $this->assertSame( 12, count( $actual['data'][0]['data'] ) );
     1820
     1821        // Check that the item added by the filter was retained.
     1822        $this->assertSame(
     1823            1,
     1824            count(
     1825                wp_list_filter(
     1826                    $actual['data'][0]['data'],
     1827                    array(
     1828                        'name'  => 'Test Additional Data Name',
     1829                        'value' => 'Test Additional Data Value',
     1830                    )
     1831                )
     1832            )
     1833        );
     1834
     1835        // _doing_wrong() should be called because the filter callback
     1836        // adds a item with a 'name' that is the same as one generated by core.
     1837        $this->setExpectedIncorrectUsage( 'wp_user_personal_data_exporter' );
     1838        add_filter( 'wp_privacy_additional_user_profile_data', array( $this, 'export_additional_user_profile_data_with_dup_name' ) );
     1839
     1840        $actual = wp_user_personal_data_exporter( $test_user->user_email );
     1841
     1842        remove_filter( 'wp_privacy_additional_user_profile_data', array( $this, 'export_additional_user_profile_data' ) );
     1843
     1844        $this->assertTrue( $actual['done'] );
     1845
     1846        // Number of exported users.
     1847        $this->assertSame( 1, count( $actual['data'] ) );
     1848
     1849        // Number of exported user properties
     1850        // (the 11 core properties, plus 1 additional from the filter).
     1851        $this->assertSame( 12, count( $actual['data'][0]['data'] ) );
     1852
     1853        // Check that the duplicate 'name' => 'User ID' was stripped.
     1854        $this->assertSame(
     1855            1,
     1856            count(
     1857                wp_list_filter(
     1858                    $actual['data'][0]['data'],
     1859                    array(
     1860                        'name' => 'User ID',
     1861                    )
     1862                )
     1863            )
     1864        );
     1865
     1866        // Check that the item added by the filter was retained.
     1867        $this->assertSame(
     1868            1,
     1869            count(
     1870                wp_list_filter(
     1871                    $actual['data'][0]['data'],
     1872                    array(
     1873                        'name'  => 'Test Additional Data Name',
     1874                        'value' => 'Test Additional Data Value',
     1875                    )
     1876                )
     1877            )
     1878        );
     1879    }
     1880
     1881    /**
     1882     * Filter callback to add additional profile data to the User Group on Export Requests.
     1883     *
     1884     * @ticket 47509
     1885     *
     1886     * @return array $additional_profile_data The additional user data.
     1887     */
     1888    public function export_additional_user_profile_data() {
     1889        $additional_profile_data = array(
     1890            // This item should be retained and included in the export.
     1891            array(
     1892                'name'  => 'Test Additional Data Name',
     1893                'value' => 'Test Additional Data Value',
     1894            ),
     1895        );
     1896
     1897        return $additional_profile_data;
     1898    }
     1899
     1900    /**
     1901     * Filter callback to add additional profile data to the User Group on Export Requests.
     1902     *
     1903     * This callback should generate a `_doing_it_wrong()`.
     1904     *
     1905     * @ticket 47509
     1906     *
     1907     * @return array $additional_profile_data The additional user data.
     1908     */
     1909    public function export_additional_user_profile_data_with_dup_name() {
     1910        $additional_profile_data = array(
     1911            // This item should be stripped out by wp_user_personal_data_exporter()
     1912            // because it's 'name' duplicates one exported by core.
     1913            array(
     1914                'name'  => 'User ID',
     1915                'value' => 'Some User ID',
     1916            ),
     1917            // This item should be retained and included in the export.
     1918            array(
     1919                'name'  => 'Test Additional Data Name',
     1920                'value' => 'Test Additional Data Value',
     1921            ),
     1922        );
     1923
     1924        return $additional_profile_data;
     1925    }
    17971926}
Note: See TracChangeset for help on using the changeset viewer.