Bug 19776: Fix random failures (category_type vs categorycode='X')
[koha.git] / t / db_dependent / DecreaseLoanHighHolds.t
blob52cd53606138eacce95ad176d11e12230acc4148
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 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;
30 use Test::More tests => 17;
32 my $dbh = C4::Context->dbh;
33 my $schema = Koha::Database->new()->schema();
34 my $builder = t::lib::TestBuilder->new;
36 # Start transaction
37 $dbh->{RaiseError} = 1;
38 $schema->storage->txn_begin();
40 $dbh->do('DELETE FROM issues');
41 $dbh->do('DELETE FROM issuingrules');
42 $dbh->do('DELETE FROM borrowers');
43 $dbh->do('DELETE FROM items');
45 my $library = $builder->build( { source => 'Branch' } );
46 my $category = $builder->build( { source => 'Category' } );
47 my $itemtype = $builder->build( { source => 'Itemtype' } )->{itemtype};
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' );
54 my $patron_category = $builder->build({ source => 'Category', value => { category_type => '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 );
63 my $biblio = Koha::Biblio->new()->store();
64 my $biblioitem =
65 $schema->resultset('Biblioitem')->new( { biblionumber => $biblio->biblionumber } )->insert();
67 my @items;
68 for my $i ( 1 .. 10 ) {
69 my $item = Koha::Item->new(
71 biblionumber => $biblio->id(),
72 biblioitemnumber => $biblioitem->id(),
73 barcode => $i,
74 itype => $itemtype
76 )->store();
77 push( @items, $item );
80 for my $i ( 0 .. 5 ) {
81 my $patron = $patrons[$i];
82 my $hold = Koha::Hold->new(
84 borrowernumber => $patron->id,
85 biblionumber => $biblio->id,
86 branchcode => $library->{branchcode},
88 )->store();
91 $builder->build(
93 source => 'Issuingrule',
94 value => {
95 branchcode => '*',
96 categorycode => '*',
97 itemtype => '*',
98 issuelength => '14',
99 lengthunit => 'days',
100 reservesallowed => '99',
105 my $item = pop(@items);
106 my $patron = pop(@patrons);
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
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' );
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} };
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" );
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.');
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" );
139 for my $i ( 5 .. 10 ) {
140 my $patron = $patrons[$i];
141 my $hold = Koha::Hold->new(
143 borrowernumber => $patron->id,
144 biblionumber => $biblio->id,
145 branchcode => $library->{branchcode},
147 )->store();
150 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
151 is( $data->{exceeded}, 1, "Should exceed threshold of 1" );
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" );
157 my $unholdable = pop(@items);
158 $unholdable->damaged(-1);
159 $unholdable->store();
161 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
162 is( $data->{exceeded}, 1, "Should exceed threshold with one damaged item" );
164 $unholdable->damaged(0);
165 $unholdable->itemlost(-1);
166 $unholdable->store();
168 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
169 is( $data->{exceeded}, 1, "Should exceed threshold with one lost item" );
171 $unholdable->itemlost(0);
172 $unholdable->notforloan(-1);
173 $unholdable->store();
175 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
176 is( $data->{exceeded}, 1, "Should exceed threshold with one notforloan item" );
178 $unholdable->notforloan(0);
179 $unholdable->withdrawn(-1);
180 $unholdable->store();
182 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
183 is( $data->{exceeded}, 1, "Should exceed threshold with one withdrawn item" );
185 t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
187 my ( $un, $needsconfirmation ) = CanBookBeIssued( $patron_hr, $item->barcode );
188 ok( $needsconfirmation->{HIGHHOLDS}, "High holds checkout needs confirmation" );
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" );
193 $schema->storage->txn_rollback();