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>.
20 use Test::More tests => 32;
23 use t::lib::TestBuilder;
35 use DateTime::Duration;
39 use_ok('C4::Circulation');
58 my $dbh = C4::Context->dbh;
59 my $schema = Koha::Database->schema;
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 }
82 my $dt_today = dt_from_string;
83 my $today = output_pref({ dt => $dt_today, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
85 my $dt_today2 = dt_from_string;
86 my $dur10 = DateTime::Duration->new( days => -10 );
87 $dt_today2->add_duration($dur10);
88 my $daysago10 = output_pref({ dt => $dt_today2, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
90 #Add branch and category
93 branchname => 'Sample Branch',
94 branchaddress1 => 'sample adr1',
95 branchaddress2 => 'sample adr2',
96 branchaddress3 => 'sample adr3',
97 branchzip => 'sample zip',
98 branchcity => 'sample city',
99 branchstate => 'sample state',
100 branchcountry => 'sample country',
101 branchphone => 'sample phone',
102 branchfax => 'sample fax',
103 branchemail => 'sample email',
104 branchurl => 'sample url',
105 branchip => 'sample ip',
106 branchprinter => undef,
107 opac_info => 'sample opac',
109 my $samplebranch2 = {
111 branchname => 'Sample Branch2',
112 branchaddress1 => 'sample adr1_2',
113 branchaddress2 => 'sample adr2_2',
114 branchaddress3 => 'sample adr3_2',
115 branchzip => 'sample zip2',
116 branchcity => 'sample city2',
117 branchstate => 'sample state2',
118 branchcountry => 'sample country2',
119 branchphone => 'sample phone2',
120 branchfax => 'sample fax2',
121 branchemail => 'sample email2',
122 branchurl => 'sample url2',
123 branchip => 'sample ip2',
124 branchprinter => undef,
125 opac_info => 'sample opac2',
127 Koha::Library->new($samplebranch1)->store;
128 Koha::Library->new($samplebranch2)->store;
131 categorycode => 'CAT1',
132 description => 'Description1',
133 enrolmentperiod => 'Null',
134 enrolmentperioddate => 'Null',
135 dateofbirthrequired => 'Null',
138 enrolmentfee => 'Null',
139 overduenoticerequired => 'Null',
140 issuelimit => 'Null',
141 reservefee => 'Null',
143 category_type => 'Null'
146 "INSERT INTO categories (categorycode,description,enrolmentperiod,enrolmentperioddate,dateofbirthrequired ,finetype,bulk,enrolmentfee,overduenoticerequired,issuelimit ,reservefee ,hidelostitems ,category_type) VALUES( ?,?,?,?,?,?,?,?,?,?,?,?,?)";
149 $samplecat->{categorycode}, $samplecat->{description},
150 $samplecat->{enrolmentperiod}, $samplecat->{enrolmentperioddate},
151 $samplecat->{dateofbirthrequired}, $samplecat->{finetype},
152 $samplecat->{bulk}, $samplecat->{enrolmentfee},
153 $samplecat->{overduenoticerequired}, $samplecat->{issuelimit},
154 $samplecat->{reservefee}, $samplecat->{hidelostitems},
155 $samplecat->{category_type}
159 my $record = MARC::Record->new();
160 $record->append_fields(
161 MARC::Field->new( '952', '0', '0', a => $samplebranch1->{branchcode} ) );
162 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '' );
164 my $barcode_1 = 'barcode_1';
165 my $barcode_2 = 'barcode_2';
166 my @sampleitem1 = C4::Items::AddItem(
168 barcode => $barcode_1,
169 itemcallnumber => 'callnumber1',
170 homebranch => $samplebranch1->{branchcode},
171 holdingbranch => $samplebranch1->{branchcode},
178 my $item_id1 = $sampleitem1[2];
179 my @sampleitem2 = C4::Items::AddItem(
181 barcode => $barcode_2,
182 itemcallnumber => 'callnumber2',
183 homebranch => $samplebranch2->{branchcode},
184 holdingbranch => $samplebranch2->{branchcode},
191 my $item_id2 = $sampleitem2[2];
194 my $borrower_id1 = C4::Members::AddMember(
195 firstname => 'firstname1',
196 surname => 'surname1 ',
197 categorycode => $samplecat->{categorycode},
198 branchcode => $samplebranch1->{branchcode},
200 my $borrower_1 = C4::Members::GetMember(borrowernumber => $borrower_id1);
201 my $borrower_id2 = C4::Members::AddMember(
202 firstname => 'firstname2',
203 surname => 'surname2 ',
204 categorycode => $samplecat->{categorycode},
205 branchcode => $samplebranch2->{branchcode},
207 my $borrower_2 = C4::Members::GetMember(borrowernumber => $borrower_id2);
209 # NEED TO BE FIXED !!!
210 # The first parameter for set_userenv is the class ref
211 #my @USERENV = ( $borrower_id1, 'test', 'MASTERTEST', 'firstname', 'username', 'CPL', 'CPL', 'email@example.org' );
212 my @USERENV = ( $borrower_id1, 'test', 'MASTERTEST', 'firstname', 'CPL', 'CPL', 'email@example.org' );
214 C4::Context->_new_userenv('DUMMY_SESSION_ID');
215 C4::Context->set_userenv(@USERENV);
217 my $userenv = C4::Context->userenv
218 or BAIL_OUT("No userenv");
223 $query = " SELECT count(*) FROM issues";
224 my $sth = $dbh->prepare($query);
226 my $countissue = $sth -> fetchrow_array;
227 is ($countissue ,0, "there is no issue");
228 my $issue1 = C4::Circulation::AddIssue( $borrower_1, $barcode_1, $daysago10,0, $today, '' );
229 is( ref $issue1, 'Koha::Schema::Result::Issue',
230 'AddIssue returns a Koha::Schema::Result::Issue object' );
231 my $datedue1 = dt_from_string( $issue1->date_due() );
234 qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
235 "Koha::Schema::Result::Issue->date_due() returns a date"
237 my $issue_id1 = $dbh->last_insert_id( undef, undef, 'issues', undef );
239 my $issue2 = C4::Circulation::AddIssue( $borrower_1, 'nonexistent_barcode' );
240 is( $issue2, undef, "AddIssue returns undef if no datedue is specified" );
241 my $issue_id2 = $dbh->last_insert_id( undef, undef, 'issues', undef );
244 $countissue = $sth -> fetchrow_array;
245 is ($countissue,1,"1 issues have been added");
247 #Test AddIssuingCharge
248 $query = " SELECT count(*) FROM accountlines";
249 $sth = $dbh->prepare($query);
251 my $countaccount = $sth -> fetchrow_array;
252 is ($countaccount,0,"0 accountline exists");
253 is( C4::Circulation::AddIssuingCharge( $item_id1, $borrower_id1, 10 ),
254 1, "An issuing charge has been added" );
255 my $account_id = $dbh->last_insert_id( undef, undef, 'accountlines', undef );
257 $countaccount = $sth -> fetchrow_array;
258 is ($countaccount,1,"1 accountline has been added");
262 AddRenewal( $borrower_id1, $item_id1, $samplebranch1->{branchcode},
263 $datedue1, $daysago10 );
266 qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
267 "AddRenewal returns a date"
270 #Test GetBiblioIssues
271 is( GetBiblioIssues(), undef, "GetBiblio Issues without parameters" );
274 #FIXME : As the issues are not correctly added in the database, these tests don't work correctly
275 is(GetItemIssue,undef,"Without parameter GetItemIssue returns undef");
276 #is(GetItemIssue($item_id1),{},"Item1's issues");
279 #FIXME: this routine currently doesn't work be
280 #is_deeply (GetItemIssues,{},"Without parameter, GetItemIssue returns all the issues");
283 is( GetOpenIssue(), undef, "Without parameter GetOpenIssue returns undef" );
284 is( GetOpenIssue(-1), undef,
285 "With wrong parameter GetOpenIssue returns undef" );
286 my $openissue = GetOpenIssue($borrower_id1, $item_id1);
290 my $issue3 = C4::Circulation::AddIssue( $borrower_1, $barcode_1 );
291 #Without anything in DB
292 @renewcount = C4::Circulation::GetRenewCount();
295 [ 0, undef, 0 ], # FIXME Need to be fixed
296 "Without issuing rules and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
298 @renewcount = C4::Circulation::GetRenewCount(-1);
301 [ 0, undef, 0 ], # FIXME Need to be fixed
302 "Without issuing rules and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
304 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
308 "Without issuing rules and with a valid parameter, renewcount = 2, renewsallowed = undef, renewsleft = 0"
311 #With something in DB
312 # Add a default rule: No renewal allowed
314 INSERT INTO issuingrules( categorycode, itemtype, branchcode, issuelength, renewalsallowed )
315 VALUES ( '*', '*', '*', 10, 0 )
317 @renewcount = C4::Circulation::GetRenewCount();
321 "With issuing rules (renewal disallowed) and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
323 @renewcount = C4::Circulation::GetRenewCount(-1);
327 "With issuing rules (renewal disallowed) and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
329 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
333 "With issuing rules (renewal disallowed) and with a valid parameter, Getrenewcount returns renewcount = 2, renewsallowed = 0, renewsleft = 0"
336 # Add a default rule: renewal is allowed
338 UPDATE issuingrules SET renewalsallowed = 3
340 @renewcount = C4::Circulation::GetRenewCount();
344 "With issuing rules (renewal allowed) and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = 3, renewsleft = 3"
346 @renewcount = C4::Circulation::GetRenewCount(-1);
350 "With issuing rules (renewal allowed) and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = 3, renewsleft = 3"
352 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
356 "With issuing rules (renewal allowed) and with a valid parameter, Getrenewcount of item1 returns 3 renews left"
359 AddRenewal( $borrower_id1, $item_id1, $samplebranch1->{branchcode},
360 $datedue3, $daysago10 );
361 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
365 "With issuing rules (renewal allowed, 1 remaining) and with a valid parameter, Getrenewcount of item1 returns 0 renews left"
368 $dbh->do("DELETE FROM old_issues");
369 AddReturn($barcode_1);
370 my $return = $dbh->selectrow_hashref("SELECT DATE(returndate) AS return_date, CURRENT_DATE() AS today FROM old_issues LIMIT 1" );
371 ok( $return->{return_date} eq $return->{today}, "Item returned with no return date specified has todays date" );
373 $dbh->do("DELETE FROM old_issues");
374 C4::Circulation::AddIssue( $borrower_1, $barcode_1, $daysago10, 0, $today );
375 AddReturn($barcode_1, undef, undef, undef, '2014-04-01 23:42');
376 $return = $dbh->selectrow_hashref("SELECT * FROM old_issues LIMIT 1" );
377 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" );
380 ($biblionumber, $biblioitemnumber, $itemnumber) = C4::Items::AddItem(
382 barcode => 'barcode_3',
383 itemcallnumber => 'callnumber3',
384 homebranch => $samplebranch1->{branchcode},
385 holdingbranch => $samplebranch1->{branchcode},
392 t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', q{} );
393 AddReturn( 'barcode_3', $samplebranch1->{branchcode} );
394 my $item = GetItem( $itemnumber );
395 ok( $item->{notforloan} eq 1, 'UpdateNotForLoanStatusOnCheckin does not modify value when not enabled' );
397 t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', '1: 9' );
398 AddReturn( 'barcode_3', $samplebranch1->{branchcode} );
399 $item = GetItem( $itemnumber );
400 ok( $item->{notforloan} eq 9, q{UpdateNotForLoanStatusOnCheckin updates notforloan value from 1 to 9 with setting "1: 9"} );
402 AddReturn( 'barcode_3', $samplebranch1->{branchcode} );
403 $item = GetItem( $itemnumber );
404 ok( $item->{notforloan} eq 9, q{UpdateNotForLoanStatusOnCheckin does not update notforloan value from 9 with setting "1: 9"} );
406 # Bug 14640 - Cancel the hold on checking out if asked
407 my $reserve_id = AddReserve('CPL', $borrower_id1, $biblionumber,
408 undef, 1, undef, undef, "a note", "a title", undef, '');
409 ok( $reserve_id, 'The reserve should have been inserted' );
410 AddIssue( $borrower_2, $barcode_1, dt_from_string, 'cancel' );
411 my $reserve = GetReserve( $reserve_id );
412 is( $reserve, undef, 'The reserve should have been correctly cancelled' );
415 $schema->storage->txn_rollback;