- Timestamp:
- 11/05/2019 08:41:12 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/rest-api/rest-users-controller.php
r46586 r46657 16 16 protected static $draft_editor; 17 17 protected static $subscriber; 18 protected static $authors = array(); 19 protected static $posts = array(); 18 19 protected static $authors = array(); 20 protected static $posts = array(); 21 protected static $user_ids = array(); 22 protected static $total_users = 30; 23 protected static $per_page = 50; 24 20 25 protected static $site; 21 26 … … 101 106 update_site_option( 'site_admins', array( 'superadmin' ) ); 102 107 } 108 109 // Set up users for pagination tests. 110 for ( $i = 0; $i < self::$total_users - 10; $i++ ) { 111 self::$user_ids[] = $factory->user->create( 112 array( 113 'role' => 'contributor', 114 'display_name' => "User {$i}", 115 ) 116 ); 117 } 103 118 } 104 119 … … 111 126 wp_delete_post( $post, true ); 112 127 } 128 113 129 foreach ( self::$authors as $author ) { 114 130 self::delete_user( $author ); 115 131 } 132 116 133 _unregister_post_type( 'r_true_p_true' ); 117 134 _unregister_post_type( 'r_true_p_false' ); … … 121 138 if ( is_multisite() ) { 122 139 wpmu_delete_blog( self::$site, true ); 140 } 141 142 // Remove users for pagination tests. 143 foreach ( self::$user_ids as $user_id ) { 144 self::delete_user( $user_id ); 123 145 } 124 146 } … … 213 235 214 236 public function test_get_items_with_edit_context_without_permission() { 215 // test with a user not logged in237 // Test with a user not logged in. 216 238 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 217 239 $request->set_param( 'context', 'edit' ); … … 220 242 $this->assertEquals( 401, $response->get_status() ); 221 243 222 //test with a user logged in but without sufficient capabilities; capability in question: 'list_users' 244 // Test with a user logged in but without sufficient capabilities; 245 // capability in question: 'list_users'. 223 246 wp_set_current_user( self::$editor ); 247 224 248 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 225 249 $request->set_param( 'context', 'edit' ); … … 281 305 282 306 public function test_get_items_pagination_headers() { 283 wp_set_current_user( self::$user ); 284 for ( $i = 0; $i < 44; $i++ ) { 285 $this->factory->user->create( 286 array( 287 'name' => "User {$i}", 288 ) 289 ); 290 } 307 $total_users = self::$total_users; 308 $total_pages = (int) ceil( $total_users / 10 ); 309 310 wp_set_current_user( self::$user ); 311 312 // Start of the index. 291 313 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 292 314 $response = rest_get_server()->dispatch( $request ); 293 315 $headers = $response->get_headers(); 294 $this->assertEquals( 54, $headers['X-WP-Total'] );295 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );316 $this->assertEquals( $total_users, $headers['X-WP-Total'] ); 317 $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); 296 318 $next_link = add_query_arg( 297 319 array( … … 302 324 $this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) ); 303 325 $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] ); 304 // 3rd page 305 $this->factory->user->create( 306 array( 307 'name' => 'User 51', 308 ) 309 ); 326 327 // 3rd page. 328 $this->factory->user->create(); 329 $total_users++; 330 $total_pages++; 310 331 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 311 332 $request->set_param( 'page', 3 ); 312 333 $response = rest_get_server()->dispatch( $request ); 313 334 $headers = $response->get_headers(); 314 $this->assertEquals( 55, $headers['X-WP-Total'] );315 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );335 $this->assertEquals( $total_users, $headers['X-WP-Total'] ); 336 $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); 316 337 $prev_link = add_query_arg( 317 338 array( … … 328 349 ); 329 350 $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] ); 330 // Last page 331 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 332 $request->set_param( 'page', 6 ); 351 352 // Last page. 353 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 354 $request->set_param( 'page', $total_pages ); 333 355 $response = rest_get_server()->dispatch( $request ); 334 356 $headers = $response->get_headers(); 335 $this->assertEquals( 55, $headers['X-WP-Total'] );336 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );357 $this->assertEquals( $total_users, $headers['X-WP-Total'] ); 358 $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); 337 359 $prev_link = add_query_arg( 338 360 array( 339 'page' => 5,361 'page' => $total_pages - 1, 340 362 ), 341 363 rest_url( 'wp/v2/users' ) … … 343 365 $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] ); 344 366 $this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) ); 345 // Out of bounds 346 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 347 $request->set_param( 'page', 8 ); 367 368 // Out of bounds. 369 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 370 $request->set_param( 'page', 100 ); 348 371 $response = rest_get_server()->dispatch( $request ); 349 372 $headers = $response->get_headers(); 350 $this->assertEquals( 55, $headers['X-WP-Total'] );351 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );373 $this->assertEquals( $total_users, $headers['X-WP-Total'] ); 374 $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] ); 352 375 $prev_link = add_query_arg( 353 376 array( 354 'page' => 6,377 'page' => $total_pages, 355 378 ), 356 379 rest_url( 'wp/v2/users' ) … … 362 385 public function test_get_items_per_page() { 363 386 wp_set_current_user( self::$user ); 364 for ( $i = 0; $i < 20; $i++ ) { 365 $this->factory->user->create( array( 'display_name' => "User {$i}" ) ); 366 } 387 367 388 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 368 389 $response = rest_get_server()->dispatch( $request ); 369 390 $this->assertEquals( 10, count( $response->get_data() ) ); 391 370 392 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 371 393 $request->set_param( 'per_page', 5 ); … … 376 398 public function test_get_items_page() { 377 399 wp_set_current_user( self::$user ); 378 for ( $i = 0; $i < 20; $i++ ) { 379 $this->factory->user->create( array( 'display_name' => "User {$i}" ) ); 380 } 400 381 401 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 382 402 $request->set_param( 'per_page', 5 ); … … 397 417 public function test_get_items_orderby_name() { 398 418 wp_set_current_user( self::$user ); 419 399 420 $low_id = $this->factory->user->create( array( 'display_name' => 'AAAAA' ) ); 400 421 $mid_id = $this->factory->user->create( array( 'display_name' => 'NNNNN' ) ); 401 422 $high_id = $this->factory->user->create( array( 'display_name' => 'ZZZZ' ) ); 423 402 424 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 403 425 $request->set_param( 'orderby', 'name' ); … … 407 429 $data = $response->get_data(); 408 430 $this->assertEquals( $high_id, $data[0]['id'] ); 431 409 432 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 410 433 $request->set_param( 'orderby', 'name' ); … … 429 452 $response = rest_get_server()->dispatch( $request ); 430 453 $data = $response->get_data(); 431 432 454 $this->assertEquals( $high_id, $data[0]['id'] ); 433 455 … … 455 477 $response = rest_get_server()->dispatch( $request ); 456 478 $data = $response->get_data(); 457 458 479 $this->assertEquals( $high_id, $data[0]['id'] ); 459 480 … … 543 564 public function test_get_items_offset() { 544 565 wp_set_current_user( self::$user ); 545 // 9 users created in wpSetUpBeforeClass(), plus default user. 546 $ this->factory->user->create();547 $request = new WP_REST_Request( 'GET', '/wp/v2/users');566 567 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 568 $request->set_param( 'per_page', self::$per_page ); 548 569 $request->set_param( 'offset', 1 ); 549 570 $response = rest_get_server()->dispatch( $request ); 550 $this->assertCount( 10, $response->get_data() ); 551 // 'offset' works with 'per_page' 571 $this->assertCount( self::$total_users - 1, $response->get_data() ); 572 573 // 'offset' works with 'per_page'. 552 574 $request->set_param( 'per_page', 2 ); 553 575 $response = rest_get_server()->dispatch( $request ); 554 576 $this->assertCount( 2, $response->get_data() ); 555 // 'offset' takes priority over 'page' 577 578 // 'offset' takes priority over 'page'. 556 579 $request->set_param( 'page', 3 ); 557 580 $response = rest_get_server()->dispatch( $request ); 558 581 $this->assertCount( 2, $response->get_data() ); 559 // 'offset' invalid value should error 582 583 // Invalid 'offset' should error. 560 584 $request->set_param( 'offset', 'moreplease' ); 561 585 $response = rest_get_server()->dispatch( $request ); … … 565 589 public function test_get_items_include_query() { 566 590 wp_set_current_user( self::$user ); 567 $id1 = $this->factory->user->create(); 568 $id2 = $this->factory->user->create(); 569 $id3 = $this->factory->user->create(); 570 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 571 // Orderby=>asc 572 $request->set_param( 'include', array( $id3, $id1 ) ); 591 592 $id1 = $this->factory->user->create(); 593 $id2 = $this->factory->user->create(); 594 595 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 596 597 // 'orderby' => 'asc'. 598 $request->set_param( 'include', array( $id2, $id1 ) ); 573 599 $response = rest_get_server()->dispatch( $request ); 574 600 $data = $response->get_data(); 575 601 $this->assertEquals( 2, count( $data ) ); 576 602 $this->assertEquals( $id1, $data[0]['id'] ); 577 // Orderby=>include 603 604 // 'orderby' => 'include'. 578 605 $request->set_param( 'orderby', 'include' ); 579 606 $response = rest_get_server()->dispatch( $request ); 580 607 $data = $response->get_data(); 581 608 $this->assertEquals( 2, count( $data ) ); 582 $this->assertEquals( $id3, $data[0]['id'] ); 583 // Invalid include should fail 609 $this->assertEquals( $id2, $data[0]['id'] ); 610 611 // Invalid 'include' should error. 584 612 $request->set_param( 'include', 'invalid' ); 585 613 $response = rest_get_server()->dispatch( $request ); 586 614 $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); 587 // No privileges 588 $request->set_param( 'include', array( $id3, $id1 ) ); 615 616 // No privileges. 617 $request->set_param( 'include', array( $id2, $id1 ) ); 589 618 wp_set_current_user( 0 ); 590 619 $response = rest_get_server()->dispatch( $request ); … … 596 625 public function test_get_items_exclude_query() { 597 626 wp_set_current_user( self::$user ); 598 $id1 = $this->factory->user->create(); 599 $id2 = $this->factory->user->create(); 600 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 601 $request->set_param( 'per_page', 20 ); // there are >10 users at this point 602 $response = rest_get_server()->dispatch( $request ); 603 $data = $response->get_data(); 604 $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) ); 605 $this->assertTrue( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) ); 627 628 $id1 = $this->factory->user->create(); 629 $id2 = $this->factory->user->create(); 630 631 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 632 $request->set_param( 'per_page', self::$per_page ); // There are >10 users at this point. 633 $response = rest_get_server()->dispatch( $request ); 634 $data = $response->get_data(); 635 $ids = wp_list_pluck( $data, 'id' ); 636 $this->assertTrue( in_array( $id1, $ids, true ) ); 637 $this->assertTrue( in_array( $id2, $ids, true ) ); 638 606 639 $request->set_param( 'exclude', array( $id2 ) ); 607 640 $response = rest_get_server()->dispatch( $request ); 608 641 $data = $response->get_data(); 609 $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) ); 610 $this->assertFalse( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) ); 611 // Invalid exlude value should error. 642 $ids = wp_list_pluck( $data, 'id' ); 643 $this->assertTrue( in_array( $id1, $ids, true ) ); 644 $this->assertFalse( in_array( $id2, $ids, true ) ); 645 646 // Invalid 'exclude' should error. 612 647 $request->set_param( 'exclude', 'none-of-those-please' ); 613 648 $response = rest_get_server()->dispatch( $request ); … … 617 652 public function test_get_items_search() { 618 653 wp_set_current_user( self::$user ); 654 619 655 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 620 656 $request->set_param( 'search', 'yololololo' ); 621 657 $response = rest_get_server()->dispatch( $request ); 622 658 $this->assertEquals( 0, count( $response->get_data() ) ); 659 623 660 $yolo_id = $this->factory->user->create( array( 'display_name' => 'yololololo' ) ); 661 624 662 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 625 663 $request->set_param( 'search', 'yololololo' ); … … 633 671 ) 634 672 ); 673 635 674 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 636 675 $request->set_param( 'search', 'ada' ); … … 643 682 public function test_get_items_slug_query() { 644 683 wp_set_current_user( self::$user ); 684 645 685 $this->factory->user->create( 646 686 array( … … 649 689 ) 650 690 ); 651 $id2 691 $id2 = $this->factory->user->create( 652 692 array( 653 693 'display_name' => 'Moo', … … 655 695 ) 656 696 ); 697 657 698 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 658 699 $request->set_param( 'slug', 'foo' ); … … 665 706 public function test_get_items_slug_array_query() { 666 707 wp_set_current_user( self::$user ); 708 667 709 $id1 = $this->factory->user->create( 668 710 array( … … 689 731 ) 690 732 ); 733 691 734 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 692 735 $request->set_param( … … 709 752 public function test_get_items_slug_csv_query() { 710 753 wp_set_current_user( self::$user ); 754 711 755 $id1 = $this->factory->user->create( 712 756 array( … … 733 777 ) 734 778 ); 779 735 780 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 736 781 $request->set_param( 'slug', 'taco,burrito , enchilada' ); … … 747 792 public function test_get_items_roles() { 748 793 wp_set_current_user( self::$user ); 749 $tango = $this->factory->user->create( 794 795 $tango = $this->factory->user->create( 750 796 array( 751 797 'display_name' => 'tango', … … 753 799 ) 754 800 ); 755 $yolo 801 $yolo = $this->factory->user->create( 756 802 array( 757 803 'display_name' => 'yolo', … … 759 805 ) 760 806 ); 807 761 808 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 762 809 $request->set_param( 'roles', 'author,subscriber' ); … … 766 813 $this->assertEquals( $tango, $data[1]['id'] ); 767 814 $this->assertEquals( $yolo, $data[2]['id'] ); 815 768 816 $request->set_param( 'roles', 'author' ); 769 817 $response = rest_get_server()->dispatch( $request ); … … 771 819 $this->assertEquals( 1, count( $data ) ); 772 820 $this->assertEquals( $yolo, $data[0]['id'] ); 821 773 822 wp_set_current_user( 0 ); 823 774 824 $request->set_param( 'roles', 'author' ); 775 825 $response = rest_get_server()->dispatch( $request ); 776 826 $this->assertErrorResponse( 'rest_user_cannot_view', $response, 401 ); 827 777 828 wp_set_current_user( self::$editor ); 829 778 830 $request->set_param( 'roles', 'author' ); 779 831 $response = rest_get_server()->dispatch( $request ); … … 783 835 public function test_get_items_invalid_roles() { 784 836 wp_set_current_user( self::$user ); 785 $lolz = $this->factory->user->create( 837 838 $lolz = $this->factory->user->create( 786 839 array( 787 840 'display_name' => 'lolz', … … 789 842 ) 790 843 ); 844 791 845 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 792 846 $request->set_param( 'roles', 'ilovesteak,author' ); … … 795 849 $this->assertEquals( 1, count( $data ) ); 796 850 $this->assertEquals( $lolz, $data[0]['id'] ); 851 797 852 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 798 853 $request->set_param( 'roles', 'steakisgood' ); … … 805 860 public function test_get_items_who_author_query() { 806 861 wp_set_current_user( self::$superadmin ); 862 807 863 // First request should include subscriber in the set. 808 864 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); … … 811 867 $this->assertEquals( 200, $response->get_status() ); 812 868 $this->assertCount( 1, $response->get_data() ); 869 813 870 // Second request should exclude subscriber. 814 871 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); … … 822 879 public function test_get_items_who_invalid_query() { 823 880 wp_set_current_user( self::$user ); 881 824 882 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 825 883 $request->set_param( 'who', 'editor' ); … … 834 892 public function test_get_items_who_unauthorized_query() { 835 893 wp_set_current_user( self::$subscriber ); 894 836 895 $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); 837 896 $request->set_param( 'who', 'authors' ); … … 842 901 public function test_get_item() { 843 902 $user_id = $this->factory->user->create(); 844 wp_set_current_user( self::$user ); 845 846 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', $user_id ) ); 847 903 904 wp_set_current_user( self::$user ); 905 906 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', $user_id ) ); 848 907 $response = rest_get_server()->dispatch( $request ); 849 908 $this->check_get_user_response( $response, 'embed' ); … … 852 911 public function test_prepare_item() { 853 912 wp_set_current_user( self::$user ); 913 854 914 $request = new WP_REST_Request; 855 915 $request->set_param( 'context', 'edit' ); … … 861 921 public function test_prepare_item_limit_fields() { 862 922 wp_set_current_user( self::$user ); 923 863 924 $request = new WP_REST_Request; 864 925 $request->set_param( 'context', 'edit' ); … … 878 939 wp_set_current_user( self::$user ); 879 940 880 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', self::$editor ) ); 881 941 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', self::$editor ) ); 882 942 $response = rest_get_server()->dispatch( $request ); 883 943 … … 906 966 public function test_get_user_empty_capabilities() { 907 967 wp_set_current_user( self::$user ); 968 908 969 $this->allow_user_to_manage_multisite(); 909 970 … … 914 975 ) 915 976 ); 977 916 978 delete_user_option( $lolz, 'capabilities' ); 917 979 delete_user_option( $lolz, 'user_level' ); 980 918 981 $request = new WP_REST_Request( 'GET', '/wp/v2/users/' . $lolz ); 919 982 $request->set_param( 'context', 'edit' ); … … 932 995 public function test_cannot_get_item_without_permission() { 933 996 wp_set_current_user( self::$editor ); 997 934 998 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', self::$user ) ); 935 999 $response = rest_get_server()->dispatch( $request ); … … 945 1009 public function test_can_get_item_author_of_rest_true_public_true_authenticated() { 946 1010 wp_set_current_user( self::$editor ); 1011 947 1012 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', self::$authors['r_true_p_true'] ) ); 948 1013 $response = rest_get_server()->dispatch( $request ); … … 964 1029 public function test_cannot_get_item_author_of_rest_false_public_true_without_permission() { 965 1030 wp_set_current_user( self::$editor ); 1031 966 1032 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', self::$authors['r_false_p_true'] ) ); 967 1033 $response = rest_get_server()->dispatch( $request ); … … 993 1059 ) 994 1060 ); 995 $this->post_id = $this->factory->post->create( 1061 1062 $this->post_id = $this->factory->post->create( 996 1063 array( 997 1064 'post_author' => $this->author_id, 998 1065 ) 999 1066 ); 1067 1000 1068 wp_set_current_user( 0 ); 1069 1001 1070 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', $this->author_id ) ); 1002 1071 $response = rest_get_server()->dispatch( $request ); … … 1010 1079 ) 1011 1080 ); 1081 1012 1082 wp_set_current_user( 0 ); 1083 1013 1084 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', $this->author_id ) ); 1014 1085 $response = rest_get_server()->dispatch( $request ); 1015 1086 $this->assertEquals( 401, $response->get_status() ); 1087 1016 1088 $this->post_id = $this->factory->post->create( 1017 1089 array( … … 1020 1092 ) 1021 1093 ); 1022 $response = rest_get_server()->dispatch( $request ); 1094 1095 $response = rest_get_server()->dispatch( $request ); 1023 1096 $this->check_get_user_response( $response, 'embed' ); 1024 1097 } … … 1026 1099 public function test_get_user_with_edit_context() { 1027 1100 $user_id = $this->factory->user->create(); 1101 1028 1102 $this->allow_user_to_manage_multisite(); 1029 1103 1030 1104 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', $user_id ) ); 1031 1105 $request->set_param( 'context', 'edit' ); 1032 1033 1106 $response = rest_get_server()->dispatch( $request ); 1034 1107 $this->check_get_user_response( $response, 'edit' ); … … 1041 1114 ) 1042 1115 ); 1043 $this->post_id = $this->factory->post->create( 1116 1117 $this->post_id = $this->factory->post->create( 1044 1118 array( 1045 1119 'post_author' => $this->author_id, 1046 1120 ) 1047 1121 ); 1122 1048 1123 wp_set_current_user( 0 ); 1124 1049 1125 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', $this->author_id ) ); 1050 1126 $request->set_param( 'context', 'edit' ); … … 1056 1132 wp_set_current_user( self::$user ); 1057 1133 1058 $request = new WP_REST_Request( 'GET', '/wp/v2/users/me' ); 1059 1134 $request = new WP_REST_Request( 'GET', '/wp/v2/users/me' ); 1060 1135 $response = rest_get_server()->dispatch( $request ); 1061 1136 $this->assertEquals( 200, $response->get_status() ); … … 1071 1146 public function test_get_current_user_without_permission() { 1072 1147 wp_set_current_user( 0 ); 1148 1073 1149 $request = new WP_REST_Request( 'GET', '/wp/v2/users/me' ); 1074 1150 $response = rest_get_server()->dispatch( $request ); 1075 1076 1151 $this->assertErrorResponse( 'rest_not_logged_in', $response, 401 ); 1077 1152 } … … 1079 1154 public function test_create_item() { 1080 1155 $this->allow_user_to_manage_multisite(); 1156 1081 1157 wp_set_current_user( self::$user ); 1082 1158 … … 1096 1172 $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); 1097 1173 $request->set_body_params( $params ); 1098 1099 $response = rest_get_server()->dispatch( $request ); 1100 $data 1174 $response = rest_get_server()->dispatch( $request ); 1175 1176 $data = $response->get_data(); 1101 1177 $this->assertEquals( 'http://example.com', $data['url'] ); 1102 1178 $this->assertEquals( array( 'editor' ), $data['roles'] ); … … 1106 1182 public function test_create_item_invalid_username() { 1107 1183 $this->allow_user_to_manage_multisite(); 1184 1108 1185 wp_set_current_user( self::$user ); 1109 1186 … … 1128 1205 $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); 1129 1206 $request->set_body_params( $params ); 1130 1131 1207 $response = rest_get_server()->dispatch( $request ); 1132 1208 $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); 1133 1209 1134 1210 $data = $response->get_data(); 1211 1135 1212 if ( is_multisite() ) { 1136 1213 $this->assertInternalType( 'array', $data['additional_errors'] ); … … 1153 1230 public function test_create_item_illegal_username() { 1154 1231 $this->allow_user_to_manage_multisite(); 1232 1155 1233 wp_set_current_user( self::$user ); 1156 1234 … … 1172 1250 $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); 1173 1251 $request->set_body_params( $params ); 1174 1175 1252 $response = rest_get_server()->dispatch( $request ); 1176 1253 … … 1320 1397 public function test_json_create_user() { 1321 1398 $this->allow_user_to_manage_multisite(); 1399 1322 1400 wp_set_current_user( self::$user ); 1323 1401 … … 1331 1409 $request->add_header( 'content-type', 'application/json' ); 1332 1410 $request->set_body( wp_json_encode( $params ) ); 1333 1334 $response = rest_get_server()->dispatch( $request ); 1411 $response = rest_get_server()->dispatch( $request ); 1412 1335 1413 $this->check_add_edit_user_response( $response ); 1336 1414 } … … 1355 1433 public function test_create_user_invalid_id() { 1356 1434 $this->allow_user_to_manage_multisite(); 1435 1357 1436 wp_set_current_user( self::$user ); 1358 1437 … … 1374 1453 public function test_create_user_invalid_email() { 1375 1454 $this->allow_user_to_manage_multisite(); 1455 1376 1456 wp_set_current_user( self::$user ); 1377 1457 … … 1392 1472 public function test_create_user_invalid_role() { 1393 1473 $this->allow_user_to_manage_multisite(); 1474 1394 1475 wp_set_current_user( self::$user ); 1395 1476 … … 1420 1501 ) 1421 1502 ); 1422 $this->allow_user_to_manage_multisite(); 1503 1504 $this->allow_user_to_manage_multisite(); 1505 1423 1506 wp_set_current_user( self::$user ); 1424 1507 … … 1435 1518 $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); 1436 1519 $request->set_body_params( $_POST ); 1437 1438 $response = rest_get_server()->dispatch( $request ); 1520 $response = rest_get_server()->dispatch( $request ); 1521 1439 1522 $this->check_add_edit_user_response( $response, true ); 1440 1523 … … 1456 1539 public function test_update_item_no_change() { 1457 1540 $this->allow_user_to_manage_multisite(); 1458 wp_set_current_user( self::$user ); 1541 1542 wp_set_current_user( self::$user ); 1543 1459 1544 $user = get_userdata( self::$editor ); 1460 1545 … … 1484 1569 ) 1485 1570 ); 1486 $this->allow_user_to_manage_multisite(); 1571 1572 $this->allow_user_to_manage_multisite(); 1573 1487 1574 wp_set_current_user( self::$user ); 1488 1575 … … 1540 1627 ) 1541 1628 ); 1542 $this->allow_user_to_manage_multisite(); 1629 1630 $this->allow_user_to_manage_multisite(); 1631 1543 1632 wp_set_current_user( self::$user ); 1544 1633 … … 1557 1646 ) 1558 1647 ); 1559 $this->allow_user_to_manage_multisite(); 1648 1649 $this->allow_user_to_manage_multisite(); 1650 1560 1651 wp_set_current_user( self::$user ); 1561 1652 … … 1580 1671 ) 1581 1672 ); 1582 $this->allow_user_to_manage_multisite(); 1673 1674 $this->allow_user_to_manage_multisite(); 1675 1583 1676 wp_set_current_user( self::$user ); 1584 1677 … … 1607 1700 ) 1608 1701 ); 1609 $this->allow_user_to_manage_multisite(); 1702 1703 $this->allow_user_to_manage_multisite(); 1704 1610 1705 wp_set_current_user( self::$user ); 1611 1706 … … 1630 1725 ) 1631 1726 ); 1632 $this->allow_user_to_manage_multisite(); 1727 1728 $this->allow_user_to_manage_multisite(); 1729 1633 1730 wp_set_current_user( self::$user ); 1634 1731 … … 1650 1747 ) 1651 1748 ); 1652 $this->allow_user_to_manage_multisite(); 1749 1750 $this->allow_user_to_manage_multisite(); 1751 1653 1752 wp_set_current_user( self::$user ); 1654 1753 … … 1687 1786 1688 1787 wp_set_current_user( self::$user ); 1788 1689 1789 $this->allow_user_to_manage_multisite(); 1690 1790 … … 1707 1807 1708 1808 wp_set_current_user( self::$user ); 1809 1709 1810 $this->allow_user_to_manage_multisite(); 1710 1811 … … 1810 1911 public function test_update_user_role_invalid_role() { 1811 1912 wp_set_current_user( self::$user ); 1913 1812 1914 $this->allow_user_to_manage_multisite(); 1813 1915 … … 1859 1961 public function test_update_user_invalid_id() { 1860 1962 $this->allow_user_to_manage_multisite(); 1963 1861 1964 wp_set_current_user( self::$user ); 1862 1965 1863 1966 $params = array( 1864 'id' => ' 156',1967 'id' => '0', 1865 1968 'username' => 'lisasimpson', 1866 1969 'password' => 'DavidHasselhoff', … … 1887 1990 1888 1991 wp_set_current_user( self::$editor ); 1992 1889 1993 $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', $user_id ) ); 1890 1994 $request->set_param( 'roles', array( 'editor' ) ); … … 1904 2008 1905 2009 wp_set_current_user( self::$user ); 2010 1906 2011 $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', $user_id ) ); 1907 2012 $request->set_param( 'roles', array( 'editor' ) ); … … 1924 2029 1925 2030 wp_set_current_user( self::$user ); 2031 1926 2032 $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', $user_id ) ); 1927 2033 $request->set_param( 'roles', array( 'editor' ) ); … … 1946 2052 public function test_update_item_invalid_password() { 1947 2053 $this->allow_user_to_manage_multisite(); 2054 1948 2055 wp_set_current_user( self::$user ); 1949 2056 1950 2057 $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', self::$editor ) ); 1951 1952 2058 $request->set_param( 'password', 'no\\backslashes\\allowed' ); 1953 2059 $response = rest_get_server()->dispatch( $request ); … … 2035 2141 public function test_user_roundtrip_as_editor() { 2036 2142 wp_set_current_user( self::$editor ); 2143 2037 2144 $this->assertEquals( ! is_multisite(), current_user_can( 'unfiltered_html' ) ); 2038 2145 $this->verify_user_roundtrip( … … 2061 2168 public function test_user_roundtrip_as_editor_html() { 2062 2169 wp_set_current_user( self::$editor ); 2170 2063 2171 if ( is_multisite() ) { 2064 2172 $this->assertFalse( current_user_can( 'unfiltered_html' ) ); … … 2112 2220 public function test_user_roundtrip_as_superadmin() { 2113 2221 wp_set_current_user( self::$superadmin ); 2222 2114 2223 $this->assertTrue( current_user_can( 'unfiltered_html' ) ); 2115 2224 $valid_username = is_multisite() ? 'noinvalidcharshere' : 'no-invalid-chars-here'; … … 2140 2249 public function test_user_roundtrip_as_superadmin_html() { 2141 2250 wp_set_current_user( self::$superadmin ); 2251 2142 2252 $this->assertTrue( current_user_can( 'unfiltered_html' ) ); 2143 2253 $valid_username = is_multisite() ? 'noinvalidcharshere' : 'no-invalid-chars-here'; … … 2170 2280 2171 2281 $this->allow_user_to_manage_multisite(); 2172 wp_set_current_user( self::$user ); 2173 2174 $userdata = get_userdata( $user_id ); // cache for later 2282 2283 wp_set_current_user( self::$user ); 2284 2285 $userdata = get_userdata( $user_id ); // Cache for later. 2175 2286 $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) ); 2176 2287 $request->set_param( 'force', true ); … … 2194 2305 2195 2306 $this->allow_user_to_manage_multisite(); 2196 wp_set_current_user( self::$user ); 2197 2198 $userdata = get_userdata( $user_id ); // cache for later 2307 2308 wp_set_current_user( self::$user ); 2309 2310 $userdata = get_userdata( $user_id ); // Cache for later. 2199 2311 2200 2312 $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) ); … … 2214 2326 $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 ); 2215 2327 2216 // Ensure the user still exists 2328 // Ensure the user still exists. 2217 2329 $user = get_user_by( 'id', $user_id ); 2218 2330 $this->assertNotEmpty( $user ); … … 2285 2397 2286 2398 $this->allow_user_to_manage_multisite(); 2399 2287 2400 wp_set_current_user( self::$editor ); 2288 2401 … … 2304 2417 public function test_delete_user_invalid_id() { 2305 2418 $this->allow_user_to_manage_multisite(); 2419 2306 2420 wp_set_current_user( self::$user ); 2307 2421 … … 2317 2431 $this->allow_user_to_manage_multisite(); 2318 2432 2319 // Test with a new user, to avoid any complications 2433 // Test with a new user, to avoid any complications. 2320 2434 $user_id = $this->factory->user->create(); 2321 2435 $reassign_id = $this->factory->user->create(); … … 2326 2440 ); 2327 2441 2328 // Sanity check to ensure the factory created the post correctly 2442 // Sanity check to ensure the factory created the post correctly. 2329 2443 $post = get_post( $test_post ); 2330 2444 $this->assertEquals( $user_id, $post->post_author ); 2331 2445 2332 // Delete our test user, and reassign to the new author 2333 wp_set_current_user( self::$user ); 2446 wp_set_current_user( self::$user ); 2447 2448 // Delete our test user, and reassign to the new author. 2334 2449 $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) ); 2335 2450 $request['force'] = true; … … 2354 2469 2355 2470 $this->allow_user_to_manage_multisite(); 2471 2356 2472 wp_set_current_user( self::$user ); 2357 2473 … … 2374 2490 2375 2491 $this->allow_user_to_manage_multisite(); 2492 2376 2493 wp_set_current_user( self::$user ); 2377 2494 … … 2388 2505 2389 2506 $this->allow_user_to_manage_multisite(); 2507 2390 2508 wp_set_current_user( self::$user ); 2391 2509 … … 2415 2533 2416 2534 $this->allow_user_to_manage_multisite(); 2535 2417 2536 wp_set_current_user( self::$user ); 2418 2537 … … 2442 2561 2443 2562 $this->allow_user_to_manage_multisite(); 2563 2444 2564 wp_set_current_user( self::$user ); 2445 2565 … … 2469 2589 2470 2590 $this->allow_user_to_manage_multisite(); 2591 2471 2592 wp_set_current_user( self::$user ); 2472 2593 … … 2557 2678 ); 2558 2679 2559 $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/users' ); 2560 2680 $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/users' ); 2561 2681 $response = rest_get_server()->dispatch( $request ); 2562 2682 $data = $response->get_data(); … … 2566 2686 2567 2687 wp_set_current_user( 1 ); 2688 2568 2689 if ( is_multisite() ) { 2569 2690 $current_user = wp_get_current_user( 1 ); … … 2571 2692 } 2572 2693 2573 $request = new WP_REST_Request( 'GET', '/wp/v2/users/1' ); 2574 2694 $request = new WP_REST_Request( 'GET', '/wp/v2/users/1' ); 2575 2695 $response = rest_get_server()->dispatch( $request ); 2576 2696 $this->assertArrayHasKey( 'my_custom_int', $response->data ); … … 2582 2702 ) 2583 2703 ); 2584 2585 2704 $response = rest_get_server()->dispatch( $request ); 2586 2705 $this->assertEquals( 123, get_user_meta( 1, 'my_custom_int', true ) ); … … 2595 2714 ) 2596 2715 ); 2597 2598 $response = rest_get_server()->dispatch( $request ); 2599 2716 $response = rest_get_server()->dispatch( $request ); 2600 2717 $this->assertEquals( 123, $response->data['my_custom_int'] ); 2601 2718 … … 2623 2740 2624 2741 wp_set_current_user( 1 ); 2742 2625 2743 if ( is_multisite() ) { 2626 2744 $current_user = wp_get_current_user( 1 ); … … 2635 2753 ) 2636 2754 ); 2637 2638 2755 $response = rest_get_server()->dispatch( $request ); 2639 2756 … … 2658 2775 2659 2776 wp_set_current_user( self::$user ); 2660 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', $user_id ) ); 2661 2777 2778 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', $user_id ) ); 2662 2779 $response = rest_get_server()->dispatch( $request ); 2663 2780 $this->assertErrorResponse( 'rest_user_invalid_id', $response, 404 ); … … 2678 2795 2679 2796 wp_set_current_user( self::$superadmin ); 2680 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', $user_id ) ); 2681 2797 2798 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', $user_id ) ); 2682 2799 $response = rest_get_server()->dispatch( $request ); 2683 2800 $this->assertErrorResponse( 'rest_user_invalid_id', $response, 404 ); … … 2698 2815 2699 2816 wp_set_current_user( self::$user ); 2817 2700 2818 $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', $user_id ) ); 2701 2819 $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); 2702 2820 $request->set_body_params( array( 'first_name' => 'New Name' ) ); 2703 2704 2821 $response = rest_get_server()->dispatch( $request ); 2705 2822 $this->assertErrorResponse( 'rest_user_invalid_id', $response, 404 ); … … 2720 2837 2721 2838 wp_set_current_user( self::$superadmin ); 2839 2722 2840 $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', $user_id ) ); 2723 2841 $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); 2724 2842 $request->set_body_params( array( 'first_name' => 'New Name' ) ); 2725 2726 2843 $response = rest_get_server()->dispatch( $request ); 2727 2844 $this->assertErrorResponse( 'rest_user_invalid_id', $response, 404 ); … … 2742 2859 2743 2860 wp_set_current_user( self::$user ); 2861 2744 2862 $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) ); 2745 2863 $request->set_param( 'force', true ); 2746 2864 $request->set_param( 'reassign', false ); 2747 2748 2865 $response = rest_get_server()->dispatch( $request ); 2749 2866 $this->assertErrorResponse( 'rest_user_invalid_id', $response, 404 ); … … 2764 2881 2765 2882 wp_set_current_user( self::$superadmin ); 2883 2766 2884 $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) ); 2767 2885 $request->set_param( 'force', true ); 2768 2886 $request->set_param( 'reassign', false ); 2769 2770 2887 $response = rest_get_server()->dispatch( $request ); 2771 2888 $this->assertErrorResponse( 'rest_user_invalid_id', $response, 404 );
Note: See TracChangeset
for help on using the changeset viewer.