Bug 17933: Add test and return unless defined dob
[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 => 5;
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     ok( $retrieved_patron->updated_on, 'updated_on should be set for borrowers table' );
331     ok( $deleted_patron->{updated_on}, 'updated_on should be set for deleted_borrowers table' );
332     isnt( $deleted_patron->{updated_on}, $retrieved_patron->updated_on, 'Koha::Patron->move_to_deleted should have correctly updated the updated_on column');
333     $deleted_patron->{updated_on} = $originally_updated_on; #reset for simplicity in comparing all other fields
334     is_deeply( $deleted_patron, $patron, 'Koha::Patron->move_to_deleted should have correctly moved the patron to the deleted table' );
335     $retrieved_patron->delete( $patron->{borrowernumber} );    # Cleanup
336 };
337
338 subtest "delete" => sub {
339     plan tests => 5;
340     t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
341     my $patron           = $builder->build( { source => 'Borrower' } );
342     my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
343     my $hold             = $builder->build(
344         {   source => 'Reserve',
345             value  => { borrowernumber => $patron->{borrowernumber} }
346         }
347     );
348     my $list = $builder->build(
349         {   source => 'Virtualshelve',
350             value  => { owner => $patron->{borrowernumber} }
351         }
352     );
353
354     my $deleted = $retrieved_patron->delete;
355     is( $deleted, 1, 'Koha::Patron->delete should return 1 if the patron has been correctly deleted' );
356
357     is( Koha::Patrons->find( $patron->{borrowernumber} ), undef, 'Koha::Patron->delete should have deleted the patron' );
358
359     is( Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's holds| );
360
361     is( Koha::Virtualshelves->search( { owner => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's lists| );
362
363     my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'DELETE', object => $retrieved_patron->borrowernumber } )->count;
364     is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->delete should have logged' );
365 };
366
367 subtest 'add_enrolment_fee_if_needed' => sub {
368     plan tests => 4;
369
370     my $enrolmentfee_K  = 5;
371     my $enrolmentfee_J  = 10;
372     my $enrolmentfee_YA = 20;
373
374     my $dbh = C4::Context->dbh;
375     $dbh->do(q|UPDATE categories set enrolmentfee=? where categorycode=?|, undef, $enrolmentfee_K, 'K');
376     $dbh->do(q|UPDATE categories set enrolmentfee=? where categorycode=?|, undef, $enrolmentfee_J, 'J');
377     $dbh->do(q|UPDATE categories set enrolmentfee=? where categorycode=?|, undef, $enrolmentfee_YA, 'YA');
378
379     my %borrower_data = (
380         firstname    => 'my firstname',
381         surname      => 'my surname',
382         categorycode => 'K',
383         branchcode   => $library->{branchcode},
384     );
385
386     my $borrowernumber = C4::Members::AddMember(%borrower_data);
387     $borrower_data{borrowernumber} = $borrowernumber;
388
389     my ($total) = C4::Members::GetMemberAccountRecords($borrowernumber);
390     is( $total, $enrolmentfee_K, "New kid pay $enrolmentfee_K" );
391
392     t::lib::Mocks::mock_preference( 'FeeOnChangePatronCategory', 0 );
393     $borrower_data{categorycode} = 'J';
394     C4::Members::ModMember(%borrower_data);
395     ($total) = C4::Members::GetMemberAccountRecords($borrowernumber);
396     is( $total, $enrolmentfee_K, "Kid growing and become a juvenile, but shouldn't pay for the upgrade " );
397
398     $borrower_data{categorycode} = 'K';
399     C4::Members::ModMember(%borrower_data);
400     t::lib::Mocks::mock_preference( 'FeeOnChangePatronCategory', 1 );
401
402     $borrower_data{categorycode} = 'J';
403     C4::Members::ModMember(%borrower_data);
404     ($total) = C4::Members::GetMemberAccountRecords($borrowernumber);
405     is( $total, $enrolmentfee_K + $enrolmentfee_J, "Kid growing and become a juvenile, he should pay " . ( $enrolmentfee_K + $enrolmentfee_J ) );
406
407     # Check with calling directly Koha::Patron->get_enrolment_fee_if_needed
408     my $patron = Koha::Patrons->find($borrowernumber);
409     $patron->categorycode('YA')->store;
410     my $fee = $patron->add_enrolment_fee_if_needed;
411     ($total) = C4::Members::GetMemberAccountRecords($borrowernumber);
412     is( $total,
413         $enrolmentfee_K + $enrolmentfee_J + $enrolmentfee_YA,
414         "Juvenile growing and become an young adult, he should pay " . ( $enrolmentfee_K + $enrolmentfee_J + $enrolmentfee_YA )
415     );
416
417     $patron->delete;
418 };
419
420 subtest 'checkouts + get_overdues' => sub {
421     plan tests => 8;
422
423     my $library = $builder->build( { source => 'Branch' } );
424     my ($biblionumber_1) = AddBiblio( MARC::Record->new, '' );
425     my $item_1 = $builder->build(
426         {
427             source => 'Item',
428             value  => {
429                 homebranch    => $library->{branchcode},
430                 holdingbranch => $library->{branchcode},
431                 biblionumber  => $biblionumber_1
432             }
433         }
434     );
435     my $item_2 = $builder->build(
436         {
437             source => 'Item',
438             value  => {
439                 homebranch    => $library->{branchcode},
440                 holdingbranch => $library->{branchcode},
441                 biblionumber  => $biblionumber_1
442             }
443         }
444     );
445     my ($biblionumber_2) = AddBiblio( MARC::Record->new, '' );
446     my $item_3 = $builder->build(
447         {
448             source => 'Item',
449             value  => {
450                 homebranch    => $library->{branchcode},
451                 holdingbranch => $library->{branchcode},
452                 biblionumber  => $biblionumber_2
453             }
454         }
455     );
456     my $patron = $builder->build(
457         {
458             source => 'Borrower',
459             value  => { branchcode => $library->{branchcode} }
460         }
461     );
462
463     $patron = Koha::Patrons->find( $patron->{borrowernumber} );
464     my $checkouts = $patron->checkouts;
465     is( $checkouts->count, 0, 'checkouts should not return any issues for that patron' );
466     is( ref($checkouts), 'Koha::Checkouts', 'checkouts should return a Koha::Checkouts object' );
467
468     # Not sure how this is useful, but AddIssue pass this variable to different other subroutines
469     $patron = GetMember( borrowernumber => $patron->borrowernumber );
470
471     my $module = new Test::MockModule('C4::Context');
472     $module->mock( 'userenv', sub { { branch => $library->{branchcode} } } );
473
474     AddIssue( $patron, $item_1->{barcode}, DateTime->now->subtract( days => 1 ) );
475     AddIssue( $patron, $item_2->{barcode}, DateTime->now->subtract( days => 5 ) );
476     AddIssue( $patron, $item_3->{barcode} );
477
478     $patron = Koha::Patrons->find( $patron->{borrowernumber} );
479     $checkouts = $patron->checkouts;
480     is( $checkouts->count, 3, 'checkouts should return 3 issues for that patron' );
481     is( ref($checkouts), 'Koha::Checkouts', 'checkouts should return a Koha::Checkouts object' );
482
483     my $overdues = $patron->get_overdues;
484     is( $overdues->count, 2, 'Patron should have 2 overdues');
485     is( ref($overdues), 'Koha::Checkouts', 'Koha::Patron->get_overdues should return Koha::Checkouts' );
486     is( $overdues->next->itemnumber, $item_1->{itemnumber}, 'The issue should be returned in the same order as they have been done, first is correct' );
487     is( $overdues->next->itemnumber, $item_2->{itemnumber}, 'The issue should be returned in the same order as they have been done, second is correct' );
488
489     # Clean stuffs
490     Koha::Checkouts->search( { borrowernumber => $patron->borrowernumber } )->delete;
491     $patron->delete;
492 };
493
494 subtest 'get_age' => sub {
495     plan tests => 7;
496
497     my $patron = $builder->build( { source => 'Borrower' } );
498     $patron = Koha::Patrons->find( $patron->{borrowernumber} );
499
500     my $today = dt_from_string;
501
502     $patron->dateofbirth( undef );
503     is( $patron->get_age, undef, 'get_age should return undef if no dateofbirth is defined' );
504     $patron->dateofbirth( $today->clone->add( years => -12, months => -6, days => -1 ) );
505     is( $patron->get_age, 12, 'Patron should be 12' );
506     $patron->dateofbirth( $today->clone->add( years => -18, months => 0, days => 1 ) );
507     is( $patron->get_age, 17, 'Patron should be 17, happy birthday tomorrow!' );
508     $patron->dateofbirth( $today->clone->add( years => -18, months => 0, days => 0 ) );
509     is( $patron->get_age, 18, 'Patron should be 18' );
510     $patron->dateofbirth( $today->clone->add( years => -18, months => -12, days => -31 ) );
511     is( $patron->get_age, 19, 'Patron should be 19' );
512     $patron->dateofbirth( $today->clone->add( years => -18, months => -12, days => -30 ) );
513     is( $patron->get_age, 19, 'Patron should be 19 again' );
514     $patron->dateofbirth( $today->clone->add( years => 0,   months => -1, days => -1 ) );
515     is( $patron->get_age, 0, 'Patron is a newborn child' );
516
517     $patron->delete;
518 };
519
520 subtest 'account' => sub {
521     plan tests => 1;
522
523     my $patron = $builder->build({source => 'Borrower'});
524
525     $patron = Koha::Patrons->find( $patron->{borrowernumber} );
526     my $account = $patron->account;
527     is( ref($account),   'Koha::Account', 'account should return a Koha::Account object' );
528
529     $patron->delete;
530 };
531
532 subtest 'search_upcoming_membership_expires' => sub {
533     plan tests => 9;
534
535     my $expiry_days = 15;
536     t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', $expiry_days );
537     my $nb_of_days_before = 1;
538     my $nb_of_days_after = 2;
539
540     my $builder = t::lib::TestBuilder->new();
541
542     my $library = $builder->build({ source => 'Branch' });
543
544     # before we add borrowers to this branch, add the expires we have now
545     # note that this pertains to the current mocked setting of the pref
546     # for this reason we add the new branchcode to most of the tests
547     my $nb_of_expires = Koha::Patrons->search_upcoming_membership_expires->count;
548
549     my $patron_1 = $builder->build({
550         source => 'Borrower',
551         value  => {
552             branchcode              => $library->{branchcode},
553             dateexpiry              => dt_from_string->add( days => $expiry_days )
554         },
555     });
556
557     my $patron_2 = $builder->build({
558         source => 'Borrower',
559         value  => {
560             branchcode              => $library->{branchcode},
561             dateexpiry              => dt_from_string->add( days => $expiry_days - $nb_of_days_before )
562         },
563     });
564
565     my $patron_3 = $builder->build({
566         source => 'Borrower',
567         value  => {
568             branchcode              => $library->{branchcode},
569             dateexpiry              => dt_from_string->add( days => $expiry_days + $nb_of_days_after )
570         },
571     });
572
573     # Test without extra parameters
574     my $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires();
575     is( $upcoming_mem_expires->count, $nb_of_expires + 1, 'Get upcoming membership expires should return one new borrower.' );
576
577     # Test with branch
578     $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode} });
579     is( $upcoming_mem_expires->count, 1, 'Test with branch parameter' );
580     my $expired = $upcoming_mem_expires->next;
581     is( $expired->surname, $patron_1->{surname}, 'Get upcoming membership expires should return the correct patron.' );
582     is( $expired->library->branchemail, $library->{branchemail}, 'Get upcoming membership expires should return the correct patron.' );
583     is( $expired->branchcode, $patron_1->{branchcode}, 'Get upcoming membership expires should return the correct patron.' );
584
585     t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', 0 );
586     $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode} });
587     is( $upcoming_mem_expires->count, 0, 'Get upcoming membership expires with MembershipExpiryDaysNotice==0 should not return new records.' );
588
589     # Test MembershipExpiryDaysNotice == undef
590     t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', undef );
591     $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode} });
592     is( $upcoming_mem_expires->count, 0, 'Get upcoming membership expires without MembershipExpiryDaysNotice should not return new records.' );
593
594     # Test the before parameter
595     t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', 15 );
596     $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode}, before => $nb_of_days_before });
597     is( $upcoming_mem_expires->count, 2, 'Expect two results for before');
598     # Test after parameter also
599     $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode}, before => $nb_of_days_before, after => $nb_of_days_after });
600     is( $upcoming_mem_expires->count, 3, 'Expect three results when adding after' );
601     Koha::Patrons->search({ borrowernumber => { in => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber}, $patron_3->{borrowernumber} ] } })->delete;
602 };
603
604 subtest 'holds' => sub {
605     plan tests => 3;
606
607     my $library = $builder->build( { source => 'Branch' } );
608     my ($biblionumber_1) = AddBiblio( MARC::Record->new, '' );
609     my $item_1 = $builder->build(
610         {
611             source => 'Item',
612             value  => {
613                 homebranch    => $library->{branchcode},
614                 holdingbranch => $library->{branchcode},
615                 biblionumber  => $biblionumber_1
616             }
617         }
618     );
619     my $item_2 = $builder->build(
620         {
621             source => 'Item',
622             value  => {
623                 homebranch    => $library->{branchcode},
624                 holdingbranch => $library->{branchcode},
625                 biblionumber  => $biblionumber_1
626             }
627         }
628     );
629     my ($biblionumber_2) = AddBiblio( MARC::Record->new, '' );
630     my $item_3 = $builder->build(
631         {
632             source => 'Item',
633             value  => {
634                 homebranch    => $library->{branchcode},
635                 holdingbranch => $library->{branchcode},
636                 biblionumber  => $biblionumber_2
637             }
638         }
639     );
640     my $patron = $builder->build(
641         {
642             source => 'Borrower',
643             value  => { branchcode => $library->{branchcode} }
644         }
645     );
646
647     $patron = Koha::Patrons->find( $patron->{borrowernumber} );
648     my $holds = $patron->holds;
649     is( ref($holds), 'Koha::Holds',
650         'Koha::Patron->holds should return a Koha::Holds objects' );
651     is( $holds->count, 0, 'There should not be holds placed by this patron yet' );
652
653     C4::Reserves::AddReserve( $library->{branchcode},
654         $patron->borrowernumber, $biblionumber_1 );
655     # In the future
656     C4::Reserves::AddReserve( $library->{branchcode},
657         $patron->borrowernumber, $biblionumber_2, undef, undef, dt_from_string->add( days => 2 ) );
658
659     $holds = $patron->holds;
660     is( $holds->count, 2, 'There should be 2 holds placed by this patron' );
661
662     $holds->delete;
663     $patron->delete;
664 };
665
666 $retrieved_patron_1->delete;
667 is( Koha::Patrons->search->count, $nb_of_patrons + 1, 'Delete should have deleted the patron' );
668
669 $schema->storage->txn_rollback;
670
671 1;