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