WordPress.org

Make WordPress Core

Ticket #38369: 38369.2.diff

File 38369.2.diff, 1.7 KB (added by pkostadinov, 4 years ago)

Modify return results, add description and unit test

  • src/wp-includes/comment-template.php

    diff --git src/wp-includes/comment-template.php src/wp-includes/comment-template.php
    index dc63e36..afa9fd8 100644
    function comments_link( $deprecated = '', $deprecated_2 = '' ) { 
    835835 * @since 1.5.0
    836836 *
    837837 * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global $post.
    838  * @return int The number of comments a post has.
     838 * @return string The number of comments a post has. Numeric string is returned for compatibility reasons.
    839839 */
    840840function get_comments_number( $post_id = 0 ) {
    841841        $post = get_post( $post_id );
    842842
    843843        if ( ! $post ) {
    844                 $count = 0;
     844                $count = '0';
    845845        } else {
    846846                $count = $post->comment_count;
    847847                $post_id = $post->ID;
  • tests/phpunit/tests/comment/template.php

    diff --git tests/phpunit/tests/comment/template.php tests/phpunit/tests/comment/template.php
    index ce93be2..ab67b82 100644
    class Tests_Comment_Template extends WP_UnitTestCase { 
    1717                $this->assertEquals( 12, get_comments_number( get_post( $post_id ) ) );
    1818        }
    1919
     20        function test_get_comments_number_return_type() {
     21                $post_id = self::factory()->post->create();
     22
     23                $this->assertSame( '0', get_comments_number( 0 ) );
     24                $this->assertSame( '0', get_comments_number( $post_id ) );
     25                $this->assertSame( '0', get_comments_number( get_post( $post_id ) ) );
     26
     27                self::factory()->comment->create_post_comments( $post_id, 12 );
     28
     29                $this->assertSame( '12', get_comments_number( $post_id ) );
     30                $this->assertSame( '12', get_comments_number( get_post( $post_id ) ) );
     31        }
     32
    2033        function test_get_comments_number_without_arg() {
    2134                $post_id = self::factory()->post->create();
    2235                $permalink = get_permalink( $post_id );