3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use Time::HiRes qw/gettimeofday time/;
22 use Test::More tests => 2;
28 use t::lib::TestBuilder;
29 use t::lib::Mocks qw( mock_preference );
31 my $schema = Koha::Database->new->schema;
32 $schema->storage->txn_begin;
34 my $builder = t::lib::TestBuilder->new();
38 subtest 'Tests for CanBookBeIssued related to dateexpiry' => sub {
42 subtest 'Tests for CalcDateDue related to dateexpiry' => sub {
47 sub can_book_be_issued {
48 my $item = $builder->build( { source => 'Item' } );
49 my $patron = $builder->build(
50 { source => 'Borrower',
51 value => { dateexpiry => '9999-12-31' }
54 $patron->{flags} = C4::Members::patronflags( $patron );
55 my $duration = gettimeofday();
56 my ( $issuingimpossible, $needsconfirmation ) = C4::Circulation::CanBookBeIssued( $patron, $item->{barcode} );
57 $duration = gettimeofday() - $duration;
58 cmp_ok $duration, '<', 1, "CanBookBeIssued should not be take more than 1s if the patron is expired";
59 is( not( exists $issuingimpossible->{EXPIRED} ), 1, 'The patron should not be considered as expired if dateexpiry is 9999-*' );
61 $item = $builder->build( { source => 'Item' } );
62 $patron = $builder->build(
63 { source => 'Borrower',
64 value => { dateexpiry => '0000-00-00' }
67 $patron->{flags} = C4::Members::patronflags( $patron );
68 ( $issuingimpossible, $needsconfirmation ) = C4::Circulation::CanBookBeIssued( $patron, $item->{barcode} );
69 is( $issuingimpossible->{EXPIRED}, 1, 'The patron should be considered as expired if dateexpiry is 0000-00-00' );
71 my $tomorrow = dt_from_string->add_duration( DateTime::Duration->new( days => 1 ) );
72 $item = $builder->build( { source => 'Item' } );
73 $patron = $builder->build(
74 { source => 'Borrower',
75 value => { dateexpiry => output_pref( { dt => $tomorrow, dateonly => 1, dateformat => 'sql' } ) },
78 $patron->{flags} = C4::Members::patronflags( $patron );
79 ( $issuingimpossible, $needsconfirmation ) = C4::Circulation::CanBookBeIssued( $patron, $item->{barcode} );
80 is( not( exists $issuingimpossible->{EXPIRED} ), 1, 'The patron should not be considered as expired if dateexpiry is tomorrow' );
85 t::lib::Mocks::mock_preference( 'ReturnBeforeExpiry', 1 );
86 t::lib::Mocks::mock_preference( 'useDaysMode', 'Days' );
88 # this triggers the compare between expiry and due date
90 my $patron = $builder->build( { source => 'Borrower' } );
91 my $item = $builder->build( { source => 'Item' } );
92 my $branch = $builder->build( { source => 'Branch' } );
93 my $today = dt_from_string();
95 # first test with empty expiry date
96 # note that this expiry date will never lead to an issue btw !!
97 $patron->{dateexpiry} = '0000-00-00';
98 my $d = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron );
99 is( ref $d eq "DateTime" && $d->mdy() =~ /^\d+/, 1, "CalcDateDue with expiry 0000-00-00" );
101 # second test expiry date==today
102 my $d2 = output_pref( { dt => $today, dateonly => 1, dateformat => 'sql' } );
103 $patron->{dateexpiry} = $d2;
104 $d = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron );
105 is( ref $d eq "DateTime" && DateTime->compare( $d->truncate( to => 'day' ), $today->truncate( to => 'day' ) ) == 0, 1, "CalcDateDue with expiry today" );
107 # third test expiry date tomorrow
108 my $dur = DateTime::Duration->new( days => 1 );
109 my $tomorrow = $today->clone->add_duration($dur);
110 $d2 = output_pref( { dt => $tomorrow, dateonly => 1, dateformat => 'sql' } );
111 $patron->{dateexpiry} = $d2;
112 $d = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron );
113 is( ref $d eq "DateTime" && $d->mdy() =~ /^\d+/, 1, "CalcDateDue with expiry tomorrow" );
115 # fourth test far future
116 $patron->{dateexpiry} = '9876-12-31';
118 $d = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron );
120 is( ref $d eq "DateTime" && $t2 - $t1 < 1, 1, "CalcDateDue with expiry in year 9876 in " . sprintf( "%6.4f", $t2 - $t1 ) . " seconds." );
122 # fifth test takes account of closed days
123 $d = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron );
124 t::lib::Mocks::mock_preference( 'useDaysMode', 'Datedue' );
125 my $calendar = C4::Calendar->new(branchcode => $branch->{branchcode});
126 $calendar->insert_single_holiday(
128 month => $d->month(),
130 title =>'holidayTest',
131 description => 'holidayDesc'
133 $calendar->delete_holiday(weekday => $d->day_of_week() - 1, day => $d->day()-1, month =>$d->month(), year=>$d->year() );
134 $d2 = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron );
136 $d->truncate( to => 'day' );
137 $d2->truncate( to => 'day' );
138 is ( DateTime->compare( $d, $d2) == 0, 1, "no problem with closed days");
141 $schema->storage->txn_rollback;