Skip to content

Commit 70b3647

Browse files
refactor(tests): split comment type tests and add data provider
1 parent ee5c6b5 commit 70b3647

File tree

1 file changed

+82
-55
lines changed

1 file changed

+82
-55
lines changed

tests/phpunit/tests/rest-api/rest-comments-controller.php

Lines changed: 82 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4134,89 +4134,116 @@ public function test_get_note_with_children_link() {
41344134
$this->assertStringContainsString( 'type=note', $children[0]['href'] );
41354135
}
41364136
/**
4137-
* Test comment permissions.
4137+
* Data provider for comment type tests.
41384138
*
4139+
* @return array
4140+
*/
4141+
public function data_comment_type_provider() {
4142+
return array(
4143+
'annotation type' => array( 'annotation', 5 ),
4144+
'discussion type' => array( 'discussion', 9 ),
4145+
'note type' => array( 'note', 3 ),
4146+
);
4147+
}
4148+
4149+
/**
4150+
* Test retrieving comments by type as authenticated user.
4151+
*
4152+
* @dataProvider data_comment_type_provider
41394153
* @ticket 44157
4154+
*
4155+
* @param string $comment_type The comment type to test.
4156+
* @param int $count The number of comments to create.
41404157
*/
4141-
public function test_get_items_type_arg() {
4158+
public function test_get_items_type_arg_authenticated( $comment_type, $count ) {
41424159
wp_set_current_user( self::$admin_id );
4143-
$comment_type_1 = 'annotation';
4144-
$comment_type_2 = 'discussion';
4145-
$note_comment_type = 'note';
4146-
$args = array(
4160+
4161+
$args = array(
41474162
'comment_approved' => 1,
41484163
'comment_post_ID' => self::$post_id,
41494164
'user_id' => self::$author_id,
4150-
'post_id' => self::$post_id,
4165+
'comment_type' => $comment_type,
41514166
);
41524167

4153-
$count_1 = 5;
4154-
$args['comment_type'] = $comment_type_1;
4155-
for ( $i = 0; $i < $count_1; $i++ ) {
4156-
self::factory()->comment->create( $args );
4157-
}
4158-
4159-
$count_2 = 9;
4160-
$args['comment_type'] = $comment_type_2;
4161-
for ( $i = 0; $i < $count_2; $i++ ) {
4162-
self::factory()->comment->create( $args );
4163-
}
4164-
4165-
$count_3 = 3;
4166-
$args['comment_type'] = $note_comment_type;
4167-
for ( $i = 0; $i < $count_3; $i++ ) {
4168+
// Create comments of the specified type.
4169+
for ( $i = 0; $i < $count; $i++ ) {
41684170
self::factory()->comment->create( $args );
41694171
}
41704172

41714173
$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
4172-
$request->set_param( 'type', $comment_type_1 );
4174+
$request->set_param( 'type', $comment_type );
41734175

41744176
$response = rest_get_server()->dispatch( $request );
41754177
$this->assertEquals( 200, $response->get_status() );
4176-
$comments = $response->get_data();
4177-
$this->assertCount( $count_1, $comments );
41784178

4179-
$request->set_param( 'type', $comment_type_2 );
4180-
$response = rest_get_server()->dispatch( $request );
4181-
$this->assertEquals( 200, $response->get_status() );
41824179
$comments = $response->get_data();
4183-
$this->assertCount( $count_2, $comments );
4184-
$comment_type_ids = wp_list_pluck( $comments, 'id' );
4180+
$this->assertCount( $count, $comments );
4181+
}
41854182

4186-
$request->set_param( 'type', $note_comment_type );
4187-
$response = rest_get_server()->dispatch( $request );
4188-
$this->assertEquals( 200, $response->get_status() );
4189-
$comments = $response->get_data();
4190-
$this->assertCount( $count_3, $comments );
4191-
$note_type_ids = wp_list_pluck( $comments, 'id' );
4183+
/**
4184+
* Test retrieving comments by type as unauthenticated user.
4185+
*
4186+
* @dataProvider data_comment_type_provider
4187+
* @ticket 44157
4188+
*
4189+
* @param string $comment_type The comment type to test.
4190+
* @param int $count The number of comments to create.
4191+
*/
4192+
public function test_get_items_type_arg_unauthenticated( $comment_type, $count ) {
4193+
// First, create comments as admin.
4194+
wp_set_current_user( self::$admin_id );
41924195

4193-
// Log out the current user.
4196+
$args = array(
4197+
'comment_approved' => 1,
4198+
'comment_post_ID' => self::$post_id,
4199+
'user_id' => self::$author_id,
4200+
'comment_type' => $comment_type,
4201+
);
4202+
4203+
for ( $i = 0; $i < $count; $i++ ) {
4204+
self::factory()->comment->create( $args );
4205+
}
4206+
4207+
// Log out and test as unauthenticated user.
41944208
wp_logout();
41954209

4196-
$request->set_param( 'type', 'comments' );
4210+
$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
4211+
$request->set_param( 'type', $comment_type );
41974212
$request->set_param( 'per_page', self::$per_page );
4213+
41984214
$response = rest_get_server()->dispatch( $request );
41994215
$this->assertEquals( 401, $response->get_status() );
4200-
$comments = $response->get_data();
42014216
$this->assertErrorResponse( 'rest_forbidden_param', $response, 401 );
4217+
}
42024218

4203-
$request->set_param( 'comment_type', $comment_type_2 );
4204-
$response = rest_get_server()->dispatch( $request );
4205-
$comments = $response->get_data();
4206-
$this->assertErrorResponse( 'rest_forbidden_param', $response, 401 );
4219+
/**
4220+
* Test retrieving individual comments by type as unauthenticated user.
4221+
*
4222+
* @dataProvider data_comment_type_provider
4223+
* @ticket 44157
4224+
*
4225+
* @param string $comment_type The comment type to test.
4226+
* @param int $count The number of comments to create (only 1 used).
4227+
*/
4228+
public function test_get_individual_comment_type_unauthenticated( $comment_type, $count ) {
4229+
// Create a single comment as admin.
4230+
wp_set_current_user( self::$admin_id );
42074231

4208-
$request->set_param( 'comment_type', $note_comment_type );
4209-
foreach ( $note_type_ids as $note_type_id ) {
4210-
$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', $note_type_id ) );
4211-
$response = rest_get_server()->dispatch( $request );
4212-
$this->assertEquals( 401, $response->get_status() );
4213-
}
4232+
$args = array(
4233+
'comment_approved' => 1,
4234+
'comment_post_ID' => self::$post_id,
4235+
'user_id' => self::$author_id,
4236+
'comment_type' => $comment_type,
4237+
);
42144238

4215-
// Custom comment types should also not be visible to unauthenticated users.
4216-
foreach ( $comment_type_ids as $comment_type_id ) {
4217-
$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', $comment_type_id ) );
4218-
$response = rest_get_server()->dispatch( $request );
4219-
$this->assertEquals( 401, $response->get_status() );
4220-
}
4239+
$comment_id = self::factory()->comment->create( $args );
4240+
4241+
// Log out and test as unauthenticated user.
4242+
wp_logout();
4243+
4244+
$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', $comment_id ) );
4245+
$response = rest_get_server()->dispatch( $request );
4246+
4247+
$this->assertEquals( 401, $response->get_status() );
42214248
}
42224249
}

0 commit comments

Comments
 (0)