Skip to content

Commit

Permalink
Fix human_time_diff issues
Browse files Browse the repository at this point in the history
Make sure that 1 second time difference is fixed
Should take care of #1345 a bit more
Fingers crossed
  • Loading branch information
soulseekah committed Nov 16, 2019
1 parent 59cd719 commit ba298e4
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions tests/unit-tests/GravityView_Merge_Tags_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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,
Expand All @@ -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 ),
Expand All @@ -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() {
Expand Down

0 comments on commit ba298e4

Please sign in to comment.