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