Ticket #38855: 38855.2.diff
File 38855.2.diff, 8.9 KB (added by , 9 years ago) |
---|
-
src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php
366 366 * @return WP_Error|bool True if the request has access to create items, error object otherwise. 367 367 */ 368 368 public function create_item_permissions_check( $request ) { 369 370 369 if ( ! is_user_logged_in() && get_option( 'comment_registration' ) ) { 371 370 return new WP_Error( 'rest_comment_login_required', __( 'Sorry, you must be logged in to comment.' ), array( 'status' => 401 ) ); 372 371 } 373 372 373 /** 374 * Filters whether comments can be created without authentication. 375 * 376 * Enables creating comments for anonymous users. 377 * 378 * @since 4.7.0 379 * 380 * @param bool $allow_anonymous Whether to allow anonymous 381 * comments to be created. 382 * Default `false`. 383 * @param WP_REST_Request $request Request used to generate the 384 * response. 385 */ 386 $allow_anonymous = apply_filters( 'rest_allow_anonymous_comments', false, $request ); 387 if ( ! is_user_logged_in() && false === $allow_anonymous ) { 388 return new WP_Error( 'rest_comment_login_required', __( 'Sorry, you must be logged in to comment.' ), array( 'status' => 401 ) ); 389 } 390 374 391 // Limit who can set comment `author`, `author_ip` or `status` to anything other than the default. 375 392 if ( isset( $request['author'] ) && get_current_user_id() !== $request['author'] && ! current_user_can( 'moderate_comments' ) ) { 376 393 return new WP_Error( 'rest_comment_invalid_author', -
tests/phpunit/tests/rest-api/rest-comments-controller.php
800 800 } 801 801 802 802 public function test_get_comment_not_approved_same_user() { 803 wp_set_current_user( self::$ subscriber_id );803 wp_set_current_user( self::$admin_id ); 804 804 805 805 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$hold_id ) ); 806 806 … … 842 842 } 843 843 844 844 public function test_get_comment_with_password_without_edit_post_permission() { 845 wp_set_current_user( 0);845 wp_set_current_user( self::$subscriber_id ); 846 846 $args = array( 847 847 'comment_approved' => 1, 848 848 'comment_post_ID' => self::$password_id, … … 850 850 $password_comment = $this->factory->comment->create( $args ); 851 851 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', $password_comment ) ); 852 852 $response = $this->server->dispatch( $request ); 853 $this->assertErrorResponse( 'rest_cannot_read', $response, 40 1);853 $this->assertErrorResponse( 'rest_cannot_read', $response, 403 ); 854 854 } 855 855 856 856 public function test_create_item() { 857 wp_set_current_user( 0);857 wp_set_current_user( self::$admin_id ); 858 858 859 859 $params = array( 860 860 'post' => self::$post_id, … … 873 873 $this->assertEquals( 201, $response->get_status() ); 874 874 875 875 $data = $response->get_data(); 876 $this->check_comment_data( $data, ' view', $response->get_links() );876 $this->check_comment_data( $data, 'edit', $response->get_links() ); 877 877 $this->assertEquals( 'hold', $data['status'] ); 878 878 $this->assertEquals( '2014-11-07T10:14:25', $data['date'] ); 879 879 $this->assertEquals( self::$post_id, $data['post'] ); … … 880 880 } 881 881 882 882 public function test_create_item_using_accepted_content_raw_value() { 883 wp_set_current_user( 0);883 wp_set_current_user( self::$admin_id ); 884 884 885 885 $params = array( 886 886 'post' => self::$post_id, … … 905 905 } 906 906 907 907 public function test_create_comment_missing_required_author_name_and_email_per_option_value() { 908 add_filter( 'rest_allow_anonymous_comments', '__return_true' ); 908 909 update_option( 'require_name_email', 1 ); 909 910 910 911 $params = array( … … 917 918 $request->set_body( wp_json_encode( $params ) ); 918 919 919 920 $response = $this->server->dispatch( $request ); 921 920 922 $this->assertErrorResponse( 'rest_comment_author_data_required', $response, 400 ); 921 923 922 924 update_option( 'require_name_email', 0 ); … … 923 925 } 924 926 925 927 public function test_create_comment_missing_required_author_name_per_option_value() { 928 wp_set_current_user( self::$admin_id ); 926 929 update_option( 'require_name_email', 1 ); 927 930 928 931 $params = array( … … 942 945 } 943 946 944 947 public function test_create_comment_missing_required_author_email_per_option_value() { 948 wp_set_current_user( self::$admin_id ); 945 949 update_option( 'require_name_email', 1 ); 946 950 947 951 $params = array( … … 961 965 } 962 966 963 967 public function test_create_comment_author_email_too_short() { 964 wp_set_current_user( 0);968 wp_set_current_user( self::$admin_id ); 965 969 966 970 $params = array( 967 971 'post' => self::$post_id, … … 982 986 } 983 987 984 988 public function test_create_item_invalid_no_content() { 985 wp_set_current_user( 0);989 wp_set_current_user( self::$admin_id ); 986 990 987 991 $params = array( 988 992 'post' => self::$post_id, … … 1005 1009 } 1006 1010 1007 1011 public function test_create_item_invalid_date() { 1008 wp_set_current_user( 0);1012 wp_set_current_user( self::$admin_id ); 1009 1013 1010 1014 $params = array( 1011 1015 'post' => self::$post_id, … … 1349 1353 } 1350 1354 1351 1355 public function test_create_comment_author_ip_no_permission() { 1356 wp_set_current_user( self::$subscriber_id ); 1352 1357 $params = array( 1353 1358 'author_name' => 'Comic Book Guy', 1354 1359 'author_email' => 'cbg@androidsdungeon.com', … … 1361 1366 $request->add_header( 'content-type', 'application/json' ); 1362 1367 $request->set_body( wp_json_encode( $params ) ); 1363 1368 $response = $this->server->dispatch( $request ); 1364 $this->assertErrorResponse( 'rest_comment_invalid_author_ip', $response, 40 1);1369 $this->assertErrorResponse( 'rest_comment_invalid_author_ip', $response, 403 ); 1365 1370 } 1366 1371 1367 1372 public function test_create_comment_author_ip_defaults_to_remote_addr() { 1373 wp_set_current_user( self::$admin_id ); 1368 1374 $_SERVER['REMOTE_ADDR'] = '127.0.0.2'; 1369 1375 $params = array( 1370 1376 'post' => self::$post_id, … … 1500 1506 } 1501 1507 1502 1508 public function test_create_item_duplicate() { 1509 wp_set_current_user( self::$subscriber_id ); 1503 1510 $this->factory->comment->create( 1504 1511 array( 1505 1512 'comment_post_ID' => self::$post_id, … … 1508 1515 'comment_content' => 'Homer? Who is Homer? My name is Guy N. Cognito.', 1509 1516 ) 1510 1517 ); 1511 wp_set_current_user( 0 );1512 1518 1513 1519 $params = array( 1514 1520 'post' => self::$post_id, … … 1529 1535 $post_id = $this->factory->post->create( array( 1530 1536 'comment_status' => 'closed', 1531 1537 )); 1532 wp_set_current_user( 0);1538 wp_set_current_user( self::$subscriber_id ); 1533 1539 1534 1540 $params = array( 1535 1541 'post' => $post_id, … … 1546 1552 public function test_create_comment_require_login() { 1547 1553 wp_set_current_user( 0 ); 1548 1554 update_option( 'comment_registration', 1 ); 1555 add_filter( 'rest_allow_anonymous_comments', '__return_true' ); 1549 1556 $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); 1550 1557 $request->set_param( 'post', self::$post_id ); 1551 1558 $response = $this->server->dispatch( $request ); … … 1595 1602 } 1596 1603 1597 1604 public function test_create_comment_two_times() { 1598 wp_set_current_user( 0);1605 add_filter( 'rest_allow_anonymous_comments', '__return_true' ); 1599 1606 1600 1607 $params = array( 1601 1608 'post' => self::$post_id, … … 1632 1639 * @ticket 38477 1633 1640 */ 1634 1641 public function test_create_comment_author_name_too_long() { 1635 wp_set_current_user( 0);1642 wp_set_current_user( self::$subscriber_id ); 1636 1643 1637 1644 $params = array( 1638 1645 'post' => self::$post_id, … … 1655 1662 * @ticket 38477 1656 1663 */ 1657 1664 public function test_create_comment_author_email_too_long() { 1658 wp_set_current_user( 0);1665 wp_set_current_user( self::$subscriber_id ); 1659 1666 1660 1667 $params = array( 1661 1668 'post' => self::$post_id, … … 1678 1685 * @ticket 38477 1679 1686 */ 1680 1687 public function test_create_comment_author_url_too_long() { 1681 wp_set_current_user( 0);1688 wp_set_current_user( self::$subscriber_id ); 1682 1689 1683 1690 $params = array( 1684 1691 'post' => self::$post_id, … … 1701 1708 * @ticket 38477 1702 1709 */ 1703 1710 public function test_create_comment_content_too_long() { 1704 wp_set_current_user( 0);1711 wp_set_current_user( self::$subscriber_id ); 1705 1712 1706 1713 $params = array( 1707 1714 'post' => self::$post_id, … … 1913 1920 } 1914 1921 1915 1922 public function test_update_comment_invalid_id() { 1916 wp_set_current_user( 0);1923 wp_set_current_user( self::$subscriber_id ); 1917 1924 1918 1925 $params = array( 1919 1926 'content' => 'Oh, they have the internet on computers now!', … … 1927 1934 } 1928 1935 1929 1936 public function test_update_comment_invalid_permission() { 1930 wp_set_current_user( 0);1937 add_filter( 'rest_allow_anonymous_comments', '__return_true' ); 1931 1938 1932 1939 $params = array( 1933 1940 'content' => 'Disco Stu likes disco music.',