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