Skip to content

Commit

Permalink
Add tests for count_user_posts behavior and caching scenarios
Browse files Browse the repository at this point in the history
Introduce tests to validate count_user_posts correctness for non-existent users, users created after post assignment, and caching mechanisms. Ensure accurate caching for post type order variations and equivalence of string and array queries.

Feedback from @peterwilsoncc
  • Loading branch information
spacedmonkey committed Feb 3, 2025
1 parent 32e28e6 commit fe595f2
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions tests/phpunit/tests/user/countUserPosts.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,86 @@ public function test_count_user_posts_with_multiple_post_types() {
public function test_count_user_posts_should_ignore_non_existent_post_types() {
$this->assertSame( '4', count_user_posts( self::$user_id, array( 'foo', 'post' ) ) );
}

/**
* User count should work for users that don't exist but have posts assigned.
*
* @ticket 39242
*/
public function test_count_user_posts_for_non_existent_user() {
$next_user_id = self::$user_id + 1;

// Assign post to next user.
self::factory()->post->create(
array(
'post_author' => $next_user_id,
'post_type' => 'post',
)
);

$next_user_post_count = count_user_posts( $next_user_id );
$this->assertSame( '1', $next_user_post_count, 'Non-existent user is expected to have count of one post.' );
}

/**
* Cached user count value should be accurate after user is created.
*
* @ticket 39242
*/
public function test_count_user_posts_for_user_created_after_being_assigned_posts() {
$next_user_id = self::$user_id + 1;

// Assign post to next user.
self::factory()->post->create(
array(
'post_author' => $next_user_id,
'post_type' => 'post',
)
);

// Cache the user count.
count_user_posts( $next_user_id );

// Create user.
$real_next_user_id = self::factory()->user->create(
array(
'role' => 'author',
)
);

$this->assertSame( $next_user_id, $real_next_user_id, 'User ID should match calculated value' );
$this->assertSame( '1', count_user_posts( $next_user_id ), 'User is expected to have count of one post.' );
}

/**
* User count cache should be hit regardless of post type order.
*
* @ticket 39242
*/
public function test_cache_should_be_hit_regardless_of_post_type_order() {
// Prime Cache
count_user_posts( self::$user_id, array( 'wptests_pt', 'post' ) );

$query_num_start = get_num_queries();
count_user_posts( self::$user_id, array( 'post', 'wptests_pt' ) );
$total_queries = get_num_queries() - $query_num_start;

$this->assertSame( 0, $total_queries );
}

/**
* User count cache should be hit for string and array of post types.
*
* @ticket 39242
*/
public function test_cache_should_be_hit_for_string_and_array_equivalent_queries() {
// Prime Cache
count_user_posts( self::$user_id, 'post' );

$query_num_start = get_num_queries();
count_user_posts( self::$user_id, array( 'post' ) );
$total_queries = get_num_queries() - $query_num_start;

$this->assertSame( 0, $total_queries );
}
}

0 comments on commit fe595f2

Please sign in to comment.