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;
25 use t::lib::TestBuilder;
26 use t::lib::Mocks qw( mock_preference );
28 my $builder = t::lib::TestBuilder->new();
29 subtest 'Tests for CanBookBeIssued related to dateexpiry' => sub {
33 subtest 'Tests for CalcDateDue related to dateexpiry' => sub {
38 sub can_book_be_issued {
39 my $item = $builder->build( { source => 'Item' } );
40 my $patron = $builder->build(
41 { source => 'Borrower',
42 value => { dateexpiry => '9999-12-31' }
45 $patron->{flags} = C4::Members::patronflags( $patron );
46 my $duration = gettimeofday();
47 my ( $issuingimpossible, $needsconfirmation ) = C4::Circulation::CanBookBeIssued( $patron, $item->{barcode} );
48 $duration = gettimeofday() - $duration;
49 cmp_ok $duration, '<', 1, "CanBookBeIssued should not be take more than 1s if the patron is expired";
50 is( not( exists $issuingimpossible->{EXPIRED} ), 1, 'The patron should not be considered as expired if dateexpiry is 9999-*' );
52 $item = $builder->build( { source => 'Item' } );
53 $patron = $builder->build(
54 { source => 'Borrower',
55 value => { dateexpiry => '0000-00-00' }
58 $patron->{flags} = C4::Members::patronflags( $patron );
59 ( $issuingimpossible, $needsconfirmation ) = C4::Circulation::CanBookBeIssued( $patron, $item->{barcode} );
60 is( $issuingimpossible->{EXPIRED}, 1, 'The patron should be considered as expired if dateexpiry is 0000-00-00' );
62 my $tomorrow = dt_from_string->add_duration( DateTime::Duration->new( days => 1 ) );
63 $item = $builder->build( { source => 'Item' } );
64 $patron = $builder->build(
65 { source => 'Borrower',
66 value => { dateexpiry => output_pref( { dt => $tomorrow, dateonly => 1, dateformat => 'sql' } ) },
69 $patron->{flags} = C4::Members::patronflags( $patron );
70 ( $issuingimpossible, $needsconfirmation ) = C4::Circulation::CanBookBeIssued( $patron, $item->{barcode} );
71 is( not( exists $issuingimpossible->{EXPIRED} ), 1, 'The patron should not be considered as expired if dateexpiry is tomorrow' );
76 t::lib::Mocks::mock_preference( 'ReturnBeforeExpiry', 1 );
78 # this triggers the compare between expiry and due date
80 my $patron = $builder->build( { source => 'Borrower' } );
81 my $item = $builder->build( { source => 'Item' } );
82 my $branch = $builder->build( { source => 'Branch' } );
83 my $today = dt_from_string();
85 # first test with empty expiry date
86 # note that this expiry date will never lead to an issue btw !!
87 $patron->{dateexpiry} = '0000-00-00';
88 my $d = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron );
89 is( ref $d eq "DateTime" && $d->mdy() =~ /^\d+/, 1, "CalcDateDue with expiry 0000-00-00" );
91 # second test expiry date==today
92 my $d2 = output_pref( { dt => $today, dateonly => 1, dateformat => 'sql' } );
93 $patron->{dateexpiry} = $d2;
94 $d = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron );
95 is( ref $d eq "DateTime" && DateTime->compare( $d->truncate( to => 'day' ), $today->truncate( to => 'day' ) ) == 0, 1, "CalcDateDue with expiry today" );
97 # third test expiry date tomorrow
98 my $dur = DateTime::Duration->new( days => 1 );
99 my $tomorrow = $today->clone->add_duration($dur);
100 $d2 = output_pref( { dt => $tomorrow, dateonly => 1, dateformat => 'sql' } );
101 $patron->{dateexpiry} = $d2;
102 $d = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron );
103 is( ref $d eq "DateTime" && $d->mdy() =~ /^\d+/, 1, "CalcDateDue with expiry tomorrow" );
105 # fourth test far future
106 $patron->{dateexpiry} = '9876-12-31';
108 $d = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron );
110 is( ref $d eq "DateTime" && $t2 - $t1 < 1, 1, "CalcDateDue with expiry in year 9876 in " . sprintf( "%6.4f", $t2 - $t1 ) . " seconds." );