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