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 $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' } )
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
336 subtest "delete" => sub {
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} }
346 my $list = $builder->build(
347 { source => 'Virtualshelve',
348 value => { owner => $patron->{borrowernumber} }
352 my $deleted = $retrieved_patron->delete;
353 is( $deleted, 1, 'Koha::Patron->delete should return 1 if the patron has been correctly deleted' );
355 is( Koha::Patrons->find( $patron->{borrowernumber} ), undef, 'Koha::Patron->delete should have deleted the patron' );
357 is( Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's holds| );
359 is( Koha::Virtualshelves->search( { owner => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's lists| );
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' );
365 subtest 'add_enrolment_fee_if_needed' => sub {
368 my $enrolmentfee_K = 5;
369 my $enrolmentfee_J = 10;
370 my $enrolmentfee_YA = 20;
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');
377 my %borrower_data = (
378 firstname => 'my firstname',
379 surname => 'my surname',
381 branchcode => $library->{branchcode},
384 my $borrowernumber = C4::Members::AddMember(%borrower_data);
385 $borrower_data{borrowernumber} = $borrowernumber;
387 my ($total) = C4::Members::GetMemberAccountRecords($borrowernumber);
388 is( $total, $enrolmentfee_K, "New kid pay $enrolmentfee_K" );
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 " );
396 $borrower_data{categorycode} = 'K';
397 C4::Members::ModMember(%borrower_data);
398 t::lib::Mocks::mock_preference( 'FeeOnChangePatronCategory', 1 );
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 ) );
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);
411 $enrolmentfee_K + $enrolmentfee_J + $enrolmentfee_YA,
412 "Juvenile growing and become an young adult, he should pay " . ( $enrolmentfee_K + $enrolmentfee_J + $enrolmentfee_YA )
418 subtest 'checkouts + get_overdues' => sub {
421 my $library = $builder->build( { source => 'Branch' } );
422 my ($biblionumber_1) = AddBiblio( MARC::Record->new, '' );
423 my $item_1 = $builder->build(
427 homebranch => $library->{branchcode},
428 holdingbranch => $library->{branchcode},
429 biblionumber => $biblionumber_1
433 my $item_2 = $builder->build(
437 homebranch => $library->{branchcode},
438 holdingbranch => $library->{branchcode},
439 biblionumber => $biblionumber_1
443 my ($biblionumber_2) = AddBiblio( MARC::Record->new, '' );
444 my $item_3 = $builder->build(
448 homebranch => $library->{branchcode},
449 holdingbranch => $library->{branchcode},
450 biblionumber => $biblionumber_2
454 my $patron = $builder->build(
456 source => 'Borrower',
457 value => { branchcode => $library->{branchcode} }
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' );
466 # Not sure how this is useful, but AddIssue pass this variable to different other subroutines
467 $patron = GetMember( borrowernumber => $patron->borrowernumber );
469 my $module = new Test::MockModule('C4::Context');
470 $module->mock( 'userenv', sub { { branch => $library->{branchcode} } } );
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} );
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' );
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' );
488 Koha::Checkouts->search( { borrowernumber => $patron->borrowernumber } )->delete;
492 subtest 'get_age' => sub {
495 my $patron = $builder->build( { source => 'Borrower' } );
496 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
498 my $today = dt_from_string;
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' );
516 subtest 'account' => sub {
519 my $patron = $builder->build({source => 'Borrower'});
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' );
528 subtest 'search_upcoming_membership_expires' => sub {
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;
536 my $builder = t::lib::TestBuilder->new();
538 my $library = $builder->build({ source => 'Branch' });
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;
545 my $patron_1 = $builder->build({
546 source => 'Borrower',
548 branchcode => $library->{branchcode},
549 dateexpiry => dt_from_string->add( days => $expiry_days )
553 my $patron_2 = $builder->build({
554 source => 'Borrower',
556 branchcode => $library->{branchcode},
557 dateexpiry => dt_from_string->add( days => $expiry_days - $nb_of_days_before )
561 my $patron_3 = $builder->build({
562 source => 'Borrower',
564 branchcode => $library->{branchcode},
565 dateexpiry => dt_from_string->add( days => $expiry_days + $nb_of_days_after )
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.' );
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.' );
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.' );
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.' );
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;
600 subtest 'holds' => sub {
603 my $library = $builder->build( { source => 'Branch' } );
604 my ($biblionumber_1) = AddBiblio( MARC::Record->new, '' );
605 my $item_1 = $builder->build(
609 homebranch => $library->{branchcode},
610 holdingbranch => $library->{branchcode},
611 biblionumber => $biblionumber_1
615 my $item_2 = $builder->build(
619 homebranch => $library->{branchcode},
620 holdingbranch => $library->{branchcode},
621 biblionumber => $biblionumber_1
625 my ($biblionumber_2) = AddBiblio( MARC::Record->new, '' );
626 my $item_3 = $builder->build(
630 homebranch => $library->{branchcode},
631 holdingbranch => $library->{branchcode},
632 biblionumber => $biblionumber_2
636 my $patron = $builder->build(
638 source => 'Borrower',
639 value => { branchcode => $library->{branchcode} }
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' );
649 C4::Reserves::AddReserve( $library->{branchcode},
650 $patron->borrowernumber, $biblionumber_1 );
652 C4::Reserves::AddReserve( $library->{branchcode},
653 $patron->borrowernumber, $biblionumber_2, undef, undef, dt_from_string->add( days => 2 ) );
655 $holds = $patron->holds;
656 is( $holds->count, 2, 'There should be 2 holds placed by this patron' );
662 $retrieved_patron_1->delete;
663 is( Koha::Patrons->search->count, $nb_of_patrons + 1, 'Delete should have deleted the patron' );
665 $schema->storage->txn_rollback;