Bug 17572: (followup) Remove test's hardcoded values
[koha.git] / t / db_dependent / Circulation / issue.t
blobcca050bcc5a0b5289657747181114865c33ff5b3
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;
20 use Test::More tests => 32;
22 use t::lib::Mocks;
23 use t::lib::TestBuilder;
25 use C4::Biblio;
26 use C4::Circulation;
27 use C4::Context;
28 use C4::Items;
29 use C4::Members;
30 use C4::Reserves;
31 use Koha::Database;
32 use Koha::DateUtils;
33 use Koha::Library;
35 use DateTime::Duration;
38 BEGIN {
39 use_ok('C4::Circulation');
41 can_ok(
42 'C4::Circulation',
43 qw(AddIssue
44 AddIssuingCharge
45 AddRenewal
46 AddReturn
47 GetBiblioIssues
48 GetIssuingCharges
49 GetIssuingRule
50 GetItemIssue
51 GetItemIssues
52 GetOpenIssue
53 GetRenewCount
54 GetUpcomingDueIssues
58 my $dbh = C4::Context->dbh;
59 my $schema = Koha::Database->schema;
60 #Start transaction
61 $schema->storage->txn_begin;
63 my $builder = t::lib::TestBuilder->new();
65 $dbh->do(q|DELETE FROM issues|);
66 $dbh->do(q|DELETE FROM items|);
67 $dbh->do(q|DELETE FROM borrowers|);
68 $dbh->do(q|DELETE FROM branches|);
69 $dbh->do(q|DELETE FROM categories|);
70 $dbh->do(q|DELETE FROM accountlines|);
71 $dbh->do(q|DELETE FROM issuingrules|);
73 # Generate sample datas
74 my $itemtype = $builder->build(
75 { source => 'Itemtype',
76 value => { notforloan => undef, rentalcharge => 0 }
78 )->{itemtype};
79 my $branchcode_1 = $builder->build({ source => 'Branch' })->{branchcode};
80 my $branchcode_2 = $builder->build({ source => 'Branch' })->{branchcode};
81 my $categorycode = $builder->build({
82 source => 'Category',
83 value => { enrolmentfee => undef }
84 })->{categorycode};
86 # Add Dates
87 my $dt_today = dt_from_string;
88 my $today = output_pref(
89 { dt => $dt_today,
90 dateformat => 'iso',
91 timeformat => '24hr',
92 dateonly => 1
96 my $dt_today2 = dt_from_string;
97 my $dur10 = DateTime::Duration->new( days => -10 );
98 $dt_today2->add_duration($dur10);
99 my $daysago10 = output_pref(
100 { dt => $dt_today2,
101 dateformat => 'iso',
102 timeformat => '24hr',
103 dateonly => 1
107 # Add biblio and item
108 my $record = MARC::Record->new();
109 $record->append_fields(
110 MARC::Field->new( '952', '0', '0', a => $branchcode_1 ) );
112 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '' );
114 my $barcode_1 = 'barcode_1';
115 my $barcode_2 = 'barcode_2';
116 my @sampleitem1 = C4::Items::AddItem(
118 barcode => $barcode_1,
119 itemcallnumber => 'callnumber1',
120 homebranch => $branchcode_1,
121 holdingbranch => $branchcode_1,
122 issue => 1,
123 reserve => 1,
124 itype => $itemtype
126 $biblionumber
128 my $item_id1 = $sampleitem1[2];
129 my @sampleitem2 = C4::Items::AddItem(
131 barcode => $barcode_2,
132 itemcallnumber => 'callnumber2',
133 homebranch => $branchcode_2,
134 holdingbranch => $branchcode_2,
135 notforloan => 1,
136 issue => 1,
137 itype => $itemtype
139 $biblionumber
141 my $item_id2 = $sampleitem2[2];
143 #Add borrower
144 my $borrower_id1 = C4::Members::AddMember(
145 firstname => 'firstname1',
146 surname => 'surname1 ',
147 categorycode => $categorycode,
148 branchcode => $branchcode_1
150 my $borrower_1 = C4::Members::GetMember(borrowernumber => $borrower_id1);
151 my $borrower_id2 = C4::Members::AddMember(
152 firstname => 'firstname2',
153 surname => 'surname2 ',
154 categorycode => $categorycode,
155 branchcode => $branchcode_2,
157 my $borrower_2 = C4::Members::GetMember(borrowernumber => $borrower_id2);
159 my @USERENV = (
160 $borrower_id1, 'test', 'MASTERTEST', 'firstname', $branchcode_1,
161 $branchcode_1, 'email@example.org'
165 C4::Context->_new_userenv('DUMMY_SESSION_ID');
166 C4::Context->set_userenv(@USERENV);
168 my $userenv = C4::Context->userenv
169 or BAIL_OUT("No userenv");
171 #Begin Tests
173 #Test AddIssue
174 my $query = " SELECT count(*) FROM issues";
175 my $sth = $dbh->prepare($query);
176 $sth->execute;
177 my $countissue = $sth -> fetchrow_array;
178 is ($countissue ,0, "there is no issue");
179 my $issue1 = C4::Circulation::AddIssue( $borrower_1, $barcode_1, $daysago10,0, $today, '' );
180 is( ref $issue1, 'Koha::Schema::Result::Issue',
181 'AddIssue returns a Koha::Schema::Result::Issue object' );
182 my $datedue1 = dt_from_string( $issue1->date_due() );
183 like(
184 $datedue1,
185 qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
186 "Koha::Schema::Result::Issue->date_due() returns a date"
188 my $issue_id1 = $dbh->last_insert_id( undef, undef, 'issues', undef );
190 my $issue2 = C4::Circulation::AddIssue( $borrower_1, 'nonexistent_barcode' );
191 is( $issue2, undef, "AddIssue returns undef if no datedue is specified" );
192 my $issue_id2 = $dbh->last_insert_id( undef, undef, 'issues', undef );
194 $sth->execute;
195 $countissue = $sth -> fetchrow_array;
196 is ($countissue,1,"1 issues have been added");
198 #Test AddIssuingCharge
199 $query = " SELECT count(*) FROM accountlines";
200 $sth = $dbh->prepare($query);
201 $sth->execute;
202 my $countaccount = $sth -> fetchrow_array;
203 is ($countaccount,0,"0 accountline exists");
204 is( C4::Circulation::AddIssuingCharge( $item_id1, $borrower_id1, 10 ),
205 1, "An issuing charge has been added" );
206 my $account_id = $dbh->last_insert_id( undef, undef, 'accountlines', undef );
207 $sth->execute;
208 $countaccount = $sth -> fetchrow_array;
209 is ($countaccount,1,"1 accountline has been added");
211 #Test AddRenewal
212 my $datedue3 =
213 AddRenewal( $borrower_id1, $item_id1, $branchcode_1,
214 $datedue1, $daysago10 );
215 like(
216 $datedue3,
217 qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
218 "AddRenewal returns a date"
221 #Test GetBiblioIssues
222 is( GetBiblioIssues(), undef, "GetBiblio Issues without parameters" );
224 #Test GetItemIssue
225 #FIXME : As the issues are not correctly added in the database, these tests don't work correctly
226 is(GetItemIssue,undef,"Without parameter GetItemIssue returns undef");
227 #is(GetItemIssue($item_id1),{},"Item1's issues");
229 #Test GetItemIssues
230 #FIXME: this routine currently doesn't work be
231 #is_deeply (GetItemIssues,{},"Without parameter, GetItemIssue returns all the issues");
233 #Test GetOpenIssue
234 is( GetOpenIssue(), undef, "Without parameter GetOpenIssue returns undef" );
235 is( GetOpenIssue(-1), undef,
236 "With wrong parameter GetOpenIssue returns undef" );
237 my $openissue = GetOpenIssue($borrower_id1, $item_id1);
239 my @renewcount;
240 #Test GetRenewCount
241 my $issue3 = C4::Circulation::AddIssue( $borrower_1, $barcode_1 );
242 #Without anything in DB
243 @renewcount = C4::Circulation::GetRenewCount();
244 is_deeply(
245 \@renewcount,
246 [ 0, undef, 0 ], # FIXME Need to be fixed
247 "Without issuing rules and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
249 @renewcount = C4::Circulation::GetRenewCount(-1);
250 is_deeply(
251 \@renewcount,
252 [ 0, undef, 0 ], # FIXME Need to be fixed
253 "Without issuing rules and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
255 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
256 is_deeply(
257 \@renewcount,
258 [ 2, undef, 0 ],
259 "Without issuing rules and with a valid parameter, renewcount = 2, renewsallowed = undef, renewsleft = 0"
262 #With something in DB
263 # Add a default rule: No renewal allowed
264 $dbh->do(q|
265 INSERT INTO issuingrules( categorycode, itemtype, branchcode, issuelength, renewalsallowed )
266 VALUES ( '*', '*', '*', 10, 0 )
268 @renewcount = C4::Circulation::GetRenewCount();
269 is_deeply(
270 \@renewcount,
271 [ 0, 0, 0 ],
272 "With issuing rules (renewal disallowed) and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
274 @renewcount = C4::Circulation::GetRenewCount(-1);
275 is_deeply(
276 \@renewcount,
277 [ 0, 0, 0 ],
278 "With issuing rules (renewal disallowed) and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
280 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
281 is_deeply(
282 \@renewcount,
283 [ 2, 0, 0 ],
284 "With issuing rules (renewal disallowed) and with a valid parameter, Getrenewcount returns renewcount = 2, renewsallowed = 0, renewsleft = 0"
287 # Add a default rule: renewal is allowed
288 $dbh->do(q|
289 UPDATE issuingrules SET renewalsallowed = 3
291 @renewcount = C4::Circulation::GetRenewCount();
292 is_deeply(
293 \@renewcount,
294 [ 0, 3, 3 ],
295 "With issuing rules (renewal allowed) and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = 3, renewsleft = 3"
297 @renewcount = C4::Circulation::GetRenewCount(-1);
298 is_deeply(
299 \@renewcount,
300 [ 0, 3, 3 ],
301 "With issuing rules (renewal allowed) and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = 3, renewsleft = 3"
303 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
304 is_deeply(
305 \@renewcount,
306 [ 2, 3, 1 ],
307 "With issuing rules (renewal allowed) and with a valid parameter, Getrenewcount of item1 returns 3 renews left"
310 AddRenewal( $borrower_id1, $item_id1, $branchcode_1,
311 $datedue3, $daysago10 );
312 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
313 is_deeply(
314 \@renewcount,
315 [ 3, 3, 0 ],
316 "With issuing rules (renewal allowed, 1 remaining) and with a valid parameter, Getrenewcount of item1 returns 0 renews left"
319 $dbh->do("DELETE FROM old_issues");
320 AddReturn($barcode_1);
321 my $return = $dbh->selectrow_hashref("SELECT DATE(returndate) AS return_date, CURRENT_DATE() AS today FROM old_issues LIMIT 1" );
322 ok( $return->{return_date} eq $return->{today}, "Item returned with no return date specified has todays date" );
324 $dbh->do("DELETE FROM old_issues");
325 C4::Circulation::AddIssue( $borrower_1, $barcode_1, $daysago10, 0, $today );
326 AddReturn($barcode_1, undef, undef, undef, '2014-04-01 23:42');
327 $return = $dbh->selectrow_hashref("SELECT * FROM old_issues LIMIT 1" );
328 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" );
330 my $itemnumber;
331 ($biblionumber, $biblioitemnumber, $itemnumber) = C4::Items::AddItem(
333 barcode => 'barcode_3',
334 itemcallnumber => 'callnumber3',
335 homebranch => $branchcode_1,
336 holdingbranch => $branchcode_1,
337 notforloan => 1,
338 itype => $itemtype
340 $biblionumber
343 t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', q{} );
344 AddReturn( 'barcode_3', $branchcode_1 );
345 my $item = GetItem( $itemnumber );
346 ok( $item->{notforloan} eq 1, 'UpdateNotForLoanStatusOnCheckin does not modify value when not enabled' );
348 t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', '1: 9' );
349 AddReturn( 'barcode_3', $branchcode_1 );
350 $item = GetItem( $itemnumber );
351 ok( $item->{notforloan} eq 9, q{UpdateNotForLoanStatusOnCheckin updates notforloan value from 1 to 9 with setting "1: 9"} );
353 AddReturn( 'barcode_3', $branchcode_1 );
354 $item = GetItem( $itemnumber );
355 ok( $item->{notforloan} eq 9, q{UpdateNotForLoanStatusOnCheckin does not update notforloan value from 9 with setting "1: 9"} );
357 # Bug 14640 - Cancel the hold on checking out if asked
358 my $reserve_id = AddReserve($branchcode_1, $borrower_id1, $biblionumber,
359 undef, 1, undef, undef, "a note", "a title", undef, '');
360 ok( $reserve_id, 'The reserve should have been inserted' );
361 AddIssue( $borrower_2, $barcode_1, dt_from_string, 'cancel' );
362 my $reserve = GetReserve( $reserve_id );
363 is( $reserve, undef, 'The reserve should have been correctly cancelled' );
365 #End transaction
366 $schema->storage->txn_rollback;