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