Bug 14310 [QA Followup] - Adapt existing code to use new methods
[koha.git] / t / db_dependent / Circulation_dateexpiry.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use DateTime;
21 use Time::HiRes qw/gettimeofday time/;
22 use Test::More tests => 2;
23 use C4::Members;
24 use Koha::DateUtils;
25 use Koha::Database;
26
27 use t::lib::TestBuilder;
28 use t::lib::Mocks qw( mock_preference );
29
30 my $schema  = Koha::Database->new->schema;
31 $schema->storage->txn_begin;
32
33 my $builder = t::lib::TestBuilder->new();
34
35 $ENV{ DEBUG } = 0;
36
37 subtest 'Tests for CanBookBeIssued related to dateexpiry' => sub {
38     plan tests => 4;
39     can_book_be_issued();
40 };
41 subtest 'Tests for CalcDateDue related to dateexpiry' => sub {
42     plan tests => 4;
43     calc_date_due();
44 };
45
46 sub can_book_be_issued {
47     my $item    = $builder->build( { source => 'Item' } );
48     my $patron  = $builder->build(
49         {   source => 'Borrower',
50             value  => { dateexpiry => '9999-12-31' }
51         }
52     );
53     $patron->{flags} = C4::Members::patronflags( $patron );
54     my $duration = gettimeofday();
55     my ( $issuingimpossible, $needsconfirmation ) = C4::Circulation::CanBookBeIssued( $patron, $item->{barcode} );
56     $duration = gettimeofday() - $duration;
57     cmp_ok $duration, '<', 1, "CanBookBeIssued should not be take more than 1s if the patron is expired";
58     is( not( exists $issuingimpossible->{EXPIRED} ), 1, 'The patron should not be considered as expired if dateexpiry is 9999-*' );
59
60     $item = $builder->build( { source => 'Item' } );
61     $patron = $builder->build(
62         {   source => 'Borrower',
63             value  => { dateexpiry => '0000-00-00' }
64         }
65     );
66     $patron->{flags} = C4::Members::patronflags( $patron );
67     ( $issuingimpossible, $needsconfirmation ) = C4::Circulation::CanBookBeIssued( $patron, $item->{barcode} );
68     is( $issuingimpossible->{EXPIRED}, 1, 'The patron should be considered as expired if dateexpiry is 0000-00-00' );
69
70     my $tomorrow = dt_from_string->add_duration( DateTime::Duration->new( days => 1 ) );
71     $item = $builder->build( { source => 'Item' } );
72     $patron = $builder->build(
73         {   source => 'Borrower',
74             value  => { dateexpiry => output_pref( { dt => $tomorrow, dateonly => 1, dateformat => 'sql' } ) },
75         }
76     );
77     $patron->{flags} = C4::Members::patronflags( $patron );
78     ( $issuingimpossible, $needsconfirmation ) = C4::Circulation::CanBookBeIssued( $patron, $item->{barcode} );
79     is( not( exists $issuingimpossible->{EXPIRED} ), 1, 'The patron should not be considered as expired if dateexpiry is tomorrow' );
80
81 }
82
83 sub calc_date_due {
84     t::lib::Mocks::mock_preference( 'ReturnBeforeExpiry', 1 );
85
86     # this triggers the compare between expiry and due date
87
88     my $patron = $builder->build( { source => 'Borrower' } );
89     my $item   = $builder->build( { source => 'Item' } );
90     my $branch = $builder->build( { source => 'Branch' } );
91     my $today  = dt_from_string();
92
93     # first test with empty expiry date
94     # note that this expiry date will never lead to an issue btw !!
95     $patron->{dateexpiry} = '0000-00-00';
96     my $d = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron );
97     is( ref $d eq "DateTime" && $d->mdy() =~ /^\d+/, 1, "CalcDateDue with expiry 0000-00-00" );
98
99     # second test expiry date==today
100     my $d2 = output_pref( { dt => $today, 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" && DateTime->compare( $d->truncate( to => 'day' ), $today->truncate( to => 'day' ) ) == 0, 1, "CalcDateDue with expiry today" );
104
105     # third test expiry date tomorrow
106     my $dur = DateTime::Duration->new( days => 1 );
107     my $tomorrow = $today->clone->add_duration($dur);
108     $d2 = output_pref( { dt => $tomorrow, dateonly => 1, dateformat => 'sql' } );
109     $patron->{dateexpiry} = $d2;
110     $d = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron );
111     is( ref $d eq "DateTime" && $d->mdy() =~ /^\d+/, 1, "CalcDateDue with expiry tomorrow" );
112
113     # fourth test far future
114     $patron->{dateexpiry} = '9876-12-31';
115     my $t1 = time;
116     $d = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron );
117     my $t2 = time;
118     is( ref $d eq "DateTime" && $t2 - $t1 < 1, 1, "CalcDateDue with expiry in year 9876 in " . sprintf( "%6.4f", $t2 - $t1 ) . " seconds." );
119 }
120
121 $schema->storage->txn_rollback;
122
123 1;