Bug 17580: Add the Koha::Patron->get_overdues method
[koha.git] / t / db_dependent / Koha / Patrons.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 => 14;
23 use Test::Warn;
24 use DateTime;
25
26 use C4::Biblio;
27 use C4::Circulation;
28 use C4::Members;
29
30 use Koha::Holds;
31 use Koha::Patron;
32 use Koha::Patrons;
33 use Koha::Database;
34 use Koha::DateUtils;
35 use Koha::Virtualshelves;
36
37 use t::lib::TestBuilder;
38 use t::lib::Mocks;
39
40 my $schema = Koha::Database->new->schema;
41 $schema->storage->txn_begin;
42
43 my $builder       = t::lib::TestBuilder->new;
44 my $library = $builder->build({source => 'Branch' });
45 my $category = $builder->build({source => 'Category' });
46 my $nb_of_patrons = Koha::Patrons->search->count;
47 my $new_patron_1  = Koha::Patron->new(
48     {   cardnumber => 'test_cn_1',
49         branchcode => $library->{branchcode},
50         categorycode => $category->{categorycode},
51         surname => 'surname for patron1',
52         firstname => 'firstname for patron1',
53         userid => 'a_nonexistent_userid_1',
54     }
55 )->store;
56 my $new_patron_2  = Koha::Patron->new(
57     {   cardnumber => 'test_cn_2',
58         branchcode => $library->{branchcode},
59         categorycode => $category->{categorycode},
60         surname => 'surname for patron2',
61         firstname => 'firstname for patron2',
62         userid => 'a_nonexistent_userid_2',
63     }
64 )->store;
65
66 C4::Context->_new_userenv('xxx');
67 C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, 'Midway Public Library', '', '', '');
68
69 is( Koha::Patrons->search->count, $nb_of_patrons + 2, 'The 2 patrons should have been added' );
70
71 my $retrieved_patron_1 = Koha::Patrons->find( $new_patron_1->borrowernumber );
72 is( $retrieved_patron_1->cardnumber, $new_patron_1->cardnumber, 'Find a patron by borrowernumber should return the correct patron' );
73
74 subtest 'guarantees' => sub {
75     plan tests => 8;
76     my $guarantees = $new_patron_1->guarantees;
77     is( ref($guarantees), 'Koha::Patrons', 'Koha::Patron->guarantees should return a Koha::Patrons result set in a scalar context' );
78     is( $guarantees->count, 0, 'new_patron_1 should have 0 guarantee' );
79     my @guarantees = $new_patron_1->guarantees;
80     is( ref(\@guarantees), 'ARRAY', 'Koha::Patron->guarantees should return an array in a list context' );
81     is( scalar(@guarantees), 0, 'new_patron_1 should have 0 guarantee' );
82
83     my $guarantee_1 = $builder->build({ source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber }});
84     my $guarantee_2 = $builder->build({ source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber }});
85
86     $guarantees = $new_patron_1->guarantees;
87     is( ref($guarantees), 'Koha::Patrons', 'Koha::Patron->guarantees should return a Koha::Patrons result set in a scalar context' );
88     is( $guarantees->count, 2, 'new_patron_1 should have 2 guarantees' );
89     @guarantees = $new_patron_1->guarantees;
90     is( ref(\@guarantees), 'ARRAY', 'Koha::Patron->guarantees should return an array in a list context' );
91     is( scalar(@guarantees), 2, 'new_patron_1 should have 2 guarantees' );
92     $_->delete for @guarantees;
93 };
94
95 subtest 'category' => sub {
96     plan tests => 2;
97     my $patron_category = $new_patron_1->category;
98     is( ref( $patron_category), 'Koha::Patron::Category', );
99     is( $patron_category->categorycode, $category->{categorycode}, );
100 };
101
102 subtest 'siblings' => sub {
103     plan tests => 7;
104     my $siblings = $new_patron_1->siblings;
105     is( $siblings, undef, 'Koha::Patron->siblings should not crashed if the patron has no guarantor' );
106     my $guarantee_1 = $builder->build( { source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber } } );
107     my $retrieved_guarantee_1 = Koha::Patrons->find($guarantee_1);
108     $siblings = $retrieved_guarantee_1->siblings;
109     is( ref($siblings), 'Koha::Patrons', 'Koha::Patron->siblings should return a Koha::Patrons result set in a scalar context' );
110     my @siblings = $retrieved_guarantee_1->siblings;
111     is( ref( \@siblings ), 'ARRAY', 'Koha::Patron->siblings should return an array in a list context' );
112     is( $siblings->count,  0,       'guarantee_1 should not have siblings yet' );
113     my $guarantee_2 = $builder->build( { source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber } } );
114     my $guarantee_3 = $builder->build( { source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber } } );
115     $siblings = $retrieved_guarantee_1->siblings;
116     is( $siblings->count,               2,                               'guarantee_1 should have 2 siblings' );
117     is( $guarantee_2->{borrowernumber}, $siblings->next->borrowernumber, 'guarantee_2 should exist in the guarantees' );
118     is( $guarantee_3->{borrowernumber}, $siblings->next->borrowernumber, 'guarantee_3 should exist in the guarantees' );
119     $_->delete for $retrieved_guarantee_1->siblings;
120     $retrieved_guarantee_1->delete;
121 };
122
123 subtest 'has_overdues' => sub {
124     plan tests => 3;
125
126     my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
127     my $item_1 = $builder->build(
128         {   source => 'Item',
129             value  => {
130                 homebranch    => $library->{branchcode},
131                 holdingbranch => $library->{branchcode},
132                 notforloan    => 0,
133                 itemlost      => 0,
134                 withdrawn     => 0,
135                 biblionumber  => $biblioitem_1->{biblionumber}
136             }
137         }
138     );
139     my $retrieved_patron = Koha::Patrons->find( $new_patron_1->borrowernumber );
140     is( $retrieved_patron->has_overdues, 0, );
141
142     my $tomorrow = DateTime->today( time_zone => C4::Context->tz() )->add( days => 1 );
143     my $issue = Koha::Issue->new({ borrowernumber => $new_patron_1->id, itemnumber => $item_1->{itemnumber}, date_due => $tomorrow, branchcode => $library->{branchcode} })->store();
144     is( $retrieved_patron->has_overdues, 0, );
145     $issue->delete();
146     my $yesterday = DateTime->today(time_zone => C4::Context->tz())->add( days => -1 );
147     $issue = Koha::Issue->new({ borrowernumber => $new_patron_1->id, itemnumber => $item_1->{itemnumber}, date_due => $yesterday, branchcode => $library->{branchcode} })->store();
148     $retrieved_patron = Koha::Patrons->find( $new_patron_1->borrowernumber );
149     is( $retrieved_patron->has_overdues, 1, );
150     $issue->delete();
151 };
152
153 subtest 'update_password' => sub {
154     plan tests => 7;
155
156     t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
157     my $original_userid   = $new_patron_1->userid;
158     my $original_password = $new_patron_1->password;
159     warning_like { $retrieved_patron_1->update_password( $new_patron_2->userid, 'another_password' ) }
160     qr{Duplicate entry},
161       'Koha::Patron->update_password should warn if the userid is already used by another patron';
162     is( Koha::Patrons->find( $new_patron_1->borrowernumber )->userid,   $original_userid,   'Koha::Patron->update_password should not have updated the userid' );
163     is( Koha::Patrons->find( $new_patron_1->borrowernumber )->password, $original_password, 'Koha::Patron->update_password should not have updated the userid' );
164
165     $retrieved_patron_1->update_password( 'another_nonexistent_userid_1', 'another_password' );
166     is( Koha::Patrons->find( $new_patron_1->borrowernumber )->userid,   'another_nonexistent_userid_1', 'Koha::Patron->update_password should have updated the userid' );
167     is( Koha::Patrons->find( $new_patron_1->borrowernumber )->password, 'another_password',             'Koha::Patron->update_password should have updated the password' );
168
169     my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'CHANGE PASS', object => $new_patron_1->borrowernumber } )->count;
170     is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->update_password should have logged' );
171
172     t::lib::Mocks::mock_preference( 'BorrowersLog', 0 );
173     $retrieved_patron_1->update_password( 'yet_another_nonexistent_userid_1', 'another_password' );
174     $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'CHANGE PASS', object => $new_patron_1->borrowernumber } )->count;
175     is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->update_password should not have logged' );
176 };
177
178 subtest 'is_expired' => sub {
179     plan tests => 5;
180     my $patron = $builder->build({ source => 'Borrower' });
181     $patron = Koha::Patrons->find( $patron->{borrowernumber} );
182     $patron->dateexpiry( undef )->store->discard_changes;
183     is( $patron->is_expired, 0, 'Patron should not be considered expired if dateexpiry is not set');
184     $patron->dateexpiry( '0000-00-00' )->store->discard_changes;
185     is( $patron->is_expired, 0, 'Patron should not be considered expired if dateexpiry is not 0000-00-00');
186     $patron->dateexpiry( dt_from_string )->store->discard_changes;
187     is( $patron->is_expired, 0, 'Patron should not be considered expired if dateexpiry is today');
188     $patron->dateexpiry( dt_from_string->add( days => 1 ) )->store->discard_changes;
189     is( $patron->is_expired, 0, 'Patron should not be considered expired if dateexpiry is tomorrow');
190     $patron->dateexpiry( dt_from_string->add( days => -1 ) )->store->discard_changes;
191     is( $patron->is_expired, 1, 'Patron should be considered expired if dateexpiry is yesterday');
192
193     $patron->delete;
194 };
195
196 subtest 'is_going_to_expire' => sub {
197     plan tests => 9;
198     my $patron = $builder->build({ source => 'Borrower' });
199     $patron = Koha::Patrons->find( $patron->{borrowernumber} );
200     $patron->dateexpiry( undef )->store->discard_changes;
201     is( $patron->is_going_to_expire, 0, 'Patron should not be considered going to expire if dateexpiry is not set');
202     $patron->dateexpiry( '0000-00-00' )->store->discard_changes;
203     is( $patron->is_going_to_expire, 0, 'Patron should not be considered going to expire if dateexpiry is not 0000-00-00');
204
205     t::lib::Mocks::mock_preference('NotifyBorrowerDeparture', 0);
206     $patron->dateexpiry( dt_from_string )->store->discard_changes;
207     is( $patron->is_going_to_expire, 0, 'Patron should not be considered going to expire if dateexpiry is today');
208
209     $patron->dateexpiry( dt_from_string )->store->discard_changes;
210     is( $patron->is_going_to_expire, 0, 'Patron should not be considered going to expire if dateexpiry is today and pref is 0');
211
212     t::lib::Mocks::mock_preference('NotifyBorrowerDeparture', 10);
213     $patron->dateexpiry( dt_from_string->add( days => 11 ) )->store->discard_changes;
214     is( $patron->is_going_to_expire, 0, 'Patron should not be considered going to expire if dateexpiry is 11 days ahead and pref is 10');
215
216     t::lib::Mocks::mock_preference('NotifyBorrowerDeparture', 0);
217     $patron->dateexpiry( dt_from_string->add( days => 10 ) )->store->discard_changes;
218     is( $patron->is_going_to_expire, 0, 'Patron should not be considered going to expire if dateexpiry is 10 days ahead and pref is 0');
219
220     t::lib::Mocks::mock_preference('NotifyBorrowerDeparture', 10);
221     $patron->dateexpiry( dt_from_string->add( days => 10 ) )->store->discard_changes;
222     is( $patron->is_going_to_expire, 0, 'Patron should not be considered going to expire if dateexpiry is 10 days ahead and pref is 10');
223     $patron->delete;
224
225     t::lib::Mocks::mock_preference('NotifyBorrowerDeparture', 10);
226     $patron->dateexpiry( dt_from_string->add( days => 20 ) )->store->discard_changes;
227     is( $patron->is_going_to_expire, 0, 'Patron should not be considered going to expire if dateexpiry is 20 days ahead and pref is 10');
228
229     t::lib::Mocks::mock_preference('NotifyBorrowerDeparture', 20);
230     $patron->dateexpiry( dt_from_string->add( days => 10 ) )->store->discard_changes;
231     is( $patron->is_going_to_expire, 1, 'Patron should be considered going to expire if dateexpiry is 10 days ahead and pref is 20');
232
233     $patron->delete;
234 };
235
236
237 subtest 'renew_account' => sub {
238     plan tests => 10;
239     my $a_month_ago                = dt_from_string->add( months => -1 )->truncate( to => 'day' );
240     my $a_year_later               = dt_from_string->add( months => 12 )->truncate( to => 'day' );
241     my $a_year_later_minus_a_month = dt_from_string->add( months => 11 )->truncate( to => 'day' );
242     my $a_month_later              = dt_from_string->add( months => 1  )->truncate( to => 'day' );
243     my $a_year_later_plus_a_month  = dt_from_string->add( months => 13 )->truncate( to => 'day' );
244     my $patron_category = $builder->build(
245         {   source => 'Category',
246             value  => {
247                 enrolmentperiod     => 12,
248                 enrolmentperioddate => undef,
249             }
250         }
251     );
252     my $patron = $builder->build(
253         {   source => 'Borrower',
254             value  => {
255                 dateexpiry   => $a_month_ago,
256                 categorycode => $patron_category->{categorycode},
257             }
258         }
259     );
260     my $patron_2 = $builder->build(
261         {  source => 'Borrower',
262            value  => {
263                dateexpiry => $a_month_ago,
264                categorycode => $patron_category->{categorycode},
265             }
266         }
267     );
268     my $patron_3 = $builder->build(
269         {  source => 'Borrower',
270            value  => {
271                dateexpiry => $a_month_later,
272                categorycode => $patron_category->{categorycode},
273            }
274         }
275     );
276     my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
277     my $retrieved_patron_2 = Koha::Patrons->find( $patron_2->{borrowernumber} );
278     my $retrieved_patron_3 = Koha::Patrons->find( $patron_3->{borrowernumber} );
279
280     t::lib::Mocks::mock_preference( 'BorrowerRenewalPeriodBase', 'dateexpiry' );
281     t::lib::Mocks::mock_preference( 'BorrowersLog',              1 );
282     my $expiry_date = $retrieved_patron->renew_account;
283     is( $expiry_date, $a_year_later_minus_a_month, );
284     my $retrieved_expiry_date = Koha::Patrons->find( $patron->{borrowernumber} )->dateexpiry;
285     is( dt_from_string($retrieved_expiry_date), $a_year_later_minus_a_month );
286     my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'RENEW', object => $retrieved_patron->borrowernumber } )->count;
287     is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->renew_account should have logged' );
288
289     t::lib::Mocks::mock_preference( 'BorrowerRenewalPeriodBase', 'now' );
290     t::lib::Mocks::mock_preference( 'BorrowersLog',              0 );
291     $expiry_date = $retrieved_patron->renew_account;
292     is( $expiry_date, $a_year_later, );
293     $retrieved_expiry_date = Koha::Patrons->find( $patron->{borrowernumber} )->dateexpiry;
294     is( dt_from_string($retrieved_expiry_date), $a_year_later );
295     $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'RENEW', object => $retrieved_patron->borrowernumber } )->count;
296     is( $number_of_logs, 1, 'Without BorrowerLogs, Koha::Patron->renew_account should not have logged' );
297
298     t::lib::Mocks::mock_preference( 'BorrowerRenewalPeriodBase', 'combination' );
299     $expiry_date = $retrieved_patron_2->renew_account;
300     is( $expiry_date, $a_year_later );
301     $retrieved_expiry_date = Koha::Patrons->find( $patron_2->{borrowernumber} )->dateexpiry;
302     is( dt_from_string($retrieved_expiry_date), $a_year_later );
303
304     $expiry_date = $retrieved_patron_3->renew_account;
305     is( $expiry_date, $a_year_later_plus_a_month );
306     $retrieved_expiry_date = Koha::Patrons->find( $patron_3->{borrowernumber} )->dateexpiry;
307     is( dt_from_string($retrieved_expiry_date), $a_year_later_plus_a_month );
308
309     $retrieved_patron->delete;
310     $retrieved_patron_2->delete;
311     $retrieved_patron_3->delete;
312 };
313
314 subtest "move_to_deleted" => sub {
315     plan tests => 2;
316     my $patron = $builder->build( { source => 'Borrower' } );
317     my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
318     is( ref( $retrieved_patron->move_to_deleted ), 'Koha::Schema::Result::Deletedborrower', 'Koha::Patron->move_to_deleted should return the Deleted patron' )
319       ;    # FIXME This should be Koha::Deleted::Patron
320     my $deleted_patron = $schema->resultset('Deletedborrower')
321         ->search( { borrowernumber => $patron->{borrowernumber} }, { result_class => 'DBIx::Class::ResultClass::HashRefInflator' } )
322         ->next;
323     is_deeply( $deleted_patron, $patron, 'Koha::Patron->move_to_deleted should have correctly moved the patron to the deleted table' );
324     $retrieved_patron->delete( $patron->{borrowernumber} );    # Cleanup
325 };
326
327 subtest "delete" => sub {
328     plan tests => 5;
329     t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
330     my $patron           = $builder->build( { source => 'Borrower' } );
331     my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
332     my $hold             = $builder->build(
333         {   source => 'Reserve',
334             value  => { borrowernumber => $patron->{borrowernumber} }
335         }
336     );
337     my $list = $builder->build(
338         {   source => 'Virtualshelve',
339             value  => { owner => $patron->{borrowernumber} }
340         }
341     );
342
343     my $deleted = $retrieved_patron->delete;
344     is( $deleted, 1, 'Koha::Patron->delete should return 1 if the patron has been correctly deleted' );
345
346     is( Koha::Patrons->find( $patron->{borrowernumber} ), undef, 'Koha::Patron->delete should have deleted the patron' );
347
348     is( Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's holds| );
349
350     is( Koha::Virtualshelves->search( { owner => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's lists| );
351
352     my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'DELETE', object => $retrieved_patron->borrowernumber } )->count;
353     is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->delete should have logged' );
354 };
355
356 subtest 'add_enrolment_fee_if_needed' => sub {
357     plan tests => 4;
358
359     my $enrolmentfee_K  = 5;
360     my $enrolmentfee_J  = 10;
361     my $enrolmentfee_YA = 20;
362
363     my $dbh = C4::Context->dbh;
364     $dbh->do(q|UPDATE categories set enrolmentfee=? where categorycode=?|, undef, $enrolmentfee_K, 'K');
365     $dbh->do(q|UPDATE categories set enrolmentfee=? where categorycode=?|, undef, $enrolmentfee_J, 'J');
366     $dbh->do(q|UPDATE categories set enrolmentfee=? where categorycode=?|, undef, $enrolmentfee_YA, 'YA');
367
368     my %borrower_data = (
369         firstname    => 'my firstname',
370         surname      => 'my surname',
371         categorycode => 'K',
372         branchcode   => $library->{branchcode},
373     );
374
375     my $borrowernumber = C4::Members::AddMember(%borrower_data);
376     $borrower_data{borrowernumber} = $borrowernumber;
377
378     my ($total) = C4::Members::GetMemberAccountRecords($borrowernumber);
379     is( $total, $enrolmentfee_K, "New kid pay $enrolmentfee_K" );
380
381     t::lib::Mocks::mock_preference( 'FeeOnChangePatronCategory', 0 );
382     $borrower_data{categorycode} = 'J';
383     C4::Members::ModMember(%borrower_data);
384     ($total) = C4::Members::GetMemberAccountRecords($borrowernumber);
385     is( $total, $enrolmentfee_K, "Kid growing and become a juvenile, but shouldn't pay for the upgrade " );
386
387     $borrower_data{categorycode} = 'K';
388     C4::Members::ModMember(%borrower_data);
389     t::lib::Mocks::mock_preference( 'FeeOnChangePatronCategory', 1 );
390
391     $borrower_data{categorycode} = 'J';
392     C4::Members::ModMember(%borrower_data);
393     ($total) = C4::Members::GetMemberAccountRecords($borrowernumber);
394     is( $total, $enrolmentfee_K + $enrolmentfee_J, "Kid growing and become a juvenile, he should pay " . ( $enrolmentfee_K + $enrolmentfee_J ) );
395
396     # Check with calling directly Koha::Patron->get_enrolment_fee_if_needed
397     my $patron = Koha::Patrons->find($borrowernumber);
398     $patron->categorycode('YA')->store;
399     my $fee = $patron->add_enrolment_fee_if_needed;
400     ($total) = C4::Members::GetMemberAccountRecords($borrowernumber);
401     is( $total,
402         $enrolmentfee_K + $enrolmentfee_J + $enrolmentfee_YA,
403         "Juvenile growing and become an young adult, he should pay " . ( $enrolmentfee_K + $enrolmentfee_J + $enrolmentfee_YA )
404     );
405
406     $patron->delete;
407 };
408
409 subtest 'get_overdues' => sub {
410     plan tests => 4;
411
412     my $library = $builder->build( { source => 'Branch' } );
413     my ($biblionumber_1) = AddBiblio( MARC::Record->new, '' );
414     my $item_1 = $builder->build(
415         {
416             source => 'Item',
417             value  => {
418                 homebranch    => $library->{branchcode},
419                 holdingbranch => $library->{branchcode},
420                 biblionumber  => $biblionumber_1
421             }
422         }
423     );
424     my $item_2 = $builder->build(
425         {
426             source => 'Item',
427             value  => {
428                 homebranch    => $library->{branchcode},
429                 holdingbranch => $library->{branchcode},
430                 biblionumber  => $biblionumber_1
431             }
432         }
433     );
434     my ($biblionumber_2) = AddBiblio( MARC::Record->new, '' );
435     my $item_3 = $builder->build(
436         {
437             source => 'Item',
438             value  => {
439                 homebranch    => $library->{branchcode},
440                 holdingbranch => $library->{branchcode},
441                 biblionumber  => $biblionumber_2
442             }
443         }
444     );
445     my $patron = $builder->build(
446         {
447             source => 'Borrower',
448             value  => { branchcode => $library->{branchcode} }
449         }
450     );
451
452     # Not sure how this is useful, but AddIssue pass this variable to different other subroutines
453     $patron = GetMember( borrowernumber => $patron->{borrowernumber} );
454
455     my $module = new Test::MockModule('C4::Context');
456     $module->mock( 'userenv', sub { { branch => $library->{branchcode} } } );
457
458     AddIssue( $patron, $item_1->{barcode}, DateTime->now->subtract( days => 1 ) );
459     AddIssue( $patron, $item_2->{barcode}, DateTime->now->subtract( days => 5 ) );
460     AddIssue( $patron, $item_3->{barcode} );
461
462     $patron = Koha::Patrons->find( $patron->{borrowernumber} );
463     my $overdues = $patron->get_overdues;
464     is( $overdues->count, 2, 'Patron should have 2 overdues');
465     is( ref($overdues), 'Koha::Issues', 'Koha::Patron->get_overdues should return Koha::Issues' );
466     is( $overdues->next->itemnumber, $item_1->{itemnumber}, 'The issue should be returned in the same order as they have been done, first is correct' );
467     is( $overdues->next->itemnumber, $item_2->{itemnumber}, 'The issue should be returned in the same order as they have been done, second is correct' );
468
469     # Clean stuffs
470     Koha::Issues->search( { borrowernumber => $patron->borrowernumber } )->delete;
471     $patron->delete;
472 };
473
474 $retrieved_patron_1->delete;
475 is( Koha::Patrons->search->count, $nb_of_patrons + 1, 'Delete should have deleted the patron' );
476
477 $schema->storage->txn_rollback;
478
479 1;