# Failed test 'borrowers.updated_on should have been set to now on creating'
# at t/db_dependent/Patrons.t line 74.
# got: '2017-08-10T20:53:03'
# expected: '2017-08-10T20:53:04'
# Looks like you failed 1 test of 17.
[20:53:15] t/db_dependent/Patrons.t .....................................
The plan here is to compare the number of seconds between two dates.
If < 60 the dates are consired as identicals.
Signed-off-by: Josef Moravec <josef.moravec@gmail.com>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
--- /dev/null
+use Modern::Perl;
+use Test::More tests => 7;
+use t::lib::Dates;
+use Koha::DateUtils qw( dt_from_string );
+
+my $date_1 = '2017-01-01 01:00:00';
+my $date_2 = '2018-02-02 01:00:00';
+my $dt_1 = dt_from_string($date_1);
+my $dt_2 = dt_from_string($date_2);
+
+is( t::lib::Dates::compare( $dt_1, $dt_2 ), -1, '2017 is before 2018' );
+is( t::lib::Dates::compare( $dt_2, $dt_1 ), 1, '2018 is after 2017' );
+
+is( t::lib::Dates::compare( $date_1, $date_2 ), -1, '2017 is before 2018 (strings comparison)' );
+is( t::lib::Dates::compare( $date_2, $date_1 ), 1, '2018 is after 2017 (strings comparison)' );
+
+my $dt_3 = $dt_1->clone->subtract( seconds => 59 );
+is( t::lib::Dates::compare( $dt_1, $dt_3 ),
+ 0, 'If there is less than 1min, the dates are considered identicals' );
+is( t::lib::Dates::compare( $dt_3, $dt_1 ),
+ 0, 'If there is less than 1min, the dates are considered identicals' );
+
+$dt_1->set_time_zone('+0000');
+$dt_3 = $dt_1->clone->set_time_zone('+0400');
+
+is( t::lib::Dates::compare( $dt_1, $dt_3 ), -1, "Compare different timezones" );
use Koha::Database;
use Koha::DateUtils;
+use t::lib::Dates;
use t::lib::TestBuilder;
BEGIN {
my $b1_new = Koha::Patrons->find( $b1->borrowernumber() );
is( $b1->surname(), $b1_new->surname(), "Found matching patron" );
isnt( $b1_new->updated_on, undef, "borrowers.updated_on should be set" );
-is( dt_from_string($b1_new->updated_on), $now, "borrowers.updated_on should have been set to now on creating" );
+is( t::lib::Dates::compare( $b1_new->updated_on, $now), 0, "borrowers.updated_on should have been set to now on creating" );
my $b3_new = Koha::Patrons->find( $b3->borrowernumber() );
-is( dt_from_string($b3_new->updated_on), $three_days_ago, "borrowers.updated_on should have been kept to what we set on creating" );
+is( t::lib::Dates::compare( $b3_new->updated_on, $three_days_ago), 0, "borrowers.updated_on should have been kept to what we set on creating" );
$b3_new->set({ firstname => 'Some first name for Test 3' })->store();
$b3_new = Koha::Patrons->find( $b3->borrowernumber() );
-is( dt_from_string($b3_new->updated_on), dt_from_string, "borrowers.updated_on should have been set to now on updating" );
+is( t::lib::Dates::compare( $b3_new->updated_on, $now), 0, "borrowers.updated_on should have been set to now on updating" );
my @patrons = Koha::Patrons->search( { branchcode => $branchcode } );
is( @patrons, 3, "Found 3 patrons with Search" );
use Koha::Virtualshelfshares;
use Koha::Virtualshelfcontents;
+use t::lib::Dates;
use t::lib::TestBuilder;
my $builder = t::lib::TestBuilder->new;
is( $number_of_shelves, 1, '1 shelf should have been inserted' );
is( $shelf->allow_change_from_owner, 1, 'The default value for allow_change_from_owner should be 1' );
is( $shelf->allow_change_from_others, 0, 'The default value for allow_change_from_others should be 0' );
- is( output_pref($shelf->created_on), output_pref(dt_from_string), 'The creation time should have been set to today' );
+ is( t::lib::Dates::compare( $shelf->created_on, dt_from_string), 0, 'The creation time should have been set to today' );
# Test if creation date will not be overwritten by store
my $created = dt_from_string->subtract( hours => 1 );
my $retrieved_shelf = Koha::Virtualshelves->find( $shelf->shelfnumber );
is( $retrieved_shelf->shelfname, $shelf->shelfname, 'Find should correctly return the shelfname' );
- is( dt_from_string($retrieved_shelf->created_on), $created, 'Creation date is the same after update (Bug 18672)' );
+ is( t::lib::Dates::compare( $retrieved_shelf->created_on, $created), 0, 'Creation date is the same after update (Bug 18672)' );
# Insert with the same name
eval {
)->store;
$shelf = Koha::Virtualshelves->find( $shelf->shelfnumber );
- is( output_pref( dt_from_string $shelf->lastmodified ), output_pref($dt_yesterday), 'The lastmodified has been set to yesterday, will be useful for another test later' );
+ is( t::lib::Dates::compare( $shelf->lastmodified, $dt_yesterday), 0, 'The lastmodified has been set to yesterday, will be useful for another test later' );
my $content1 = $shelf->add_biblio( $biblio1->{biblionumber}, $patron1->{borrowernumber} );
is( ref($content1), 'Koha::Virtualshelfcontent', 'add_biblio to a shelf should return a Koha::Virtualshelfcontent object if inserted' );
$shelf = Koha::Virtualshelves->find( $shelf->shelfnumber );
- is( output_pref( dt_from_string( $shelf->lastmodified ) ), output_pref(dt_from_string), 'Adding a biblio to a shelf should update the lastmodified for the shelf' );
+ is( t::lib::Dates::compare( $shelf->lastmodified, dt_from_string), 0, 'Adding a biblio to a shelf should update the lastmodified for the shelf' );
my $content2 = $shelf->add_biblio( $biblio2->{biblionumber}, $patron1->{borrowernumber} );
$number_of_contents = Koha::Virtualshelfcontents->search->count;
is( $number_of_contents, 2, '2 biblio should have been inserted' );
--- /dev/null
+package t::lib::Dates;
+
+use Modern::Perl;
+use Test::More;
+use Koha::DateUtils;
+use DateTime;
+=head2 compare
+
+ compare( $got_dt, $expected_dt, $test_description );
+
+Will execute a test and compare the 2 dates given in parameters
+The date will be compared truncated to minutes
+
+=cut
+
+sub compare {
+ my ( $got, $expected, $description ) = @_;
+ my $dt_got = dt_from_string($got);
+ my $dt_expected = dt_from_string($expected);
+ $dt_got->set_time_zone('floating');
+ $dt_expected->set_time_zone('floating');
+ my $diff = $dt_got->epoch - $dt_expected->epoch;
+ if ( abs($diff) < 60 ) { return 0 }
+ return $diff > 0 ? 1 : -1;
+}
+
+1;