From e30ce9d4b57a38e7d7fe8c0665c71e444943c555 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Fri, 7 Apr 2023 14:34:51 +0000 Subject: [PATCH] Comments: Add missing arguments for `get_comment_time()` in `comment_time()`. In a recent change, `comment_time()` was updated to accept a `$comment_id` parameter for consistency with `comment_date()`, following a similar change for `get_comment_time()`. However, the new parameter was not correctly passed to `get_comment_time()` inside the function. It should be passed as the fourth parameter after `$format`, `$gmt` and `$translate`, not the second. This commit adds the missing arguments and a few unit tests to confirm the correct behavior. Follow-up to [55284], [55287], [55308]. Props costdev, tmatsuur, ugyensupport, johnbillion. Fixes #58064. git-svn-id: https://develop.svn.wordpress.org/trunk@55632 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/comment-template.php | 2 +- tests/phpunit/tests/comment/commentTime.php | 130 ++++++++++++++++++++ 2 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 tests/phpunit/tests/comment/commentTime.php diff --git a/src/wp-includes/comment-template.php b/src/wp-includes/comment-template.php index 9a2064cec2..899fc26b72 100644 --- a/src/wp-includes/comment-template.php +++ b/src/wp-includes/comment-template.php @@ -1109,7 +1109,7 @@ function get_comment_time( $format = '', $gmt = false, $translate = true, $comme * Default current comment. */ function comment_time( $format = '', $comment_id = 0 ) { - echo get_comment_time( $format, $comment_id ); + echo get_comment_time( $format, false, true, $comment_id ); } /** diff --git a/tests/phpunit/tests/comment/commentTime.php b/tests/phpunit/tests/comment/commentTime.php new file mode 100644 index 0000000000..fd9e8fe6e0 --- /dev/null +++ b/tests/phpunit/tests/comment/commentTime.php @@ -0,0 +1,130 @@ +post->create( + array( + 'post_title' => 'Post title for comment_time() tests', + 'post_content' => 'Post content for comment_time() tests', + ) + ); + + self::$comment_id = self::factory()->comment->create( + array( + 'comment_post_ID' => self::$post_id, + 'user_id' => 1, + ) + ); + } + + /** + * Tests that comment_time() displays the same value that get_comment_time() returns. + * + * @ticket 58064 + * + * @dataProvider data_should_output_the_same_value_that_get_comment_time_returns + * + * @param string $format PHP date format. + */ + public function test_should_output_the_same_value_that_get_comment_time_returns( $format ) { + $expected = get_comment_time( $format, false, true, self::$comment_id ); + + ob_start(); + comment_time( $format, self::$comment_id ); + $actual = ob_get_clean(); + + $this->assertSame( $expected, $actual ); + } + + /** + * Data provider. + * + * @return array[] + */ + public function data_should_output_the_same_value_that_get_comment_time_returns() { + return array( + 'an empty format' => array( + 'format' => '', + ), + 'a PHP date format' => array( + 'format' => 'h:i:s A', + ), + ); + } + + /** + * Tests that comment_time() defaults to the global comment when comment ID + * is not provided. + * + * @ticket 58064 + */ + public function test_should_default_to_the_global_comment_when_comment_id_is_not_provided() { + global $comment; + + // Back up the global comment before setting the value. + $comment_backup = $comment; + $comment = self::$comment_id; + + $expected = get_comment_time(); + + ob_start(); + comment_time(); + $actual = ob_get_clean(); + + // Restore the global comment value. + $comment = $comment_backup; + + $this->assertSame( $expected, $actual ); + } + + /** + * Tests that comment_time() displays an empty string when global comment is not set + * and comment ID is not provided. + * + * @ticket 58064 + */ + public function test_should_output_an_empty_string_when_global_comment_is_not_set_and_comment_id_is_not_provided() { + global $comment; + + // Back up the global comment before setting the value. + $comment_backup = $comment; + $comment = null; + + ob_start(); + comment_time(); + $actual = ob_get_clean(); + + // Restore the global comment value. + $comment = $comment_backup; + + $this->assertSame( '', $actual ); + } + +}