Bug 9302: Use patron-title.inc
[koha.git] / t / db_dependent / DecreaseLoanHighHolds.t
blobb35dbfd14e41d3dbfde5af0edecf47846433b9d8
1 #!/usr/bin/perl
3 # This file is part of Koha.
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.
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.
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>.
18 use Modern::Perl;
19 use DateTime;
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;
31 use Test::More tests => 17;
33 my $dbh = C4::Context->dbh;
34 my $schema = Koha::Database->new()->schema();
35 my $builder = t::lib::TestBuilder->new;
37 # Start transaction
38 $dbh->{RaiseError} = 1;
39 $schema->storage->txn_begin();
41 $dbh->do('DELETE FROM issues');
42 $dbh->do('DELETE FROM issuingrules');
43 $dbh->do('DELETE FROM borrowers');
44 $dbh->do('DELETE FROM items');
46 my $now_value = DateTime->now();
47 my $mocked_datetime = Test::MockModule->new('DateTime');
48 $mocked_datetime->mock( 'now', sub { return $now_value; } );
50 my $library = $builder->build( { source => 'Branch' } );
51 my $category = $builder->build( { source => 'Category' } );
52 my $itemtype = $builder->build( { source => 'Itemtype' } )->{itemtype};
54 # Set userenv
55 C4::Context->_new_userenv('xxx');
56 C4::Context->set_userenv( 0, 0, 0, 'firstname', 'surname', $library->{branchcode}, 'Midway Public Library', '', '', '' );
57 is( C4::Context->userenv->{branch}, $library->{branchcode}, 'userenv set' );
59 my $patron_category = $builder->build({ source => 'Category', value => { category_type => 'P', enrolmentfee => 0 } });
60 my @patrons;
61 for my $i ( 1 .. 20 ) {
62 my $patron = Koha::Patron->new(
63 { cardnumber => $i, firstname => 'Kyle', surname => 'Hall', categorycode => $category->{categorycode}, branchcode => $library->{branchcode}, categorycode => $patron_category->{categorycode}, } )
64 ->store();
65 push( @patrons, $patron );
68 my $biblio = Koha::Biblio->new()->store();
69 my $biblioitem =
70 $schema->resultset('Biblioitem')->new( { biblionumber => $biblio->biblionumber } )->insert();
72 my @items;
73 for my $i ( 1 .. 10 ) {
74 my $item = Koha::Item->new(
76 biblionumber => $biblio->id(),
77 biblioitemnumber => $biblioitem->id(),
78 barcode => $i,
79 itype => $itemtype
81 )->store();
82 push( @items, $item );
85 for my $i ( 0 .. 5 ) {
86 my $patron = $patrons[$i];
87 my $hold = Koha::Hold->new(
89 borrowernumber => $patron->id,
90 biblionumber => $biblio->id,
91 branchcode => $library->{branchcode},
93 )->store();
96 $builder->build(
98 source => 'Issuingrule',
99 value => {
100 branchcode => '*',
101 categorycode => '*',
102 itemtype => '*',
103 issuelength => '14',
104 lengthunit => 'days',
105 reservesallowed => '99',
110 my $item = pop(@items);
111 my $patron = pop(@patrons);
113 my $orig_due = C4::Circulation::CalcDateDue(
114 DateTime->now(time_zone => C4::Context->tz()),
115 $item->effective_itemtype,
116 $patron->branchcode,
117 $patron->unblessed
120 t::lib::Mocks::mock_preference( 'decreaseLoanHighHolds', 1 );
121 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsDuration', 1 );
122 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsValue', 1 );
123 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsControl', 'static' );
124 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsIgnoreStatuses', 'damaged,itemlost,notforloan,withdrawn' );
126 my $item_hr = { itemnumber => $item->id, biblionumber => $biblio->id, homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, barcode => $item->barcode };
127 my $patron_hr = { borrowernumber => $patron->id, branchcode => $library->{branchcode} };
129 my $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
130 is( $data->{exceeded}, 1, "Static mode should exceed threshold" );
131 is( $data->{outstanding}, 6, "Should have 5 outstanding holds" );
132 is( $data->{duration}, 1, "Should have duration of 1" );
133 is( ref( $data->{due_date} ), 'DateTime', "due_date should be a DateTime object" );
135 my $duedate = $data->{due_date};
136 is($duedate->hour, $orig_due->hour, 'New due hour is equal to original due hour.');
137 is($duedate->min, $orig_due->min, 'New due minute is equal to original due minute.');
138 is($duedate->sec, 0, 'New due date second is zero.');
140 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsControl', 'dynamic' );
141 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
142 is( $data->{exceeded}, 0, "Should not exceed threshold" );
144 for my $i ( 5 .. 10 ) {
145 my $patron = $patrons[$i];
146 my $hold = Koha::Hold->new(
148 borrowernumber => $patron->id,
149 biblionumber => $biblio->id,
150 branchcode => $library->{branchcode},
152 )->store();
155 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
156 is( $data->{exceeded}, 1, "Should exceed threshold of 1" );
158 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsValue', 2 );
159 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
160 is( $data->{exceeded}, 0, "Should not exceed threshold of 2" );
162 my $unholdable = pop(@items);
163 $unholdable->damaged(-1);
164 $unholdable->store();
166 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
167 is( $data->{exceeded}, 1, "Should exceed threshold with one damaged item" );
169 $unholdable->damaged(0);
170 $unholdable->itemlost(-1);
171 $unholdable->store();
173 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
174 is( $data->{exceeded}, 1, "Should exceed threshold with one lost item" );
176 $unholdable->itemlost(0);
177 $unholdable->notforloan(-1);
178 $unholdable->store();
180 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
181 is( $data->{exceeded}, 1, "Should exceed threshold with one notforloan item" );
183 $unholdable->notforloan(0);
184 $unholdable->withdrawn(-1);
185 $unholdable->store();
187 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
188 is( $data->{exceeded}, 1, "Should exceed threshold with one withdrawn item" );
190 t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
192 my $patron_object = Koha::Patrons->find( $patron_hr->{borrowernumber} );
193 my ( undef, $needsconfirmation ) = CanBookBeIssued( $patron_object, $item->barcode );
194 ok( $needsconfirmation->{HIGHHOLDS}, "High holds checkout needs confirmation" );
196 ( undef, $needsconfirmation ) = CanBookBeIssued( $patron_object, $item->barcode, undef, undef, undef, { override_high_holds => 1 } );
197 ok( !$needsconfirmation->{HIGHHOLDS}, "High holds checkout does not need confirmation" );
199 $schema->storage->txn_rollback();