]> git.koha-community.org Git - koha.git/blob - t/db_dependent/Circulation/issue.t
Bug 21206: Replace C4::Items::GetItem
[koha.git] / t / db_dependent / Circulation / issue.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 Test::More tests => 32;
21 use DateTime::Duration;
22
23 use t::lib::Mocks;
24 use t::lib::TestBuilder;
25
26 use C4::Circulation;
27 use C4::Items;
28 use C4::Biblio;
29 use C4::Context;
30 use C4::Reserves;
31 use Koha::Checkouts;
32 use Koha::Database;
33 use Koha::DateUtils;
34 use Koha::Holds;
35 use Koha::Items;
36 use Koha::Library;
37 use Koha::Patrons;
38
39 BEGIN {
40     require_ok('C4::Circulation');
41 }
42
43 can_ok(
44     'C4::Circulation',
45     qw(AddIssue
46       AddIssuingCharge
47       AddRenewal
48       AddReturn
49       GetBiblioIssues
50       GetIssuingCharges
51       GetOpenIssue
52       GetRenewCount
53       GetUpcomingDueIssues
54       )
55 );
56
57 #Start transaction
58 my $schema = Koha::Database->schema;
59 $schema->storage->txn_begin;
60 my $dbh = C4::Context->dbh;
61
62 my $builder = t::lib::TestBuilder->new();
63
64 $dbh->do(q|DELETE FROM issues|);
65 $dbh->do(q|DELETE FROM items|);
66 $dbh->do(q|DELETE FROM borrowers|);
67 $dbh->do(q|DELETE FROM categories|);
68 $dbh->do(q|DELETE FROM accountlines|);
69 $dbh->do(q|DELETE FROM issuingrules|);
70 $dbh->do(q|DELETE FROM reserves|);
71 $dbh->do(q|DELETE FROM old_reserves|);
72 $dbh->do(q|DELETE FROM statistics|);
73
74 # Generate sample datas
75 my $itemtype = $builder->build(
76     {   source => 'Itemtype',
77         value  => { notforloan => undef, rentalcharge => 0 }
78     }
79 )->{itemtype};
80 my $branchcode_1 = $builder->build({ source => 'Branch' })->{branchcode};
81 my $branchcode_2 = $builder->build({ source => 'Branch' })->{branchcode};
82 my $branchcode_3 = $builder->build({ source => 'Branch' })->{branchcode};
83 my $categorycode = $builder->build({
84         source => 'Category',
85         value => { enrolmentfee => undef }
86     })->{categorycode};
87
88 # Add Dates
89 my $dt_today = dt_from_string;
90 my $today    = output_pref(
91     {   dt         => $dt_today,
92         dateformat => 'iso',
93         timeformat => '24hr',
94         dateonly   => 1
95     }
96 );
97
98 my $dt_today2 = dt_from_string;
99 my $dur10 = DateTime::Duration->new( days => -10 );
100 $dt_today2->add_duration($dur10);
101 my $daysago10 = output_pref(
102     {   dt         => $dt_today2,
103         dateformat => 'iso',
104         timeformat => '24hr',
105         dateonly   => 1
106     }
107 );
108
109 # Add biblio and item
110 my $record = MARC::Record->new();
111 $record->append_fields(
112     MARC::Field->new( '952', '0', '0', a => $branchcode_1 ) );
113
114 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '' );
115
116 my $barcode_1 = 'barcode_1';
117 my $barcode_2 = 'barcode_2';
118 my @sampleitem1 = C4::Items::AddItem(
119     {
120         barcode        => $barcode_1,
121         itemcallnumber => 'callnumber1',
122         homebranch     => $branchcode_1,
123         holdingbranch  => $branchcode_1,
124         issue          => 1,
125         reserve        => 1,
126         itype          => $itemtype
127     },
128     $biblionumber
129 );
130 my $item_id1    = $sampleitem1[2];
131 my @sampleitem2 = C4::Items::AddItem(
132     {
133         barcode        => $barcode_2,
134         itemcallnumber => 'callnumber2',
135         homebranch     => $branchcode_2,
136         holdingbranch  => $branchcode_2,
137         notforloan     => 1,
138         issue          => 1,
139         itype          => $itemtype
140     },
141     $biblionumber
142 );
143 my $item_id2 = $sampleitem2[2];
144
145 #Add borrower
146 my $borrower_id1 = Koha::Patron->new({
147     firstname    => 'firstname1',
148     surname      => 'surname1 ',
149     categorycode => $categorycode,
150     branchcode   => $branchcode_1
151 })->store->borrowernumber;
152 my $patron_1 = Koha::Patrons->find( $borrower_id1 );
153 my $borrower_1 = $patron_1->unblessed;
154 my $borrower_id2 = Koha::Patron->new({
155     firstname    => 'firstname2',
156     surname      => 'surname2 ',
157     categorycode => $categorycode,
158     branchcode   => $branchcode_2,
159 })->store->borrowernumber;
160 my $patron_2 = Koha::Patrons->find( $borrower_id2 );
161 my $borrower_2 = $patron_2->unblessed;
162
163 t::lib::Mocks::mock_userenv({ patron => $patron_1 });
164
165 #Begin Tests
166
167 #Test AddIssue
168 my $query = " SELECT count(*) FROM issues";
169 my $sth = $dbh->prepare($query);
170 $sth->execute;
171 my $countissue = $sth -> fetchrow_array;
172 is ($countissue ,0, "there is no issue");
173 my $issue1 = C4::Circulation::AddIssue( $borrower_1, $barcode_1, $daysago10,0, $today, '' );
174 is( ref $issue1, 'Koha::Checkout',
175        'AddIssue returns a Koha::Checkout object' );
176 my $datedue1 = dt_from_string( $issue1->date_due() );
177 like(
178     $datedue1,
179     qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
180     "Koha::Checkout->date_due() returns a date"
181 );
182 my $issue_id1 = $issue1->issue_id;
183
184 my $issue2 = C4::Circulation::AddIssue( $borrower_1, 'nonexistent_barcode' );
185 is( $issue2, undef, "AddIssue returns undef if no datedue is specified" );
186
187 $sth->execute;
188 $countissue = $sth -> fetchrow_array;
189 is ($countissue,1,"1 issues have been added");
190
191 #Test AddIssuingCharge
192 $query = " SELECT count(*) FROM accountlines";
193 $sth = $dbh->prepare($query);
194 $sth->execute;
195 my $countaccount = $sth -> fetchrow_array;
196 is ($countaccount,0,"0 accountline exists");
197 my $checkout = Koha::Checkouts->find( $issue_id1 );
198 my $offset = C4::Circulation::AddIssuingCharge( $checkout, 10 );
199 is( ref( $offset ), 'Koha::Account::Offset', "An issuing charge has been added" );
200 my $charge = Koha::Account::Lines->find( $offset->debit_id );
201 is( $charge->issue_id, $issue_id1, 'Issue id is set correctly for issuing charge' );
202 my $account_id = $dbh->last_insert_id( undef, undef, 'accountlines', undef );
203 $sth->execute;
204 $countaccount = $sth -> fetchrow_array;
205 is ($countaccount,1,"1 accountline has been added");
206
207 # Test AddRenewal
208
209 my $se = Test::MockModule->new( 'C4::Context' );
210 $se->mock( 'interface', sub {return 'intranet'});
211
212 # Let's renew this one at a different library for statistical purposes to test Bug 17781
213 # Mocking userenv with a different branchcode
214 t::lib::Mocks::mock_userenv({ patron => $patron_2, branchcode => $branchcode_3 });
215
216 my $datedue3 = AddRenewal( $borrower_id1, $item_id1, $branchcode_1, $datedue1, $daysago10 );
217
218 # Restoring the userenv with the original branchcode
219 t::lib::Mocks::mock_userenv({ patron => $patron_1});
220
221 like(
222     $datedue3,
223     qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
224     "AddRenewal returns a date"
225 );
226
227 my $stat = $dbh->selectrow_hashref("SELECT * FROM statistics WHERE type = 'renew' AND borrowernumber = ? AND itemnumber = ? AND branch = ?", undef, $borrower_id1, $item_id1, $branchcode_3 );
228 ok( $stat, "Bug 17781 - 'Improper branchcode set during renewal' still fixed" );
229
230 $se->mock( 'interface', sub {return 'opac'});
231
232 #Let's do an opac renewal - whatever branchcode we send should be used
233 my $opac_renew_issue = $builder->build({
234     source=>"Issue",
235     value=>{
236         date_due => '2017-01-01',
237         branch => $branchcode_1,
238         itype => $itemtype,
239         borrowernumber => $borrower_id1
240     }
241 });
242
243 my $datedue4 = AddRenewal( $opac_renew_issue->{borrowernumber}, $opac_renew_issue->{itemnumber}, "Stavromula", $datedue1, $daysago10 );
244
245 $stat = $dbh->selectrow_hashref("SELECT * FROM statistics WHERE type = 'renew' AND borrowernumber = ? AND itemnumber = ? AND branch = ?", undef,  $opac_renew_issue->{borrowernumber},  $opac_renew_issue->{itemnumber}, "Stavromula" );
246 ok( $stat, "Bug 18572 - 'Bug 18572 - OpacRenewalBranch is now respected" );
247
248
249
250 #Test GetBiblioIssues
251 is( GetBiblioIssues(), undef, "GetBiblio Issues without parameters" );
252
253 #Test GetOpenIssue
254 is( GetOpenIssue(), undef, "Without parameter GetOpenIssue returns undef" );
255 is( GetOpenIssue(-1), undef,
256     "With wrong parameter GetOpenIssue returns undef" );
257 my $openissue = GetOpenIssue($borrower_id1, $item_id1);
258
259 my @renewcount;
260 #Test GetRenewCount
261 my $issue3 = C4::Circulation::AddIssue( $borrower_1, $barcode_1 );
262 #Without anything in DB
263 @renewcount = C4::Circulation::GetRenewCount();
264 is_deeply(
265     \@renewcount,
266     [ 0, 0, 0 ], # FIXME Need to be fixed, see FIXME in GetRenewCount
267     "Without issuing rules and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
268 );
269 @renewcount = C4::Circulation::GetRenewCount(-1);
270 is_deeply(
271     \@renewcount,
272     [ 0, 0, 0 ], # FIXME Need to be fixed
273     "Without issuing rules and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
274 );
275 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
276 is_deeply(
277     \@renewcount,
278     [ 2, 0, 0 ],
279     "Without issuing rules and with a valid parameter, renewcount = 2, renewsallowed = undef, renewsleft = 0"
280 );
281
282 #With something in DB
283 # Add a default rule: No renewal allowed
284 $dbh->do(q|
285     INSERT INTO issuingrules( categorycode, itemtype, branchcode, issuelength, renewalsallowed )
286     VALUES ( '*', '*', '*', 10, 0 )
287 |);
288 @renewcount = C4::Circulation::GetRenewCount();
289 is_deeply(
290     \@renewcount,
291     [ 0, 0, 0 ],
292     "With issuing rules (renewal disallowed) and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
293 );
294 @renewcount = C4::Circulation::GetRenewCount(-1);
295 is_deeply(
296     \@renewcount,
297     [ 0, 0, 0 ],
298     "With issuing rules (renewal disallowed) and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
299 );
300 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
301 is_deeply(
302     \@renewcount,
303     [ 2, 0, 0 ],
304     "With issuing rules (renewal disallowed) and with a valid parameter, Getrenewcount returns renewcount = 2, renewsallowed = 0, renewsleft = 0"
305 );
306
307 # Add a default rule: renewal is allowed
308 $dbh->do(q|
309     UPDATE issuingrules SET renewalsallowed = 3
310 |);
311 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
312 is_deeply(
313     \@renewcount,
314     [ 2, 3, 1 ],
315     "With issuing rules (renewal allowed) and with a valid parameter, Getrenewcount of item1 returns 3 renews left"
316 );
317
318 AddRenewal( $borrower_id1, $item_id1, $branchcode_1,
319     $datedue3, $daysago10 );
320 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
321 is_deeply(
322     \@renewcount,
323     [ 3, 3, 0 ],
324     "With issuing rules (renewal allowed, 1 remaining) and with a valid parameter, Getrenewcount of item1 returns 0 renews left"
325 );
326
327 $dbh->do("DELETE FROM old_issues");
328 AddReturn($barcode_1);
329 my $return = $dbh->selectrow_hashref("SELECT DATE(returndate) AS return_date, CURRENT_DATE() AS today FROM old_issues LIMIT 1" );
330 ok( $return->{return_date} eq $return->{today}, "Item returned with no return date specified has todays date" );
331
332 $dbh->do("DELETE FROM old_issues");
333 C4::Circulation::AddIssue( $borrower_1, $barcode_1, $daysago10, 0, $today );
334 AddReturn($barcode_1, undef, undef, undef, '2014-04-01 23:42');
335 $return = $dbh->selectrow_hashref("SELECT * FROM old_issues LIMIT 1" );
336 ok( $return->{returndate} eq '2014-04-01 23:42:00', "Item returned with a return date of '2014-04-01 23:42' has that return date" );
337
338 my $itemnumber;
339 ($biblionumber, $biblioitemnumber, $itemnumber) = C4::Items::AddItem(
340     {
341         barcode        => 'barcode_3',
342         itemcallnumber => 'callnumber3',
343         homebranch     => $branchcode_1,
344         holdingbranch  => $branchcode_1,
345         notforloan     => 1,
346         itype          => $itemtype
347     },
348     $biblionumber
349 );
350
351 t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', q{} );
352 AddReturn( 'barcode_3', $branchcode_1 );
353 my $item = Koha::Items->find( $itemnumber );
354 ok( $item->notforloan eq 1, 'UpdateNotForLoanStatusOnCheckin does not modify value when not enabled' );
355
356 t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', '1: 9' );
357 AddReturn( 'barcode_3', $branchcode_1 );
358 $item = Koha::Items->find( $itemnumber );
359 ok( $item->notforloan eq 9, q{UpdateNotForLoanStatusOnCheckin updates notforloan value from 1 to 9 with setting "1: 9"} );
360
361 AddReturn( 'barcode_3', $branchcode_1 );
362 $item = Koha::Items->find( $itemnumber );
363 ok( $item->notforloan eq 9, q{UpdateNotForLoanStatusOnCheckin does not update notforloan value from 9 with setting "1: 9"} );
364
365 # Bug 14640 - Cancel the hold on checking out if asked
366 my $reserve_id = AddReserve($branchcode_1, $borrower_id1, $biblionumber,
367     undef,  1, undef, undef, "a note", "a title", undef, '');
368 ok( $reserve_id, 'The reserve should have been inserted' );
369 AddIssue( $borrower_2, $barcode_1, dt_from_string, 'cancel' );
370 my $hold = Koha::Holds->find( $reserve_id );
371 is( $hold, undef, 'The reserve should have been correctly cancelled' );
372
373 #End transaction
374 $schema->storage->txn_rollback;