3 # Copyright 2015 Koha Development team
5 # This file is part of Koha
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.
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.
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>.
22 use Test::More tests => 20;
35 use Koha::Virtualshelves;
37 use t::lib::TestBuilder;
40 my $schema = Koha::Database->new->schema;
41 $schema->storage->txn_begin;
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',
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',
66 C4::Context->_new_userenv('xxx');
67 C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, 'Midway Public Library', '', '', '');
69 is( Koha::Patrons->search->count, $nb_of_patrons + 2, 'The 2 patrons should have been added' );
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' );
74 subtest 'library' => sub {
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' );
80 subtest 'guarantees' => sub {
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' );
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 }});
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;
101 subtest 'category' => sub {
103 my $patron_category = $new_patron_1->category;
104 is( ref( $patron_category), 'Koha::Patron::Category', );
105 is( $patron_category->categorycode, $category->{categorycode}, );
108 subtest 'siblings' => sub {
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;
129 subtest 'has_overdues' => sub {
132 my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
133 my $item_1 = $builder->build(
136 homebranch => $library->{branchcode},
137 holdingbranch => $library->{branchcode},
141 biblionumber => $biblioitem_1->{biblionumber}
145 my $retrieved_patron = Koha::Patrons->find( $new_patron_1->borrowernumber );
146 is( $retrieved_patron->has_overdues, 0, );
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, );
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, );
159 subtest 'update_password' => sub {
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' ) }
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' );
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' );
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' );
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' );
184 subtest 'is_expired' => sub {
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');
202 subtest 'is_going_to_expire' => sub {
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');
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');
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');
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');
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');
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');
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');
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');
243 subtest 'renew_account' => sub {
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',
253 enrolmentperiod => 12,
254 enrolmentperioddate => undef,
258 my $patron = $builder->build(
259 { source => 'Borrower',
261 dateexpiry => $a_month_ago,
262 categorycode => $patron_category->{categorycode},
266 my $patron_2 = $builder->build(
267 { source => 'Borrower',
269 dateexpiry => $a_month_ago,
270 categorycode => $patron_category->{categorycode},
274 my $patron_3 = $builder->build(
275 { source => 'Borrower',
277 dateexpiry => $a_month_later,
278 categorycode => $patron_category->{categorycode},
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} );
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' );
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' );
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 );
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 );
315 $retrieved_patron->delete;
316 $retrieved_patron_2->delete;
317 $retrieved_patron_3->delete;
320 subtest "move_to_deleted" => sub {
322 my $patron = $builder->build( { source => 'Borrower' } );
323 my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
324 is( ref( $retrieved_patron->move_to_deleted ), 'Koha::Schema::Result::Deletedborrower', 'Koha::Patron->move_to_deleted should return the Deleted patron' )
325 ; # FIXME This should be Koha::Deleted::Patron
326 my $deleted_patron = $schema->resultset('Deletedborrower')
327 ->search( { borrowernumber => $patron->{borrowernumber} }, { result_class => 'DBIx::Class::ResultClass::HashRefInflator' } )
329 is_deeply( $deleted_patron, $patron, 'Koha::Patron->move_to_deleted should have correctly moved the patron to the deleted table' );
330 $retrieved_patron->delete( $patron->{borrowernumber} ); # Cleanup
333 subtest "delete" => sub {
335 t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
336 my $patron = $builder->build( { source => 'Borrower' } );
337 my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
338 my $hold = $builder->build(
339 { source => 'Reserve',
340 value => { borrowernumber => $patron->{borrowernumber} }
343 my $list = $builder->build(
344 { source => 'Virtualshelve',
345 value => { owner => $patron->{borrowernumber} }
349 my $deleted = $retrieved_patron->delete;
350 is( $deleted, 1, 'Koha::Patron->delete should return 1 if the patron has been correctly deleted' );
352 is( Koha::Patrons->find( $patron->{borrowernumber} ), undef, 'Koha::Patron->delete should have deleted the patron' );
354 is( Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's holds| );
356 is( Koha::Virtualshelves->search( { owner => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's lists| );
358 my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'DELETE', object => $retrieved_patron->borrowernumber } )->count;
359 is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->delete should have logged' );
362 subtest 'add_enrolment_fee_if_needed' => sub {
365 my $enrolmentfee_K = 5;
366 my $enrolmentfee_J = 10;
367 my $enrolmentfee_YA = 20;
369 my $dbh = C4::Context->dbh;
370 $dbh->do(q|UPDATE categories set enrolmentfee=? where categorycode=?|, undef, $enrolmentfee_K, 'K');
371 $dbh->do(q|UPDATE categories set enrolmentfee=? where categorycode=?|, undef, $enrolmentfee_J, 'J');
372 $dbh->do(q|UPDATE categories set enrolmentfee=? where categorycode=?|, undef, $enrolmentfee_YA, 'YA');
374 my %borrower_data = (
375 firstname => 'my firstname',
376 surname => 'my surname',
378 branchcode => $library->{branchcode},
381 my $borrowernumber = C4::Members::AddMember(%borrower_data);
382 $borrower_data{borrowernumber} = $borrowernumber;
384 my ($total) = C4::Members::GetMemberAccountRecords($borrowernumber);
385 is( $total, $enrolmentfee_K, "New kid pay $enrolmentfee_K" );
387 t::lib::Mocks::mock_preference( 'FeeOnChangePatronCategory', 0 );
388 $borrower_data{categorycode} = 'J';
389 C4::Members::ModMember(%borrower_data);
390 ($total) = C4::Members::GetMemberAccountRecords($borrowernumber);
391 is( $total, $enrolmentfee_K, "Kid growing and become a juvenile, but shouldn't pay for the upgrade " );
393 $borrower_data{categorycode} = 'K';
394 C4::Members::ModMember(%borrower_data);
395 t::lib::Mocks::mock_preference( 'FeeOnChangePatronCategory', 1 );
397 $borrower_data{categorycode} = 'J';
398 C4::Members::ModMember(%borrower_data);
399 ($total) = C4::Members::GetMemberAccountRecords($borrowernumber);
400 is( $total, $enrolmentfee_K + $enrolmentfee_J, "Kid growing and become a juvenile, he should pay " . ( $enrolmentfee_K + $enrolmentfee_J ) );
402 # Check with calling directly Koha::Patron->get_enrolment_fee_if_needed
403 my $patron = Koha::Patrons->find($borrowernumber);
404 $patron->categorycode('YA')->store;
405 my $fee = $patron->add_enrolment_fee_if_needed;
406 ($total) = C4::Members::GetMemberAccountRecords($borrowernumber);
408 $enrolmentfee_K + $enrolmentfee_J + $enrolmentfee_YA,
409 "Juvenile growing and become an young adult, he should pay " . ( $enrolmentfee_K + $enrolmentfee_J + $enrolmentfee_YA )
415 subtest 'checkouts + get_overdues' => sub {
418 my $library = $builder->build( { source => 'Branch' } );
419 my ($biblionumber_1) = AddBiblio( MARC::Record->new, '' );
420 my $item_1 = $builder->build(
424 homebranch => $library->{branchcode},
425 holdingbranch => $library->{branchcode},
426 biblionumber => $biblionumber_1
430 my $item_2 = $builder->build(
434 homebranch => $library->{branchcode},
435 holdingbranch => $library->{branchcode},
436 biblionumber => $biblionumber_1
440 my ($biblionumber_2) = AddBiblio( MARC::Record->new, '' );
441 my $item_3 = $builder->build(
445 homebranch => $library->{branchcode},
446 holdingbranch => $library->{branchcode},
447 biblionumber => $biblionumber_2
451 my $patron = $builder->build(
453 source => 'Borrower',
454 value => { branchcode => $library->{branchcode} }
458 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
459 my $checkouts = $patron->checkouts;
460 is( $checkouts->count, 0, 'checkouts should not return any issues for that patron' );
461 is( ref($checkouts), 'Koha::Checkouts', 'checkouts should return a Koha::Checkouts object' );
463 # Not sure how this is useful, but AddIssue pass this variable to different other subroutines
464 $patron = GetMember( borrowernumber => $patron->borrowernumber );
466 my $module = new Test::MockModule('C4::Context');
467 $module->mock( 'userenv', sub { { branch => $library->{branchcode} } } );
469 AddIssue( $patron, $item_1->{barcode}, DateTime->now->subtract( days => 1 ) );
470 AddIssue( $patron, $item_2->{barcode}, DateTime->now->subtract( days => 5 ) );
471 AddIssue( $patron, $item_3->{barcode} );
473 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
474 $checkouts = $patron->checkouts;
475 is( $checkouts->count, 3, 'checkouts should return 3 issues for that patron' );
476 is( ref($checkouts), 'Koha::Checkouts', 'checkouts should return a Koha::Checkouts object' );
478 my $overdues = $patron->get_overdues;
479 is( $overdues->count, 2, 'Patron should have 2 overdues');
480 is( ref($overdues), 'Koha::Checkouts', 'Koha::Patron->get_overdues should return Koha::Checkouts' );
481 is( $overdues->next->itemnumber, $item_1->{itemnumber}, 'The issue should be returned in the same order as they have been done, first is correct' );
482 is( $overdues->next->itemnumber, $item_2->{itemnumber}, 'The issue should be returned in the same order as they have been done, second is correct' );
485 Koha::Checkouts->search( { borrowernumber => $patron->borrowernumber } )->delete;
489 subtest 'get_age' => sub {
492 my $patron = $builder->build( { source => 'Borrower' } );
493 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
495 my $today = dt_from_string;
497 $patron->dateofbirth( $today->clone->add( years => -12, months => -6, days => -1 ) );
498 is( $patron->get_age, 12, 'Patron should be 12' );
499 $patron->dateofbirth( $today->clone->add( years => -18, months => 0, days => 1 ) );
500 is( $patron->get_age, 17, 'Patron should be 17, happy birthday tomorrow!' );
501 $patron->dateofbirth( $today->clone->add( years => -18, months => 0, days => 0 ) );
502 is( $patron->get_age, 18, 'Patron should be 18' );
503 $patron->dateofbirth( $today->clone->add( years => -18, months => -12, days => -31 ) );
504 is( $patron->get_age, 19, 'Patron should be 19' );
505 $patron->dateofbirth( $today->clone->add( years => -18, months => -12, days => -30 ) );
506 is( $patron->get_age, 19, 'Patron should be 19 again' );
507 $patron->dateofbirth( $today->clone->add( years => 0, months => -1, days => -1 ) );
508 is( $patron->get_age, 0, 'Patron is a newborn child' );
513 subtest 'account' => sub {
516 my $patron = $builder->build({source => 'Borrower'});
518 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
519 my $account = $patron->account;
520 is( ref($account), 'Koha::Account', 'account should return a Koha::Account object' );
525 subtest 'search_upcoming_membership_expires' => sub {
528 my $expiry_days = 15;
529 t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', $expiry_days );
530 my $nb_of_days_before = 1;
531 my $nb_of_days_after = 2;
533 my $builder = t::lib::TestBuilder->new();
535 my $library = $builder->build({ source => 'Branch' });
537 # before we add borrowers to this branch, add the expires we have now
538 # note that this pertains to the current mocked setting of the pref
539 # for this reason we add the new branchcode to most of the tests
540 my $nb_of_expires = Koha::Patrons->search_upcoming_membership_expires->count;
542 my $patron_1 = $builder->build({
543 source => 'Borrower',
545 branchcode => $library->{branchcode},
546 dateexpiry => dt_from_string->add( days => $expiry_days )
550 my $patron_2 = $builder->build({
551 source => 'Borrower',
553 branchcode => $library->{branchcode},
554 dateexpiry => dt_from_string->add( days => $expiry_days - $nb_of_days_before )
558 my $patron_3 = $builder->build({
559 source => 'Borrower',
561 branchcode => $library->{branchcode},
562 dateexpiry => dt_from_string->add( days => $expiry_days + $nb_of_days_after )
566 # Test without extra parameters
567 my $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires();
568 is( $upcoming_mem_expires->count, $nb_of_expires + 1, 'Get upcoming membership expires should return one new borrower.' );
571 $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode} });
572 is( $upcoming_mem_expires->count, 1, 'Test with branch parameter' );
573 my $expired = $upcoming_mem_expires->next;
574 is( $expired->surname, $patron_1->{surname}, 'Get upcoming membership expires should return the correct patron.' );
575 is( $expired->library->branchemail, $library->{branchemail}, 'Get upcoming membership expires should return the correct patron.' );
576 is( $expired->branchcode, $patron_1->{branchcode}, 'Get upcoming membership expires should return the correct patron.' );
578 t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', 0 );
579 $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode} });
580 is( $upcoming_mem_expires->count, 0, 'Get upcoming membership expires with MembershipExpiryDaysNotice==0 should not return new records.' );
582 # Test MembershipExpiryDaysNotice == undef
583 t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', undef );
584 $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode} });
585 is( $upcoming_mem_expires->count, 0, 'Get upcoming membership expires without MembershipExpiryDaysNotice should not return new records.' );
587 # Test the before parameter
588 t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', 15 );
589 $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode}, before => $nb_of_days_before });
590 is( $upcoming_mem_expires->count, 2, 'Expect two results for before');
591 # Test after parameter also
592 $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode}, before => $nb_of_days_before, after => $nb_of_days_after });
593 is( $upcoming_mem_expires->count, 3, 'Expect three results when adding after' );
594 Koha::Patrons->search({ borrowernumber => { in => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber}, $patron_3->{borrowernumber} ] } })->delete;
597 subtest 'holds' => sub {
600 my $library = $builder->build( { source => 'Branch' } );
601 my ($biblionumber_1) = AddBiblio( MARC::Record->new, '' );
602 my $item_1 = $builder->build(
606 homebranch => $library->{branchcode},
607 holdingbranch => $library->{branchcode},
608 biblionumber => $biblionumber_1
612 my $item_2 = $builder->build(
616 homebranch => $library->{branchcode},
617 holdingbranch => $library->{branchcode},
618 biblionumber => $biblionumber_1
622 my ($biblionumber_2) = AddBiblio( MARC::Record->new, '' );
623 my $item_3 = $builder->build(
627 homebranch => $library->{branchcode},
628 holdingbranch => $library->{branchcode},
629 biblionumber => $biblionumber_2
633 my $patron = $builder->build(
635 source => 'Borrower',
636 value => { branchcode => $library->{branchcode} }
640 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
641 my $holds = $patron->holds;
642 is( ref($holds), 'Koha::Holds',
643 'Koha::Patron->holds should return a Koha::Holds objects' );
644 is( $holds->count, 0, 'There should not be holds placed by this patron yet' );
646 C4::Reserves::AddReserve( $library->{branchcode},
647 $patron->borrowernumber, $biblionumber_1 );
649 C4::Reserves::AddReserve( $library->{branchcode},
650 $patron->borrowernumber, $biblionumber_2, undef, undef, dt_from_string->add( days => 2 ) );
652 $holds = $patron->holds;
653 is( $holds->count, 2, 'There should be 2 holds placed by this patron' );
659 $retrieved_patron_1->delete;
660 is( Koha::Patrons->search->count, $nb_of_patrons + 1, 'Delete should have deleted the patron' );
662 $schema->storage->txn_rollback;