Bug 11580 - [QA Followup] Remove debug statements
[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 use C4::Calendar;
27
28 use t::lib::TestBuilder;
29 use t::lib::Mocks qw( mock_preference );
30
31 my $schema  = Koha::Database->new->schema;
32 $schema->storage->txn_begin;
33
34 my $builder = t::lib::TestBuilder->new();
35
36 $ENV{ DEBUG } = 0;
37
38 subtest 'Tests for CanBookBeIssued related to dateexpiry' => sub {
39     plan tests => 4;
40     can_book_be_issued();
41 };
42 subtest 'Tests for CalcDateDue related to dateexpiry' => sub {
43     plan tests => 5;
44     calc_date_due();
45 };
46
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' }
52         }
53     );
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-*' );
60
61     $item = $builder->build( { source => 'Item' } );
62     $patron = $builder->build(
63         {   source => 'Borrower',
64             value  => { dateexpiry => '0000-00-00' }
65         }
66     );
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' );
70
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' } ) },
76         }
77     );
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' );
81
82 }
83
84 sub calc_date_due {
85     t::lib::Mocks::mock_preference( 'ReturnBeforeExpiry', 1 );
86     t::lib::Mocks::mock_preference( 'useDaysMode', 'Days' );
87
88     # this triggers the compare between expiry and due date
89
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();
94
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" );
100
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" );
106
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" );
114
115     # fourth test far future
116     $patron->{dateexpiry} = '9876-12-31';
117     my $t1 = time;
118     $d = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron );
119     my $t2 = time;
120     is( ref $d eq "DateTime" && $t2 - $t1 < 1, 1, "CalcDateDue with expiry in year 9876 in " . sprintf( "%6.4f", $t2 - $t1 ) . " seconds." );
121
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(
127         day             => $d->day(),
128         month           => $d->month(),
129         year            => $d->year(),
130         title           =>'holidayTest',
131         description     => 'holidayDesc'
132     );
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 );
135     $d2->add(days => 1);
136     $d->truncate( to => 'day' );
137     $d2->truncate( to => 'day' );
138     is ( DateTime->compare( $d, $d2) == 0, 1, "no problem with closed days");
139 }
140
141 $schema->storage->txn_rollback;
142