diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php
index 3454dc1..3981050 100644
a
|
b
|
class WP_REST_Comments_Controller extends WP_REST_Controller { |
83 | 83 | 'permission_callback' => array( $this, 'delete_item_permissions_check' ), |
84 | 84 | 'args' => array( |
85 | 85 | 'force' => array( |
| 86 | 'type' => 'boolean', |
86 | 87 | 'default' => false, |
87 | 88 | 'description' => __( 'Whether to bypass trash and force deletion.' ), |
88 | 89 | ), |
… |
… |
class WP_REST_Comments_Controller extends WP_REST_Controller { |
726 | 727 | |
727 | 728 | $request->set_param( 'context', 'edit' ); |
728 | 729 | |
729 | | $response = $this->prepare_item_for_response( $comment, $request ); |
730 | | |
731 | 730 | if ( $force ) { |
| 731 | $previous = $this->prepare_item_for_response( $comment, $request ); |
732 | 732 | $result = wp_delete_comment( $comment->comment_ID, true ); |
| 733 | $response = new WP_REST_Response(); |
| 734 | $response->set_data( array( 'deleted' => true, 'previous' => $previous->get_data() ) ); |
733 | 735 | } else { |
734 | 736 | // If this type doesn't support trashing, error out. |
735 | 737 | if ( ! $supports_trash ) { |
736 | | return new WP_Error( 'rest_trash_not_supported', __( 'The comment does not support trashing.' ), array( 'status' => 501 ) ); |
| 738 | return new WP_Error( 'rest_trash_not_supported', __( 'The comment does not support trashing. Set force=true to delete.' ), array( 'status' => 501 ) ); |
737 | 739 | } |
738 | 740 | |
739 | 741 | if ( 'trash' === $comment->comment_approved ) { |
… |
… |
class WP_REST_Comments_Controller extends WP_REST_Controller { |
741 | 743 | } |
742 | 744 | |
743 | 745 | $result = wp_trash_comment( $comment->comment_ID ); |
| 746 | $comment = get_comment( $comment->comment_ID ); |
| 747 | $response = $this->prepare_item_for_response( $comment, $request ); |
744 | 748 | } |
745 | 749 | |
746 | 750 | if ( ! $result ) { |
diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
index 0122f9a..24fd100 100644
a
|
b
|
class WP_REST_Posts_Controller extends WP_REST_Controller { |
101 | 101 | 'permission_callback' => array( $this, 'delete_item_permissions_check' ), |
102 | 102 | 'args' => array( |
103 | 103 | 'force' => array( |
| 104 | 'type' => 'boolean', |
104 | 105 | 'default' => false, |
105 | 106 | 'description' => __( 'Whether to bypass trash and force deletion.' ), |
106 | 107 | ), |
… |
… |
class WP_REST_Posts_Controller extends WP_REST_Controller { |
749 | 750 | |
750 | 751 | $request->set_param( 'context', 'edit' ); |
751 | 752 | |
752 | | $response = $this->prepare_item_for_response( $post, $request ); |
753 | 753 | |
754 | 754 | // If we're forcing, then delete permanently. |
755 | 755 | if ( $force ) { |
| 756 | $previous = $this->prepare_item_for_response( $post, $request ); |
756 | 757 | $result = wp_delete_post( $id, true ); |
| 758 | $response = new WP_REST_Response(); |
| 759 | $response->set_data( array( 'deleted' => true, 'previous' => $previous->get_data() ) ); |
757 | 760 | } else { |
758 | 761 | // If we don't support trashing for this type, error out. |
759 | 762 | if ( ! $supports_trash ) { |
760 | | return new WP_Error( 'rest_trash_not_supported', __( 'The post does not support trashing.' ), array( 'status' => 501 ) ); |
| 763 | return new WP_Error( 'rest_trash_not_supported', __( 'The post does not support trashing. Set force=true to delete.' ), array( 'status' => 501 ) ); |
761 | 764 | } |
762 | 765 | |
763 | 766 | // Otherwise, only trash if we haven't already. |
… |
… |
class WP_REST_Posts_Controller extends WP_REST_Controller { |
768 | 771 | // (Note that internally this falls through to `wp_delete_post` if |
769 | 772 | // the trash is disabled.) |
770 | 773 | $result = wp_trash_post( $id ); |
| 774 | $post = $this->get_post( $id ); |
| 775 | $response = $this->prepare_item_for_response( $post, $request ); |
771 | 776 | } |
772 | 777 | |
773 | 778 | if ( ! $result ) { |
diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php
index 9ecd0ad..9373a9b 100644
a
|
b
|
class WP_REST_Revisions_Controller extends WP_REST_Controller { |
93 | 93 | 'methods' => WP_REST_Server::DELETABLE, |
94 | 94 | 'callback' => array( $this, 'delete_item' ), |
95 | 95 | 'permission_callback' => array( $this, 'delete_item_permissions_check' ), |
| 96 | 'args' => array( |
| 97 | 'force' => array( |
| 98 | 'type' => 'boolean', |
| 99 | 'default' => false, |
| 100 | 'description' => __( 'Required to be true, as resource does not support trashing.' ), |
| 101 | ), |
| 102 | ), |
96 | 103 | ), |
97 | 104 | 'schema' => array( $this, 'get_public_item_schema' ), |
98 | 105 | )); |
… |
… |
class WP_REST_Revisions_Controller extends WP_REST_Controller { |
220 | 227 | * @return true|WP_Error True on success, or WP_Error object on failure. |
221 | 228 | */ |
222 | 229 | public function delete_item( $request ) { |
| 230 | $force = isset( $request['force'] ) ? (bool) $request['force'] : false; |
| 231 | |
| 232 | // We don't support trashing for this resource type. |
| 233 | if ( ! $force ) { |
| 234 | return new WP_Error( 'rest_trash_not_supported', __( 'Revisions do not support trashing. Set force=true to delete.' ), array( 'status' => 501 ) ); |
| 235 | } |
| 236 | |
| 237 | $revision = $this->get_post( $request['id'] ); |
| 238 | $previous = $this->prepare_item_for_response( $revision, $request ); |
| 239 | |
223 | 240 | $result = wp_delete_post( $request['id'], true ); |
224 | 241 | |
225 | 242 | /** |
… |
… |
class WP_REST_Revisions_Controller extends WP_REST_Controller { |
234 | 251 | */ |
235 | 252 | do_action( 'rest_delete_revision', $result, $request ); |
236 | 253 | |
237 | | if ( $result ) { |
238 | | return true; |
239 | | } else { |
| 254 | if ( ! $result ) { |
240 | 255 | return new WP_Error( 'rest_cannot_delete', __( 'The post cannot be deleted.' ), array( 'status' => 500 ) ); |
241 | 256 | } |
| 257 | |
| 258 | $response = new WP_REST_Response(); |
| 259 | $response->set_data( array( 'deleted' => true, 'previous' => $previous->get_data() ) ); |
| 260 | return $response; |
242 | 261 | } |
243 | 262 | |
244 | 263 | /** |
diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php
index ceaa26b..13962a7 100644
a
|
b
|
class WP_REST_Terms_Controller extends WP_REST_Controller { |
116 | 116 | 'permission_callback' => array( $this, 'delete_item_permissions_check' ), |
117 | 117 | 'args' => array( |
118 | 118 | 'force' => array( |
| 119 | 'type' => 'boolean', |
119 | 120 | 'default' => false, |
120 | 121 | 'description' => __( 'Required to be true, as resource does not support trashing.' ), |
121 | 122 | ), |
… |
… |
class WP_REST_Terms_Controller extends WP_REST_Controller { |
566 | 567 | |
567 | 568 | // We don't support trashing for this resource type. |
568 | 569 | if ( ! $force ) { |
569 | | return new WP_Error( 'rest_trash_not_supported', __( 'Resource does not support trashing.' ), array( 'status' => 501 ) ); |
| 570 | return new WP_Error( 'rest_trash_not_supported', __( 'Terms do not support trashing. Set force=true to delete.' ), array( 'status' => 501 ) ); |
570 | 571 | } |
571 | 572 | |
572 | 573 | $term = get_term( (int) $request['id'], $this->taxonomy ); |
573 | 574 | |
574 | 575 | $request->set_param( 'context', 'view' ); |
575 | 576 | |
576 | | $response = $this->prepare_item_for_response( $term, $request ); |
| 577 | $previous = $this->prepare_item_for_response( $term, $request ); |
577 | 578 | |
578 | 579 | $retval = wp_delete_term( $term->term_id, $term->taxonomy ); |
579 | 580 | |
… |
… |
class WP_REST_Terms_Controller extends WP_REST_Controller { |
581 | 582 | return new WP_Error( 'rest_cannot_delete', __( 'The resource cannot be deleted.' ), array( 'status' => 500 ) ); |
582 | 583 | } |
583 | 584 | |
| 585 | $response = new WP_REST_Response(); |
| 586 | $response->set_data( array( 'deleted' => true, 'previous' => $previous->get_data() ) ); |
| 587 | |
584 | 588 | /** |
585 | 589 | * Fires after a single term is deleted via the REST API. |
586 | 590 | * |
diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php
index 9a8cc21..57e2aaa 100644
a
|
b
|
class WP_REST_Users_Controller extends WP_REST_Controller { |
85 | 85 | 'permission_callback' => array( $this, 'delete_item_permissions_check' ), |
86 | 86 | 'args' => array( |
87 | 87 | 'force' => array( |
| 88 | 'type' => 'boolean', |
88 | 89 | 'default' => false, |
89 | 90 | 'description' => __( 'Required to be true, as resource does not support trashing.' ), |
90 | 91 | ), |
… |
… |
class WP_REST_Users_Controller extends WP_REST_Controller { |
114 | 115 | 'permission_callback' => array( $this, 'delete_current_item_permissions_check' ), |
115 | 116 | 'args' => array( |
116 | 117 | 'force' => array( |
| 118 | 'type' => 'boolean', |
117 | 119 | 'default' => false, |
118 | 120 | 'description' => __( 'Required to be true, as resource does not support trashing.' ), |
119 | 121 | ), |
… |
… |
class WP_REST_Users_Controller extends WP_REST_Controller { |
653 | 655 | |
654 | 656 | // We don't support trashing for this type, error out. |
655 | 657 | if ( ! $force ) { |
656 | | return new WP_Error( 'rest_trash_not_supported', __( 'Users do not support trashing.' ), array( 'status' => 501 ) ); |
| 658 | return new WP_Error( 'rest_trash_not_supported', __( 'Users do not support trashing. Set force=true to delete.' ), array( 'status' => 501 ) ); |
657 | 659 | } |
658 | 660 | |
659 | 661 | $user = get_userdata( $id ); |
… |
… |
class WP_REST_Users_Controller extends WP_REST_Controller { |
670 | 672 | |
671 | 673 | $request->set_param( 'context', 'edit' ); |
672 | 674 | |
673 | | $response = $this->prepare_item_for_response( $user, $request ); |
| 675 | $previous = $this->prepare_item_for_response( $user, $request ); |
674 | 676 | |
675 | 677 | /** Include admin user functions to get access to wp_delete_user() */ |
676 | 678 | require_once ABSPATH . 'wp-admin/includes/user.php'; |
… |
… |
class WP_REST_Users_Controller extends WP_REST_Controller { |
681 | 683 | return new WP_Error( 'rest_cannot_delete', __( 'The resource cannot be deleted.' ), array( 'status' => 500 ) ); |
682 | 684 | } |
683 | 685 | |
| 686 | $response = new WP_REST_Response(); |
| 687 | $response->set_data( array( 'deleted' => true, 'previous' => $previous->get_data() ) ); |
| 688 | |
684 | 689 | /** |
685 | 690 | * Fires immediately after a user is deleted via the REST API. |
686 | 691 | * |
diff --git a/tests/phpunit/tests/rest-api/rest-attachments-controller.php b/tests/phpunit/tests/rest-api/rest-attachments-controller.php
index 7960312..747f1a0 100644
a
|
b
|
class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control |
699 | 699 | $response = $this->server->dispatch( $request ); |
700 | 700 | $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 ); |
701 | 701 | |
| 702 | $request->set_param( 'force', 'false' ); |
| 703 | $response = $this->server->dispatch( $request ); |
| 704 | $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 ); |
| 705 | |
702 | 706 | // Ensure the post still exists |
703 | 707 | $post = get_post( $attachment_id ); |
704 | 708 | $this->assertNotEmpty( $post ); |
diff --git a/tests/phpunit/tests/rest-api/rest-categories-controller.php b/tests/phpunit/tests/rest-api/rest-categories-controller.php
index 440cb45..11754f9 100644
a
|
b
|
class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas |
716 | 716 | $response = $this->server->dispatch( $request ); |
717 | 717 | $this->assertEquals( 200, $response->get_status() ); |
718 | 718 | $data = $response->get_data(); |
719 | | $this->assertEquals( 'Deleted Category', $data['name'] ); |
| 719 | $this->assertTrue( $data['deleted'] ); |
| 720 | $this->assertEquals( 'Deleted Category', $data['previous']['name'] ); |
720 | 721 | } |
721 | 722 | |
722 | | public function test_delete_item_force_false() { |
| 723 | public function test_delete_item_no_trash() { |
723 | 724 | wp_set_current_user( self::$administrator ); |
724 | 725 | $term = get_term_by( 'id', $this->factory->category->create( array( 'name' => 'Deleted Category' ) ), 'category' ); |
| 726 | |
725 | 727 | $request = new WP_REST_Request( 'DELETE', '/wp/v2/categories/' . $term->term_id ); |
726 | | // force defaults to false |
727 | 728 | $response = $this->server->dispatch( $request ); |
728 | | $this->assertEquals( 501, $response->get_status() ); |
| 729 | $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 ); |
| 730 | |
| 731 | $request->set_param( 'force', 'false' ); |
| 732 | $response = $this->server->dispatch( $request ); |
| 733 | $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 ); |
729 | 734 | } |
730 | 735 | |
731 | 736 | public function test_delete_item_invalid_taxonomy() { |
diff --git a/tests/phpunit/tests/rest-api/rest-comments-controller.php b/tests/phpunit/tests/rest-api/rest-comments-controller.php
index 8dc8c4c..4316fbd 100644
a
|
b
|
class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase |
1617 | 1617 | 'comment_post_ID' => self::$post_id, |
1618 | 1618 | 'user_id' => self::$subscriber_id, |
1619 | 1619 | )); |
1620 | | $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $comment_id ) ); |
1621 | 1620 | |
| 1621 | $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $comment_id ) ); |
| 1622 | $request->set_param( 'force', 'false' ); |
1622 | 1623 | $response = $this->server->dispatch( $request ); |
1623 | 1624 | $this->assertEquals( 200, $response->get_status() ); |
| 1625 | |
1624 | 1626 | $data = $response->get_data(); |
1625 | | $this->assertEquals( self::$post_id, $data['post'] ); |
| 1627 | $this->assertEquals( 'trash', $data['status'] ); |
1626 | 1628 | } |
1627 | 1629 | |
1628 | 1630 | public function test_delete_item_skip_trash() { |
… |
… |
class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase |
1639 | 1641 | $response = $this->server->dispatch( $request ); |
1640 | 1642 | $this->assertEquals( 200, $response->get_status() ); |
1641 | 1643 | $data = $response->get_data(); |
1642 | | $this->assertEquals( self::$post_id, $data['post'] ); |
| 1644 | $this->assertTrue( $data['deleted'] ); |
| 1645 | $this->assertNotEmpty( $data['previous']['post'] ); |
1643 | 1646 | } |
1644 | 1647 | |
1645 | 1648 | public function test_delete_item_already_trashed() { |
diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php
index dcebfe8..4fca5df 100644
a
|
b
|
class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te |
1860 | 1860 | wp_set_current_user( self::$editor_id ); |
1861 | 1861 | |
1862 | 1862 | $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/posts/%d', $post_id ) ); |
| 1863 | $request->set_param( 'force', 'false' ); |
1863 | 1864 | $response = $this->server->dispatch( $request ); |
1864 | 1865 | |
1865 | 1866 | $this->assertNotInstanceOf( 'WP_Error', $response ); |
1866 | 1867 | $this->assertEquals( 200, $response->get_status() ); |
1867 | 1868 | $data = $response->get_data(); |
1868 | 1869 | $this->assertEquals( 'Deleted post', $data['title']['raw'] ); |
| 1870 | $this->assertEquals( 'trash', $data['status'] ); |
1869 | 1871 | } |
1870 | 1872 | |
1871 | 1873 | public function test_delete_item_skip_trash() { |
… |
… |
class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te |
1879 | 1881 | $this->assertNotInstanceOf( 'WP_Error', $response ); |
1880 | 1882 | $this->assertEquals( 200, $response->get_status() ); |
1881 | 1883 | $data = $response->get_data(); |
1882 | | $this->assertEquals( 'Deleted post', $data['title']['raw'] ); |
| 1884 | $this->assertTrue( $data['deleted'] ); |
| 1885 | $this->assertNotEmpty( $data['previous'] ); |
1883 | 1886 | } |
1884 | 1887 | |
1885 | 1888 | public function test_delete_item_already_trashed() { |
diff --git a/tests/phpunit/tests/rest-api/rest-revisions-controller.php b/tests/phpunit/tests/rest-api/rest-revisions-controller.php
index 5d08366..3c88c6f 100644
a
|
b
|
class WP_Test_REST_Revisions_Controller extends WP_Test_REST_Controller_Testcase |
184 | 184 | public function test_delete_item() { |
185 | 185 | wp_set_current_user( self::$editor_id ); |
186 | 186 | $request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 ); |
| 187 | $request->set_param( 'force', true ); |
187 | 188 | $response = $this->server->dispatch( $request ); |
188 | 189 | $this->assertEquals( 200, $response->get_status() ); |
189 | 190 | $this->assertNull( get_post( $this->revision_id1 ) ); |
190 | 191 | } |
191 | 192 | |
| 193 | public function test_delete_item_no_trash() { |
| 194 | wp_set_current_user( self::$editor_id ); |
| 195 | |
| 196 | $request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 ); |
| 197 | $response = $this->server->dispatch( $request ); |
| 198 | $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 ); |
| 199 | |
| 200 | $request->set_param( 'force', 'false' ); |
| 201 | $response = $this->server->dispatch( $request ); |
| 202 | $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 ); |
| 203 | |
| 204 | // Ensure the revision still exists |
| 205 | $this->assertNotNull( get_post( $this->revision_id1 ) ); |
| 206 | } |
| 207 | |
192 | 208 | public function test_delete_item_no_permission() { |
193 | 209 | wp_set_current_user( self::$contributor_id ); |
194 | 210 | $request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 ); |
diff --git a/tests/phpunit/tests/rest-api/rest-tags-controller.php b/tests/phpunit/tests/rest-api/rest-tags-controller.php
index 0bdae85..e94f113 100644
a
|
b
|
class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { |
599 | 599 | $response = $this->server->dispatch( $request ); |
600 | 600 | $this->assertEquals( 200, $response->get_status() ); |
601 | 601 | $data = $response->get_data(); |
602 | | $this->assertEquals( 'Deleted Tag', $data['name'] ); |
| 602 | $this->assertTrue( $data['deleted'] ); |
| 603 | $this->assertEquals( 'Deleted Tag', $data['previous']['name'] ); |
603 | 604 | } |
604 | 605 | |
605 | | public function test_delete_item_force_false() { |
| 606 | public function test_delete_item_no_trash() { |
606 | 607 | wp_set_current_user( self::$administrator ); |
607 | 608 | $term = get_term_by( 'id', $this->factory->tag->create( array( 'name' => 'Deleted Tag' ) ), 'post_tag' ); |
| 609 | |
608 | 610 | $request = new WP_REST_Request( 'DELETE', '/wp/v2/tags/' . $term->term_id ); |
609 | | // force defaults to false |
610 | 611 | $response = $this->server->dispatch( $request ); |
611 | | $this->assertEquals( 501, $response->get_status() ); |
| 612 | $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 ); |
| 613 | |
| 614 | $request->set_param( 'force', 'false' ); |
| 615 | $response = $this->server->dispatch( $request ); |
| 616 | $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 ); |
612 | 617 | } |
613 | 618 | |
614 | 619 | public function test_delete_item_invalid_term() { |
… |
… |
class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase { |
641 | 646 | |
642 | 647 | $this->assertEquals( 200, $response->get_status() ); |
643 | 648 | $data = $response->get_data(); |
644 | | $this->assertEquals( 'Deleted Tag', $data['name'] ); |
| 649 | $this->assertTrue( $data['deleted'] ); |
| 650 | $this->assertEquals( 'Deleted Tag', $data['previous']['name'] ); |
645 | 651 | } |
646 | 652 | |
647 | 653 | public function grant_delete_term( $caps, $cap ) { |
diff --git a/tests/phpunit/tests/rest-api/rest-users-controller.php b/tests/phpunit/tests/rest-api/rest-users-controller.php
index 574e571..d526700 100644
a
|
b
|
class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { |
1113 | 1113 | |
1114 | 1114 | $this->assertEquals( 200, $response->get_status() ); |
1115 | 1115 | $data = $response->get_data(); |
1116 | | $this->assertEquals( 'Deleted User', $data['name'] ); |
1117 | | } |
1118 | | |
1119 | | public function test_delete_current_item() { |
1120 | | $user_id = $this->factory->user->create( array( 'role' => 'administrator', 'display_name' => 'Deleted User' ) ); |
1121 | | |
1122 | | wp_set_current_user( $user_id ); |
1123 | | $user = wp_get_current_user(); |
1124 | | update_site_option( 'site_admins', array( $user->user_login ) ); |
1125 | | |
1126 | | $request = new WP_REST_Request( 'DELETE', '/wp/v2/users/me' ); |
1127 | | $request['force'] = true; |
1128 | | $response = $this->server->dispatch( $request ); |
1129 | | |
1130 | | $this->assertEquals( 200, $response->get_status() ); |
1131 | | $data = $response->get_data(); |
1132 | | $this->assertEquals( 'Deleted User', $data['name'] ); |
| 1116 | $this->assertTrue( $data['deleted'] ); |
| 1117 | $this->assertEquals( 'Deleted User', $data['previous']['name'] ); |
1133 | 1118 | } |
1134 | 1119 | |
1135 | 1120 | public function test_delete_item_no_trash() { |
… |
… |
class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { |
1139 | 1124 | wp_set_current_user( self::$user ); |
1140 | 1125 | |
1141 | 1126 | $userdata = get_userdata( $user_id ); // cache for later |
| 1127 | |
1142 | 1128 | $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) ); |
1143 | 1129 | $response = $this->server->dispatch( $request ); |
1144 | 1130 | $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 ); |
1145 | 1131 | |
| 1132 | $request->set_param( 'force', 'false' ); |
| 1133 | $response = $this->server->dispatch( $request ); |
| 1134 | $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 ); |
| 1135 | |
1146 | 1136 | // Ensure the user still exists |
1147 | 1137 | $user = get_user_by( 'id', $user_id ); |
1148 | 1138 | $this->assertNotEmpty( $user ); |
1149 | 1139 | } |
1150 | 1140 | |
| 1141 | public function test_delete_current_item() { |
| 1142 | $user_id = $this->factory->user->create( array( 'role' => 'administrator', 'display_name' => 'Deleted User' ) ); |
| 1143 | |
| 1144 | wp_set_current_user( $user_id ); |
| 1145 | $user = wp_get_current_user(); |
| 1146 | update_site_option( 'site_admins', array( $user->user_login ) ); |
| 1147 | |
| 1148 | $request = new WP_REST_Request( 'DELETE', '/wp/v2/users/me' ); |
| 1149 | $request['force'] = true; |
| 1150 | $response = $this->server->dispatch( $request ); |
| 1151 | |
| 1152 | $this->assertEquals( 200, $response->get_status() ); |
| 1153 | $data = $response->get_data(); |
| 1154 | $this->assertTrue( $data['deleted'] ); |
| 1155 | $this->assertEquals( 'Deleted User', $data['previous']['name'] ); |
| 1156 | } |
| 1157 | |
1151 | 1158 | public function test_delete_current_item_no_trash() { |
1152 | | $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); |
| 1159 | $user_id = $this->factory->user->create( array( 'role' => 'administrator', 'display_name' => 'Deleted User' ) ); |
1153 | 1160 | |
1154 | 1161 | wp_set_current_user( $user_id ); |
1155 | 1162 | $user = wp_get_current_user(); |
1156 | 1163 | update_site_option( 'site_admins', array( $user->user_login ) ); |
1157 | 1164 | |
1158 | | $userdata = get_userdata( $user_id ); // cache for later |
1159 | 1165 | $request = new WP_REST_Request( 'DELETE', '/wp/v2/users/me' ); |
1160 | 1166 | $response = $this->server->dispatch( $request ); |
1161 | 1167 | $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 ); |
1162 | 1168 | |
| 1169 | $request->set_param( 'force', 'false' ); |
| 1170 | $response = $this->server->dispatch( $request ); |
| 1171 | $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 ); |
| 1172 | |
1163 | 1173 | // Ensure the user still exists |
1164 | 1174 | $user = get_user_by( 'id', $user_id ); |
1165 | 1175 | $this->assertNotEmpty( $user ); |