Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
burhandodhy committed Feb 13, 2025
1 parent 37552b6 commit 2a7f6d3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 27 deletions.
20 changes: 10 additions & 10 deletions includes/classes/Indexable/Comment/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,7 @@ public function query_db( $args ) {
'orderby' => 'comment_ID',
'order' => 'desc',
'ep_indexing_advanced_pagination' => true,
'no_found_rows' => false,
];

if ( isset( $args['per_page'] ) ) {
Expand Down Expand Up @@ -832,7 +833,6 @@ public function query_db( $args ) {
'order' => 'DESC',
'paged' => 1,
'offset' => 0,
'no_found_rows' => false,
]
);
add_filter( 'comments_clauses', array( $this, 'bulk_indexing_filter_comments_where' ), 9999, 2 );
Expand Down Expand Up @@ -869,28 +869,28 @@ public function bulk_indexing_filter_comments_where( $clauses, $query ) {
$using_advanced_pagination = $this->get_query_var( $query, 'ep_indexing_advanced_pagination', false );

if ( $using_advanced_pagination ) {
$requested_upper_limit_id = $this->get_query_var( $query, 'ep_indexing_upper_limit_object_id', PHP_INT_MAX );
$requested_lower_limit_post_id = $this->get_query_var( $query, 'ep_indexing_lower_limit_object_id', 0 );
$last_processed_id = $this->get_query_var( $query, 'ep_indexing_last_processed_object_id', null );
$requested_upper_limit_id = $this->get_query_var( $query, 'ep_indexing_upper_limit_object_id', PHP_INT_MAX );
$requested_lower_limit_object_id = $this->get_query_var( $query, 'ep_indexing_lower_limit_object_id', 0 );
$last_processed_id = $this->get_query_var( $query, 'ep_indexing_last_processed_object_id', null );

// On the first loopthrough we begin with the requested upper limit ID. Afterwards, use the last processed ID to paginate.
$upper_limit_range_post_id = $requested_upper_limit_id;
$upper_limit_range_object_id = $requested_upper_limit_id;
if ( is_numeric( $last_processed_id ) ) {
$upper_limit_range_post_id = $last_processed_id - 1;
$upper_limit_range_object_id = $last_processed_id - 1;
}

// Sanitize. Abort if unexpected data at this point.
if ( ! is_numeric( $upper_limit_range_post_id ) || ! is_numeric( $requested_lower_limit_post_id ) ) {
if ( ! is_numeric( $upper_limit_range_object_id ) || ! is_numeric( $requested_lower_limit_object_id ) ) {
return $clauses;
}

$range = [
'upper_limit' => "{$wpdb->comments}.comment_ID <= {$upper_limit_range_post_id}",
'lower_limit' => "{$wpdb->comments}.comment_ID >= {$requested_lower_limit_post_id}",
'upper_limit' => "{$wpdb->comments}.comment_ID <= {$upper_limit_range_object_id}",
'lower_limit' => "{$wpdb->comments}.comment_ID >= {$requested_lower_limit_object_id}",
];

// Skip the end range if it's unnecessary.
$skip_ending_range = 0 === $requested_lower_limit_post_id;
$skip_ending_range = 0 === $requested_lower_limit_object_id;
$where = $clauses['where'];
$where = $skip_ending_range ? " {$range['upper_limit']} AND {$where}" : " {$range['upper_limit']} AND {$range['lower_limit']} AND {$where}";

Expand Down
47 changes: 30 additions & 17 deletions tests/php/indexables/TestComment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2189,31 +2189,23 @@ public function testCommentIndexableQueryDb() {
]
);

$this->ep_factory->comment->create_many(
2,
[
'comment_post_ID' => $post_id,
]
);

$product_id = $this->ep_factory->product->create(
$this->ep_factory->comment->create(
[
'post_content' => 'product 1',
'comment_post_ID' => $this->ep_factory->product->create(),
'comment_type' => 'review',
]
);

$this->ep_factory->comment->create(
$middle_comment_id = $this->ep_factory->comment->create(
[
'comment_post_ID' => $product_id,
'comment_type' => 'review',
'comment_post_ID' => $post_id,
]
);

wp_insert_comment(
$this->ep_factory->comment->create_many(
10,
[
'comment_content' => 'Test review',
'comment_post_ID' => $product_id,
'comment_type' => 'review',
'comment_post_ID' => $post_id,
]
);

Expand All @@ -2223,19 +2215,40 @@ public function testCommentIndexableQueryDb() {
$results = $comment_indexable->query_db( [] );
$this->assertArrayHasKey( 'objects', $results );
$this->assertArrayHasKey( 'total_objects', $results );
$this->assertEquals( 3, $results['total_objects'] );
$this->assertEquals( 12, $results['total_objects'] );

// It's important to flush the cache here because WordPress caches results based on the default query arguments and doesn't account for custom arguments. @see \WP_Comment_Query::get_comments()
wp_cache_flush_group( 'comment-queries' );

// Test only 1 comment is returned.
$results = $comment_indexable->query_db( [ 'include' => $comment_1_id ] );
$this->assertArrayHasKey( 'objects', $results );
$this->assertArrayHasKey( 'total_objects', $results );
$this->assertEquals( 1, $results['total_objects'] );

wp_cache_flush_group( 'comment-queries' );

// Test all comments are returned except the one with ID.
$results = $comment_indexable->query_db( [ 'exclude' => $comment_1_id ] );
$this->assertArrayHasKey( 'objects', $results );
$this->assertArrayHasKey( 'total_objects', $results );
$this->assertEquals( 11, $results['total_objects'] );

wp_cache_flush_group( 'comment-queries' );

// Test when upper limit is set and it returns only 2 comments.
$results = $comment_indexable->query_db( [ 'ep_indexing_upper_limit_object_id' => $middle_comment_id ] );
$this->assertArrayHasKey( 'objects', $results );
$this->assertArrayHasKey( 'total_objects', $results );
$this->assertEquals( 2, $results['total_objects'] );

wp_cache_flush_group( 'comment-queries' );

// Test when lower limit is set and it returns only 11 comments.
$results = $comment_indexable->query_db( [ 'ep_indexing_lower_limit_object_id' => $middle_comment_id ] );
$this->assertArrayHasKey( 'objects', $results );
$this->assertArrayHasKey( 'total_objects', $results );
$this->assertEquals( 11, $results['total_objects'] );
}

/**
Expand Down

0 comments on commit 2a7f6d3

Please sign in to comment.