Bug 22001: Remove the RaiseError occurrences from tests
[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 Koha::CirculationRules;
29 use t::lib::TestBuilder;
30 use t::lib::Mocks;
31
32 use Test::More tests => 17;
33
34 my $dbh    = C4::Context->dbh;
35 my $schema = Koha::Database->new()->schema();
36 my $builder = t::lib::TestBuilder->new;
37
38 $schema->storage->txn_begin();
39
40 my $now_value       = DateTime->now();
41 my $mocked_datetime = Test::MockModule->new('DateTime');
42 $mocked_datetime->mock( 'now', sub { return $now_value->clone; } );
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 t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
49 is( C4::Context->userenv->{branch}, $library->{branchcode}, 'userenv set' );
50
51 my $patron_category = $builder->build({
52     source => 'Category',
53     value => {
54         category_type => 'P',
55         enrolmentfee => 0
56     }
57 });
58
59 my @patrons;
60 for my $i ( 1 .. 20 ) {
61     my $patron = Koha::Patron->new({
62         firstname => 'Kyle',
63         surname => 'Hall',
64         categorycode => $category->{categorycode},
65         branchcode => $library->{branchcode},
66         categorycode => $patron_category->{categorycode},
67     })->store();
68     push( @patrons, $patron );
69 }
70
71 my $biblio = $builder->build_sample_biblio();
72
73 my @items;
74 for my $i ( 1 .. 10 ) {
75     my $item = $builder->build_sample_item(
76         {
77             biblionumber     => $biblio->id(),
78             itype            => $itemtype
79         }
80     );
81     push( @items, $item );
82 }
83
84 for my $i ( 0 .. 5 ) {
85     my $patron = $patrons[$i];
86     my $hold   = Koha::Hold->new(
87         {
88             borrowernumber => $patron->id,
89             biblionumber   => $biblio->id,
90             branchcode     => $library->{branchcode},
91         }
92     )->store();
93 }
94
95 my $item   = pop(@items);
96 my $patron = pop(@patrons);
97
98 Koha::CirculationRules->set_rules(
99     {
100         branchcode   => undef,
101         categorycode => undef,
102         itemtype     => $item->itype,
103         rules        => {
104             issuelength     => '14',
105             lengthunit      => 'days',
106             reservesallowed => '99',
107             holds_per_record => '99',
108         }
109     }
110 );
111
112
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
118 );
119
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' );
125
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} };
128
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" );
134
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.');
139
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" );
143
144 for my $i ( 5 .. 10 ) {
145     my $patron = $patrons[$i];
146     my $hold   = Koha::Hold->new(
147         {
148             borrowernumber => $patron->id,
149             biblionumber   => $biblio->id,
150             branchcode     => $library->{branchcode},
151         }
152     )->store();
153 }
154
155 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
156 is( $data->{exceeded}, 1, "Should exceed threshold of 1" );
157
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" );
161
162 my $unholdable = pop(@items);
163 $unholdable->damaged(-1);
164 $unholdable->store();
165
166 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
167 is( $data->{exceeded}, 1, "Should exceed threshold with one damaged item" );
168
169 $unholdable->damaged(0);
170 $unholdable->itemlost(-1);
171 $unholdable->store();
172
173 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
174 is( $data->{exceeded}, 1, "Should exceed threshold with one lost item" );
175
176 $unholdable->itemlost(0);
177 $unholdable->notforloan(-1);
178 $unholdable->store();
179
180 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
181 is( $data->{exceeded}, 1, "Should exceed threshold with one notforloan item" );
182
183 $unholdable->notforloan(0);
184 $unholdable->withdrawn(-1);
185 $unholdable->store();
186
187 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
188 is( $data->{exceeded}, 1, "Should exceed threshold with one withdrawn item" );
189
190 t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
191
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" );
195
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" );
198
199 $schema->storage->txn_rollback();