From abf3fc453b6629359262ca398bcca3976824e754 Mon Sep 17 00:00:00 2001 From: Alex Arnaud Date: Wed, 6 Sep 2017 15:36:15 +0000 Subject: [PATCH] Bug 11580 - Fix expiry calculation and rewrite tests --- C4/Circulation.pm | 6 ++++-- t/db_dependent/Circulation/CalcDateDue.t | 19 +++++++++++++++++-- t/db_dependent/Circulation/dateexpiry.t | 22 +--------------------- 3 files changed, 22 insertions(+), 25 deletions(-) diff --git a/C4/Circulation.pm b/C4/Circulation.pm index 75c3814..e4fee8f 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -3511,9 +3511,11 @@ sub CalcDateDue { } } if ( C4::Context->preference('useDaysMode') ne 'Days' ) { - # Don't return on a closed day my $calendar = Koha::Calendar->new( branchcode => $branch ); - $datedue = $calendar->prev_open_day( $datedue ); + if ( $calendar->is_holiday($datedue) ) { + # Don't return on a closed day + $datedue = $calendar->prev_open_day( $datedue ); + } } } diff --git a/t/db_dependent/Circulation/CalcDateDue.t b/t/db_dependent/Circulation/CalcDateDue.t index e533806..98efd48 100644 --- a/t/db_dependent/Circulation/CalcDateDue.t +++ b/t/db_dependent/Circulation/CalcDateDue.t @@ -2,12 +2,13 @@ use Modern::Perl; -use Test::More tests => 5; +use Test::More tests => 6; use Test::MockModule; use DBI; use DateTime; use t::lib::Mocks; use t::lib::TestBuilder; +use C4::Calendar; use_ok('C4::Circulation'); @@ -51,7 +52,21 @@ t::lib::Mocks::mock_preference('useDaysMode', 'noDays'); $borrower = {categorycode => 'B', dateexpiry => $dateexpiry}; $start_date = DateTime->new({year => 2013, month => 2, day => 9}); $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower ); -is($date, $dateexpiry . 'T23:59:00', 'date expiry'); +is($date, $dateexpiry . 'T23:59:00', 'date expiry with useDaysMode to noDays'); + +# Let's add a special holiday on 2013-01-01. With ReturnBeforeExpiry and +# useDaysMode different from 'Days', return should forward the dateexpiry. +my $calendar = C4::Calendar->new(branchcode => $branchcode); +$calendar->insert_single_holiday( + day => 1, + month => 1, + year => 2013, + title =>'holidayTest', + description => 'holidayDesc' +); +$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower ); +is($date, '2012-12-31T23:59:00', 'date expiry should be 2013-01-01 -1 day'); + $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower, 1 ); diff --git a/t/db_dependent/Circulation/dateexpiry.t b/t/db_dependent/Circulation/dateexpiry.t index 4ed3505..dbb7e0b 100644 --- a/t/db_dependent/Circulation/dateexpiry.t +++ b/t/db_dependent/Circulation/dateexpiry.t @@ -23,7 +23,6 @@ use Test::More tests => 2; use C4::Members; use Koha::DateUtils; use Koha::Database; -use C4::Calendar; use t::lib::TestBuilder; use t::lib::Mocks qw( mock_preference ); @@ -40,7 +39,7 @@ subtest 'Tests for CanBookBeIssued related to dateexpiry' => sub { can_book_be_issued(); }; subtest 'Tests for CalcDateDue related to dateexpiry' => sub { - plan tests => 5; + plan tests => 4; calc_date_due(); }; @@ -83,7 +82,6 @@ sub can_book_be_issued { sub calc_date_due { t::lib::Mocks::mock_preference( 'ReturnBeforeExpiry', 1 ); - t::lib::Mocks::mock_preference( 'useDaysMode', 'Days' ); # this triggers the compare between expiry and due date @@ -118,24 +116,6 @@ sub calc_date_due { $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." ); - - # fifth test takes account of closed days - $d = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron ); - t::lib::Mocks::mock_preference( 'useDaysMode', 'Datedue' ); - my $calendar = C4::Calendar->new(branchcode => $branch->{branchcode}); - $calendar->insert_single_holiday( - day => $d->day(), - month => $d->month(), - year => $d->year(), - title =>'holidayTest', - description => 'holidayDesc' - ); - $calendar->delete_holiday(weekday => $d->day_of_week() - 1, day => $d->day()-1, month =>$d->month(), year=>$d->year() ); - $d2 = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron ); - $d2->add(days => 1); - $d->truncate( to => 'day' ); - $d2->truncate( to => 'day' ); - is ( DateTime->compare( $d, $d2) == 0, 1, "no problem with closed days"); } $schema->storage->txn_rollback; -- 2.7.4