Skip to content

Commit d556331

Browse files
Add some unit tests covering note functionality
1 parent cb8cdbe commit d556331

File tree

1 file changed

+199
-0
lines changed

1 file changed

+199
-0
lines changed

tests/phpunit/tests/comment/query.php

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,39 @@ class Tests_Comment_Query extends WP_UnitTestCase {
2020
*/
2121
private $to_exclude;
2222

23+
/**
24+
* Test-specific posts created during individual tests that need cleanup.
25+
*
26+
* @since 6.9.0
27+
* @var array
28+
*/
29+
private $test_posts = array();
30+
31+
/**
32+
* Test-specific comments created during individual tests that need cleanup.
33+
*
34+
* @since 6.9.0
35+
* @var array
36+
*/
37+
private $test_comments = array();
38+
2339
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
2440
self::$post_id = $factory->post->create();
2541
}
2642

2743
public function tear_down() {
44+
// Clean up test-specific comments.
45+
foreach ( $this->test_comments as $comment_id ) {
46+
wp_delete_comment( $comment_id, true );
47+
}
48+
$this->test_comments = array();
49+
50+
// Clean up test-specific posts.
51+
foreach ( $this->test_posts as $post_id ) {
52+
wp_delete_post( $post_id, true );
53+
}
54+
$this->test_posts = array();
55+
2856
unset( $this->to_exclude );
2957
parent::tear_down();
3058
}
@@ -5372,4 +5400,175 @@ public function test_query_does_not_have_leading_whitespace() {
53725400

53735401
$this->assertSame( ltrim( $q->request ), $q->request, 'The query has leading whitespace' );
53745402
}
5403+
5404+
/**
5405+
* Helper method to create standard test comments for note type exclusion tests.
5406+
*
5407+
* @since 6.9.0
5408+
*
5409+
* @return int[] Array of comment IDs indexed by type: [0] => regular comment, [1] => pingback, [2] => note.
5410+
*/
5411+
protected function create_note_type_test_comments() {
5412+
$comments = array();
5413+
$comments[0] = self::factory()->comment->create(
5414+
array(
5415+
'comment_post_ID' => self::$post_id,
5416+
'comment_approved' => '1',
5417+
)
5418+
);
5419+
$comments[1] = self::factory()->comment->create(
5420+
array(
5421+
'comment_post_ID' => self::$post_id,
5422+
'comment_approved' => '1',
5423+
'comment_type' => 'pingback',
5424+
)
5425+
);
5426+
$comments[2] = self::factory()->comment->create(
5427+
array(
5428+
'comment_post_ID' => self::$post_id,
5429+
'comment_approved' => '1',
5430+
'comment_type' => 'note',
5431+
)
5432+
);
5433+
5434+
// Track for cleanup.
5435+
$this->test_comments = array_merge( $this->test_comments, $comments );
5436+
5437+
return $comments;
5438+
}
5439+
5440+
/**
5441+
* @ticket 64145
5442+
* @covers WP_Comment_Query::get_comment_ids
5443+
* @dataProvider data_note_type_exclusion
5444+
*
5445+
* @param array $query_args Query arguments for WP_Comment_Query.
5446+
* @param array $expected_indices Indices of expected comments in the $comments array.
5447+
*/
5448+
public function test_note_type_exclusion( $query_args, $expected_indices ) {
5449+
$comments = $this->create_note_type_test_comments();
5450+
5451+
$query_args['fields'] = 'ids';
5452+
$q = new WP_Comment_Query();
5453+
$found = $q->query( $query_args );
5454+
5455+
$expected = array();
5456+
foreach ( $expected_indices as $index ) {
5457+
$expected[] = $comments[ $index ];
5458+
}
5459+
5460+
$this->assertSameSets( $expected, $found );
5461+
}
5462+
5463+
/**
5464+
* Data provider for note type exclusion tests.
5465+
*
5466+
* @since 6.9.0
5467+
*
5468+
* @return array[]
5469+
*/
5470+
public function data_note_type_exclusion() {
5471+
return array(
5472+
'default query excludes note' => array(
5473+
'query_args' => array(),
5474+
'expected_indices' => array( 0, 1 ), // comment and pingback.
5475+
),
5476+
'empty type parameter excludes note' => array(
5477+
'query_args' => array( 'type' => '' ),
5478+
'expected_indices' => array( 0, 1 ), // comment and pingback.
5479+
),
5480+
'type all includes note' => array(
5481+
'query_args' => array( 'type' => 'all' ),
5482+
'expected_indices' => array( 0, 1, 2 ), // comment, pingback, and note.
5483+
),
5484+
'explicit note type' => array(
5485+
'query_args' => array( 'type' => 'note' ),
5486+
'expected_indices' => array( 2 ), // only note.
5487+
),
5488+
'type__in with note' => array(
5489+
'query_args' => array( 'type__in' => array( 'note' ) ),
5490+
'expected_indices' => array( 2 ), // only note.
5491+
),
5492+
'type__in with note and pingback' => array(
5493+
'query_args' => array( 'type__in' => array( 'note', 'pingback' ) ),
5494+
'expected_indices' => array( 1, 2 ), // pingback and note.
5495+
),
5496+
'type pings excludes note' => array(
5497+
'query_args' => array( 'type' => 'pings' ),
5498+
'expected_indices' => array( 1 ), // only pingback.
5499+
),
5500+
'type__not_in with note' => array(
5501+
'query_args' => array( 'type__not_in' => array( 'note' ) ),
5502+
'expected_indices' => array( 0, 1 ), // comment and pingback.
5503+
),
5504+
);
5505+
}
5506+
5507+
/**
5508+
* @ticket 64145
5509+
* @covers WP_Comment_Query::get_comment_ids
5510+
*/
5511+
public function test_note_type_not_duplicated_in_type__not_in() {
5512+
global $wpdb;
5513+
5514+
$comments = $this->create_note_type_test_comments();
5515+
5516+
$q = new WP_Comment_Query();
5517+
$found = $q->query(
5518+
array(
5519+
'type__not_in' => array( 'note' ),
5520+
'fields' => 'ids',
5521+
)
5522+
);
5523+
5524+
$this->assertSameSets( array( $comments[0], $comments[1] ), $found );
5525+
$this->assertNotContains( $comments[2], $found );
5526+
5527+
// Verify that 'note' doesn't appear twice in the query.
5528+
$note_count = substr_count( $wpdb->last_query, "'note'" );
5529+
$this->assertSame( 1, $note_count, 'The note type should only appear once in the query' );
5530+
}
5531+
5532+
/**
5533+
* @ticket 64145
5534+
* @covers ::get_comment_count
5535+
*/
5536+
public function test_get_comment_count_excludes_note_type() {
5537+
$post_id = self::factory()->post->create();
5538+
$this->test_posts[] = $post_id;
5539+
5540+
$c1 = self::factory()->comment->create(
5541+
array(
5542+
'comment_post_ID' => $post_id,
5543+
'comment_approved' => '1',
5544+
)
5545+
);
5546+
$c2 = self::factory()->comment->create(
5547+
array(
5548+
'comment_post_ID' => $post_id,
5549+
'comment_approved' => '1',
5550+
'comment_type' => 'note',
5551+
)
5552+
);
5553+
$c3 = self::factory()->comment->create(
5554+
array(
5555+
'comment_post_ID' => $post_id,
5556+
'comment_approved' => '0',
5557+
'comment_type' => 'note',
5558+
)
5559+
);
5560+
5561+
// Track comments for cleanup.
5562+
$this->test_comments = array_merge( $this->test_comments, array( $c1, $c2, $c3 ) );
5563+
5564+
$counts = get_comment_count( $post_id );
5565+
5566+
$this->assertSame( 1, $counts['approved'] );
5567+
$this->assertSame( 0, $counts['awaiting_moderation'] );
5568+
$this->assertSame( 0, $counts['spam'] );
5569+
$this->assertSame( 0, $counts['trash'] );
5570+
$this->assertSame( 0, $counts['post-trashed'] );
5571+
$this->assertSame( 1, $counts['all'] );
5572+
$this->assertSame( 1, $counts['total_comments'] );
5573+
}
53755574
}

0 commit comments

Comments
 (0)