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 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
338 subtest "delete" => sub {
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} }
348 my $list = $builder->build(
349 { source => 'Virtualshelve',
350 value => { owner => $patron->{borrowernumber} }
354 my $deleted = $retrieved_patron->delete;
355 is( $deleted, 1, 'Koha::Patron->delete should return 1 if the patron has been correctly deleted' );
357 is( Koha::Patrons->find( $patron->{borrowernumber} ), undef, 'Koha::Patron->delete should have deleted the patron' );
359 is( Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's holds| );
361 is( Koha::Virtualshelves->search( { owner => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's lists| );
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' );
367 subtest 'add_enrolment_fee_if_needed' => sub {
370 my $enrolmentfee_K = 5;
371 my $enrolmentfee_J = 10;
372 my $enrolmentfee_YA = 20;
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');
379 my %borrower_data = (
380 firstname => 'my firstname',
381 surname => 'my surname',
383 branchcode => $library->{branchcode},
386 my $borrowernumber = C4::Members::AddMember(%borrower_data);
387 $borrower_data{borrowernumber} = $borrowernumber;
389 my ($total) = C4::Members::GetMemberAccountRecords($borrowernumber);
390 is( $total, $enrolmentfee_K, "New kid pay $enrolmentfee_K" );
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 " );
398 $borrower_data{categorycode} = 'K';
399 C4::Members::ModMember(%borrower_data);
400 t::lib::Mocks::mock_preference( 'FeeOnChangePatronCategory', 1 );
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 ) );
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);
413 $enrolmentfee_K + $enrolmentfee_J + $enrolmentfee_YA,
414 "Juvenile growing and become an young adult, he should pay " . ( $enrolmentfee_K + $enrolmentfee_J + $enrolmentfee_YA )
420 subtest 'checkouts + get_overdues' => sub {
423 my $library = $builder->build( { source => 'Branch' } );
424 my ($biblionumber_1) = AddBiblio( MARC::Record->new, '' );
425 my $item_1 = $builder->build(
429 homebranch => $library->{branchcode},
430 holdingbranch => $library->{branchcode},
431 biblionumber => $biblionumber_1
435 my $item_2 = $builder->build(
439 homebranch => $library->{branchcode},
440 holdingbranch => $library->{branchcode},
441 biblionumber => $biblionumber_1
445 my ($biblionumber_2) = AddBiblio( MARC::Record->new, '' );
446 my $item_3 = $builder->build(
450 homebranch => $library->{branchcode},
451 holdingbranch => $library->{branchcode},
452 biblionumber => $biblionumber_2
456 my $patron = $builder->build(
458 source => 'Borrower',
459 value => { branchcode => $library->{branchcode} }
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' );
468 # Not sure how this is useful, but AddIssue pass this variable to different other subroutines
469 $patron = GetMember( borrowernumber => $patron->borrowernumber );
471 my $module = new Test::MockModule('C4::Context');
472 $module->mock( 'userenv', sub { { branch => $library->{branchcode} } } );
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} );
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' );
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' );
490 Koha::Checkouts->search( { borrowernumber => $patron->borrowernumber } )->delete;
494 subtest 'get_age' => sub {
497 my $patron = $builder->build( { source => 'Borrower' } );
498 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
500 my $today = dt_from_string;
502 $patron->dateofbirth( $today->clone->add( years => -12, months => -6, days => -1 ) );
503 is( $patron->get_age, 12, 'Patron should be 12' );
504 $patron->dateofbirth( $today->clone->add( years => -18, months => 0, days => 1 ) );
505 is( $patron->get_age, 17, 'Patron should be 17, happy birthday tomorrow!' );
506 $patron->dateofbirth( $today->clone->add( years => -18, months => 0, days => 0 ) );
507 is( $patron->get_age, 18, 'Patron should be 18' );
508 $patron->dateofbirth( $today->clone->add( years => -18, months => -12, days => -31 ) );
509 is( $patron->get_age, 19, 'Patron should be 19' );
510 $patron->dateofbirth( $today->clone->add( years => -18, months => -12, days => -30 ) );
511 is( $patron->get_age, 19, 'Patron should be 19 again' );
512 $patron->dateofbirth( $today->clone->add( years => 0, months => -1, days => -1 ) );
513 is( $patron->get_age, 0, 'Patron is a newborn child' );
518 subtest 'account' => sub {
521 my $patron = $builder->build({source => 'Borrower'});
523 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
524 my $account = $patron->account;
525 is( ref($account), 'Koha::Account', 'account should return a Koha::Account object' );
530 subtest 'search_upcoming_membership_expires' => sub {
533 my $expiry_days = 15;
534 t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', $expiry_days );
535 my $nb_of_days_before = 1;
536 my $nb_of_days_after = 2;
538 my $builder = t::lib::TestBuilder->new();
540 my $library = $builder->build({ source => 'Branch' });
542 # before we add borrowers to this branch, add the expires we have now
543 # note that this pertains to the current mocked setting of the pref
544 # for this reason we add the new branchcode to most of the tests
545 my $nb_of_expires = Koha::Patrons->search_upcoming_membership_expires->count;
547 my $patron_1 = $builder->build({
548 source => 'Borrower',
550 branchcode => $library->{branchcode},
551 dateexpiry => dt_from_string->add( days => $expiry_days )
555 my $patron_2 = $builder->build({
556 source => 'Borrower',
558 branchcode => $library->{branchcode},
559 dateexpiry => dt_from_string->add( days => $expiry_days - $nb_of_days_before )
563 my $patron_3 = $builder->build({
564 source => 'Borrower',
566 branchcode => $library->{branchcode},
567 dateexpiry => dt_from_string->add( days => $expiry_days + $nb_of_days_after )
571 # Test without extra parameters
572 my $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires();
573 is( $upcoming_mem_expires->count, $nb_of_expires + 1, 'Get upcoming membership expires should return one new borrower.' );
576 $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode} });
577 is( $upcoming_mem_expires->count, 1, 'Test with branch parameter' );
578 my $expired = $upcoming_mem_expires->next;
579 is( $expired->surname, $patron_1->{surname}, 'Get upcoming membership expires should return the correct patron.' );
580 is( $expired->library->branchemail, $library->{branchemail}, 'Get upcoming membership expires should return the correct patron.' );
581 is( $expired->branchcode, $patron_1->{branchcode}, 'Get upcoming membership expires should return the correct patron.' );
583 t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', 0 );
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 with MembershipExpiryDaysNotice==0 should not return new records.' );
587 # Test MembershipExpiryDaysNotice == undef
588 t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', undef );
589 $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode} });
590 is( $upcoming_mem_expires->count, 0, 'Get upcoming membership expires without MembershipExpiryDaysNotice should not return new records.' );
592 # Test the before parameter
593 t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', 15 );
594 $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode}, before => $nb_of_days_before });
595 is( $upcoming_mem_expires->count, 2, 'Expect two results for before');
596 # Test after parameter also
597 $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode}, before => $nb_of_days_before, after => $nb_of_days_after });
598 is( $upcoming_mem_expires->count, 3, 'Expect three results when adding after' );
599 Koha::Patrons->search({ borrowernumber => { in => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber}, $patron_3->{borrowernumber} ] } })->delete;
602 subtest 'holds' => sub {
605 my $library = $builder->build( { source => 'Branch' } );
606 my ($biblionumber_1) = AddBiblio( MARC::Record->new, '' );
607 my $item_1 = $builder->build(
611 homebranch => $library->{branchcode},
612 holdingbranch => $library->{branchcode},
613 biblionumber => $biblionumber_1
617 my $item_2 = $builder->build(
621 homebranch => $library->{branchcode},
622 holdingbranch => $library->{branchcode},
623 biblionumber => $biblionumber_1
627 my ($biblionumber_2) = AddBiblio( MARC::Record->new, '' );
628 my $item_3 = $builder->build(
632 homebranch => $library->{branchcode},
633 holdingbranch => $library->{branchcode},
634 biblionumber => $biblionumber_2
638 my $patron = $builder->build(
640 source => 'Borrower',
641 value => { branchcode => $library->{branchcode} }
645 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
646 my $holds = $patron->holds;
647 is( ref($holds), 'Koha::Holds',
648 'Koha::Patron->holds should return a Koha::Holds objects' );
649 is( $holds->count, 0, 'There should not be holds placed by this patron yet' );
651 C4::Reserves::AddReserve( $library->{branchcode},
652 $patron->borrowernumber, $biblionumber_1 );
654 C4::Reserves::AddReserve( $library->{branchcode},
655 $patron->borrowernumber, $biblionumber_2, undef, undef, dt_from_string->add( days => 2 ) );
657 $holds = $patron->holds;
658 is( $holds->count, 2, 'There should be 2 holds placed by this patron' );
664 $retrieved_patron_1->delete;
665 is( Koha::Patrons->search->count, $nb_of_patrons + 1, 'Delete should have deleted the patron' );
667 $schema->storage->txn_rollback;