d4faaa33173af973efb0ce36c5bfd7e8d2c7b95e
[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 @patrons;
55 for my $i ( 1 .. 20 ) {
56     my $patron = Koha::Patron->new(
57         { cardnumber => $i, firstname => 'Kyle', surname => 'Hall', categorycode => $category->{categorycode}, branchcode => $library->{branchcode} } )
58       ->store();
59     push( @patrons, $patron );
60 }
61
62 my $biblio = Koha::Biblio->new()->store();
63 my $biblioitem =
64   $schema->resultset('Biblioitem')->new( { biblionumber => $biblio->biblionumber } )->insert();
65
66 my @items;
67 for my $i ( 1 .. 10 ) {
68     my $item = Koha::Item->new(
69         {
70             biblionumber     => $biblio->id(),
71             biblioitemnumber => $biblioitem->id(),
72             barcode          => $i,
73             itype            => $itemtype
74         }
75     )->store();
76     push( @items, $item );
77 }
78
79 for my $i ( 0 .. 5 ) {
80     my $patron = $patrons[$i];
81     my $hold   = Koha::Hold->new(
82         {
83             borrowernumber => $patron->id,
84             biblionumber   => $biblio->id,
85             branchcode     => $library->{branchcode},
86         }
87     )->store();
88 }
89
90 $builder->build(
91     {
92         source => 'Issuingrule',
93         value => {
94             branchcode => '*',
95             categorycode => '*',
96             itemtype => '*',
97             issuelength => '14',
98             lengthunit => 'days',
99             reservesallowed => '99',
100         }
101     }
102 );
103
104 my $item   = pop(@items);
105 my $patron = pop(@patrons);
106
107 my $orig_due = C4::Circulation::CalcDateDue(
108     DateTime->now(time_zone => C4::Context->tz()),
109     $item->effective_itemtype,
110     $patron->branchcode,
111     $patron->unblessed
112 );
113
114 t::lib::Mocks::mock_preference( 'decreaseLoanHighHolds',               1 );
115 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsDuration',       1 );
116 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsValue',          1 );
117 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsControl',        'static' );
118 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsIgnoreStatuses', 'damaged,itemlost,notforloan,withdrawn' );
119
120 my $item_hr = { itemnumber => $item->id, biblionumber => $biblio->id, homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, barcode => $item->barcode };
121 my $patron_hr = { borrowernumber => $patron->id, branchcode => $library->{branchcode} };
122
123 my $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
124 is( $data->{exceeded},        1,          "Static mode should exceed threshold" );
125 is( $data->{outstanding},     6,          "Should have 5 outstanding holds" );
126 is( $data->{duration},        1,          "Should have duration of 1" );
127 is( ref( $data->{due_date} ), 'DateTime', "due_date should be a DateTime object" );
128
129 my $duedate = $data->{due_date};
130 is($duedate->hour, $orig_due->hour, 'New due hour is equal to original due hour.');
131 is($duedate->min, $orig_due->min, 'New due minute is equal to original due minute.');
132 is($duedate->sec, 0, 'New due date second is zero.');
133
134 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsControl', 'dynamic' );
135 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
136 is( $data->{exceeded}, 0, "Should not exceed threshold" );
137
138 for my $i ( 5 .. 10 ) {
139     my $patron = $patrons[$i];
140     my $hold   = Koha::Hold->new(
141         {
142             borrowernumber => $patron->id,
143             biblionumber   => $biblio->id,
144             branchcode     => $library->{branchcode},
145         }
146     )->store();
147 }
148
149 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
150 is( $data->{exceeded}, 1, "Should exceed threshold of 1" );
151
152 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsValue', 2 );
153 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
154 is( $data->{exceeded}, 0, "Should not exceed threshold of 2" );
155
156 my $unholdable = pop(@items);
157 $unholdable->damaged(-1);
158 $unholdable->store();
159
160 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
161 is( $data->{exceeded}, 1, "Should exceed threshold with one damaged item" );
162
163 $unholdable->damaged(0);
164 $unholdable->itemlost(-1);
165 $unholdable->store();
166
167 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
168 is( $data->{exceeded}, 1, "Should exceed threshold with one lost item" );
169
170 $unholdable->itemlost(0);
171 $unholdable->notforloan(-1);
172 $unholdable->store();
173
174 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
175 is( $data->{exceeded}, 1, "Should exceed threshold with one notforloan item" );
176
177 $unholdable->notforloan(0);
178 $unholdable->withdrawn(-1);
179 $unholdable->store();
180
181 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
182 is( $data->{exceeded}, 1, "Should exceed threshold with one withdrawn item" );
183
184 t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
185
186 my ( undef, $needsconfirmation ) = CanBookBeIssued( $patron_hr, $item->barcode );
187 ok( $needsconfirmation->{HIGHHOLDS}, "High holds checkout needs confirmation" );
188
189 ( undef, $needsconfirmation ) = CanBookBeIssued( $patron_hr, $item->barcode, undef, undef, undef, { override_high_holds => 1 } );
190 ok( !$needsconfirmation->{HIGHHOLDS}, "High holds checkout does not need confirmation" );
191
192 $schema->storage->txn_rollback();
193 1;