Bug 17782 - (QA Followup)
[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 => 20;
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 'library' => sub {
75     plan tests => 2;
76     is( $retrieved_patron_1->library->branchcode, $library->{branchcode}, 'Koha::Patron->library should return the correct library' );
77     is( ref($retrieved_patron_1->library), 'Koha::Library', 'Koha::Patron->library should return a Koha::Library object' );
78 };
79
80 subtest 'guarantees' => sub {
81     plan tests => 8;
82     my $guarantees = $new_patron_1->guarantees;
83     is( ref($guarantees), 'Koha::Patrons', 'Koha::Patron->guarantees should return a Koha::Patrons result set in a scalar context' );
84     is( $guarantees->count, 0, 'new_patron_1 should have 0 guarantee' );
85     my @guarantees = $new_patron_1->guarantees;
86     is( ref(\@guarantees), 'ARRAY', 'Koha::Patron->guarantees should return an array in a list context' );
87     is( scalar(@guarantees), 0, 'new_patron_1 should have 0 guarantee' );
88
89     my $guarantee_1 = $builder->build({ source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber }});
90     my $guarantee_2 = $builder->build({ source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber }});
91
92     $guarantees = $new_patron_1->guarantees;
93     is( ref($guarantees), 'Koha::Patrons', 'Koha::Patron->guarantees should return a Koha::Patrons result set in a scalar context' );
94     is( $guarantees->count, 2, 'new_patron_1 should have 2 guarantees' );
95     @guarantees = $new_patron_1->guarantees;
96     is( ref(\@guarantees), 'ARRAY', 'Koha::Patron->guarantees should return an array in a list context' );
97     is( scalar(@guarantees), 2, 'new_patron_1 should have 2 guarantees' );
98     $_->delete for @guarantees;
99 };
100
101 subtest 'category' => sub {
102     plan tests => 2;
103     my $patron_category = $new_patron_1->category;
104     is( ref( $patron_category), 'Koha::Patron::Category', );
105     is( $patron_category->categorycode, $category->{categorycode}, );
106 };
107
108 subtest 'siblings' => sub {
109     plan tests => 7;
110     my $siblings = $new_patron_1->siblings;
111     is( $siblings, undef, 'Koha::Patron->siblings should not crashed if the patron has no guarantor' );
112     my $guarantee_1 = $builder->build( { source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber } } );
113     my $retrieved_guarantee_1 = Koha::Patrons->find($guarantee_1);
114     $siblings = $retrieved_guarantee_1->siblings;
115     is( ref($siblings), 'Koha::Patrons', 'Koha::Patron->siblings should return a Koha::Patrons result set in a scalar context' );
116     my @siblings = $retrieved_guarantee_1->siblings;
117     is( ref( \@siblings ), 'ARRAY', 'Koha::Patron->siblings should return an array in a list context' );
118     is( $siblings->count,  0,       'guarantee_1 should not have siblings yet' );
119     my $guarantee_2 = $builder->build( { source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber } } );
120     my $guarantee_3 = $builder->build( { source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber } } );
121     $siblings = $retrieved_guarantee_1->siblings;
122     is( $siblings->count,               2,                               'guarantee_1 should have 2 siblings' );
123     is( $guarantee_2->{borrowernumber}, $siblings->next->borrowernumber, 'guarantee_2 should exist in the guarantees' );
124     is( $guarantee_3->{borrowernumber}, $siblings->next->borrowernumber, 'guarantee_3 should exist in the guarantees' );
125     $_->delete for $retrieved_guarantee_1->siblings;
126     $retrieved_guarantee_1->delete;
127 };
128
129 subtest 'has_overdues' => sub {
130     plan tests => 3;
131
132     my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
133     my $item_1 = $builder->build(
134         {   source => 'Item',
135             value  => {
136                 homebranch    => $library->{branchcode},
137                 holdingbranch => $library->{branchcode},
138                 notforloan    => 0,
139                 itemlost      => 0,
140                 withdrawn     => 0,
141                 biblionumber  => $biblioitem_1->{biblionumber}
142             }
143         }
144     );
145     my $retrieved_patron = Koha::Patrons->find( $new_patron_1->borrowernumber );
146     is( $retrieved_patron->has_overdues, 0, );
147
148     my $tomorrow = DateTime->today( time_zone => C4::Context->tz() )->add( days => 1 );
149     my $issue = Koha::Checkout->new({ borrowernumber => $new_patron_1->id, itemnumber => $item_1->{itemnumber}, date_due => $tomorrow, branchcode => $library->{branchcode} })->store();
150     is( $retrieved_patron->has_overdues, 0, );
151     $issue->delete();
152     my $yesterday = DateTime->today(time_zone => C4::Context->tz())->add( days => -1 );
153     $issue = Koha::Checkout->new({ borrowernumber => $new_patron_1->id, itemnumber => $item_1->{itemnumber}, date_due => $yesterday, branchcode => $library->{branchcode} })->store();
154     $retrieved_patron = Koha::Patrons->find( $new_patron_1->borrowernumber );
155     is( $retrieved_patron->has_overdues, 1, );
156     $issue->delete();
157 };
158
159 subtest 'update_password' => sub {
160     plan tests => 7;
161
162     t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
163     my $original_userid   = $new_patron_1->userid;
164     my $original_password = $new_patron_1->password;
165     warning_like { $retrieved_patron_1->update_password( $new_patron_2->userid, 'another_password' ) }
166     qr{Duplicate entry},
167       'Koha::Patron->update_password should warn if the userid is already used by another patron';
168     is( Koha::Patrons->find( $new_patron_1->borrowernumber )->userid,   $original_userid,   'Koha::Patron->update_password should not have updated the userid' );
169     is( Koha::Patrons->find( $new_patron_1->borrowernumber )->password, $original_password, 'Koha::Patron->update_password should not have updated the userid' );
170
171     $retrieved_patron_1->update_password( 'another_nonexistent_userid_1', 'another_password' );
172     is( Koha::Patrons->find( $new_patron_1->borrowernumber )->userid,   'another_nonexistent_userid_1', 'Koha::Patron->update_password should have updated the userid' );
173     is( Koha::Patrons->find( $new_patron_1->borrowernumber )->password, 'another_password',             'Koha::Patron->update_password should have updated the password' );
174
175     my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'CHANGE PASS', object => $new_patron_1->borrowernumber } )->count;
176     is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->update_password should have logged' );
177
178     t::lib::Mocks::mock_preference( 'BorrowersLog', 0 );
179     $retrieved_patron_1->update_password( 'yet_another_nonexistent_userid_1', 'another_password' );
180     $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'CHANGE PASS', object => $new_patron_1->borrowernumber } )->count;
181     is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->update_password should not have logged' );
182 };
183
184 subtest 'is_expired' => sub {
185     plan tests => 5;
186     my $patron = $builder->build({ source => 'Borrower' });
187     $patron = Koha::Patrons->find( $patron->{borrowernumber} );
188     $patron->dateexpiry( undef )->store->discard_changes;
189     is( $patron->is_expired, 0, 'Patron should not be considered expired if dateexpiry is not set');
190     $patron->dateexpiry( '0000-00-00' )->store->discard_changes;
191     is( $patron->is_expired, 0, 'Patron should not be considered expired if dateexpiry is not 0000-00-00');
192     $patron->dateexpiry( dt_from_string )->store->discard_changes;
193     is( $patron->is_expired, 0, 'Patron should not be considered expired if dateexpiry is today');
194     $patron->dateexpiry( dt_from_string->add( days => 1 ) )->store->discard_changes;
195     is( $patron->is_expired, 0, 'Patron should not be considered expired if dateexpiry is tomorrow');
196     $patron->dateexpiry( dt_from_string->add( days => -1 ) )->store->discard_changes;
197     is( $patron->is_expired, 1, 'Patron should be considered expired if dateexpiry is yesterday');
198
199     $patron->delete;
200 };
201
202 subtest 'is_going_to_expire' => sub {
203     plan tests => 9;
204     my $patron = $builder->build({ source => 'Borrower' });
205     $patron = Koha::Patrons->find( $patron->{borrowernumber} );
206     $patron->dateexpiry( undef )->store->discard_changes;
207     is( $patron->is_going_to_expire, 0, 'Patron should not be considered going to expire if dateexpiry is not set');
208     $patron->dateexpiry( '0000-00-00' )->store->discard_changes;
209     is( $patron->is_going_to_expire, 0, 'Patron should not be considered going to expire if dateexpiry is not 0000-00-00');
210
211     t::lib::Mocks::mock_preference('NotifyBorrowerDeparture', 0);
212     $patron->dateexpiry( dt_from_string )->store->discard_changes;
213     is( $patron->is_going_to_expire, 0, 'Patron should not be considered going to expire if dateexpiry is today');
214
215     $patron->dateexpiry( dt_from_string )->store->discard_changes;
216     is( $patron->is_going_to_expire, 0, 'Patron should not be considered going to expire if dateexpiry is today and pref is 0');
217
218     t::lib::Mocks::mock_preference('NotifyBorrowerDeparture', 10);
219     $patron->dateexpiry( dt_from_string->add( days => 11 ) )->store->discard_changes;
220     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');
221
222     t::lib::Mocks::mock_preference('NotifyBorrowerDeparture', 0);
223     $patron->dateexpiry( dt_from_string->add( days => 10 ) )->store->discard_changes;
224     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');
225
226     t::lib::Mocks::mock_preference('NotifyBorrowerDeparture', 10);
227     $patron->dateexpiry( dt_from_string->add( days => 10 ) )->store->discard_changes;
228     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');
229     $patron->delete;
230
231     t::lib::Mocks::mock_preference('NotifyBorrowerDeparture', 10);
232     $patron->dateexpiry( dt_from_string->add( days => 20 ) )->store->discard_changes;
233     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');
234
235     t::lib::Mocks::mock_preference('NotifyBorrowerDeparture', 20);
236     $patron->dateexpiry( dt_from_string->add( days => 10 ) )->store->discard_changes;
237     is( $patron->is_going_to_expire, 1, 'Patron should be considered going to expire if dateexpiry is 10 days ahead and pref is 20');
238
239     $patron->delete;
240 };
241
242
243 subtest 'renew_account' => sub {
244     plan tests => 10;
245     my $a_month_ago                = dt_from_string->add( months => -1 )->truncate( to => 'day' );
246     my $a_year_later               = dt_from_string->add( months => 12 )->truncate( to => 'day' );
247     my $a_year_later_minus_a_month = dt_from_string->add( months => 11 )->truncate( to => 'day' );
248     my $a_month_later              = dt_from_string->add( months => 1  )->truncate( to => 'day' );
249     my $a_year_later_plus_a_month  = dt_from_string->add( months => 13 )->truncate( to => 'day' );
250     my $patron_category = $builder->build(
251         {   source => 'Category',
252             value  => {
253                 enrolmentperiod     => 12,
254                 enrolmentperioddate => undef,
255             }
256         }
257     );
258     my $patron = $builder->build(
259         {   source => 'Borrower',
260             value  => {
261                 dateexpiry   => $a_month_ago,
262                 categorycode => $patron_category->{categorycode},
263             }
264         }
265     );
266     my $patron_2 = $builder->build(
267         {  source => 'Borrower',
268            value  => {
269                dateexpiry => $a_month_ago,
270                categorycode => $patron_category->{categorycode},
271             }
272         }
273     );
274     my $patron_3 = $builder->build(
275         {  source => 'Borrower',
276            value  => {
277                dateexpiry => $a_month_later,
278                categorycode => $patron_category->{categorycode},
279            }
280         }
281     );
282     my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
283     my $retrieved_patron_2 = Koha::Patrons->find( $patron_2->{borrowernumber} );
284     my $retrieved_patron_3 = Koha::Patrons->find( $patron_3->{borrowernumber} );
285
286     t::lib::Mocks::mock_preference( 'BorrowerRenewalPeriodBase', 'dateexpiry' );
287     t::lib::Mocks::mock_preference( 'BorrowersLog',              1 );
288     my $expiry_date = $retrieved_patron->renew_account;
289     is( $expiry_date, $a_year_later_minus_a_month, );
290     my $retrieved_expiry_date = Koha::Patrons->find( $patron->{borrowernumber} )->dateexpiry;
291     is( dt_from_string($retrieved_expiry_date), $a_year_later_minus_a_month );
292     my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'RENEW', object => $retrieved_patron->borrowernumber } )->count;
293     is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->renew_account should have logged' );
294
295     t::lib::Mocks::mock_preference( 'BorrowerRenewalPeriodBase', 'now' );
296     t::lib::Mocks::mock_preference( 'BorrowersLog',              0 );
297     $expiry_date = $retrieved_patron->renew_account;
298     is( $expiry_date, $a_year_later, );
299     $retrieved_expiry_date = Koha::Patrons->find( $patron->{borrowernumber} )->dateexpiry;
300     is( dt_from_string($retrieved_expiry_date), $a_year_later );
301     $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'RENEW', object => $retrieved_patron->borrowernumber } )->count;
302     is( $number_of_logs, 1, 'Without BorrowerLogs, Koha::Patron->renew_account should not have logged' );
303
304     t::lib::Mocks::mock_preference( 'BorrowerRenewalPeriodBase', 'combination' );
305     $expiry_date = $retrieved_patron_2->renew_account;
306     is( $expiry_date, $a_year_later );
307     $retrieved_expiry_date = Koha::Patrons->find( $patron_2->{borrowernumber} )->dateexpiry;
308     is( dt_from_string($retrieved_expiry_date), $a_year_later );
309
310     $expiry_date = $retrieved_patron_3->renew_account;
311     is( $expiry_date, $a_year_later_plus_a_month );
312     $retrieved_expiry_date = Koha::Patrons->find( $patron_3->{borrowernumber} )->dateexpiry;
313     is( dt_from_string($retrieved_expiry_date), $a_year_later_plus_a_month );
314
315     $retrieved_patron->delete;
316     $retrieved_patron_2->delete;
317     $retrieved_patron_3->delete;
318 };
319
320 subtest "move_to_deleted" => sub {
321     plan tests => 3;
322     my $originally_updated_on = '2016-01-01 12:12:12';
323     my $patron = $builder->build( { source => 'Borrower',value => { updated_on => $originally_updated_on } } );
324     my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
325     is( ref( $retrieved_patron->move_to_deleted ), 'Koha::Schema::Result::Deletedborrower', 'Koha::Patron->move_to_deleted should return the Deleted patron' )
326       ;    # FIXME This should be Koha::Deleted::Patron
327     my $deleted_patron = $schema->resultset('Deletedborrower')
328         ->search( { borrowernumber => $patron->{borrowernumber} }, { result_class => 'DBIx::Class::ResultClass::HashRefInflator' } )
329         ->next;
330     isnt( $deleted_patron->{updated_on}, $retrieved_patron->{updated_on}, 'Koha::Patron->move_to_deleted should have correctly updated the updated_on column');
331     $deleted_patron->{updated_on} = $originally_updated_on; #reset for simplicity in comparing all other fields
332     is_deeply( $deleted_patron, $patron, 'Koha::Patron->move_to_deleted should have correctly moved the patron to the deleted table' );
333     $retrieved_patron->delete( $patron->{borrowernumber} );    # Cleanup
334 };
335
336 subtest "delete" => sub {
337     plan tests => 5;
338     t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
339     my $patron           = $builder->build( { source => 'Borrower' } );
340     my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
341     my $hold             = $builder->build(
342         {   source => 'Reserve',
343             value  => { borrowernumber => $patron->{borrowernumber} }
344         }
345     );
346     my $list = $builder->build(
347         {   source => 'Virtualshelve',
348             value  => { owner => $patron->{borrowernumber} }
349         }
350     );
351
352     my $deleted = $retrieved_patron->delete;
353     is( $deleted, 1, 'Koha::Patron->delete should return 1 if the patron has been correctly deleted' );
354
355     is( Koha::Patrons->find( $patron->{borrowernumber} ), undef, 'Koha::Patron->delete should have deleted the patron' );
356
357     is( Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's holds| );
358
359     is( Koha::Virtualshelves->search( { owner => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's lists| );
360
361     my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'DELETE', object => $retrieved_patron->borrowernumber } )->count;
362     is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->delete should have logged' );
363 };
364
365 subtest 'add_enrolment_fee_if_needed' => sub {
366     plan tests => 4;
367
368     my $enrolmentfee_K  = 5;
369     my $enrolmentfee_J  = 10;
370     my $enrolmentfee_YA = 20;
371
372     my $dbh = C4::Context->dbh;
373     $dbh->do(q|UPDATE categories set enrolmentfee=? where categorycode=?|, undef, $enrolmentfee_K, 'K');
374     $dbh->do(q|UPDATE categories set enrolmentfee=? where categorycode=?|, undef, $enrolmentfee_J, 'J');
375     $dbh->do(q|UPDATE categories set enrolmentfee=? where categorycode=?|, undef, $enrolmentfee_YA, 'YA');
376
377     my %borrower_data = (
378         firstname    => 'my firstname',
379         surname      => 'my surname',
380         categorycode => 'K',
381         branchcode   => $library->{branchcode},
382     );
383
384     my $borrowernumber = C4::Members::AddMember(%borrower_data);
385     $borrower_data{borrowernumber} = $borrowernumber;
386
387     my ($total) = C4::Members::GetMemberAccountRecords($borrowernumber);
388     is( $total, $enrolmentfee_K, "New kid pay $enrolmentfee_K" );
389
390     t::lib::Mocks::mock_preference( 'FeeOnChangePatronCategory', 0 );
391     $borrower_data{categorycode} = 'J';
392     C4::Members::ModMember(%borrower_data);
393     ($total) = C4::Members::GetMemberAccountRecords($borrowernumber);
394     is( $total, $enrolmentfee_K, "Kid growing and become a juvenile, but shouldn't pay for the upgrade " );
395
396     $borrower_data{categorycode} = 'K';
397     C4::Members::ModMember(%borrower_data);
398     t::lib::Mocks::mock_preference( 'FeeOnChangePatronCategory', 1 );
399
400     $borrower_data{categorycode} = 'J';
401     C4::Members::ModMember(%borrower_data);
402     ($total) = C4::Members::GetMemberAccountRecords($borrowernumber);
403     is( $total, $enrolmentfee_K + $enrolmentfee_J, "Kid growing and become a juvenile, he should pay " . ( $enrolmentfee_K + $enrolmentfee_J ) );
404
405     # Check with calling directly Koha::Patron->get_enrolment_fee_if_needed
406     my $patron = Koha::Patrons->find($borrowernumber);
407     $patron->categorycode('YA')->store;
408     my $fee = $patron->add_enrolment_fee_if_needed;
409     ($total) = C4::Members::GetMemberAccountRecords($borrowernumber);
410     is( $total,
411         $enrolmentfee_K + $enrolmentfee_J + $enrolmentfee_YA,
412         "Juvenile growing and become an young adult, he should pay " . ( $enrolmentfee_K + $enrolmentfee_J + $enrolmentfee_YA )
413     );
414
415     $patron->delete;
416 };
417
418 subtest 'checkouts + get_overdues' => sub {
419     plan tests => 8;
420
421     my $library = $builder->build( { source => 'Branch' } );
422     my ($biblionumber_1) = AddBiblio( MARC::Record->new, '' );
423     my $item_1 = $builder->build(
424         {
425             source => 'Item',
426             value  => {
427                 homebranch    => $library->{branchcode},
428                 holdingbranch => $library->{branchcode},
429                 biblionumber  => $biblionumber_1
430             }
431         }
432     );
433     my $item_2 = $builder->build(
434         {
435             source => 'Item',
436             value  => {
437                 homebranch    => $library->{branchcode},
438                 holdingbranch => $library->{branchcode},
439                 biblionumber  => $biblionumber_1
440             }
441         }
442     );
443     my ($biblionumber_2) = AddBiblio( MARC::Record->new, '' );
444     my $item_3 = $builder->build(
445         {
446             source => 'Item',
447             value  => {
448                 homebranch    => $library->{branchcode},
449                 holdingbranch => $library->{branchcode},
450                 biblionumber  => $biblionumber_2
451             }
452         }
453     );
454     my $patron = $builder->build(
455         {
456             source => 'Borrower',
457             value  => { branchcode => $library->{branchcode} }
458         }
459     );
460
461     $patron = Koha::Patrons->find( $patron->{borrowernumber} );
462     my $checkouts = $patron->checkouts;
463     is( $checkouts->count, 0, 'checkouts should not return any issues for that patron' );
464     is( ref($checkouts), 'Koha::Checkouts', 'checkouts should return a Koha::Checkouts object' );
465
466     # Not sure how this is useful, but AddIssue pass this variable to different other subroutines
467     $patron = GetMember( borrowernumber => $patron->borrowernumber );
468
469     my $module = new Test::MockModule('C4::Context');
470     $module->mock( 'userenv', sub { { branch => $library->{branchcode} } } );
471
472     AddIssue( $patron, $item_1->{barcode}, DateTime->now->subtract( days => 1 ) );
473     AddIssue( $patron, $item_2->{barcode}, DateTime->now->subtract( days => 5 ) );
474     AddIssue( $patron, $item_3->{barcode} );
475
476     $patron = Koha::Patrons->find( $patron->{borrowernumber} );
477     $checkouts = $patron->checkouts;
478     is( $checkouts->count, 3, 'checkouts should return 3 issues for that patron' );
479     is( ref($checkouts), 'Koha::Checkouts', 'checkouts should return a Koha::Checkouts object' );
480
481     my $overdues = $patron->get_overdues;
482     is( $overdues->count, 2, 'Patron should have 2 overdues');
483     is( ref($overdues), 'Koha::Checkouts', 'Koha::Patron->get_overdues should return Koha::Checkouts' );
484     is( $overdues->next->itemnumber, $item_1->{itemnumber}, 'The issue should be returned in the same order as they have been done, first is correct' );
485     is( $overdues->next->itemnumber, $item_2->{itemnumber}, 'The issue should be returned in the same order as they have been done, second is correct' );
486
487     # Clean stuffs
488     Koha::Checkouts->search( { borrowernumber => $patron->borrowernumber } )->delete;
489     $patron->delete;
490 };
491
492 subtest 'get_age' => sub {
493     plan tests => 6;
494
495     my $patron = $builder->build( { source => 'Borrower' } );
496     $patron = Koha::Patrons->find( $patron->{borrowernumber} );
497
498     my $today = dt_from_string;
499
500     $patron->dateofbirth( $today->clone->add( years => -12, months => -6, days => -1 ) );
501     is( $patron->get_age, 12, 'Patron should be 12' );
502     $patron->dateofbirth( $today->clone->add( years => -18, months => 0, days => 1 ) );
503     is( $patron->get_age, 17, 'Patron should be 17, happy birthday tomorrow!' );
504     $patron->dateofbirth( $today->clone->add( years => -18, months => 0, days => 0 ) );
505     is( $patron->get_age, 18, 'Patron should be 18' );
506     $patron->dateofbirth( $today->clone->add( years => -18, months => -12, days => -31 ) );
507     is( $patron->get_age, 19, 'Patron should be 19' );
508     $patron->dateofbirth( $today->clone->add( years => -18, months => -12, days => -30 ) );
509     is( $patron->get_age, 19, 'Patron should be 19 again' );
510     $patron->dateofbirth( $today->clone->add( years => 0,   months => -1, days => -1 ) );
511     is( $patron->get_age, 0, 'Patron is a newborn child' );
512
513     $patron->delete;
514 };
515
516 subtest 'account' => sub {
517     plan tests => 1;
518
519     my $patron = $builder->build({source => 'Borrower'});
520
521     $patron = Koha::Patrons->find( $patron->{borrowernumber} );
522     my $account = $patron->account;
523     is( ref($account),   'Koha::Account', 'account should return a Koha::Account object' );
524
525     $patron->delete;
526 };
527
528 subtest 'search_upcoming_membership_expires' => sub {
529     plan tests => 9;
530
531     my $expiry_days = 15;
532     t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', $expiry_days );
533     my $nb_of_days_before = 1;
534     my $nb_of_days_after = 2;
535
536     my $builder = t::lib::TestBuilder->new();
537
538     my $library = $builder->build({ source => 'Branch' });
539
540     # before we add borrowers to this branch, add the expires we have now
541     # note that this pertains to the current mocked setting of the pref
542     # for this reason we add the new branchcode to most of the tests
543     my $nb_of_expires = Koha::Patrons->search_upcoming_membership_expires->count;
544
545     my $patron_1 = $builder->build({
546         source => 'Borrower',
547         value  => {
548             branchcode              => $library->{branchcode},
549             dateexpiry              => dt_from_string->add( days => $expiry_days )
550         },
551     });
552
553     my $patron_2 = $builder->build({
554         source => 'Borrower',
555         value  => {
556             branchcode              => $library->{branchcode},
557             dateexpiry              => dt_from_string->add( days => $expiry_days - $nb_of_days_before )
558         },
559     });
560
561     my $patron_3 = $builder->build({
562         source => 'Borrower',
563         value  => {
564             branchcode              => $library->{branchcode},
565             dateexpiry              => dt_from_string->add( days => $expiry_days + $nb_of_days_after )
566         },
567     });
568
569     # Test without extra parameters
570     my $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires();
571     is( $upcoming_mem_expires->count, $nb_of_expires + 1, 'Get upcoming membership expires should return one new borrower.' );
572
573     # Test with branch
574     $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode} });
575     is( $upcoming_mem_expires->count, 1, 'Test with branch parameter' );
576     my $expired = $upcoming_mem_expires->next;
577     is( $expired->surname, $patron_1->{surname}, 'Get upcoming membership expires should return the correct patron.' );
578     is( $expired->library->branchemail, $library->{branchemail}, 'Get upcoming membership expires should return the correct patron.' );
579     is( $expired->branchcode, $patron_1->{branchcode}, 'Get upcoming membership expires should return the correct patron.' );
580
581     t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', 0 );
582     $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode} });
583     is( $upcoming_mem_expires->count, 0, 'Get upcoming membership expires with MembershipExpiryDaysNotice==0 should not return new records.' );
584
585     # Test MembershipExpiryDaysNotice == undef
586     t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', undef );
587     $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode} });
588     is( $upcoming_mem_expires->count, 0, 'Get upcoming membership expires without MembershipExpiryDaysNotice should not return new records.' );
589
590     # Test the before parameter
591     t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', 15 );
592     $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode}, before => $nb_of_days_before });
593     is( $upcoming_mem_expires->count, 2, 'Expect two results for before');
594     # Test after parameter also
595     $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode}, before => $nb_of_days_before, after => $nb_of_days_after });
596     is( $upcoming_mem_expires->count, 3, 'Expect three results when adding after' );
597     Koha::Patrons->search({ borrowernumber => { in => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber}, $patron_3->{borrowernumber} ] } })->delete;
598 };
599
600 subtest 'holds' => sub {
601     plan tests => 3;
602
603     my $library = $builder->build( { source => 'Branch' } );
604     my ($biblionumber_1) = AddBiblio( MARC::Record->new, '' );
605     my $item_1 = $builder->build(
606         {
607             source => 'Item',
608             value  => {
609                 homebranch    => $library->{branchcode},
610                 holdingbranch => $library->{branchcode},
611                 biblionumber  => $biblionumber_1
612             }
613         }
614     );
615     my $item_2 = $builder->build(
616         {
617             source => 'Item',
618             value  => {
619                 homebranch    => $library->{branchcode},
620                 holdingbranch => $library->{branchcode},
621                 biblionumber  => $biblionumber_1
622             }
623         }
624     );
625     my ($biblionumber_2) = AddBiblio( MARC::Record->new, '' );
626     my $item_3 = $builder->build(
627         {
628             source => 'Item',
629             value  => {
630                 homebranch    => $library->{branchcode},
631                 holdingbranch => $library->{branchcode},
632                 biblionumber  => $biblionumber_2
633             }
634         }
635     );
636     my $patron = $builder->build(
637         {
638             source => 'Borrower',
639             value  => { branchcode => $library->{branchcode} }
640         }
641     );
642
643     $patron = Koha::Patrons->find( $patron->{borrowernumber} );
644     my $holds = $patron->holds;
645     is( ref($holds), 'Koha::Holds',
646         'Koha::Patron->holds should return a Koha::Holds objects' );
647     is( $holds->count, 0, 'There should not be holds placed by this patron yet' );
648
649     C4::Reserves::AddReserve( $library->{branchcode},
650         $patron->borrowernumber, $biblionumber_1 );
651     # In the future
652     C4::Reserves::AddReserve( $library->{branchcode},
653         $patron->borrowernumber, $biblionumber_2, undef, undef, dt_from_string->add( days => 2 ) );
654
655     $holds = $patron->holds;
656     is( $holds->count, 2, 'There should be 2 holds placed by this patron' );
657
658     $holds->delete;
659     $patron->delete;
660 };
661
662 $retrieved_patron_1->delete;
663 is( Koha::Patrons->search->count, $nb_of_patrons + 1, 'Delete should have deleted the patron' );
664
665 $schema->storage->txn_rollback;
666
667 1;