Bug 19776: Fix random failures (category_type vs categorycode='X')
[koha.git] / t / db_dependent / Circulation / dateexpiry.t
blobc01d9aa333e0f763d17b7d88ea81b82a00e75696
1 #!/usr/bin/perl
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>.
18 use Modern::Perl;
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;
27 use t::lib::TestBuilder;
28 use t::lib::Mocks qw( mock_preference );
30 my $schema = Koha::Database->new->schema;
31 $schema->storage->txn_begin;
33 my $builder = t::lib::TestBuilder->new();
35 $ENV{ DEBUG } = 0;
37 my $patron_category = $builder->build({ source => 'Category', value => { category_type => 'NOT_X', category_type => 'P', enrolmentfee => 0 } });
39 subtest 'Tests for CanBookBeIssued related to dateexpiry' => sub {
40 plan tests => 4;
41 can_book_be_issued();
43 subtest 'Tests for CalcDateDue related to dateexpiry' => sub {
44 plan tests => 4;
45 calc_date_due();
48 sub can_book_be_issued {
49 my $item = $builder->build( { source => 'Item' } );
50 my $patron = $builder->build(
51 { source => 'Borrower',
52 value => {
53 dateexpiry => '9999-12-31',
54 categorycode => $patron_category->{categorycode},
58 $patron->{flags} = C4::Members::patronflags( $patron );
59 my $duration = gettimeofday();
60 my ( $issuingimpossible, $needsconfirmation ) = C4::Circulation::CanBookBeIssued( $patron, $item->{barcode} );
61 $duration = gettimeofday() - $duration;
62 cmp_ok $duration, '<', 1, "CanBookBeIssued should not be take more than 1s if the patron is expired";
63 is( not( exists $issuingimpossible->{EXPIRED} ), 1, 'The patron should not be considered as expired if dateexpiry is 9999-*' );
65 $item = $builder->build( { source => 'Item' } );
66 $patron = $builder->build(
67 { source => 'Borrower',
68 value => {
69 dateexpiry => '0000-00-00',
70 categorycode => $patron_category->{categorycode},
74 $patron->{flags} = C4::Members::patronflags( $patron );
75 ( $issuingimpossible, $needsconfirmation ) = C4::Circulation::CanBookBeIssued( $patron, $item->{barcode} );
76 is( $issuingimpossible->{EXPIRED}, 1, 'The patron should be considered as expired if dateexpiry is 0000-00-00' );
78 my $tomorrow = dt_from_string->add_duration( DateTime::Duration->new( days => 1 ) );
79 $item = $builder->build( { source => 'Item' } );
80 $patron = $builder->build(
81 { source => 'Borrower',
82 value => {
83 dateexpiry => output_pref( { dt => $tomorrow, dateonly => 1, dateformat => 'sql' } ),
84 categorycode => $patron_category->{categorycode},
88 $patron->{flags} = C4::Members::patronflags( $patron );
89 ( $issuingimpossible, $needsconfirmation ) = C4::Circulation::CanBookBeIssued( $patron, $item->{barcode} );
90 is( not( exists $issuingimpossible->{EXPIRED} ), 1, 'The patron should not be considered as expired if dateexpiry is tomorrow' );
94 sub calc_date_due {
95 t::lib::Mocks::mock_preference( 'ReturnBeforeExpiry', 1 );
97 # this triggers the compare between expiry and due date
99 my $patron = $builder->build({
100 source => 'Borrower',
101 value => {
102 categorycode => $patron_category->{categorycode},
105 my $item = $builder->build( { source => 'Item' } );
106 my $branch = $builder->build( { source => 'Branch' } );
107 my $today = dt_from_string();
109 # first test with empty expiry date
110 # note that this expiry date will never lead to an issue btw !!
111 $patron->{dateexpiry} = '0000-00-00';
112 my $d = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron );
113 is( ref $d eq "DateTime" && $d->mdy() =~ /^\d+/, 1, "CalcDateDue with expiry 0000-00-00" );
115 # second test expiry date==today
116 my $d2 = output_pref( { dt => $today, dateonly => 1, dateformat => 'sql' } );
117 $patron->{dateexpiry} = $d2;
118 $d = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron );
119 is( ref $d eq "DateTime" && DateTime->compare( $d->truncate( to => 'day' ), $today->truncate( to => 'day' ) ) == 0, 1, "CalcDateDue with expiry today" );
121 # third test expiry date tomorrow
122 my $dur = DateTime::Duration->new( days => 1 );
123 my $tomorrow = $today->clone->add_duration($dur);
124 $d2 = output_pref( { dt => $tomorrow, dateonly => 1, dateformat => 'sql' } );
125 $patron->{dateexpiry} = $d2;
126 $d = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron );
127 is( ref $d eq "DateTime" && $d->mdy() =~ /^\d+/, 1, "CalcDateDue with expiry tomorrow" );
129 # fourth test far future
130 $patron->{dateexpiry} = '9876-12-31';
131 my $t1 = time;
132 $d = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron );
133 my $t2 = time;
134 is( ref $d eq "DateTime" && $t2 - $t1 < 1, 1, "CalcDateDue with expiry in year 9876 in " . sprintf( "%6.4f", $t2 - $t1 ) . " seconds." );
137 $schema->storage->txn_rollback;