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