Bug 19423: Prevent DecreaseLoanHighHolds.t to fail randomly
[koha.git] / t / db_dependent / DecreaseLoanHighHolds.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 C4::Circulation;
21 use Koha::Database;
22 use Koha::Patron;
23 use Koha::Biblio;
24 use Koha::Item;
25 use Koha::Holds;
26 use Koha::Hold;
27 use t::lib::TestBuilder;
28 use t::lib::Mocks;
29
30 use Test::More tests => 17;
31
32 my $dbh    = C4::Context->dbh;
33 my $schema = Koha::Database->new()->schema();
34 my $builder = t::lib::TestBuilder->new;
35
36 # Start transaction
37 $dbh->{RaiseError} = 1;
38 $schema->storage->txn_begin();
39
40 $dbh->do('DELETE FROM issues');
41 $dbh->do('DELETE FROM issuingrules');
42 $dbh->do('DELETE FROM borrowers');
43 $dbh->do('DELETE FROM items');
44
45 my $library  = $builder->build( { source => 'Branch' } );
46 my $category = $builder->build( { source => 'Category' } );
47 my $itemtype = $builder->build( { source => 'Itemtype' } )->{itemtype};
48
49 # Set userenv
50 C4::Context->_new_userenv('xxx');
51 C4::Context->set_userenv( 0, 0, 0, 'firstname', 'surname', $library->{branchcode}, 'Midway Public Library', '', '', '' );
52 is( C4::Context->userenv->{branch}, $library->{branchcode}, 'userenv set' );
53
54 my $patron_category = $builder->build({ source => 'Category', value => { categorycode => 'NOT_X', category_type => 'P', enrolmentfee => 0 } });
55 my @patrons;
56 for my $i ( 1 .. 20 ) {
57     my $patron = Koha::Patron->new(
58         { cardnumber => $i, firstname => 'Kyle', surname => 'Hall', categorycode => $category->{categorycode}, branchcode => $library->{branchcode}, categorycode => $patron_category->{categorycode}, } )
59       ->store();
60     push( @patrons, $patron );
61 }
62
63 my $biblio = Koha::Biblio->new()->store();
64 my $biblioitem =
65   $schema->resultset('Biblioitem')->new( { biblionumber => $biblio->biblionumber } )->insert();
66
67 my @items;
68 for my $i ( 1 .. 10 ) {
69     my $item = Koha::Item->new(
70         {
71             biblionumber     => $biblio->id(),
72             biblioitemnumber => $biblioitem->id(),
73             barcode          => $i,
74             itype            => $itemtype
75         }
76     )->store();
77     push( @items, $item );
78 }
79
80 for my $i ( 0 .. 5 ) {
81     my $patron = $patrons[$i];
82     my $hold   = Koha::Hold->new(
83         {
84             borrowernumber => $patron->id,
85             biblionumber   => $biblio->id,
86             branchcode     => $library->{branchcode},
87         }
88     )->store();
89 }
90
91 $builder->build(
92     {
93         source => 'Issuingrule',
94         value => {
95             branchcode => '*',
96             categorycode => '*',
97             itemtype => '*',
98             issuelength => '14',
99             lengthunit => 'days',
100             reservesallowed => '99',
101         }
102     }
103 );
104
105 my $item   = pop(@items);
106 my $patron = pop(@patrons);
107
108 my $orig_due = C4::Circulation::CalcDateDue(
109     DateTime->now(time_zone => C4::Context->tz()),
110     $item->effective_itemtype,
111     $patron->branchcode,
112     $patron->unblessed
113 );
114
115 t::lib::Mocks::mock_preference( 'decreaseLoanHighHolds',               1 );
116 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsDuration',       1 );
117 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsValue',          1 );
118 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsControl',        'static' );
119 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsIgnoreStatuses', 'damaged,itemlost,notforloan,withdrawn' );
120
121 my $item_hr = { itemnumber => $item->id, biblionumber => $biblio->id, homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, barcode => $item->barcode };
122 my $patron_hr = { borrowernumber => $patron->id, branchcode => $library->{branchcode} };
123
124 my $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
125 is( $data->{exceeded},        1,          "Static mode should exceed threshold" );
126 is( $data->{outstanding},     6,          "Should have 5 outstanding holds" );
127 is( $data->{duration},        1,          "Should have duration of 1" );
128 is( ref( $data->{due_date} ), 'DateTime', "due_date should be a DateTime object" );
129
130 my $duedate = $data->{due_date};
131 is($duedate->hour, $orig_due->hour, 'New due hour is equal to original due hour.');
132 is($duedate->min, $orig_due->min, 'New due minute is equal to original due minute.');
133 is($duedate->sec, 0, 'New due date second is zero.');
134
135 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsControl', 'dynamic' );
136 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
137 is( $data->{exceeded}, 0, "Should not exceed threshold" );
138
139 for my $i ( 5 .. 10 ) {
140     my $patron = $patrons[$i];
141     my $hold   = Koha::Hold->new(
142         {
143             borrowernumber => $patron->id,
144             biblionumber   => $biblio->id,
145             branchcode     => $library->{branchcode},
146         }
147     )->store();
148 }
149
150 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
151 is( $data->{exceeded}, 1, "Should exceed threshold of 1" );
152
153 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsValue', 2 );
154 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
155 is( $data->{exceeded}, 0, "Should not exceed threshold of 2" );
156
157 my $unholdable = pop(@items);
158 $unholdable->damaged(-1);
159 $unholdable->store();
160
161 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
162 is( $data->{exceeded}, 1, "Should exceed threshold with one damaged item" );
163
164 $unholdable->damaged(0);
165 $unholdable->itemlost(-1);
166 $unholdable->store();
167
168 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
169 is( $data->{exceeded}, 1, "Should exceed threshold with one lost item" );
170
171 $unholdable->itemlost(0);
172 $unholdable->notforloan(-1);
173 $unholdable->store();
174
175 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
176 is( $data->{exceeded}, 1, "Should exceed threshold with one notforloan item" );
177
178 $unholdable->notforloan(0);
179 $unholdable->withdrawn(-1);
180 $unholdable->store();
181
182 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
183 is( $data->{exceeded}, 1, "Should exceed threshold with one withdrawn item" );
184
185 t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
186
187 my ( $un, $needsconfirmation ) = CanBookBeIssued( $patron_hr, $item->barcode );
188 ok( $needsconfirmation->{HIGHHOLDS}, "High holds checkout needs confirmation" );
189
190 ( undef, $needsconfirmation ) = CanBookBeIssued( $patron_hr, $item->barcode, undef, undef, undef, { override_high_holds => 1 } );
191 ok( !$needsconfirmation->{HIGHHOLDS}, "High holds checkout does not need confirmation" );
192
193 $schema->storage->txn_rollback();
194 1;