Bug 14694 [QA Followup] - Fix typos
[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 DateTime;
21
22 use C4::Circulation;
23 use Koha::Database;
24 use Koha::Borrower;
25 use Koha::Biblio;
26 use Koha::Item;
27 use Koha::Holds;
28 use Koha::Hold;
29
30 use Test::More tests => 12;
31
32 my $dbh    = C4::Context->dbh;
33 my $schema = Koha::Database->new()->schema();
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 # Set userenv
45 C4::Context->_new_userenv('xxx');
46 C4::Context->set_userenv( 0, 0, 0, 'firstname', 'surname', 'MPL', 'Midway Public Library', '', '', '' );
47 is( C4::Context->userenv->{branch}, 'MPL', 'userenv set' );
48
49 my @patrons;
50 for my $i ( 1 .. 20 ) {
51     my $patron = Koha::Borrower->new(
52         { cardnumber => $i, firstname => 'Kyle', surname => 'Hall', categorycode => 'S', branchcode => 'MPL' } )
53       ->store();
54     push( @patrons, $patron );
55 }
56
57 my $biblio = Koha::Biblio->new()->store();
58 my $biblioitem =
59   $schema->resultset('Biblioitem')->new( { biblionumber => $biblio->biblionumber } )->insert();
60
61 my @items;
62 for my $i ( 1 .. 10 ) {
63     my $item = Koha::Item->new( { biblionumber => $biblio->id(), biblioitemnumber => $biblioitem->id(), } )->store();
64     push( @items, $item );
65 }
66
67 for my $i ( 0 .. 5 ) {
68     my $patron = $patrons[$i];
69     my $hold   = Koha::Hold->new(
70         {
71             borrowernumber => $patron->id,
72             biblionumber   => $biblio->id,
73             branchcode     => 'MPL',
74         }
75     )->store();
76 }
77
78 $schema->resultset('Issuingrule')
79   ->new( { branchcode => '*', categorycode => '*', itemtype => '*', issuelength => '14', lengthunit => 'days', reservesallowed => '99' } )
80   ->insert();
81
82 my $item   = pop(@items);
83 my $patron = pop(@patrons);
84
85 C4::Context->set_preference( 'decreaseLoanHighHolds',               1 );
86 C4::Context->set_preference( 'decreaseLoanHighHoldsDuration',       1 );
87 C4::Context->set_preference( 'decreaseLoanHighHoldsValue',          1 );
88 C4::Context->set_preference( 'decreaseLoanHighHoldsControl',        'static' );
89 C4::Context->set_preference( 'decreaseLoanHighHoldsIgnoreStatuses', 'damaged,itemlost,notforloan,withdrawn' );
90
91 my $item_hr = { itemnumber => $item->id, biblionumber => $biblio->id, homebranch => 'MPL', holdingbranch => 'MPL' };
92 my $patron_hr = { borrower => $patron->id, branchcode => 'MPL' };
93
94 my $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
95 is( $data->{exceeded},        1,          "Static mode should exceed threshold" );
96 is( $data->{outstanding},     6,          "Should have 5 outstanding holds" );
97 is( $data->{duration},        1,          "Should have duration of 1" );
98 is( ref( $data->{due_date} ), 'DateTime', "due_date should be a DateTime object" );
99
100 C4::Context->set_preference( 'decreaseLoanHighHoldsControl', 'dynamic' );
101 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
102 is( $data->{exceeded}, 0, "Should not exceed threshold" );
103
104 for my $i ( 5 .. 10 ) {
105     my $patron = $patrons[$i];
106     my $hold   = Koha::Hold->new(
107         {
108             borrowernumber => $patron->id,
109             biblionumber   => $biblio->id,
110             branchcode     => 'MPL',
111         }
112     )->store();
113 }
114
115 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
116 is( $data->{exceeded}, 1, "Should exceed threshold of 1" );
117
118 C4::Context->set_preference( 'decreaseLoanHighHoldsValue', 2 );
119 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
120 is( $data->{exceeded}, 0, "Should not exceed threshold of 2" );
121
122 my $unholdable = pop(@items);
123 $unholdable->damaged(-1);
124 $unholdable->store();
125
126 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
127 is( $data->{exceeded}, 1, "Should exceed threshold with one damaged item" );
128
129 $unholdable->damaged(0);
130 $unholdable->itemlost(-1);
131 $unholdable->store();
132
133 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
134 is( $data->{exceeded}, 1, "Should exceed threshold with one lost item" );
135
136 $unholdable->itemlost(0);
137 $unholdable->notforloan(-1);
138 $unholdable->store();
139
140 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
141 is( $data->{exceeded}, 1, "Should exceed threshold with one notforloan item" );
142
143 $unholdable->notforloan(0);
144 $unholdable->withdrawn(-1);
145 $unholdable->store();
146
147 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
148 is( $data->{exceeded}, 1, "Should exceed threshold with one withdrawn item" );
149
150 $schema->storage->txn_rollback();
151 1;