From 7abf1d9a184dd7cc2805f05e921a883871092b33 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Thu, 9 Jul 2015 15:35:53 +0200 Subject: [PATCH] Bug 14494: Add some unit tests too for CalcDateDue The second patch of this report made some changes to CalcDateDue. We are adding some unit tests here. See the commments on the third patch too. Test plan: Run t/db_dependent/Circulation_dateexpiry.t Signed-off-by: Jonathan Druart Amended patch: Set the number of tests for the second subtest (was commented) and perltidy the second block. Signed-off-by: Kyle M Hall Signed-off-by: Tomas Cohen Arazi (cherry picked from commit 1a8962f306d2d2f0c7789c649aa615192ccd5ca1) Signed-off-by: Chris Cormack --- t/db_dependent/Circulation_dateexpiry.t | 53 ++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/t/db_dependent/Circulation_dateexpiry.t b/t/db_dependent/Circulation_dateexpiry.t index 0c4d728d35..dfa4e4db15 100644 --- a/t/db_dependent/Circulation_dateexpiry.t +++ b/t/db_dependent/Circulation_dateexpiry.t @@ -18,19 +18,24 @@ use Modern::Perl; use DateTime; -use Time::HiRes qw/gettimeofday/; +use Time::HiRes qw/gettimeofday time/; +use Test::More tests => 2; use C4::Members; use Koha::DateUtils; use t::lib::TestBuilder; -use Test::More tests => 1; +use t::lib::Mocks qw( mock_preference ); +my $builder = t::lib::TestBuilder->new(); subtest 'Tests for CanBookBeIssued related to dateexpiry' => sub { plan tests => 4; - date_expiry(); + can_book_be_issued(); +}; +subtest 'Tests for CalcDateDue related to dateexpiry' => sub { + plan tests => 4; + calc_date_due(); }; -sub date_expiry { - my $builder = t::lib::TestBuilder->new(); +sub can_book_be_issued { my $item = $builder->build( { source => 'Item' } ); my $patron = $builder->build( { source => 'Borrower', @@ -66,3 +71,41 @@ sub date_expiry { is( not( exists $issuingimpossible->{EXPIRED} ), 1, 'The patron should not be considered as expired if dateexpiry is tomorrow' ); } + +sub calc_date_due { + t::lib::Mocks::mock_preference( 'ReturnBeforeExpiry', 1 ); + + # this triggers the compare between expiry and due date + + my $patron = $builder->build( { source => 'Borrower' } ); + my $item = $builder->build( { source => 'Item' } ); + my $branch = $builder->build( { source => 'Branch' } ); + my $today = dt_from_string(); + + # first test with empty expiry date + # note that this expiry date will never lead to an issue btw !! + $patron->{dateexpiry} = '0000-00-00'; + my $d = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron ); + is( ref $d eq "DateTime" && $d->mdy() =~ /^\d+/, 1, "CalcDateDue with expiry 0000-00-00" ); + + # second test expiry date==today + my $d2 = output_pref( { dt => $today, dateonly => 1, dateformat => 'sql' } ); + $patron->{dateexpiry} = $d2; + $d = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron ); + is( ref $d eq "DateTime" && DateTime->compare( $d->truncate( to => 'day' ), $today->truncate( to => 'day' ) ) == 0, 1, "CalcDateDue with expiry today" ); + + # third test expiry date tomorrow + my $dur = DateTime::Duration->new( days => 1 ); + my $tomorrow = $today->clone->add_duration($dur); + $d2 = output_pref( { dt => $tomorrow, dateonly => 1, dateformat => 'sql' } ); + $patron->{dateexpiry} = $d2; + $d = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron ); + is( ref $d eq "DateTime" && $d->mdy() =~ /^\d+/, 1, "CalcDateDue with expiry tomorrow" ); + + # fourth test far future + $patron->{dateexpiry} = '9876-12-31'; + my $t1 = time; + $d = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron ); + my $t2 = time; + is( ref $d eq "DateTime" && $t2 - $t1 < 1, 1, "CalcDateDue with expiry in year 9876 in " . sprintf( "%6.4f", $t2 - $t1 ) . " seconds." ); +} -- 2.20.1