/
githubmirror
/
gutenberg
Обзор
Документация
Войти
/
githubmirror
/
gutenberg
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
trunk
phpunit/experimental/class-wp-rest-comments-controller-gutenberg-test.php
458 строк
15 KB
Shail Mehta
Docs: Use `gutenberg` instead of `Gutenberg` in package name (#81051)
02 авг 2026, 09:05
Не верифицирован
02 авг 2026, 09:05
9594294
Код
Авторство
О чём код?
<?php /** * Unit tests covering WP_Test_REST_Comments_Controller_Gutenberg functionality. * * @package gutenberg */ class WP_Test_REST_Comments_Controller_Gutenberg extends WP_Test_REST_TestCase { protected static $admin_id; protected static $editor_id; protected static $contributor_id; protected static $subscriber_id; protected static $author_id; protected static $user_ids = array(); protected static $post_id; protected static $num_notes = 10; public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { self::$admin_id = $factory->user->create( array( 'role' => 'administrator', ) ); self::$editor_id = $factory->user->create( array( 'role' => 'editor', ) ); self::$contributor_id = $factory->user->create( array( 'role' => 'contributor', ) ); self::$subscriber_id = $factory->user->create( array( 'role' => 'subscriber', ) ); self::$author_id = $factory->user->create( array( 'role' => 'author', ) ); self::$user_ids = array( 'administrator' => self::$admin_id, 'editor' => self::$editor_id, 'contributor' => self::$contributor_id, 'subscriber' => self::$subscriber_id, 'author' => self::$author_id, ); } public static function wpTearDownAfterClass() { self::delete_user( self::$admin_id ); self::delete_user( self::$editor_id ); self::delete_user( self::$contributor_id ); self::delete_user( self::$subscriber_id ); self::delete_user( self::$author_id ); } /** * Create a test post with note. * * @param int $user_id Post author's user ID. * @return int Post ID. */ protected function create_test_post_with_note( $role ) { $user_id = self::$user_ids[ $role ]; $post_id = self::factory()->post->create( array( 'post_title' => 'Test Post for Notes', 'post_content' => 'This is a test post to check note permissions.', 'post_status' => 'contributor' === $role ? 'draft' : 'publish', 'post_author' => $user_id, ) ); for ( $i = 0; $i < self::$num_notes; $i++ ) { self::factory()->comment->create( array( 'comment_post_ID' => $post_id, 'comment_type' => 'note', 'comment_approved' => 0 === $i % 2 ? 1 : 0, ) ); } return $post_id; } public function test_cannot_read_note_without_post_type_support() { register_post_type( 'no-notes', array( 'label' => 'No Notes', 'supports' => array( 'title', 'editor', 'author', 'comments' ), 'show_in_rest' => true, 'public' => true, ) ); // Core calls this method but it fails for gutenberg tests. // See: https://github.com/WordPress/wordpress-develop/blob/8c2ec298ad82d32f6bd66fae5ec567d287bd6bbf/tests/phpunit/tests/rest-api/rest-comments-controller.php#L3461-L3470. // create_initial_rest_routes(); wp_set_current_user( self::$admin_id ); $post_id = self::factory()->post->create( array( 'post_type' => 'no-notes' ) ); $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); $request->set_param( 'post', $post_id ); $request->set_param( 'type', 'note' ); $request->set_param( 'context', 'edit' ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_comment_not_supported_post_type', $response, 403 ); _unregister_post_type( 'no-notes' ); } public function test_create_note_require_login() { wp_set_current_user( 0 ); $post_id = self::factory()->post->create(); $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); $request->set_param( 'post', $post_id ); $request->set_param( 'type', 'note' ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_comment_login_required', $response, 401 ); } public function test_cannot_create_note_without_post_type_support() { register_post_type( 'no-note', array( 'label' => 'No Notes', 'supports' => array( 'title', 'editor', 'author', 'comments' ), 'show_in_rest' => true, 'public' => true, ) ); wp_set_current_user( self::$admin_id ); $post_id = self::factory()->post->create( array( 'post_type' => 'no-note' ) ); $params = array( 'post' => $post_id, 'author_name' => 'Ishmael', 'author_email' => 'herman-melville@earthlink.net', 'author_url' => 'https://en.wikipedia.org/wiki/Herman_Melville', 'content' => 'Call me Ishmael.', 'author' => self::$admin_id, 'type' => 'note', ); $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); $request->add_header( 'Content-Type', 'application/json' ); $request->set_body( wp_json_encode( $params ) ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_comment_not_supported_post_type', $response, 403 ); _unregister_post_type( 'no-note' ); } public function test_create_note_draft_post() { wp_set_current_user( self::$editor_id ); $draft_id = self::factory()->post->create( array( 'post_status' => 'draft', ) ); $params = array( 'post' => $draft_id, 'author_name' => 'Ishmael', 'author_email' => 'herman-melville@earthlink.net', 'author_url' => 'https://en.wikipedia.org/wiki/Herman_Melville', 'content' => 'Call me Ishmael.', 'author' => self::$editor_id, 'type' => 'note', ); $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); $request->add_header( 'Content-Type', 'application/json' ); $request->set_body( wp_json_encode( $params ) ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $new_comment = get_comment( $data['id'] ); $this->assertSame( 'Call me Ishmael.', $new_comment->comment_content ); $this->assertSame( 'note', $new_comment->comment_type ); } public function test_create_note_status() { wp_set_current_user( self::$author_id ); $post_id = self::factory()->post->create( array( 'post_author' => self::$author_id ) ); $params = array( 'post' => $post_id, 'author_name' => 'Ishmael', 'author_email' => 'herman-melville@earthlink.net', 'author_url' => 'https://en.wikipedia.org/wiki/Herman_Melville', 'content' => 'Comic Book Guy', 'author' => self::$author_id, 'type' => 'note', 'status' => 'hold', ); $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); $request->add_header( 'Content-Type', 'application/json' ); $request->set_body( wp_json_encode( $params ) ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $new_comment = get_comment( $data['id'] ); $this->assertSame( '0', $new_comment->comment_approved ); $this->assertSame( 'note', $new_comment->comment_type ); } public function test_cannot_create_with_non_valid_comment_type() { wp_set_current_user( self::$admin_id ); $post_id = self::factory()->post->create(); $params = array( 'post' => $post_id, 'author_name' => 'Ishmael', 'author_email' => 'herman-melville@earthlink.net', 'author_url' => 'https://en.wikipedia.org/wiki/Herman_Melville', 'content' => 'Comic Book Guy', 'author' => self::$admin_id, 'type' => 'review', ); $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); $request->add_header( 'Content-Type', 'application/json' ); $request->set_body( wp_json_encode( $params ) ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_invalid_comment_type', $response, 400 ); } public function test_create_assigns_default_type() { wp_set_current_user( self::$editor_id ); $post_id = self::factory()->post->create(); $params = array( 'post' => $post_id, 'author_name' => 'Ishmael', 'author_email' => 'herman-melville@earthlink.net', 'author_url' => 'https://en.wikipedia.org/wiki/Herman_Melville', 'content' => 'Comic Book Guy', 'author' => self::$editor_id, ); $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); $request->add_header( 'Content-Type', 'application/json' ); $request->set_body( wp_json_encode( $params ) ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $new_comment = get_comment( $data['id'] ); $this->assertSame( 'comment', $new_comment->comment_type ); } /** * @dataProvider data_note_status_provider */ public function test_create_empty_note_with_resolution_meta( $status ) { wp_set_current_user( self::$editor_id ); $post_id = self::factory()->post->create(); $params = array( 'post' => $post_id, 'author_name' => 'Editor', 'author_email' => 'editor@example.com', 'author_url' => 'https://example.com', 'author' => self::$editor_id, 'type' => 'note', 'content' => '', 'meta' => array( '_wp_note_status' => $status, ), ); $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); $request->add_header( 'Content-Type', 'application/json' ); $request->set_body( wp_json_encode( $params ) ); $response = rest_get_server()->dispatch( $request ); $this->assertSame( 201, $response->get_status() ); } public function test_cannot_create_empty_note_without_resolution_meta() { wp_set_current_user( self::$editor_id ); $post_id = self::factory()->post->create(); $params = array( 'post' => $post_id, 'author_name' => 'Editor', 'author_email' => 'editor@example.com', 'author_url' => 'https://example.com', 'author' => self::$editor_id, 'type' => 'note', 'content' => '', ); $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); $request->add_header( 'Content-Type', 'application/json' ); $request->set_body( wp_json_encode( $params ) ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_comment_content_invalid', $response, 400 ); } public function test_cannot_create_empty_note_with_invalid_resolution_meta() { wp_set_current_user( self::$editor_id ); $post_id = self::factory()->post->create(); $params = array( 'post' => $post_id, 'author_name' => 'Editor', 'author_email' => 'editor@example.com', 'author_url' => 'https://example.com', 'author' => self::$editor_id, 'type' => 'note', 'content' => '', 'meta' => array( '_wp_note_status' => 'invalid', ), ); $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); $request->add_header( 'Content-Type', 'application/json' ); $request->set_body( wp_json_encode( $params ) ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_comment_content_invalid', $response, 400 ); } public function test_create_duplicate_note() { wp_set_current_user( self::$editor_id ); $post_id = self::factory()->post->create(); for ( $i = 0; $i < 2; $i++ ) { $params = array( 'post' => $post_id, 'author_name' => 'Editor', 'author_email' => 'editor@example.com', 'author_url' => 'https://example.com', 'author' => self::$editor_id, 'type' => 'note', 'content' => 'Doplicated comment', ); $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); $request->add_header( 'Content-Type', 'application/json' ); $request->set_body( wp_json_encode( $params ) ); $response = rest_get_server()->dispatch( $request ); $this->assertSame( 201, $response->get_status() ); } } /** * @dataProvider data_note_get_items_permissions_data_provider */ public function test_note_get_items_permissions_edit_context( $role, $post_author_role, $can_read ) { wp_set_current_user( self::$user_ids[ $role ] ); $post_id = $this->create_test_post_with_note( $post_author_role ); $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); $request->set_param( 'post', $post_id ); $request->set_param( 'type', 'note' ); $request->set_param( 'status', 'all' ); $request->set_param( 'per_page', 100 ); $request->set_param( 'context', 'edit' ); $response = rest_get_server()->dispatch( $request ); if ( $can_read ) { $comments = $response->get_data(); $this->assertEquals( self::$num_notes, count( $comments ) ); } else { $this->assertErrorResponse( 'rest_forbidden_context', $response, 403 ); } wp_delete_post( $post_id, true ); } public function test_note_get_items_permissions_mixed_post_authors() { $author_post_id = $this->create_test_post_with_note( 'author' ); $editor_post_id = $this->create_test_post_with_note( 'editor' ); wp_set_current_user( self::$author_id ); $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); $request->set_param( 'post', array( $author_post_id, $editor_post_id ) ); $request->set_param( 'type', 'note' ); $request->set_param( 'status', 'all' ); $request->set_param( 'per_page', 100 ); $request->set_param( 'context', 'edit' ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_forbidden_context', $response, 403 ); wp_delete_post( $author_post_id, true ); wp_delete_post( $editor_post_id, true ); } /** * @dataProvider data_note_get_items_permissions_data_provider */ public function test_note_get_item_permissions_edit_context( $role, $post_author_role, $can_read ) { wp_set_current_user( self::$user_ids[ $role ] ); $post_id = self::factory()->post->create( array( 'post_title' => 'Test Post for Block Comments', 'post_content' => 'This is a test post to check block comment permissions.', 'post_status' => 'contributor' === $post_author_role ? 'draft' : 'publish', 'post_author' => self::$user_ids[ $post_author_role ], ) ); $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => $post_id, 'comment_type' => 'note', // Test with unapproved comment, which is more restrictive. 'comment_approved' => 0, 'user_id' => self::$user_ids[ $post_author_role ], ) ); $request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . $comment_id ); $request->set_param( 'context', 'edit' ); $response = rest_get_server()->dispatch( $request ); if ( $can_read ) { $comment = $response->get_data(); $this->assertEquals( $comment_id, $comment['id'] ); } else { $this->assertErrorResponse( 'rest_forbidden_context', $response, 403 ); } wp_delete_post( $post_id, true ); } public function data_note_get_items_permissions_data_provider() { return array( 'Administrator can see note on other posts' => array( 'administrator', 'author', true ), 'Editor can see note on other posts' => array( 'editor', 'contributor', true ), 'Author cannot see note on other posts' => array( 'author', 'editor', false ), 'Contributor cannot see note on other posts' => array( 'contributor', 'author', false ), 'Subscriber cannot see note' => array( 'subscriber', 'author', false ), 'Author can see note on own post' => array( 'author', 'author', true ), 'Contributor can see note on own post' => array( 'contributor', 'contributor', true ), ); } public function data_note_status_provider() { return array( 'resolved' => array( 'resolved' ), 'reopen' => array( 'reopen' ), ); } }