Make WordPress Core

Ticket #39242: 39242-unit-tests.diff

File 39242-unit-tests.diff, 8.9 KB (added by wpgurudev, 2 years ago)

Refactored function with unit tests

  • 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 ) { 
    536536        return wp_validate_auth_cookie( $_COOKIE[ LOGGED_IN_COOKIE ], 'logged_in' );
    537537}
    538538
     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 */
     547function 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 */
     574function 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 */
     593function 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}
     601add_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 */
     612function 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}
     623add_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 */
     633function 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
    539643/**
    540644 * Gets the number of posts a user has written.
    541645 *
    function wp_validate_logged_in_cookie( $user_id ) { 
    549653 * @param int          $userid      User ID.
    550654 * @param array|string $post_type   Optional. Single post type or array of post types to count the number of posts for. Default 'post'.
    551655 * @param bool         $public_only Optional. Whether to only return counts for public posts. Default false.
    552  * @return string Number of posts the user has written in this post type.
     656 * @return int Number of posts the user has written in this post type.
    553657 */
    554658function count_user_posts( $userid, $post_type = 'post', $public_only = false ) {
    555         global $wpdb;
     659        $count = 0;
    556660
    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        }
    560666
    561667        /**
    562668         * Filters the number of posts a user has written.
    function count_user_posts( $userid, $post_type = 'post', $public_only = false ) 
    586692 * @return string[] Amount of posts each user has written, as strings, keyed by user ID.
    587693 */
    588694function count_many_users_posts( $users, $post_type = 'post', $public_only = false ) {
    589         global $wpdb;
    590 
    591695        $count = array();
    592696        if ( empty( $users ) || ! is_array( $users ) ) {
    593697                return $count;
    594698        }
    595699
    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 );
    602702        }
    603703
    604704        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 { 
    560560
    561561                wp_set_current_user( self::$author_id );
    562562                $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 ] );
    565565
    566566                $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 ] );
    569569
    570570                wp_set_current_user( $user_id_b );
    571571                $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 ] );
    574574
    575575                $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 ] );
    578578        }
    579579
    580580        /**
  • 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 { 
    5959        }
    6060
    6161        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 ) );
    6363        }
    6464
    6565        /**
    6666         * @ticket 21364
    6767         */
    6868        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' ) );
    7070        }
    7171
    7272        /**
    7373         * @ticket 21364
    7474         */
    7575        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' ) );
    7777        }
    7878
    7979        /**
    8080         * @ticket 32243
    8181         */
    8282        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' ) ) );
    8484        }
    8585
    8686        /**
    8787         * @ticket 32243
    8888         */
    8989        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' ) ) );
    9191        }
    9292}