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 => 26;
37 use Koha::Patron::Categories;
40 use Koha::Virtualshelves;
42 use t::lib::TestBuilder;
45 my $schema = Koha::Database->new->schema;
46 $schema->storage->txn_begin;
48 my $builder = t::lib::TestBuilder->new;
49 my $library = $builder->build({source => 'Branch' });
50 my $category = $builder->build({source => 'Category' });
51 my $nb_of_patrons = Koha::Patrons->search->count;
52 my $new_patron_1 = Koha::Patron->new(
53 { cardnumber => 'test_cn_1',
54 branchcode => $library->{branchcode},
55 categorycode => $category->{categorycode},
56 surname => 'surname for patron1',
57 firstname => 'firstname for patron1',
58 userid => 'a_nonexistent_userid_1',
59 flags => 1, # Is superlibrarian
62 my $new_patron_2 = Koha::Patron->new(
63 { cardnumber => 'test_cn_2',
64 branchcode => $library->{branchcode},
65 categorycode => $category->{categorycode},
66 surname => 'surname for patron2',
67 firstname => 'firstname for patron2',
68 userid => 'a_nonexistent_userid_2',
72 C4::Context->_new_userenv('xxx');
73 set_logged_in_user( $new_patron_1 );
75 is( Koha::Patrons->search->count, $nb_of_patrons + 2, 'The 2 patrons should have been added' );
77 my $retrieved_patron_1 = Koha::Patrons->find( $new_patron_1->borrowernumber );
78 is( $retrieved_patron_1->cardnumber, $new_patron_1->cardnumber, 'Find a patron by borrowernumber should return the correct patron' );
80 subtest 'library' => sub {
82 is( $retrieved_patron_1->library->branchcode, $library->{branchcode}, 'Koha::Patron->library should return the correct library' );
83 is( ref($retrieved_patron_1->library), 'Koha::Library', 'Koha::Patron->library should return a Koha::Library object' );
86 subtest 'guarantees' => sub {
88 my $guarantees = $new_patron_1->guarantees;
89 is( ref($guarantees), 'Koha::Patrons', 'Koha::Patron->guarantees should return a Koha::Patrons result set in a scalar context' );
90 is( $guarantees->count, 0, 'new_patron_1 should have 0 guarantee' );
91 my @guarantees = $new_patron_1->guarantees;
92 is( ref(\@guarantees), 'ARRAY', 'Koha::Patron->guarantees should return an array in a list context' );
93 is( scalar(@guarantees), 0, 'new_patron_1 should have 0 guarantee' );
95 my $guarantee_1 = $builder->build({ source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber }});
96 my $guarantee_2 = $builder->build({ source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber }});
98 $guarantees = $new_patron_1->guarantees;
99 is( ref($guarantees), 'Koha::Patrons', 'Koha::Patron->guarantees should return a Koha::Patrons result set in a scalar context' );
100 is( $guarantees->count, 2, 'new_patron_1 should have 2 guarantees' );
101 @guarantees = $new_patron_1->guarantees;
102 is( ref(\@guarantees), 'ARRAY', 'Koha::Patron->guarantees should return an array in a list context' );
103 is( scalar(@guarantees), 2, 'new_patron_1 should have 2 guarantees' );
104 $_->delete for @guarantees;
107 subtest 'category' => sub {
109 my $patron_category = $new_patron_1->category;
110 is( ref( $patron_category), 'Koha::Patron::Category', );
111 is( $patron_category->categorycode, $category->{categorycode}, );
114 subtest 'siblings' => sub {
116 my $siblings = $new_patron_1->siblings;
117 is( $siblings, undef, 'Koha::Patron->siblings should not crashed if the patron has no guarantor' );
118 my $guarantee_1 = $builder->build( { source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber } } );
119 my $retrieved_guarantee_1 = Koha::Patrons->find($guarantee_1);
120 $siblings = $retrieved_guarantee_1->siblings;
121 is( ref($siblings), 'Koha::Patrons', 'Koha::Patron->siblings should return a Koha::Patrons result set in a scalar context' );
122 my @siblings = $retrieved_guarantee_1->siblings;
123 is( ref( \@siblings ), 'ARRAY', 'Koha::Patron->siblings should return an array in a list context' );
124 is( $siblings->count, 0, 'guarantee_1 should not have siblings yet' );
125 my $guarantee_2 = $builder->build( { source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber } } );
126 my $guarantee_3 = $builder->build( { source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber } } );
127 $siblings = $retrieved_guarantee_1->siblings;
128 is( $siblings->count, 2, 'guarantee_1 should have 2 siblings' );
129 is( $guarantee_2->{borrowernumber}, $siblings->next->borrowernumber, 'guarantee_2 should exist in the guarantees' );
130 is( $guarantee_3->{borrowernumber}, $siblings->next->borrowernumber, 'guarantee_3 should exist in the guarantees' );
131 $_->delete for $retrieved_guarantee_1->siblings;
132 $retrieved_guarantee_1->delete;
135 subtest 'has_overdues' => sub {
138 my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
139 my $item_1 = $builder->build(
142 homebranch => $library->{branchcode},
143 holdingbranch => $library->{branchcode},
147 biblionumber => $biblioitem_1->{biblionumber}
151 my $retrieved_patron = Koha::Patrons->find( $new_patron_1->borrowernumber );
152 is( $retrieved_patron->has_overdues, 0, );
154 my $tomorrow = DateTime->today( time_zone => C4::Context->tz() )->add( days => 1 );
155 my $issue = Koha::Checkout->new({ borrowernumber => $new_patron_1->id, itemnumber => $item_1->{itemnumber}, date_due => $tomorrow, branchcode => $library->{branchcode} })->store();
156 is( $retrieved_patron->has_overdues, 0, );
158 my $yesterday = DateTime->today(time_zone => C4::Context->tz())->add( days => -1 );
159 $issue = Koha::Checkout->new({ borrowernumber => $new_patron_1->id, itemnumber => $item_1->{itemnumber}, date_due => $yesterday, branchcode => $library->{branchcode} })->store();
160 $retrieved_patron = Koha::Patrons->find( $new_patron_1->borrowernumber );
161 is( $retrieved_patron->has_overdues, 1, );
165 subtest 'update_password' => sub {
168 t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
169 my $original_userid = $new_patron_1->userid;
170 my $original_password = $new_patron_1->password;
171 warning_like { $retrieved_patron_1->update_password( $new_patron_2->userid, 'another_password' ) }
173 'Koha::Patron->update_password should warn if the userid is already used by another patron';
174 is( Koha::Patrons->find( $new_patron_1->borrowernumber )->userid, $original_userid, 'Koha::Patron->update_password should not have updated the userid' );
175 is( Koha::Patrons->find( $new_patron_1->borrowernumber )->password, $original_password, 'Koha::Patron->update_password should not have updated the userid' );
177 $retrieved_patron_1->update_password( 'another_nonexistent_userid_1', 'another_password' );
178 is( Koha::Patrons->find( $new_patron_1->borrowernumber )->userid, 'another_nonexistent_userid_1', 'Koha::Patron->update_password should have updated the userid' );
179 is( Koha::Patrons->find( $new_patron_1->borrowernumber )->password, 'another_password', 'Koha::Patron->update_password should have updated the password' );
181 my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'CHANGE PASS', object => $new_patron_1->borrowernumber } )->count;
182 is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->update_password should have logged' );
184 t::lib::Mocks::mock_preference( 'BorrowersLog', 0 );
185 $retrieved_patron_1->update_password( 'yet_another_nonexistent_userid_1', 'another_password' );
186 $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'CHANGE PASS', object => $new_patron_1->borrowernumber } )->count;
187 is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->update_password should not have logged' );
190 subtest 'is_expired' => sub {
192 my $patron = $builder->build({ source => 'Borrower' });
193 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
194 $patron->dateexpiry( undef )->store->discard_changes;
195 is( $patron->is_expired, 0, 'Patron should not be considered expired if dateexpiry is not set');
196 $patron->dateexpiry( dt_from_string )->store->discard_changes;
197 is( $patron->is_expired, 0, 'Patron should not be considered expired if dateexpiry is today');
198 $patron->dateexpiry( dt_from_string->add( days => 1 ) )->store->discard_changes;
199 is( $patron->is_expired, 0, 'Patron should not be considered expired if dateexpiry is tomorrow');
200 $patron->dateexpiry( dt_from_string->add( days => -1 ) )->store->discard_changes;
201 is( $patron->is_expired, 1, 'Patron should be considered expired if dateexpiry is yesterday');
206 subtest 'is_going_to_expire' => sub {
208 my $patron = $builder->build({ source => 'Borrower' });
209 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
210 $patron->dateexpiry( undef )->store->discard_changes;
211 is( $patron->is_going_to_expire, 0, 'Patron should not be considered going to expire if dateexpiry is not set');
213 t::lib::Mocks::mock_preference('NotifyBorrowerDeparture', 0);
214 $patron->dateexpiry( dt_from_string )->store->discard_changes;
215 is( $patron->is_going_to_expire, 0, 'Patron should not be considered going to expire if dateexpiry is today');
217 $patron->dateexpiry( dt_from_string )->store->discard_changes;
218 is( $patron->is_going_to_expire, 0, 'Patron should not be considered going to expire if dateexpiry is today and pref is 0');
220 t::lib::Mocks::mock_preference('NotifyBorrowerDeparture', 10);
221 $patron->dateexpiry( dt_from_string->add( days => 11 ) )->store->discard_changes;
222 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');
224 t::lib::Mocks::mock_preference('NotifyBorrowerDeparture', 0);
225 $patron->dateexpiry( dt_from_string->add( days => 10 ) )->store->discard_changes;
226 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');
228 t::lib::Mocks::mock_preference('NotifyBorrowerDeparture', 10);
229 $patron->dateexpiry( dt_from_string->add( days => 10 ) )->store->discard_changes;
230 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');
233 t::lib::Mocks::mock_preference('NotifyBorrowerDeparture', 10);
234 $patron->dateexpiry( dt_from_string->add( days => 20 ) )->store->discard_changes;
235 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');
237 t::lib::Mocks::mock_preference('NotifyBorrowerDeparture', 20);
238 $patron->dateexpiry( dt_from_string->add( days => 10 ) )->store->discard_changes;
239 is( $patron->is_going_to_expire, 1, 'Patron should be considered going to expire if dateexpiry is 10 days ahead and pref is 20');
245 subtest 'renew_account' => sub {
248 for my $date ( '2016-03-31', '2016-11-30', dt_from_string() ) {
249 my $dt = dt_from_string( $date, 'iso' );
250 Time::Fake->offset( $dt->epoch );
251 my $a_month_ago = $dt->clone->subtract( months => 1, end_of_month => 'limit' )->truncate( to => 'day' );
252 my $a_year_later = $dt->clone->add( months => 12, end_of_month => 'limit' )->truncate( to => 'day' );
253 my $a_year_later_minus_a_month = $dt->clone->add( months => 11, end_of_month => 'limit' )->truncate( to => 'day' );
254 my $a_month_later = $dt->clone->add( months => 1 , end_of_month => 'limit' )->truncate( to => 'day' );
255 my $a_year_later_plus_a_month = $dt->clone->add( months => 13, end_of_month => 'limit' )->truncate( to => 'day' );
256 my $patron_category = $builder->build(
257 { source => 'Category',
259 enrolmentperiod => 12,
260 enrolmentperioddate => undef,
264 my $patron = $builder->build(
265 { source => 'Borrower',
267 dateexpiry => $a_month_ago,
268 categorycode => $patron_category->{categorycode},
269 date_renewed => undef, # Force builder to not populate the column for new patron
273 my $patron_2 = $builder->build(
274 { source => 'Borrower',
276 dateexpiry => $a_month_ago,
277 categorycode => $patron_category->{categorycode},
281 my $patron_3 = $builder->build(
282 { source => 'Borrower',
284 dateexpiry => $a_month_later,
285 categorycode => $patron_category->{categorycode},
289 my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
290 my $retrieved_patron_2 = Koha::Patrons->find( $patron_2->{borrowernumber} );
291 my $retrieved_patron_3 = Koha::Patrons->find( $patron_3->{borrowernumber} );
293 is( $retrieved_patron->date_renewed, undef, "Date renewed is not set for patrons that have never been renewed" );
295 t::lib::Mocks::mock_preference( 'BorrowerRenewalPeriodBase', 'dateexpiry' );
296 t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
297 my $expiry_date = $retrieved_patron->renew_account;
298 is( $expiry_date, $a_year_later_minus_a_month, "$a_month_ago + 12 months must be $a_year_later_minus_a_month" );
299 my $retrieved_expiry_date = Koha::Patrons->find( $patron->{borrowernumber} )->dateexpiry;
300 is( dt_from_string($retrieved_expiry_date), $a_year_later_minus_a_month, "$a_month_ago + 12 months must be $a_year_later_minus_a_month" );
301 my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'RENEW', object => $retrieved_patron->borrowernumber } )->count;
302 is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->renew_account should have logged' );
304 t::lib::Mocks::mock_preference( 'BorrowerRenewalPeriodBase', 'now' );
305 t::lib::Mocks::mock_preference( 'BorrowersLog', 0 );
306 $expiry_date = $retrieved_patron->renew_account;
307 is( $expiry_date, $a_year_later, "today + 12 months must be $a_year_later" );
308 $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
309 is( $retrieved_patron->date_renewed, output_pref({ dt => $dt, dateformat => 'iso', dateonly => 1 }), "Date renewed is set when calling renew_account" );
310 $retrieved_expiry_date = $retrieved_patron->dateexpiry;
311 is( dt_from_string($retrieved_expiry_date), $a_year_later, "today + 12 months must be $a_year_later" );
312 $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'RENEW', object => $retrieved_patron->borrowernumber } )->count;
313 is( $number_of_logs, 1, 'Without BorrowerLogs, Koha::Patron->renew_account should not have logged' );
315 t::lib::Mocks::mock_preference( 'BorrowerRenewalPeriodBase', 'combination' );
316 $expiry_date = $retrieved_patron_2->renew_account;
317 is( $expiry_date, $a_year_later, "today + 12 months must be $a_year_later" );
318 $retrieved_expiry_date = Koha::Patrons->find( $patron_2->{borrowernumber} )->dateexpiry;
319 is( dt_from_string($retrieved_expiry_date), $a_year_later, "today + 12 months must be $a_year_later" );
321 $expiry_date = $retrieved_patron_3->renew_account;
322 is( $expiry_date, $a_year_later_plus_a_month, "$a_month_later + 12 months must be $a_year_later_plus_a_month" );
323 $retrieved_expiry_date = Koha::Patrons->find( $patron_3->{borrowernumber} )->dateexpiry;
324 is( dt_from_string($retrieved_expiry_date), $a_year_later_plus_a_month, "$a_month_later + 12 months must be $a_year_later_plus_a_month" );
326 $retrieved_patron->delete;
327 $retrieved_patron_2->delete;
328 $retrieved_patron_3->delete;
333 subtest "move_to_deleted" => sub {
335 my $originally_updated_on = '2016-01-01 12:12:12';
336 my $patron = $builder->build( { source => 'Borrower',value => { updated_on => $originally_updated_on } } );
337 my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
338 is( ref( $retrieved_patron->move_to_deleted ), 'Koha::Schema::Result::Deletedborrower', 'Koha::Patron->move_to_deleted should return the Deleted patron' )
339 ; # FIXME This should be Koha::Deleted::Patron
340 my $deleted_patron = $schema->resultset('Deletedborrower')
341 ->search( { borrowernumber => $patron->{borrowernumber} }, { result_class => 'DBIx::Class::ResultClass::HashRefInflator' } )
343 ok( $retrieved_patron->updated_on, 'updated_on should be set for borrowers table' );
344 ok( $deleted_patron->{updated_on}, 'updated_on should be set for deleted_borrowers table' );
345 isnt( $deleted_patron->{updated_on}, $retrieved_patron->updated_on, 'Koha::Patron->move_to_deleted should have correctly updated the updated_on column');
346 $deleted_patron->{updated_on} = $originally_updated_on; #reset for simplicity in comparing all other fields
347 is_deeply( $deleted_patron, $patron, 'Koha::Patron->move_to_deleted should have correctly moved the patron to the deleted table' );
348 $retrieved_patron->delete( $patron->{borrowernumber} ); # Cleanup
351 subtest "delete" => sub {
353 t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
354 my $patron = $builder->build( { source => 'Borrower' } );
355 my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
356 my $hold = $builder->build(
357 { source => 'Reserve',
358 value => { borrowernumber => $patron->{borrowernumber} }
361 my $list = $builder->build(
362 { source => 'Virtualshelve',
363 value => { owner => $patron->{borrowernumber} }
367 my $deleted = $retrieved_patron->delete;
368 is( $deleted, 1, 'Koha::Patron->delete should return 1 if the patron has been correctly deleted' );
370 is( Koha::Patrons->find( $patron->{borrowernumber} ), undef, 'Koha::Patron->delete should have deleted the patron' );
372 is( Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's holds| );
374 is( Koha::Virtualshelves->search( { owner => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's lists| );
376 my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'DELETE', object => $retrieved_patron->borrowernumber } )->count;
377 is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->delete should have logged' );
380 subtest 'add_enrolment_fee_if_needed' => sub {
383 my $enrolmentfees = { K => 5, J => 10, YA => 20 };
384 foreach( keys %{$enrolmentfees} ) {
385 ( Koha::Patron::Categories->find( $_ ) // $builder->build_object({ class => 'Koha::Patron::Categories', value => { categorycode => $_ } }) )->enrolmentfee( $enrolmentfees->{$_} )->store;
387 my $enrolmentfee_K = $enrolmentfees->{K};
388 my $enrolmentfee_J = $enrolmentfees->{J};
389 my $enrolmentfee_YA = $enrolmentfees->{YA};
391 my %borrower_data = (
392 firstname => 'my firstname',
393 surname => 'my surname',
395 branchcode => $library->{branchcode},
398 my $borrowernumber = C4::Members::AddMember(%borrower_data);
399 $borrower_data{borrowernumber} = $borrowernumber;
401 my $patron = Koha::Patrons->find( $borrowernumber );
402 my $total = $patron->account->balance;
403 is( int($total), int($enrolmentfee_K), "New kid pay $enrolmentfee_K" );
405 t::lib::Mocks::mock_preference( 'FeeOnChangePatronCategory', 0 );
406 $borrower_data{categorycode} = 'J';
407 C4::Members::ModMember(%borrower_data);
408 $total = $patron->account->balance;
409 is( int($total), int($enrolmentfee_K), "Kid growing and become a juvenile, but shouldn't pay for the upgrade " );
411 $borrower_data{categorycode} = 'K';
412 C4::Members::ModMember(%borrower_data);
413 t::lib::Mocks::mock_preference( 'FeeOnChangePatronCategory', 1 );
415 $borrower_data{categorycode} = 'J';
416 C4::Members::ModMember(%borrower_data);
417 $total = $patron->account->balance;
418 is( int($total), int($enrolmentfee_K + $enrolmentfee_J), "Kid growing and become a juvenile, they should pay " . ( $enrolmentfee_K + $enrolmentfee_J ) );
420 # Check with calling directly Koha::Patron->get_enrolment_fee_if_needed
421 $patron->categorycode('YA')->store;
422 my $fee = $patron->add_enrolment_fee_if_needed;
423 $total = $patron->account->balance;
425 int($enrolmentfee_K + $enrolmentfee_J + $enrolmentfee_YA),
426 "Juvenile growing and become an young adult, they should pay " . ( $enrolmentfee_K + $enrolmentfee_J + $enrolmentfee_YA )
432 subtest 'checkouts + get_overdues + old_checkouts' => sub {
435 my $library = $builder->build( { source => 'Branch' } );
436 my ($biblionumber_1) = AddBiblio( MARC::Record->new, '' );
437 my $item_1 = $builder->build(
441 homebranch => $library->{branchcode},
442 holdingbranch => $library->{branchcode},
443 biblionumber => $biblionumber_1,
449 my $item_2 = $builder->build(
453 homebranch => $library->{branchcode},
454 holdingbranch => $library->{branchcode},
455 biblionumber => $biblionumber_1,
461 my ($biblionumber_2) = AddBiblio( MARC::Record->new, '' );
462 my $item_3 = $builder->build(
466 homebranch => $library->{branchcode},
467 holdingbranch => $library->{branchcode},
468 biblionumber => $biblionumber_2,
474 my $patron = $builder->build(
476 source => 'Borrower',
477 value => { branchcode => $library->{branchcode} }
481 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
482 my $checkouts = $patron->checkouts;
483 is( $checkouts->count, 0, 'checkouts should not return any issues for that patron' );
484 is( ref($checkouts), 'Koha::Checkouts', 'checkouts should return a Koha::Checkouts object' );
485 my $old_checkouts = $patron->old_checkouts;
486 is( $old_checkouts->count, 0, 'old_checkouts should not return any issues for that patron' );
487 is( ref($old_checkouts), 'Koha::Old::Checkouts', 'old_checkouts should return a Koha::Old::Checkouts object' );
489 # Not sure how this is useful, but AddIssue pass this variable to different other subroutines
490 $patron = Koha::Patrons->find( $patron->borrowernumber )->unblessed;
492 my $module = new Test::MockModule('C4::Context');
493 $module->mock( 'userenv', sub { { branch => $library->{branchcode} } } );
495 AddIssue( $patron, $item_1->{barcode}, DateTime->now->subtract( days => 1 ) );
496 AddIssue( $patron, $item_2->{barcode}, DateTime->now->subtract( days => 5 ) );
497 AddIssue( $patron, $item_3->{barcode} );
499 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
500 $checkouts = $patron->checkouts;
501 is( $checkouts->count, 3, 'checkouts should return 3 issues for that patron' );
502 is( ref($checkouts), 'Koha::Checkouts', 'checkouts should return a Koha::Checkouts object' );
504 my $overdues = $patron->get_overdues;
505 is( $overdues->count, 2, 'Patron should have 2 overdues');
506 is( ref($overdues), 'Koha::Checkouts', 'Koha::Patron->get_overdues should return Koha::Checkouts' );
507 is( $overdues->next->itemnumber, $item_1->{itemnumber}, 'The issue should be returned in the same order as they have been done, first is correct' );
508 is( $overdues->next->itemnumber, $item_2->{itemnumber}, 'The issue should be returned in the same order as they have been done, second is correct' );
511 C4::Circulation::AddReturn( $item_1->{barcode} );
512 C4::Circulation::AddReturn( $item_2->{barcode} );
513 $old_checkouts = $patron->old_checkouts;
514 is( $old_checkouts->count, 2, 'old_checkouts should return 2 old checkouts that patron' );
515 is( ref($old_checkouts), 'Koha::Old::Checkouts', 'old_checkouts should return a Koha::Old::Checkouts object' );
518 Koha::Checkouts->search( { borrowernumber => $patron->borrowernumber } )->delete;
520 $module->unmock('userenv');
523 subtest 'get_age' => sub {
526 my $patron = $builder->build( { source => 'Borrower' } );
527 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
529 my $today = dt_from_string;
531 $patron->dateofbirth( undef );
532 is( $patron->get_age, undef, 'get_age should return undef if no dateofbirth is defined' );
533 $patron->dateofbirth( $today->clone->add( years => -12, months => -6, days => -1, end_of_month => 'limit' ) );
534 is( $patron->get_age, 12, 'Patron should be 12' );
535 $patron->dateofbirth( $today->clone->add( years => -18, months => 0, days => 1, end_of_month => 'limit' ) );
536 is( $patron->get_age, 17, 'Patron should be 17, happy birthday tomorrow!' );
537 $patron->dateofbirth( $today->clone->add( years => -18, months => 0, days => 0, end_of_month => 'limit' ) );
538 is( $patron->get_age, 18, 'Patron should be 18' );
539 $patron->dateofbirth( $today->clone->add( years => -18, months => -12, days => -31, end_of_month => 'limit' ) );
540 is( $patron->get_age, 19, 'Patron should be 19' );
541 $patron->dateofbirth( $today->clone->add( years => -18, months => -12, days => -30, end_of_month => 'limit' ) );
542 is( $patron->get_age, 19, 'Patron should be 19 again' );
543 $patron->dateofbirth( $today->clone->add( years => 0, months => -1, days => -1, end_of_month => 'limit' ) );
544 is( $patron->get_age, 0, 'Patron is a newborn child' );
549 subtest 'account' => sub {
552 my $patron = $builder->build({source => 'Borrower'});
554 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
555 my $account = $patron->account;
556 is( ref($account), 'Koha::Account', 'account should return a Koha::Account object' );
561 subtest 'search_upcoming_membership_expires' => sub {
564 my $expiry_days = 15;
565 t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', $expiry_days );
566 my $nb_of_days_before = 1;
567 my $nb_of_days_after = 2;
569 my $builder = t::lib::TestBuilder->new();
571 my $library = $builder->build({ source => 'Branch' });
573 # before we add borrowers to this branch, add the expires we have now
574 # note that this pertains to the current mocked setting of the pref
575 # for this reason we add the new branchcode to most of the tests
576 my $nb_of_expires = Koha::Patrons->search_upcoming_membership_expires->count;
578 my $patron_1 = $builder->build({
579 source => 'Borrower',
581 branchcode => $library->{branchcode},
582 dateexpiry => dt_from_string->add( days => $expiry_days )
586 my $patron_2 = $builder->build({
587 source => 'Borrower',
589 branchcode => $library->{branchcode},
590 dateexpiry => dt_from_string->add( days => $expiry_days - $nb_of_days_before )
594 my $patron_3 = $builder->build({
595 source => 'Borrower',
597 branchcode => $library->{branchcode},
598 dateexpiry => dt_from_string->add( days => $expiry_days + $nb_of_days_after )
602 # Test without extra parameters
603 my $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires();
604 is( $upcoming_mem_expires->count, $nb_of_expires + 1, 'Get upcoming membership expires should return one new borrower.' );
607 $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode} });
608 is( $upcoming_mem_expires->count, 1, 'Test with branch parameter' );
609 my $expired = $upcoming_mem_expires->next;
610 is( $expired->surname, $patron_1->{surname}, 'Get upcoming membership expires should return the correct patron.' );
611 is( $expired->library->branchemail, $library->{branchemail}, 'Get upcoming membership expires should return the correct patron.' );
612 is( $expired->branchcode, $patron_1->{branchcode}, 'Get upcoming membership expires should return the correct patron.' );
614 t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', 0 );
615 $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode} });
616 is( $upcoming_mem_expires->count, 0, 'Get upcoming membership expires with MembershipExpiryDaysNotice==0 should not return new records.' );
618 # Test MembershipExpiryDaysNotice == undef
619 t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', undef );
620 $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode} });
621 is( $upcoming_mem_expires->count, 0, 'Get upcoming membership expires without MembershipExpiryDaysNotice should not return new records.' );
623 # Test the before parameter
624 t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', 15 );
625 $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode}, before => $nb_of_days_before });
626 is( $upcoming_mem_expires->count, 2, 'Expect two results for before');
627 # Test after parameter also
628 $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode}, before => $nb_of_days_before, after => $nb_of_days_after });
629 is( $upcoming_mem_expires->count, 3, 'Expect three results when adding after' );
630 Koha::Patrons->search({ borrowernumber => { in => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber}, $patron_3->{borrowernumber} ] } })->delete;
633 subtest 'holds and old_holds' => sub {
636 my $library = $builder->build( { source => 'Branch' } );
637 my ($biblionumber_1) = AddBiblio( MARC::Record->new, '' );
638 my $item_1 = $builder->build(
642 homebranch => $library->{branchcode},
643 holdingbranch => $library->{branchcode},
644 biblionumber => $biblionumber_1
648 my $item_2 = $builder->build(
652 homebranch => $library->{branchcode},
653 holdingbranch => $library->{branchcode},
654 biblionumber => $biblionumber_1
658 my ($biblionumber_2) = AddBiblio( MARC::Record->new, '' );
659 my $item_3 = $builder->build(
663 homebranch => $library->{branchcode},
664 holdingbranch => $library->{branchcode},
665 biblionumber => $biblionumber_2
669 my $patron = $builder->build(
671 source => 'Borrower',
672 value => { branchcode => $library->{branchcode} }
676 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
677 my $holds = $patron->holds;
678 is( ref($holds), 'Koha::Holds',
679 'Koha::Patron->holds should return a Koha::Holds objects' );
680 is( $holds->count, 0, 'There should not be holds placed by this patron yet' );
682 C4::Reserves::AddReserve( $library->{branchcode},
683 $patron->borrowernumber, $biblionumber_1 );
685 C4::Reserves::AddReserve( $library->{branchcode},
686 $patron->borrowernumber, $biblionumber_2, undef, undef, dt_from_string->add( days => 2 ) );
688 $holds = $patron->holds;
689 is( $holds->count, 2, 'There should be 2 holds placed by this patron' );
691 my $old_holds = $patron->old_holds;
692 is( ref($old_holds), 'Koha::Old::Holds',
693 'Koha::Patron->old_holds should return a Koha::Old::Holds objects' );
694 is( $old_holds->count, 0, 'There should not be any old holds yet');
696 my $hold = $holds->next;
699 $old_holds = $patron->old_holds;
700 is( $old_holds->count, 1, 'There should be 1 old (cancelled) hold');
707 subtest 'notice_email_address' => sub {
710 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
712 t::lib::Mocks::mock_preference( 'AutoEmailPrimaryAddress', 'OFF' );
713 is ($patron->notice_email_address, $patron->email, "Koha::Patron->notice_email_address returns correct value when AutoEmailPrimaryAddress is off");
715 t::lib::Mocks::mock_preference( 'AutoEmailPrimaryAddress', 'emailpro' );
716 is ($patron->notice_email_address, $patron->emailpro, "Koha::Patron->notice_email_address returns correct value when AutoEmailPrimaryAddress is emailpro");
721 subtest 'search_patrons_to_anonymise & anonymise_issue_history' => sub {
724 # TODO create a subroutine in t::lib::Mocks
725 my $branch = $builder->build({ source => 'Branch' });
726 my $userenv_patron = $builder->build({
727 source => 'Borrower',
728 value => { branchcode => $branch->{branchcode} },
730 C4::Context->_new_userenv('DUMMY SESSION');
731 C4::Context->set_userenv(
732 $userenv_patron->{borrowernumber},
733 $userenv_patron->{userid},
734 'usercnum', 'First name', 'Surname',
735 $branch->{branchcode},
736 $branch->{branchname},
739 my $anonymous = $builder->build( { source => 'Borrower', }, );
741 t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous->{borrowernumber} );
743 subtest 'patron privacy is 1 (default)' => sub {
746 t::lib::Mocks::mock_preference('IndependentBranches', 0);
747 my $patron = $builder->build(
748 { source => 'Borrower',
749 value => { privacy => 1, }
752 my $item_1 = $builder->build(
760 my $issue_1 = $builder->build(
763 borrowernumber => $patron->{borrowernumber},
764 itemnumber => $item_1->{itemnumber},
768 my $item_2 = $builder->build(
776 my $issue_2 = $builder->build(
779 borrowernumber => $patron->{borrowernumber},
780 itemnumber => $item_2->{itemnumber},
785 my ( $returned_1, undef, undef ) = C4::Circulation::AddReturn( $item_1->{barcode}, undef, undef, undef, '2010-10-10' );
786 my ( $returned_2, undef, undef ) = C4::Circulation::AddReturn( $item_2->{barcode}, undef, undef, undef, '2011-11-11' );
787 is( $returned_1 && $returned_2, 1, 'The items should have been returned' );
789 my $patrons_to_anonymise = Koha::Patrons->search_patrons_to_anonymise( { before => '2010-10-11' } )->search( { 'me.borrowernumber' => $patron->{borrowernumber} } );
790 is( ref($patrons_to_anonymise), 'Koha::Patrons', 'search_patrons_to_anonymise should return Koha::Patrons' );
792 my $rows_affected = Koha::Patrons->search_patrons_to_anonymise( { before => '2011-11-12' } )->anonymise_issue_history( { before => '2010-10-11' } );
793 ok( $rows_affected > 0, 'AnonymiseIssueHistory should affect at least 1 row' );
795 my $dbh = C4::Context->dbh;
796 my $sth = $dbh->prepare(q|SELECT borrowernumber FROM old_issues where itemnumber = ?|);
797 $sth->execute($item_1->{itemnumber});
798 my ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
799 is( $borrowernumber_used_to_anonymised, $anonymous->{borrowernumber}, 'With privacy=1, the issue should have been anonymised' );
800 $sth->execute($item_2->{itemnumber});
801 ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
802 is( $borrowernumber_used_to_anonymised, $patron->{borrowernumber}, 'The issue should not have been anonymised, the returned date is later' );
804 $rows_affected = Koha::Patrons->search_patrons_to_anonymise( { before => '2011-11-12' } )->anonymise_issue_history;
805 $sth->execute($item_2->{itemnumber});
806 ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
807 is( $borrowernumber_used_to_anonymised, $anonymous->{borrowernumber}, 'The issue should have been anonymised, the returned date is before' );
809 my $sth_reset = $dbh->prepare(q|UPDATE old_issues SET borrowernumber = ? WHERE itemnumber = ?|);
810 $sth_reset->execute( $patron->{borrowernumber}, $item_1->{itemnumber} );
811 $sth_reset->execute( $patron->{borrowernumber}, $item_2->{itemnumber} );
812 $rows_affected = Koha::Patrons->search_patrons_to_anonymise->anonymise_issue_history;
813 $sth->execute($item_1->{itemnumber});
814 ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
815 is( $borrowernumber_used_to_anonymised, $anonymous->{borrowernumber}, 'The issue 1 should have been anonymised, before parameter was not passed' );
816 $sth->execute($item_2->{itemnumber});
817 ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
818 is( $borrowernumber_used_to_anonymised, $anonymous->{borrowernumber}, 'The issue 2 should have been anonymised, before parameter was not passed' );
820 Koha::Patrons->find( $patron->{borrowernumber})->delete;
823 subtest 'patron privacy is 0 (forever)' => sub {
826 t::lib::Mocks::mock_preference('IndependentBranches', 0);
827 my $patron = $builder->build(
828 { source => 'Borrower',
829 value => { privacy => 0, }
832 my $item = $builder->build(
840 my $issue = $builder->build(
843 borrowernumber => $patron->{borrowernumber},
844 itemnumber => $item->{itemnumber},
849 my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' );
850 is( $returned, 1, 'The item should have been returned' );
851 my $rows_affected = Koha::Patrons->search_patrons_to_anonymise( { before => '2010-10-11' } )->anonymise_issue_history( { before => '2010-10-11' } );
852 ok( $rows_affected > 0, 'AnonymiseIssueHistory should not return any error if success' );
854 my $dbh = C4::Context->dbh;
855 my ($borrowernumber_used_to_anonymised) = $dbh->selectrow_array(q|
856 SELECT borrowernumber FROM old_issues where itemnumber = ?
857 |, undef, $item->{itemnumber});
858 is( $borrowernumber_used_to_anonymised, $patron->{borrowernumber}, 'With privacy=0, the issue should not be anonymised' );
859 Koha::Patrons->find( $patron->{borrowernumber})->delete;
862 t::lib::Mocks::mock_preference( 'AnonymousPatron', '' );
864 subtest 'AnonymousPatron is not defined' => sub {
867 t::lib::Mocks::mock_preference('IndependentBranches', 0);
868 my $patron = $builder->build(
869 { source => 'Borrower',
870 value => { privacy => 1, }
873 my $item = $builder->build(
881 my $issue = $builder->build(
884 borrowernumber => $patron->{borrowernumber},
885 itemnumber => $item->{itemnumber},
890 my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' );
891 is( $returned, 1, 'The item should have been returned' );
892 my $rows_affected = Koha::Patrons->search_patrons_to_anonymise( { before => '2010-10-11' } )->anonymise_issue_history( { before => '2010-10-11' } );
893 ok( $rows_affected > 0, 'AnonymiseIssueHistory should affect at least 1 row' );
895 my $dbh = C4::Context->dbh;
896 my ($borrowernumber_used_to_anonymised) = $dbh->selectrow_array(q|
897 SELECT borrowernumber FROM old_issues where itemnumber = ?
898 |, undef, $item->{itemnumber});
899 is( $borrowernumber_used_to_anonymised, undef, 'With AnonymousPatron is not defined, the issue should have been anonymised anyway' );
900 Koha::Patrons->find( $patron->{borrowernumber})->delete;
903 subtest 'Logged in librarian is not superlibrarian & IndependentBranches' => sub {
905 t::lib::Mocks::mock_preference( 'IndependentBranches', 1 );
906 my $patron = $builder->build(
907 { source => 'Borrower',
908 value => { privacy => 1 } # Another branchcode than the logged in librarian
911 my $item = $builder->build(
919 my $issue = $builder->build(
922 borrowernumber => $patron->{borrowernumber},
923 itemnumber => $item->{itemnumber},
928 my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' );
929 is( Koha::Patrons->search_patrons_to_anonymise( { before => '2010-10-11' } )->count, 0 );
930 Koha::Patrons->find( $patron->{borrowernumber})->delete;
933 Koha::Patrons->find( $anonymous->{borrowernumber})->delete;
934 Koha::Patrons->find( $userenv_patron->{borrowernumber})->delete;
936 # Reset IndependentBranches for further tests
937 t::lib::Mocks::mock_preference('IndependentBranches', 0);
940 subtest 'libraries_where_can_see_patrons + can_see_patron_infos + search_limited' => sub {
948 my $group_1 = Koha::Library::Group->new( { title => 'TEST Group 1', ft_hide_patron_info => 1 } )->store;
949 my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2', ft_hide_patron_info => 1 } )->store;
950 my $library_11 = $builder->build( { source => 'Branch' } );
951 my $library_12 = $builder->build( { source => 'Branch' } );
952 my $library_21 = $builder->build( { source => 'Branch' } );
953 $library_11 = Koha::Libraries->find( $library_11->{branchcode} );
954 $library_12 = Koha::Libraries->find( $library_12->{branchcode} );
955 $library_21 = Koha::Libraries->find( $library_21->{branchcode} );
956 Koha::Library::Group->new(
957 { branchcode => $library_11->branchcode, parent_id => $group_1->id } )->store;
958 Koha::Library::Group->new(
959 { branchcode => $library_12->branchcode, parent_id => $group_1->id } )->store;
960 Koha::Library::Group->new(
961 { branchcode => $library_21->branchcode, parent_id => $group_2->id } )->store;
963 my $sth = C4::Context->dbh->prepare(q|INSERT INTO user_permissions( borrowernumber, module_bit, code ) VALUES (?, 4, ?)|); # 4 for borrowers
964 # 2 patrons from library_11 (group1)
965 # patron_11_1 see patron's infos from outside its group
966 # Setting flags => undef to not be considered as superlibrarian
967 my $patron_11_1 = $builder->build({ source => 'Borrower', value => { branchcode => $library_11->branchcode, flags => undef, }});
968 $patron_11_1 = Koha::Patrons->find( $patron_11_1->{borrowernumber} );
969 $sth->execute( $patron_11_1->borrowernumber, 'edit_borrowers' );
970 $sth->execute( $patron_11_1->borrowernumber, 'view_borrower_infos_from_any_libraries' );
971 # patron_11_2 can only see patron's info from its group
972 my $patron_11_2 = $builder->build({ source => 'Borrower', value => { branchcode => $library_11->branchcode, flags => undef, }});
973 $patron_11_2 = Koha::Patrons->find( $patron_11_2->{borrowernumber} );
974 $sth->execute( $patron_11_2->borrowernumber, 'edit_borrowers' );
975 # 1 patron from library_12 (group1)
976 my $patron_12 = $builder->build({ source => 'Borrower', value => { branchcode => $library_12->branchcode, flags => undef, }});
977 $patron_12 = Koha::Patrons->find( $patron_12->{borrowernumber} );
978 # 1 patron from library_21 (group2) can only see patron's info from its group
979 my $patron_21 = $builder->build({ source => 'Borrower', value => { branchcode => $library_21->branchcode, flags => undef, }});
980 $patron_21 = Koha::Patrons->find( $patron_21->{borrowernumber} );
981 $sth->execute( $patron_21->borrowernumber, 'edit_borrowers' );
983 # Pfiou, we can start now!
984 subtest 'libraries_where_can_see_patrons' => sub {
989 set_logged_in_user( $patron_11_1 );
990 @branchcodes = $patron_11_1->libraries_where_can_see_patrons;
991 is_deeply( \@branchcodes, [], q|patron_11_1 has view_borrower_infos_from_any_libraries => No restriction| );
993 set_logged_in_user( $patron_11_2 );
994 @branchcodes = $patron_11_2->libraries_where_can_see_patrons;
995 is_deeply( \@branchcodes, [ sort ( $library_11->branchcode, $library_12->branchcode ) ], q|patron_11_2 has not view_borrower_infos_from_any_libraries => Can only see patron's from its group| );
997 set_logged_in_user( $patron_21 );
998 @branchcodes = $patron_21->libraries_where_can_see_patrons;
999 is_deeply( \@branchcodes, [$library_21->branchcode], q|patron_21 has not view_borrower_infos_from_any_libraries => Can only see patron's from its group| );
1001 subtest 'can_see_patron_infos' => sub {
1004 set_logged_in_user( $patron_11_1 );
1005 is( $patron_11_1->can_see_patron_infos( $patron_11_2 ), 1, q|patron_11_1 can see patron_11_2, from its library| );
1006 is( $patron_11_1->can_see_patron_infos( $patron_12 ), 1, q|patron_11_1 can see patron_12, from its group| );
1007 is( $patron_11_1->can_see_patron_infos( $patron_21 ), 1, q|patron_11_1 can see patron_11_2, from another group| );
1009 set_logged_in_user( $patron_11_2 );
1010 is( $patron_11_2->can_see_patron_infos( $patron_11_1 ), 1, q|patron_11_2 can see patron_11_1, from its library| );
1011 is( $patron_11_2->can_see_patron_infos( $patron_12 ), 1, q|patron_11_2 can see patron_12, from its group| );
1012 is( $patron_11_2->can_see_patron_infos( $patron_21 ), 0, q|patron_11_2 can NOT see patron_21, from another group| );
1014 subtest 'search_limited' => sub {
1017 set_logged_in_user( $patron_11_1 );
1018 my $total_number_of_patrons = $nb_of_patrons + 6; # 2 created before + 4 for these subtests
1019 is( Koha::Patrons->search->count, $total_number_of_patrons, 'Non-limited search should return all patrons');
1020 is( Koha::Patrons->search_limited->count, $total_number_of_patrons, 'patron_11_1 is allowed to see all patrons' );
1022 set_logged_in_user( $patron_11_2 );
1023 is( Koha::Patrons->search->count, $total_number_of_patrons, 'Non-limited search should return all patrons');
1024 is( Koha::Patrons->search_limited->count, 3, 'patron_12_1 is not allowed to see patrons from other groups, only patron_11_1, patron_11_2 and patron_12' );
1026 set_logged_in_user( $patron_21 );
1027 is( Koha::Patrons->search->count, $total_number_of_patrons, 'Non-limited search should return all patrons');
1028 is( Koha::Patrons->search_limited->count, 1, 'patron_21 is not allowed to see patrons from other groups, only himself' );
1030 $patron_11_1->delete;
1031 $patron_11_2->delete;
1036 subtest 'account_locked' => sub {
1038 my $patron = $builder->build({ source => 'Borrower', value => { login_attempts => 0 } });
1039 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
1040 for my $value ( undef, '', 0 ) {
1041 t::lib::Mocks::mock_preference('FailedloginAttempts', $value);
1042 is( $patron->account_locked, 0, 'Feature is disabled, patron account should not be considered locked' );
1043 $patron->login_attempts(1)->store;
1044 is( $patron->account_locked, 0, 'Feature is disabled, patron account should not be considered locked' );
1047 t::lib::Mocks::mock_preference('FailedloginAttempts', 3);
1048 $patron->login_attempts(2)->store;
1049 is( $patron->account_locked, 0, 'Patron has 2 failed attempts, account should not be considered locked yet' );
1050 $patron->login_attempts(3)->store;
1051 is( $patron->account_locked, 1, 'Patron has 3 failed attempts, account should be considered locked yet' );
1056 subtest 'is_child | is_adult' => sub {
1058 my $category = $builder->build_object(
1060 class => 'Koha::Patron::Categories',
1061 value => { category_type => 'A' }
1064 my $patron_adult = $builder->build_object(
1066 class => 'Koha::Patrons',
1067 value => { categorycode => $category->categorycode }
1070 $category = $builder->build_object(
1072 class => 'Koha::Patron::Categories',
1073 value => { category_type => 'I' }
1076 my $patron_adult_i = $builder->build_object(
1078 class => 'Koha::Patrons',
1079 value => { categorycode => $category->categorycode }
1082 $category = $builder->build_object(
1084 class => 'Koha::Patron::Categories',
1085 value => { category_type => 'C' }
1088 my $patron_child = $builder->build_object(
1090 class => 'Koha::Patrons',
1091 value => { categorycode => $category->categorycode }
1094 $category = $builder->build_object(
1096 class => 'Koha::Patron::Categories',
1097 value => { category_type => 'O' }
1100 my $patron_other = $builder->build_object(
1102 class => 'Koha::Patrons',
1103 value => { categorycode => $category->categorycode }
1106 is( $patron_adult->is_adult, 1, 'Patron from category A should be considered adult' );
1107 is( $patron_adult_i->is_adult, 1, 'Patron from category I should be considered adult' );
1108 is( $patron_child->is_adult, 0, 'Patron from category C should not be considered adult' );
1109 is( $patron_other->is_adult, 0, 'Patron from category O should not be considered adult' );
1111 is( $patron_adult->is_child, 0, 'Patron from category A should be considered child' );
1112 is( $patron_adult_i->is_child, 0, 'Patron from category I should be considered child' );
1113 is( $patron_child->is_child, 1, 'Patron from category C should not be considered child' );
1114 is( $patron_other->is_child, 0, 'Patron from category O should not be considered child' );
1117 $patron_adult->delete;
1118 $patron_adult_i->delete;
1119 $patron_child->delete;
1120 $patron_other->delete;
1123 subtest 'get_overdues' => sub {
1126 my $library = $builder->build( { source => 'Branch' } );
1127 my ($biblionumber_1) = AddBiblio( MARC::Record->new, '' );
1128 my $item_1 = $builder->build(
1132 homebranch => $library->{branchcode},
1133 holdingbranch => $library->{branchcode},
1134 biblionumber => $biblionumber_1
1138 my $item_2 = $builder->build(
1142 homebranch => $library->{branchcode},
1143 holdingbranch => $library->{branchcode},
1144 biblionumber => $biblionumber_1
1148 my ($biblionumber_2) = AddBiblio( MARC::Record->new, '' );
1149 my $item_3 = $builder->build(
1153 homebranch => $library->{branchcode},
1154 holdingbranch => $library->{branchcode},
1155 biblionumber => $biblionumber_2
1159 my $patron = $builder->build(
1161 source => 'Borrower',
1162 value => { branchcode => $library->{branchcode} }
1166 my $module = new Test::MockModule('C4::Context');
1167 $module->mock( 'userenv', sub { { branch => $library->{branchcode} } } );
1169 AddIssue( $patron, $item_1->{barcode}, DateTime->now->subtract( days => 1 ) );
1170 AddIssue( $patron, $item_2->{barcode}, DateTime->now->subtract( days => 5 ) );
1171 AddIssue( $patron, $item_3->{barcode} );
1173 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
1174 my $overdues = $patron->get_overdues;
1175 is( $overdues->count, 2, 'Patron should have 2 overdues');
1176 is( $overdues->next->itemnumber, $item_1->{itemnumber}, 'The issue should be returned in the same order as they have been done, first is correct' );
1177 is( $overdues->next->itemnumber, $item_2->{itemnumber}, 'The issue should be returned in the same order as they have been done, second is correct' );
1179 my $o = $overdues->reset->next;
1180 my $unblessed_overdue = $o->unblessed_all_relateds;
1181 is( exists( $unblessed_overdue->{issuedate} ), 1, 'Fields from the issues table should be filled' );
1182 is( exists( $unblessed_overdue->{itemcallnumber} ), 1, 'Fields from the items table should be filled' );
1183 is( exists( $unblessed_overdue->{title} ), 1, 'Fields from the biblio table should be filled' );
1184 is( exists( $unblessed_overdue->{itemtype} ), 1, 'Fields from the biblioitems table should be filled' );
1187 $patron->checkouts->delete;
1191 $retrieved_patron_1->delete;
1192 is( Koha::Patrons->search->count, $nb_of_patrons + 1, 'Delete should have deleted the patron' );
1194 subtest 'Log cardnumber change' => sub {
1197 t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
1198 my $patron = $builder->build( { source => 'Borrower' } );
1200 my $cardnumber = $patron->{cardnumber};
1201 $patron->{cardnumber} = 'TESTCARDNUMBER';
1202 ModMember(%$patron);
1204 my @logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'MODIFY', object => $patron->{borrowernumber} } );
1205 my $log_info = from_json( $logs[0]->info );
1206 is( $log_info->{cardnumber_replaced}->{new_cardnumber}, 'TESTCARDNUMBER', 'Got correct new cardnumber' );
1207 is( $log_info->{cardnumber_replaced}->{previous_cardnumber}, $cardnumber, 'Got correct old cardnumber' );
1208 is( scalar @logs, 2, 'With BorrowerLogs, Change in cardnumber should be logged, as well as general alert of patron mod.' );
1212 $schema->storage->txn_rollback;
1214 # TODO Move to t::lib::Mocks and reuse it!
1215 sub set_logged_in_user {
1217 C4::Context->set_userenv(
1218 $patron->borrowernumber, $patron->userid,
1219 $patron->cardnumber, 'firstname',
1220 'surname', $patron->library->branchcode,
1221 'Midway Public Library', $patron->flags,