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