Bug 28445: Use the task queue for the batch delete and update items tool
[koha.git] / t / db_dependent / Koha / Checkouts.t
1 #!/usr/bin/perl
2
3 # Copyright 2015 Koha Development team
4 #
5 # This file is part of Koha
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 10;
23
24 use C4::Circulation qw( MarkIssueReturned AddReturn );
25 use Koha::Checkouts;
26 use Koha::Database;
27 use Koha::DateUtils qw( dt_from_string );
28
29 use t::lib::TestBuilder;
30 use t::lib::Mocks;
31
32 my $schema = Koha::Database->new->schema;
33 $schema->storage->txn_begin;
34
35 my $builder = t::lib::TestBuilder->new;
36 my $library = $builder->build( { source => 'Branch' } );
37 my $patron  = $builder->build(
38     { source => 'Borrower', value => { branchcode => $library->{branchcode} } }
39 );
40 my $item_1          = $builder->build_sample_item;
41 my $item_2          = $builder->build_sample_item;
42 my $nb_of_checkouts = Koha::Checkouts->search->count;
43 my $new_checkout_1  = Koha::Checkout->new(
44     {
45         borrowernumber => $patron->{borrowernumber},
46         itemnumber     => $item_1->itemnumber,
47         branchcode     => $library->{branchcode},
48     }
49 )->store;
50 my $new_checkout_2 = Koha::Checkout->new(
51     {
52         borrowernumber => $patron->{borrowernumber},
53         itemnumber     => $item_2->itemnumber,
54         branchcode     => $library->{branchcode},
55     }
56 )->store;
57
58 like( $new_checkout_1->issue_id, qr|^\d+$|,
59     'Adding a new checkout should have set the issue_id' );
60 is(
61     Koha::Checkouts->search->count,
62     $nb_of_checkouts + 2,
63     'The 2 checkouts should have been added'
64 );
65
66 my $retrieved_checkout_1 = Koha::Checkouts->find( $new_checkout_1->issue_id );
67 is(
68     $retrieved_checkout_1->itemnumber,
69     $new_checkout_1->itemnumber,
70     'Find a checkout by id should return the correct checkout'
71 );
72
73 subtest 'is_overdue' => sub {
74     plan tests => 6;
75     my $ten_days_ago   = dt_from_string->add( days => -10 );
76     my $ten_days_later = dt_from_string->add( days => 10 );
77     my $yesterday      = dt_from_string->add( days => -1 );
78     my $tomorrow       = dt_from_string->add( days => 1 );
79
80     $retrieved_checkout_1->date_due($ten_days_ago)->store;
81     is( $retrieved_checkout_1->is_overdue,
82         1, 'The item should have been returned 10 days ago' );
83
84     $retrieved_checkout_1->date_due($ten_days_later)->store;
85     is( $retrieved_checkout_1->is_overdue, 0, 'The item is due in 10 days' );
86
87     $retrieved_checkout_1->date_due($tomorrow)->store;
88     is( $retrieved_checkout_1->is_overdue($ten_days_later),
89         1, 'The item should have been returned yesterday' );
90
91     $retrieved_checkout_1->date_due($yesterday)->store;
92     is( $retrieved_checkout_1->is_overdue($ten_days_ago),
93         0, 'Ten days ago the item due yesterday was not late' );
94
95     $retrieved_checkout_1->date_due($tomorrow)->store;
96     is( $retrieved_checkout_1->is_overdue($ten_days_later),
97         1, 'In Ten days, the item due tomorrow will be late' );
98
99     $retrieved_checkout_1->date_due($yesterday)->store;
100     is( $retrieved_checkout_1->is_overdue($ten_days_ago),
101         0, 'In Ten days, the item due yesterday will still be late' );
102 };
103
104 subtest 'item' => sub {
105     plan tests => 2;
106     my $item = $retrieved_checkout_1->item;
107     is( ref($item), 'Koha::Item',
108         'Koha::Checkout->item should return a Koha::Item' );
109     is( $item->itemnumber, $item_1->itemnumber,
110         'Koha::Checkout->item should return the correct item' );
111 };
112
113 subtest 'patron' => sub {
114     plan tests => 3;
115     my $patron = $builder->build_object(
116         {
117             class => 'Koha::Patrons',
118             value => { branchcode => $library->{branchcode} }
119         }
120     );
121
122     my $item     = $builder->build_sample_item;
123     my $checkout = Koha::Checkout->new(
124         {
125             borrowernumber => $patron->borrowernumber,
126             itemnumber     => $item->itemnumber,
127             branchcode     => $library->{branchcode},
128         }
129     )->store;
130
131     my $p = $checkout->patron;
132     is( ref($p), 'Koha::Patron',
133         'Koha::Checkout->patron should return a Koha::Patron' );
134     is( $p->borrowernumber, $patron->borrowernumber,
135         'Koha::Checkout->patron should return the correct patron' );
136
137     # Testing Koha::Old::Checkout->patron now
138     my $issue_id = $checkout->issue_id;
139     C4::Circulation::MarkIssueReturned( $p->borrowernumber,
140         $checkout->itemnumber );
141     $p->delete;
142     my $old_issue = Koha::Old::Checkouts->find($issue_id);
143     is( $old_issue->patron, undef,
144 'Koha::Checkout->patron should return undef if the patron record has been deleted'
145     );
146 };
147
148 $retrieved_checkout_1->delete;
149 is(
150     Koha::Checkouts->search->count,
151     $nb_of_checkouts + 1,
152     'Delete should have deleted the checkout'
153 );
154
155 subtest 'issuer' => sub {
156     plan tests => 3;
157     my $patron = $builder->build_object(
158         {
159             class => 'Koha::Patrons',
160             value => { branchcode => $library->{branchcode} }
161         }
162     );
163     my $issuer = $builder->build_object(
164         {
165             class => 'Koha::Patrons',
166             value => { branchcode => $library->{branchcode} }
167         }
168     );
169
170     my $item     = $builder->build_sample_item;
171     my $checkout = Koha::Checkout->new(
172         {
173             borrowernumber => $patron->borrowernumber,
174             issuer_id      => $issuer->borrowernumber,
175             itemnumber     => $item->itemnumber,
176             branchcode     => $library->{branchcode},
177         }
178     )->store;
179
180     my $i = $checkout->issuer;
181     is( ref($i), 'Koha::Patron',
182         'Koha::Checkout->issuer should return a Koha::Patron' );
183     is( $i->borrowernumber, $issuer->borrowernumber,
184         'Koha::Checkout->issuer should return the correct patron' );
185
186     # Testing Koha::Old::Checkout->patron now
187     my $issue_id = $checkout->issue_id;
188     C4::Circulation::MarkIssueReturned( $patron->borrowernumber,
189         $checkout->itemnumber );
190     $i->delete;
191     my $old_issue = Koha::Old::Checkouts->find($issue_id);
192     is( $old_issue->issuer_id, undef,
193 'Koha::Checkout->issuer_id should return undef if the patron record has been deleted'
194     );
195
196 };
197
198 subtest 'Koha::Old::Checkouts->filter_by_todays_checkins' => sub {
199
200     plan tests => 3;
201
202     # We will create 7 checkins for a given patron
203     # 3 checked in today - 2 days, and 4 checked in today
204     my $librarian = $builder->build_object(
205         {
206             class => 'Koha::Patrons',
207             value => { branchcode => $library->{branchcode} }
208         }
209     );
210     t::lib::Mocks::mock_userenv( { patron => $librarian } );
211     my $patron = $builder->build_object(
212         {
213             class => 'Koha::Patrons',
214             value => { branchcode => $library->{branchcode} }
215         }
216     );
217
218     my @checkouts;
219     # Create 7 checkouts
220     for ( 0 .. 6 ) {
221         my $item = $builder->build_sample_item;
222         push @checkouts,
223           Koha::Checkout->new(
224             {
225                 borrowernumber => $patron->borrowernumber,
226                 itemnumber     => $item->itemnumber,
227                 branchcode     => $library->{branchcode},
228             }
229         )->store;
230     }
231
232     # Checkin 3 today - 2 days
233     my $not_today = dt_from_string->add( days => -2 );
234     for my $i ( 0 .. 2 ) {
235         my $checkout = $checkouts[$i];
236         C4::Circulation::AddReturn(
237             $checkout->item->barcode, $library->{branchcode},
238             undef, $not_today->set_hour( int( rand(24) ) )
239         );
240     }
241     # Checkin 4 today
242     my $today = dt_from_string;
243     for my $i ( 3 .. 6 ) {
244         my $checkout = $checkouts[$i];
245         C4::Circulation::AddReturn(
246             $checkout->item->barcode, $library->{branchcode},
247             undef, $today->set_hour( int( rand(24) ) )
248         );
249     }
250
251     my $old_checkouts = $patron->old_checkouts;
252     is( $old_checkouts->count, 7, 'There should be 7 old checkouts' );
253     my $todays_checkins = $old_checkouts->filter_by_todays_checkins;
254     is( $todays_checkins->count, 4, 'There should be 4 checkins today' );
255     is_deeply(
256         [ $todays_checkins->get_column('itemnumber') ],
257         [ map { $_->itemnumber } @checkouts[ 3 .. 6 ] ],
258         q{Correct list of today's checkins}
259     );
260 };
261
262 $schema->storage->txn_rollback;
263
264 subtest 'automatic_checkin' => sub {
265
266     plan tests => 9;
267
268     $schema->storage->txn_begin;
269
270     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
271
272     my $due_ac_item =
273       $builder->build_sample_item(
274         { homebranch => $patron->branchcode, itemlost => 0 } );
275     my $ac_item =
276       $builder->build_sample_item(
277         { homebranch => $patron->branchcode, itemlost => 0 } );
278     my $odue_ac_item =
279       $builder->build_sample_item(
280         { homebranch => $patron->branchcode, itemlost => 0 } );
281     my $normal_item =
282       $builder->build_sample_item(
283         { homebranch => $patron->branchcode, itemlost => 0 } );
284
285     $due_ac_item->itemtype->automatic_checkin(1)->store;
286     $odue_ac_item->itemtype->automatic_checkin(1)->store;
287     $ac_item->itemtype->automatic_checkin(1)->store;
288     $normal_item->itemtype->automatic_checkin(0)->store;
289
290     my $today     = dt_from_string;
291     my $tomorrow  = dt_from_string->add( days => 1 );
292     my $yesterday = dt_from_string->subtract( days => 1 );
293
294     # Checkout do for automatic checkin
295     my $checkout_due_aci = Koha::Checkout->new(
296         {
297             borrowernumber => $patron->borrowernumber,
298             itemnumber     => $due_ac_item->itemnumber,
299             branchcode     => $patron->branchcode,
300             date_due       => $today,
301         }
302     )->store;
303
304     # Checkout not due for automatic checkin
305     my $checkout_odue_aci = Koha::Checkout->new(
306         {
307             borrowernumber => $patron->borrowernumber,
308             itemnumber     => $odue_ac_item->itemnumber,
309             branchcode     => $patron->branchcode,
310             date_due       => $yesterday
311         }
312     )->store;
313
314     # Checkout not due for automatic checkin
315     my $checkout_aci = Koha::Checkout->new(
316         {
317             borrowernumber => $patron->borrowernumber,
318             itemnumber     => $ac_item->itemnumber,
319             branchcode     => $patron->branchcode,
320             date_due       => $tomorrow
321         }
322     )->store;
323
324     # due checkout for nomal itemtype
325     my $checkout_ni = Koha::Checkout->new(
326         {
327             borrowernumber => $patron->borrowernumber,
328             itemnumber     => $normal_item->itemnumber,
329             branchcode     => $patron->branchcode,
330             date_due       => $today,
331         }
332     )->store;
333
334     my $searched = Koha::Checkouts->find( $checkout_ni->issue_id );
335     is( $searched->issue_id, $checkout_ni->issue_id,
336         'checkout for normal_item exists' );
337
338     $searched = Koha::Checkouts->find( $checkout_aci->issue_id );
339     is( $searched->issue_id, $checkout_aci->issue_id,
340         'checkout for ac_item exists' );
341
342     $searched = Koha::Checkouts->find( $checkout_due_aci->issue_id );
343     is(
344         $searched->issue_id,
345         $checkout_due_aci->issue_id,
346         'checkout for due_ac_item exists'
347     );
348
349     $searched = Koha::Checkouts->find( $checkout_odue_aci->issue_id );
350     is(
351         $searched->issue_id,
352         $checkout_odue_aci->issue_id,
353         'checkout for odue_ac_item exists'
354     );
355
356     Koha::Checkouts->automatic_checkin;
357
358     $searched = Koha::Checkouts->find( $checkout_ni->issue_id );
359     is( $searched->issue_id, $checkout_ni->issue_id,
360         'checkout for normal_item still exists' );
361
362     $searched = Koha::Checkouts->find( $checkout_aci->issue_id );
363     is( $searched->issue_id, $checkout_aci->issue_id,
364         'checkout for ac_item still exists' );
365
366     $searched = Koha::Checkouts->find( $checkout_due_aci->issue_id );
367     is( $searched, undef, 'checkout for due_ac_item doesn\'t exist anymore' );
368
369     $searched = Koha::Checkouts->find( $checkout_odue_aci->issue_id );
370     is( $searched, undef, 'checkout for odue_ac_item doesn\'t exist anymore' );
371
372     $searched = Koha::Old::Checkouts->find( $checkout_odue_aci->issue_id );
373     is( dt_from_string($searched->returndate), $yesterday, 'old checkout for odue_ac_item has the right return date' );
374
375     $schema->storage->txn_rollback;
376 }