From ba298e4ee1d37c30b4149dccaabd9afccb61c644 Mon Sep 17 00:00:00 2001 From: Gennady Kovshenin Date: Sat, 16 Nov 2019 10:42:00 +0300 Subject: [PATCH] Fix human_time_diff issues Make sure that 1 second time difference is fixed Should take care of #1345 a bit more Fingers crossed --- .../GravityView_Merge_Tags_Test.php | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/tests/unit-tests/GravityView_Merge_Tags_Test.php b/tests/unit-tests/GravityView_Merge_Tags_Test.php index 7f7a816882..a38363c53b 100644 --- a/tests/unit-tests/GravityView_Merge_Tags_Test.php +++ b/tests/unit-tests/GravityView_Merge_Tags_Test.php @@ -240,18 +240,12 @@ function test_process_modifiers() { * @group date_created */ function test_replace_date_created_and_updated( $date_field = 'date_created' ) { - - /** - * GFCommon::format_date() requires at least 1 second difference in order to show human-formatted time properly - * - * @link https://travis-ci.org/gravityview/GravityView/jobs/604386139 - */ - sleep( 1 ); // Let's see if this helps with tests. - $form = $this->factory->form->create_and_get(); $entry = $this->factory->entry->create_and_get( array( 'form_id' => $form['id'], + 'date_created' => '2019-11-16 10:00:00', + 'date_updated' => '2019-11-16 10:00:00', ) ); $date_value = \GV\Utils::get( $entry, $date_field ); @@ -263,6 +257,15 @@ function test_replace_date_created_and_updated( $date_field = 'date_created' ) { $entry_gmt_time = mysql2date( 'G', $date_value ); $entry_local_time = GFCommon::get_local_timestamp( $entry_gmt_time ); + $time_now = $entry_gmt_time + 1; // 1 second difference for human_time_diff calls + + add_filter( 'human_time_diff', $callback = function( $since, $diff, $from, $to ) use ( $time_now ) { + if ( $to !== $time_now ) { + return human_time_diff( $from, $time_now ); + } + return $since; + }, 10, 4 ); + $tests = array( "{{$date_field}:raw}" => $date_value, @@ -288,12 +291,12 @@ function test_replace_date_created_and_updated( $date_field = 'date_created' ) { // 1 second ago "{{$date_field}:diff}" => sprintf( '%s ago', human_time_diff( $entry_gmt_time ) ), - "{{$date_field}:diff:format:%s is so long ago}" => sprintf( '%s is so long ago', human_time_diff( $entry_gmt_time ) ), + "{{$date_field}:diff:format:%s is so long ago}" => sprintf( '%s is so long ago', human_time_diff( $entry_gmt_time, $time_now ) ), // Relative should NOT process other modifiers - "{{$date_field}:diff:time}" => sprintf( '%s ago', human_time_diff( $entry_gmt_time ) ), - "{{$date_field}:diff:human}" => sprintf( '%s ago', human_time_diff( $entry_gmt_time ) ), - "{{$date_field}:human:diff}" => sprintf( '%s ago', human_time_diff( $entry_gmt_time ) ), + "{{$date_field}:diff:time}" => sprintf( '%s ago', human_time_diff( $entry_gmt_time, $time_now ) ), + "{{$date_field}:diff:human}" => sprintf( '%s ago', human_time_diff( $entry_gmt_time, $time_now ) ), + "{{$date_field}:human:diff}" => sprintf( '%s ago', human_time_diff( $entry_gmt_time, $time_now ) ), "{{$date_field}:format:mdy}" => GFCommon::format_date( $date_value, false, 'mdy', false ), "{{$date_field}:human:format:m/d/Y }" => GFCommon::format_date( $date_value, true, 'm/d/Y', false ), @@ -308,6 +311,8 @@ function test_replace_date_created_and_updated( $date_field = 'date_created' ) { foreach ( $tests as $merge_tag => $expected ) { $this->assertEquals( $expected, GravityView_Merge_Tags::replace_variables( $merge_tag, $form, $entry ), $merge_tag ); } + + $this->assertTrue( remove_filter( 'human_time_diff', $callback ) ); } function test_replace_date_updated() {