| 1688 | |
| 1689 | /** |
| 1690 | * Testing the `wp_privacy_additional_user_profile_data` filter works. |
| 1691 | * |
| 1692 | * @since 5.4.0 |
| 1693 | * |
| 1694 | * @ticket 47509 |
| 1695 | */ |
| 1696 | function test_filter_wp_privacy_additional_user_profile_data() { |
| 1697 | $test_user = new WP_User( self::$contrib_id ); |
| 1698 | |
| 1699 | add_filter( 'wp_privacy_additional_user_profile_data', array( $this, 'export_additional_user_profile_data' ) ); |
| 1700 | |
| 1701 | $actual = wp_user_personal_data_exporter( $test_user->user_email ); |
| 1702 | |
| 1703 | remove_filter( 'wp_privacy_additional_user_profile_data', array( $this, 'export_additional_user_profile_data' ) ); |
| 1704 | |
| 1705 | $this->assertTrue( $actual['done'] ); |
| 1706 | |
| 1707 | // Number of exported users. |
| 1708 | $this->assertSame( 1, count( $actual['data'] ) ); |
| 1709 | |
| 1710 | // Number of exported user properties (the 11 core properties, |
| 1711 | // plus 1 additional from the filter). |
| 1712 | $this->assertSame( 12, count( $actual['data'][0]['data'] ) ); |
| 1713 | |
| 1714 | // Check that the item added by the filter was retained. |
| 1715 | $this->assertSame( |
| 1716 | 1, |
| 1717 | count( |
| 1718 | wp_list_filter( |
| 1719 | $actual['data'][0]['data'], |
| 1720 | array( |
| 1721 | 'name' => 'Test Additional Data Name', |
| 1722 | 'value' => 'Test Additional Data Value' |
| 1723 | ) |
| 1724 | ) |
| 1725 | ) |
| 1726 | ); |
| 1727 | |
| 1728 | // _doing_wrong() should be called because the filter callback |
| 1729 | // adds a item with a 'name' that is the same as one generated by core. |
| 1730 | $this->setExpectedIncorrectUsage( 'wp_user_personal_data_exporter' ); |
| 1731 | add_filter( 'wp_privacy_additional_user_profile_data', array( $this, 'export_additional_user_profile_data_with_dup_name' ) ); |
| 1732 | |
| 1733 | $actual = wp_user_personal_data_exporter( $test_user->user_email ); |
| 1734 | |
| 1735 | remove_filter( 'wp_privacy_additional_user_profile_data', array( $this, 'export_additional_user_profile_data' ) ); |
| 1736 | |
| 1737 | $this->assertTrue( $actual['done'] ); |
| 1738 | |
| 1739 | // Number of exported users. |
| 1740 | $this->assertSame( 1, count( $actual['data'] ) ); |
| 1741 | |
| 1742 | // Number of exported user properties (the 11 core properties, |
| 1743 | // plus 1 additional from the filter). |
| 1744 | $this->assertSame( 12, count( $actual['data'][0]['data'] ) ); |
| 1745 | |
| 1746 | // Check that the duplicate 'name' => 'User ID' was stripped. |
| 1747 | $this->assertSame( |
| 1748 | 1, |
| 1749 | count( |
| 1750 | wp_list_filter( |
| 1751 | $actual['data'][0]['data'], |
| 1752 | array( |
| 1753 | 'name' => 'User ID', |
| 1754 | ) |
| 1755 | ) |
| 1756 | ) |
| 1757 | ); |
| 1758 | |
| 1759 | // Check that the item added by the filter was retained. |
| 1760 | $this->assertSame( |
| 1761 | 1, |
| 1762 | count( |
| 1763 | wp_list_filter( |
| 1764 | $actual['data'][0]['data'], |
| 1765 | array( |
| 1766 | 'name' => 'Test Additional Data Name', |
| 1767 | 'value' => 'Test Additional Data Value' |
| 1768 | ) |
| 1769 | ) |
| 1770 | ) |
| 1771 | ); |
| 1772 | } |
| 1773 | |
| 1774 | /** |
| 1775 | * Filter callback to add additional profile data to the User Group on Export Requests. |
| 1776 | * |
| 1777 | * @since 5.4.0 |
| 1778 | * |
| 1779 | * @ticket 47509 |
| 1780 | * |
| 1781 | * @return array $additional_profile_data The additional User data. |
| 1782 | */ |
| 1783 | public function export_additional_user_profile_data() { |
| 1784 | $additional_profile_data = array( |
| 1785 | // This item should be retained and included in the export. |
| 1786 | array( |
| 1787 | 'name' => 'Test Additional Data Name', |
| 1788 | 'value' => 'Test Additional Data Value', |
| 1789 | ), |
| 1790 | ); |
| 1791 | |
| 1792 | return $additional_profile_data; |
| 1793 | } |
| 1794 | |
| 1795 | /** |
| 1796 | * Filter callback to add additional profile data to the User Group on Export Requests. |
| 1797 | * |
| 1798 | * This callback should generate a `_doing_it_wrong()`. |
| 1799 | * |
| 1800 | * @since 5.4.0 |
| 1801 | * |
| 1802 | * @ticket 47509 |
| 1803 | * |
| 1804 | * @return array $additional_profile_data The additional User data. |
| 1805 | */ |
| 1806 | public function export_additional_user_profile_data_with_dup_name() { |
| 1807 | $additional_profile_data = array( |
| 1808 | // This item should be stripped out by wp_user_personal_data_exporter() |
| 1809 | // because it's 'name' duplicates one exported by core. |
| 1810 | array( |
| 1811 | 'name' => 'User ID', |
| 1812 | 'value' => 'Some User ID', |
| 1813 | ), |
| 1814 | // This item should be retained and included in the export. |
| 1815 | array( |
| 1816 | 'name' => 'Test Additional Data Name', |
| 1817 | 'value' => 'Test Additional Data Value', |
| 1818 | ), |
| 1819 | ); |
| 1820 | |
| 1821 | return $additional_profile_data; |
| 1822 | } |