Ticket #39242: 39242-unit-tests.diff
File 39242-unit-tests.diff, 8.9 KB (added by , 2 years ago) |
---|
-
src/wp-includes/user.php
diff --git a/src/wp-includes/user.php b/src/wp-includes/user.php index ca4b3d03fd..3770e3e18b 100644
a b function wp_validate_logged_in_cookie( $user_id ) { 536 536 return wp_validate_auth_cookie( $_COOKIE[ LOGGED_IN_COOKIE ], 'logged_in' ); 537 537 } 538 538 539 /** 540 * Counts the number of posts for a particular post type. 541 * 542 * @param int $userid User ID. 543 * @param string $post_type Optional. Single post type or array of post types to count the number of posts for. Default 'post'. 544 * @param bool $public_only Optional. Whether to only return counts for public posts. Default false. 545 * @return int Number of posts the user has written in this post type. 546 */ 547 function count_user_posts_for_single_type( $userid, $post_type = 'post', $public_only = false ) { 548 global $wpdb; 549 550 $where = get_posts_by_author_sql( $post_type, true, $userid, $public_only ); 551 $where_hash = wp_hash( $where ); 552 $cache_key = "count_user_{$post_type}_{$userid}_{$where_hash}"; 553 $cache_group = $public_only ? 'user_posts_count_public' : 'user_posts_count'; 554 555 // Try to get count from cache. 556 $count = wp_cache_get( $cache_key, $cache_group ); 557 558 // If cache is empty, query the database. 559 if( false === $count ) { 560 $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" ); 561 562 wp_cache_add( $cache_key, $count, $cache_group ); 563 } 564 565 return absint( $count ); 566 } 567 568 /** 569 * Clears the cached count for user for given post type. 570 * 571 * @param int $userid User ID. 572 * @param string $post_type Post type. 573 */ 574 function clear_count_user_posts_cache( $userid, $post_type ) { 575 $cache_key = "count_user_{$post_type}_{$userid}"; 576 $cache_groups = [ 577 'user_posts_count_public', 578 'user_posts_count', 579 ]; 580 581 foreach( $cache_groups as $cache_group ) { 582 wp_cache_delete( $cache_key, $cache_group ); 583 } 584 } 585 586 /** 587 * Check if post author has changed. If changed, clear count posts cache for user. 588 * 589 * @param int $post_ID Post ID. 590 * @param WP_Post $post_after Post object following the update. 591 * @param WP_Post $post_before Post object before the update. 592 */ 593 function check_author_change( $post_id, $post_after, $post_before ) { 594 if ( $post_after->post_author !== $post_before->post_author ) { 595 $post_type = get_post_type( $post_id ); 596 597 clear_count_user_posts_cache( $post_after->post_author, $post_type ); 598 clear_count_user_posts_cache( $post_before->post_author, $post_type ); 599 } 600 } 601 add_action( 'post_updated', 'check_author_change', 10, 3 ); 602 603 /** 604 * When the post is created, clear the cache. 605 * 606 * @param int $post_id Post ID. 607 * @param WP_Post $post Post object. 608 * @param bool $update Whether this is an existing post being updated. 609 * 610 * @return void 611 */ 612 function clear_cache_on_post_update( $post_id, $post ) { 613 // Don't do anything if revision is being saved. 614 if ( wp_is_post_revision( $post_id ) ) { 615 return; 616 } 617 618 $post_type = $post->post_type; 619 $author_id = $post->post_author; 620 621 clear_count_user_posts_cache( $author_id, $post_type ); 622 } 623 add_action( 'save_post', 'clear_cache_on_post_update', 10, 2 ); 624 625 /** 626 * Counts the number of posts for given post types. 627 * 628 * @param int $userid User ID. 629 * @param array $post_types Optional. Single post type or array of post types to count the number of posts for. Default 'post'. 630 * @param bool $public_only Optional. Whether to only return counts for public posts. Default false. 631 * @return int Number of posts the user has written in this post type. 632 */ 633 function count_user_posts_for_multiple_types( $userid, $post_types = [ 'post' ], $public_only = false ) { 634 $numposts = 0; 635 636 foreach( $post_types as $post_type ) { 637 $numposts += count_user_posts_for_single_type( $userid, $post_type, $public_only ); 638 } 639 640 return $numposts; 641 } 642 539 643 /** 540 644 * Gets the number of posts a user has written. 541 645 * … … function wp_validate_logged_in_cookie( $user_id ) { 549 653 * @param int $userid User ID. 550 654 * @param array|string $post_type Optional. Single post type or array of post types to count the number of posts for. Default 'post'. 551 655 * @param bool $public_only Optional. Whether to only return counts for public posts. Default false. 552 * @return stringNumber of posts the user has written in this post type.656 * @return int Number of posts the user has written in this post type. 553 657 */ 554 658 function count_user_posts( $userid, $post_type = 'post', $public_only = false ) { 555 global $wpdb;659 $count = 0; 556 660 557 $where = get_posts_by_author_sql( $post_type, true, $userid, $public_only ); 558 559 $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" ); 661 if ( is_array( $post_type ) ) { 662 $count = count_user_posts_for_multiple_types( $userid, $post_type, $public_only ); 663 } else { 664 $count = count_user_posts_for_single_type( $userid, $post_type, $public_only ); 665 } 560 666 561 667 /** 562 668 * Filters the number of posts a user has written. … … function count_user_posts( $userid, $post_type = 'post', $public_only = false ) 586 692 * @return string[] Amount of posts each user has written, as strings, keyed by user ID. 587 693 */ 588 694 function count_many_users_posts( $users, $post_type = 'post', $public_only = false ) { 589 global $wpdb;590 591 695 $count = array(); 592 696 if ( empty( $users ) || ! is_array( $users ) ) { 593 697 return $count; 594 698 } 595 699 596 $userlist = implode( ',', array_map( 'absint', $users ) ); 597 $where = get_posts_by_author_sql( $post_type, true, null, $public_only ); 598 599 $result = $wpdb->get_results( "SELECT post_author, COUNT(*) FROM $wpdb->posts $where AND post_author IN ($userlist) GROUP BY post_author", ARRAY_N ); 600 foreach ( $result as $row ) { 601 $count[ $row[0] ] = $row[1]; 700 foreach ( $users as $user_id ) { 701 $count[ $user_id ] = count_user_posts( $user_id, $post_type, $public_only ); 602 702 } 603 703 604 704 foreach ( $users as $id ) { -
tests/phpunit/tests/user.php
diff --git a/tests/phpunit/tests/user.php b/tests/phpunit/tests/user.php index 7157a00cfa..0df2c2dbd8 100644
a b class Tests_User extends WP_UnitTestCase { 560 560 561 561 wp_set_current_user( self::$author_id ); 562 562 $counts = count_many_users_posts( array( self::$author_id, $user_id_b ), 'post', false ); 563 $this->assertSame( '1', $counts[ self::$author_id ] );564 $this->assertSame( '1', $counts[ $user_id_b ] );563 $this->assertSame( 1, $counts[ self::$author_id ] ); 564 $this->assertSame( 1, $counts[ $user_id_b ] ); 565 565 566 566 $counts = count_many_users_posts( array( self::$author_id, $user_id_b ), 'post', true ); 567 $this->assertSame( '1', $counts[ self::$author_id ] );568 $this->assertSame( '1', $counts[ $user_id_b ] );567 $this->assertSame( 1, $counts[ self::$author_id ] ); 568 $this->assertSame( 1, $counts[ $user_id_b ] ); 569 569 570 570 wp_set_current_user( $user_id_b ); 571 571 $counts = count_many_users_posts( array( self::$author_id, $user_id_b ), 'post', false ); 572 $this->assertSame( '1', $counts[ self::$author_id ] );573 $this->assertSame( '2', $counts[ $user_id_b ] );572 $this->assertSame( 1, $counts[ self::$author_id ] ); 573 $this->assertSame( 2, $counts[ $user_id_b ] ); 574 574 575 575 $counts = count_many_users_posts( array( self::$author_id, $user_id_b ), 'post', true ); 576 $this->assertSame( '1', $counts[ self::$author_id ] );577 $this->assertSame( '1', $counts[ $user_id_b ] );576 $this->assertSame( 1, $counts[ self::$author_id ] ); 577 $this->assertSame( 1, $counts[ $user_id_b ] ); 578 578 } 579 579 580 580 /** -
tests/phpunit/tests/user/countUserPosts.php
diff --git a/tests/phpunit/tests/user/countUserPosts.php b/tests/phpunit/tests/user/countUserPosts.php index dbc94f418e..ebf9722ab2 100644
a b class Tests_User_CountUserPosts extends WP_UnitTestCase { 59 59 } 60 60 61 61 public function test_count_user_posts_post_type_should_default_to_post() { 62 $this->assertSame( '4', count_user_posts( self::$user_id ) );62 $this->assertSame( 4, count_user_posts( self::$user_id ) ); 63 63 } 64 64 65 65 /** 66 66 * @ticket 21364 67 67 */ 68 68 public function test_count_user_posts_post_type_post() { 69 $this->assertSame( '4', count_user_posts( self::$user_id, 'post' ) );69 $this->assertSame( 4, count_user_posts( self::$user_id, 'post' ) ); 70 70 } 71 71 72 72 /** 73 73 * @ticket 21364 74 74 */ 75 75 public function test_count_user_posts_post_type_cpt() { 76 $this->assertSame( '3', count_user_posts( self::$user_id, 'wptests_pt' ) );76 $this->assertSame( 3, count_user_posts( self::$user_id, 'wptests_pt' ) ); 77 77 } 78 78 79 79 /** 80 80 * @ticket 32243 81 81 */ 82 82 public function test_count_user_posts_with_multiple_post_types() { 83 $this->assertSame( '7', count_user_posts( self::$user_id, array( 'wptests_pt', 'post' ) ) );83 $this->assertSame( 7, count_user_posts( self::$user_id, array( 'wptests_pt', 'post' ) ) ); 84 84 } 85 85 86 86 /** 87 87 * @ticket 32243 88 88 */ 89 89 public function test_count_user_posts_should_ignore_non_existent_post_types() { 90 $this->assertSame( '4', count_user_posts( self::$user_id, array( 'foo', 'post' ) ) );90 $this->assertSame( 4, count_user_posts( self::$user_id, array( 'foo', 'post' ) ) ); 91 91 } 92 92 }