Ticket #39000: 39000.3.diff
File 39000.3.diff, 7.5 KB (added by , 8 years ago) |
---|
-
src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php
92 92 'reassign' => array( 93 93 'type' => 'integer', 94 94 'description' => __( 'Reassign the deleted user\'s posts and links to this user ID.' ), 95 'required' => true, 96 'sanitize_callback' => array( $this, 'check_reassign' ), 95 97 ), 96 98 ), 97 99 ), … … 125 127 'reassign' => array( 126 128 'type' => 'integer', 127 129 'description' => __( 'Reassign the deleted user\'s posts and links to this user ID.' ), 130 'required' => true, 131 'sanitize_callback' => array( $this, 'check_reassign' ), 128 132 ), 129 133 ), 130 134 ), … … 133 137 } 134 138 135 139 /** 140 * Checks for a valid value for the reassign parameter when deleting users. 141 * 142 * @since 4.7.0 143 * 144 * @param $value 145 * @param $request 146 * @param $param 147 * 148 * @return int|bool|WP_Error 149 */ 150 public function check_reassign( $value, $request, $param ) { 151 if ( is_numeric( $value ) ) { 152 return $value; 153 } 154 155 if ( empty( $value ) || false === $value || 'false' === $value ) { 156 return false; 157 } 158 159 return new WP_Error( 'rest_invalid_param', __( 'Invalid user parameter(s).' ), array( 'status' => 400 ) ); 160 } 161 162 /** 136 163 * Permissions check for getting all users. 137 164 * 138 165 * @since 4.7.0 … … 673 700 */ 674 701 public function delete_item( $request ) { 675 702 $id = (int) $request['id']; 676 $reassign = isset( $request['reassign'] ) ? absint( $request['reassign'] ) : null;703 $reassign = false === $request['reassign'] ? null : absint( $request['reassign'] ); 677 704 $force = isset( $request['force'] ) ? (bool) $request['force'] : false; 678 705 679 706 // We don't support trashing for users. -
tests/phpunit/tests/rest-api/rest-users-controller.php
1639 1639 1640 1640 $userdata = get_userdata( $user_id ); // cache for later 1641 1641 $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) ); 1642 $request['force'] = true; 1642 $request->set_param( 'force', true ); 1643 $request->set_param( 'reassign', false ); 1643 1644 $response = $this->server->dispatch( $request ); 1644 1645 1645 1646 $this->assertEquals( 200, $response->get_status() ); … … 1657 1658 $userdata = get_userdata( $user_id ); // cache for later 1658 1659 1659 1660 $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) ); 1661 $request->set_param( 'reassign', false ); 1660 1662 $response = $this->server->dispatch( $request ); 1661 1663 $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 ); 1662 1664 … … 1678 1680 1679 1681 $request = new WP_REST_Request( 'DELETE', '/wp/v2/users/me' ); 1680 1682 $request['force'] = true; 1683 $request->set_param( 'reassign', false ); 1681 1684 $response = $this->server->dispatch( $request ); 1682 1685 1683 1686 $this->assertEquals( 200, $response->get_status() ); … … 1694 1697 update_site_option( 'site_admins', array( $user->user_login ) ); 1695 1698 1696 1699 $request = new WP_REST_Request( 'DELETE', '/wp/v2/users/me' ); 1700 $request->set_param( 'reassign', false ); 1697 1701 $response = $this->server->dispatch( $request ); 1698 1702 $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 ); 1699 1703 … … 1714 1718 1715 1719 $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) ); 1716 1720 $request['force'] = true; 1721 $request->set_param( 'reassign', false ); 1717 1722 $response = $this->server->dispatch( $request ); 1718 1723 1719 1724 $this->assertErrorResponse( 'rest_user_cannot_delete', $response, 403 ); … … 1720 1725 1721 1726 $request = new WP_REST_Request( 'DELETE', '/wp/v2/users/me' ); 1722 1727 $request['force'] = true; 1728 $request->set_param( 'reassign', false ); 1723 1729 $response = $this->server->dispatch( $request ); 1724 1730 1725 1731 $this->assertErrorResponse( 'rest_user_cannot_delete', $response, 403 ); … … 1731 1737 1732 1738 $request = new WP_REST_Request( 'DELETE', '/wp/v2/users/100' ); 1733 1739 $request['force'] = true; 1740 $request->set_param( 'reassign', false ); 1734 1741 $response = $this->server->dispatch( $request ); 1735 1742 1736 1743 $this->assertErrorResponse( 'rest_user_invalid_id', $response, 404 ); … … 1778 1785 $this->assertErrorResponse( 'rest_user_invalid_reassign', $response, 400 ); 1779 1786 } 1780 1787 1788 public function test_delete_user_invalid_reassign_passed_as_string() { 1789 $user_id = $this->factory->user->create(); 1790 1791 $this->allow_user_to_manage_multisite(); 1792 wp_set_current_user( self::$user ); 1793 1794 $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) ); 1795 $request['force'] = true; 1796 $request->set_param( 'reassign', 'null' ); 1797 $response = $this->server->dispatch( $request ); 1798 1799 $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); 1800 } 1801 1802 public function test_delete_user_reassign_passed_as_boolean_false_trashes_post() { 1803 $user_id = $this->factory->user->create(); 1804 1805 $this->allow_user_to_manage_multisite(); 1806 wp_set_current_user( self::$user ); 1807 1808 $test_post = $this->factory->post->create(array( 1809 'post_author' => $user_id, 1810 )); 1811 1812 $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) ); 1813 $request['force'] = true; 1814 $request->set_param( 'reassign', false ); 1815 $this->server->dispatch( $request ); 1816 1817 $test_post = get_post( $test_post ); 1818 $this->assertEquals( 'trash', $test_post->post_status ); 1819 } 1820 1821 public function test_delete_user_reassign_passed_as_string_false_trashes_post() { 1822 $user_id = $this->factory->user->create(); 1823 1824 $this->allow_user_to_manage_multisite(); 1825 wp_set_current_user( self::$user ); 1826 1827 $test_post = $this->factory->post->create(array( 1828 'post_author' => $user_id, 1829 )); 1830 1831 $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) ); 1832 $request['force'] = true; 1833 $request->set_param( 'reassign', 'false' ); 1834 $this->server->dispatch( $request ); 1835 1836 $test_post = get_post( $test_post ); 1837 $this->assertEquals( 'trash', $test_post->post_status ); 1838 } 1839 1840 public function test_delete_user_reassign_passed_as_empty_string_trashes_post() { 1841 $user_id = $this->factory->user->create(); 1842 1843 $this->allow_user_to_manage_multisite(); 1844 wp_set_current_user( self::$user ); 1845 1846 $test_post = $this->factory->post->create(array( 1847 'post_author' => $user_id, 1848 )); 1849 1850 $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) ); 1851 $request['force'] = true; 1852 $request->set_param( 'reassign', '' ); 1853 $this->server->dispatch( $request ); 1854 1855 $test_post = get_post( $test_post ); 1856 $this->assertEquals( 'trash', $test_post->post_status ); 1857 } 1858 1859 public function test_delete_user_reassign_passed_as_0_reassigns_author() { 1860 $user_id = $this->factory->user->create(); 1861 1862 $this->allow_user_to_manage_multisite(); 1863 wp_set_current_user( self::$user ); 1864 1865 $test_post = $this->factory->post->create(array( 1866 'post_author' => $user_id, 1867 )); 1868 1869 $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) ); 1870 $request['force'] = true; 1871 $request->set_param( 'reassign', 0 ); 1872 $this->server->dispatch( $request ); 1873 1874 $test_post = get_post( $test_post ); 1875 $this->assertEquals( 0, $test_post->post_author ); 1876 } 1877 1781 1878 public function test_get_item_schema() { 1782 1879 $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/users' ); 1783 1880 $response = $this->server->dispatch( $request );