Bug 20145: Do not insert 0000-00-00 in patron tests (and more)
[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 my $patron_category = $builder->build({ source => 'Category', value => { category_type => 'P', enrolmentfee => 0 } });
38
39 subtest 'Tests for CanBookBeIssued related to dateexpiry' => sub {
40     plan tests => 4;
41     can_book_be_issued();
42 };
43 subtest 'Tests for CalcDateDue related to dateexpiry' => sub {
44     plan tests => 4;
45     calc_date_due();
46 };
47
48 sub can_book_be_issued {
49     my $item    = $builder->build( { source => 'Item' } );
50     my $patron  = $builder->build_object(
51         {   class  => 'Koha::Patrons',
52             value  => {
53                 dateexpiry => '9999-12-31',
54                 categorycode => $patron_category->{categorycode},
55             }
56         }
57     );
58     my $duration = gettimeofday();
59     my ( $issuingimpossible, $needsconfirmation ) = C4::Circulation::CanBookBeIssued( $patron, $item->{barcode} );
60     $duration = gettimeofday() - $duration;
61     cmp_ok $duration, '<', 1, "CanBookBeIssued should not be take more than 1s if the patron is expired";
62     is( not( exists $issuingimpossible->{EXPIRED} ), 1, 'The patron should not be considered as expired if dateexpiry is 9999-*' );
63
64     $item = $builder->build( { source => 'Item' } );
65     $patron = $builder->build_object(
66         {   class  => 'Koha::Patrons',
67             value  => {
68                 dateexpiry => undef,
69                 categorycode => $patron_category->{categorycode},
70             }
71         }
72     );
73     ( $issuingimpossible, $needsconfirmation ) = C4::Circulation::CanBookBeIssued( $patron, $item->{barcode} );
74     is( not( exists $issuingimpossible->{EXPIRED} ), 1, 'The patron should not be considered as expired if dateexpiry is not set' );
75
76     my $tomorrow = dt_from_string->add_duration( DateTime::Duration->new( days => 1 ) );
77     $item = $builder->build( { source => 'Item' } );
78     $patron = $builder->build_object(
79         {   class  => 'Koha::Patrons',
80             value  => {
81                 dateexpiry => output_pref( { dt => $tomorrow, dateonly => 1, dateformat => 'sql' } ),
82                 categorycode => $patron_category->{categorycode},
83             },
84         }
85     );
86     ( $issuingimpossible, $needsconfirmation ) = C4::Circulation::CanBookBeIssued( $patron, $item->{barcode} );
87     is( not( exists $issuingimpossible->{EXPIRED} ), 1, 'The patron should not be considered as expired if dateexpiry is tomorrow' );
88
89 }
90
91 sub calc_date_due {
92     t::lib::Mocks::mock_preference( 'ReturnBeforeExpiry', 1 );
93
94     # this triggers the compare between expiry and due date
95
96     my $patron = $builder->build({
97         source => 'Borrower',
98         value  => {
99             categorycode => $patron_category->{categorycode},
100         }
101     });
102     my $item   = $builder->build( { source => 'Item' } );
103     my $branch = $builder->build( { source => 'Branch' } );
104     my $today  = dt_from_string();
105
106     # first test with empty expiry date
107     # note that this expiry date will never lead to an issue btw !!
108     $patron->{dateexpiry} = '0000-00-00';
109     my $d = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron );
110     is( ref $d eq "DateTime" && $d->mdy() =~ /^\d+/, 1, "CalcDateDue with expiry 0000-00-00" );
111
112     # second test expiry date==today
113     my $d2 = output_pref( { dt => $today, dateonly => 1, dateformat => 'sql' } );
114     $patron->{dateexpiry} = $d2;
115     $d = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron );
116     is( ref $d eq "DateTime" && DateTime->compare( $d->truncate( to => 'day' ), $today->truncate( to => 'day' ) ) == 0, 1, "CalcDateDue with expiry today" );
117
118     # third test expiry date tomorrow
119     my $dur = DateTime::Duration->new( days => 1 );
120     my $tomorrow = $today->clone->add_duration($dur);
121     $d2 = output_pref( { dt => $tomorrow, dateonly => 1, dateformat => 'sql' } );
122     $patron->{dateexpiry} = $d2;
123     $d = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron );
124     is( ref $d eq "DateTime" && $d->mdy() =~ /^\d+/, 1, "CalcDateDue with expiry tomorrow" );
125
126     # fourth test far future
127     $patron->{dateexpiry} = '9876-12-31';
128     my $t1 = time;
129     $d = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron );
130     my $t2 = time;
131     is( ref $d eq "DateTime" && $t2 - $t1 < 1, 1, "CalcDateDue with expiry in year 9876 in " . sprintf( "%6.4f", $t2 - $t1 ) . " seconds." );
132 }
133
134 $schema->storage->txn_rollback;
135