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 => 22;
35 use Koha::Patron::Categories;
38 use Koha::Virtualshelves;
40 use t::lib::TestBuilder;
43 my $schema = Koha::Database->new->schema;
44 $schema->storage->txn_begin;
46 my $builder = t::lib::TestBuilder->new;
47 my $library = $builder->build({source => 'Branch' });
48 my $category = $builder->build({source => 'Category' });
49 my $nb_of_patrons = Koha::Patrons->search->count;
50 my $new_patron_1 = Koha::Patron->new(
51 { cardnumber => 'test_cn_1',
52 branchcode => $library->{branchcode},
53 categorycode => $category->{categorycode},
54 surname => 'surname for patron1',
55 firstname => 'firstname for patron1',
56 userid => 'a_nonexistent_userid_1',
59 my $new_patron_2 = Koha::Patron->new(
60 { cardnumber => 'test_cn_2',
61 branchcode => $library->{branchcode},
62 categorycode => $category->{categorycode},
63 surname => 'surname for patron2',
64 firstname => 'firstname for patron2',
65 userid => 'a_nonexistent_userid_2',
69 C4::Context->_new_userenv('xxx');
70 C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, 'Midway Public Library', '', '', '');
72 is( Koha::Patrons->search->count, $nb_of_patrons + 2, 'The 2 patrons should have been added' );
74 my $retrieved_patron_1 = Koha::Patrons->find( $new_patron_1->borrowernumber );
75 is( $retrieved_patron_1->cardnumber, $new_patron_1->cardnumber, 'Find a patron by borrowernumber should return the correct patron' );
77 subtest 'library' => sub {
79 is( $retrieved_patron_1->library->branchcode, $library->{branchcode}, 'Koha::Patron->library should return the correct library' );
80 is( ref($retrieved_patron_1->library), 'Koha::Library', 'Koha::Patron->library should return a Koha::Library object' );
83 subtest 'guarantees' => sub {
85 my $guarantees = $new_patron_1->guarantees;
86 is( ref($guarantees), 'Koha::Patrons', 'Koha::Patron->guarantees should return a Koha::Patrons result set in a scalar context' );
87 is( $guarantees->count, 0, 'new_patron_1 should have 0 guarantee' );
88 my @guarantees = $new_patron_1->guarantees;
89 is( ref(\@guarantees), 'ARRAY', 'Koha::Patron->guarantees should return an array in a list context' );
90 is( scalar(@guarantees), 0, 'new_patron_1 should have 0 guarantee' );
92 my $guarantee_1 = $builder->build({ source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber }});
93 my $guarantee_2 = $builder->build({ source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber }});
95 $guarantees = $new_patron_1->guarantees;
96 is( ref($guarantees), 'Koha::Patrons', 'Koha::Patron->guarantees should return a Koha::Patrons result set in a scalar context' );
97 is( $guarantees->count, 2, 'new_patron_1 should have 2 guarantees' );
98 @guarantees = $new_patron_1->guarantees;
99 is( ref(\@guarantees), 'ARRAY', 'Koha::Patron->guarantees should return an array in a list context' );
100 is( scalar(@guarantees), 2, 'new_patron_1 should have 2 guarantees' );
101 $_->delete for @guarantees;
104 subtest 'category' => sub {
106 my $patron_category = $new_patron_1->category;
107 is( ref( $patron_category), 'Koha::Patron::Category', );
108 is( $patron_category->categorycode, $category->{categorycode}, );
111 subtest 'siblings' => sub {
113 my $siblings = $new_patron_1->siblings;
114 is( $siblings, undef, 'Koha::Patron->siblings should not crashed if the patron has no guarantor' );
115 my $guarantee_1 = $builder->build( { source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber } } );
116 my $retrieved_guarantee_1 = Koha::Patrons->find($guarantee_1);
117 $siblings = $retrieved_guarantee_1->siblings;
118 is( ref($siblings), 'Koha::Patrons', 'Koha::Patron->siblings should return a Koha::Patrons result set in a scalar context' );
119 my @siblings = $retrieved_guarantee_1->siblings;
120 is( ref( \@siblings ), 'ARRAY', 'Koha::Patron->siblings should return an array in a list context' );
121 is( $siblings->count, 0, 'guarantee_1 should not have siblings yet' );
122 my $guarantee_2 = $builder->build( { source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber } } );
123 my $guarantee_3 = $builder->build( { source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber } } );
124 $siblings = $retrieved_guarantee_1->siblings;
125 is( $siblings->count, 2, 'guarantee_1 should have 2 siblings' );
126 is( $guarantee_2->{borrowernumber}, $siblings->next->borrowernumber, 'guarantee_2 should exist in the guarantees' );
127 is( $guarantee_3->{borrowernumber}, $siblings->next->borrowernumber, 'guarantee_3 should exist in the guarantees' );
128 $_->delete for $retrieved_guarantee_1->siblings;
129 $retrieved_guarantee_1->delete;
132 subtest 'has_overdues' => sub {
135 my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
136 my $item_1 = $builder->build(
139 homebranch => $library->{branchcode},
140 holdingbranch => $library->{branchcode},
144 biblionumber => $biblioitem_1->{biblionumber}
148 my $retrieved_patron = Koha::Patrons->find( $new_patron_1->borrowernumber );
149 is( $retrieved_patron->has_overdues, 0, );
151 my $tomorrow = DateTime->today( time_zone => C4::Context->tz() )->add( days => 1 );
152 my $issue = Koha::Checkout->new({ borrowernumber => $new_patron_1->id, itemnumber => $item_1->{itemnumber}, date_due => $tomorrow, branchcode => $library->{branchcode} })->store();
153 is( $retrieved_patron->has_overdues, 0, );
155 my $yesterday = DateTime->today(time_zone => C4::Context->tz())->add( days => -1 );
156 $issue = Koha::Checkout->new({ borrowernumber => $new_patron_1->id, itemnumber => $item_1->{itemnumber}, date_due => $yesterday, branchcode => $library->{branchcode} })->store();
157 $retrieved_patron = Koha::Patrons->find( $new_patron_1->borrowernumber );
158 is( $retrieved_patron->has_overdues, 1, );
162 subtest 'update_password' => sub {
165 t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
166 my $original_userid = $new_patron_1->userid;
167 my $original_password = $new_patron_1->password;
168 warning_like { $retrieved_patron_1->update_password( $new_patron_2->userid, 'another_password' ) }
170 'Koha::Patron->update_password should warn if the userid is already used by another patron';
171 is( Koha::Patrons->find( $new_patron_1->borrowernumber )->userid, $original_userid, 'Koha::Patron->update_password should not have updated the userid' );
172 is( Koha::Patrons->find( $new_patron_1->borrowernumber )->password, $original_password, 'Koha::Patron->update_password should not have updated the userid' );
174 $retrieved_patron_1->update_password( 'another_nonexistent_userid_1', 'another_password' );
175 is( Koha::Patrons->find( $new_patron_1->borrowernumber )->userid, 'another_nonexistent_userid_1', 'Koha::Patron->update_password should have updated the userid' );
176 is( Koha::Patrons->find( $new_patron_1->borrowernumber )->password, 'another_password', 'Koha::Patron->update_password should have updated the password' );
178 my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'CHANGE PASS', object => $new_patron_1->borrowernumber } )->count;
179 is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->update_password should have logged' );
181 t::lib::Mocks::mock_preference( 'BorrowersLog', 0 );
182 $retrieved_patron_1->update_password( 'yet_another_nonexistent_userid_1', 'another_password' );
183 $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'CHANGE PASS', object => $new_patron_1->borrowernumber } )->count;
184 is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->update_password should not have logged' );
187 subtest 'is_expired' => sub {
189 my $patron = $builder->build({ source => 'Borrower' });
190 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
191 $patron->dateexpiry( undef )->store->discard_changes;
192 is( $patron->is_expired, 0, 'Patron should not be considered expired if dateexpiry is not set');
193 $patron->dateexpiry( '0000-00-00' )->store->discard_changes;
194 is( $patron->is_expired, 0, 'Patron should not be considered expired if dateexpiry is not 0000-00-00');
195 $patron->dateexpiry( dt_from_string )->store->discard_changes;
196 is( $patron->is_expired, 0, 'Patron should not be considered expired if dateexpiry is today');
197 $patron->dateexpiry( dt_from_string->add( days => 1 ) )->store->discard_changes;
198 is( $patron->is_expired, 0, 'Patron should not be considered expired if dateexpiry is tomorrow');
199 $patron->dateexpiry( dt_from_string->add( days => -1 ) )->store->discard_changes;
200 is( $patron->is_expired, 1, 'Patron should be considered expired if dateexpiry is yesterday');
205 subtest 'is_going_to_expire' => sub {
207 my $patron = $builder->build({ source => 'Borrower' });
208 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
209 $patron->dateexpiry( undef )->store->discard_changes;
210 is( $patron->is_going_to_expire, 0, 'Patron should not be considered going to expire if dateexpiry is not set');
211 $patron->dateexpiry( '0000-00-00' )->store->discard_changes;
212 is( $patron->is_going_to_expire, 0, 'Patron should not be considered going to expire if dateexpiry is not 0000-00-00');
214 t::lib::Mocks::mock_preference('NotifyBorrowerDeparture', 0);
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');
218 $patron->dateexpiry( dt_from_string )->store->discard_changes;
219 is( $patron->is_going_to_expire, 0, 'Patron should not be considered going to expire if dateexpiry is today and pref is 0');
221 t::lib::Mocks::mock_preference('NotifyBorrowerDeparture', 10);
222 $patron->dateexpiry( dt_from_string->add( days => 11 ) )->store->discard_changes;
223 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');
225 t::lib::Mocks::mock_preference('NotifyBorrowerDeparture', 0);
226 $patron->dateexpiry( dt_from_string->add( days => 10 ) )->store->discard_changes;
227 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');
229 t::lib::Mocks::mock_preference('NotifyBorrowerDeparture', 10);
230 $patron->dateexpiry( dt_from_string->add( days => 10 ) )->store->discard_changes;
231 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');
234 t::lib::Mocks::mock_preference('NotifyBorrowerDeparture', 10);
235 $patron->dateexpiry( dt_from_string->add( days => 20 ) )->store->discard_changes;
236 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');
238 t::lib::Mocks::mock_preference('NotifyBorrowerDeparture', 20);
239 $patron->dateexpiry( dt_from_string->add( days => 10 ) )->store->discard_changes;
240 is( $patron->is_going_to_expire, 1, 'Patron should be considered going to expire if dateexpiry is 10 days ahead and pref is 20');
246 subtest 'renew_account' => sub {
249 for my $date ( '2016-03-31', '2016-11-30', dt_from_string() ) {
250 my $dt = dt_from_string( $date, 'iso' );
251 Time::Fake->offset( $dt->epoch );
252 my $a_month_ago = $dt->clone->subtract( months => 1, end_of_month => 'limit' )->truncate( to => 'day' );
253 my $a_year_later = $dt->clone->add( months => 12, end_of_month => 'limit' )->truncate( to => 'day' );
254 my $a_year_later_minus_a_month = $dt->clone->add( months => 11, end_of_month => 'limit' )->truncate( to => 'day' );
255 my $a_month_later = $dt->clone->add( months => 1 , end_of_month => 'limit' )->truncate( to => 'day' );
256 my $a_year_later_plus_a_month = $dt->clone->add( months => 13, end_of_month => 'limit' )->truncate( to => 'day' );
257 my $patron_category = $builder->build(
258 { source => 'Category',
260 enrolmentperiod => 12,
261 enrolmentperioddate => undef,
265 my $patron = $builder->build(
266 { source => 'Borrower',
268 dateexpiry => $a_month_ago,
269 categorycode => $patron_category->{categorycode},
270 date_renewed => undef, # Force builder to not populate the column for new patron
274 my $patron_2 = $builder->build(
275 { source => 'Borrower',
277 dateexpiry => $a_month_ago,
278 categorycode => $patron_category->{categorycode},
282 my $patron_3 = $builder->build(
283 { source => 'Borrower',
285 dateexpiry => $a_month_later,
286 categorycode => $patron_category->{categorycode},
290 my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
291 my $retrieved_patron_2 = Koha::Patrons->find( $patron_2->{borrowernumber} );
292 my $retrieved_patron_3 = Koha::Patrons->find( $patron_3->{borrowernumber} );
294 is( $retrieved_patron->date_renewed, undef, "Date renewed is not set for patrons that have never been renewed" );
296 t::lib::Mocks::mock_preference( 'BorrowerRenewalPeriodBase', 'dateexpiry' );
297 t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
298 my $expiry_date = $retrieved_patron->renew_account;
299 is( $expiry_date, $a_year_later_minus_a_month, "$a_month_ago + 12 months must be $a_year_later_minus_a_month" );
300 my $retrieved_expiry_date = Koha::Patrons->find( $patron->{borrowernumber} )->dateexpiry;
301 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" );
302 my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'RENEW', object => $retrieved_patron->borrowernumber } )->count;
303 is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->renew_account should have logged' );
305 t::lib::Mocks::mock_preference( 'BorrowerRenewalPeriodBase', 'now' );
306 t::lib::Mocks::mock_preference( 'BorrowersLog', 0 );
307 $expiry_date = $retrieved_patron->renew_account;
308 is( $expiry_date, $a_year_later, "today + 12 months must be $a_year_later" );
309 $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
310 is( $retrieved_patron->date_renewed, output_pref({ dt => $dt, dateformat => 'iso', dateonly => 1 }), "Date renewed is set when calling renew_account" );
311 $retrieved_expiry_date = $retrieved_patron->dateexpiry;
312 is( dt_from_string($retrieved_expiry_date), $a_year_later, "today + 12 months must be $a_year_later" );
313 $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'RENEW', object => $retrieved_patron->borrowernumber } )->count;
314 is( $number_of_logs, 1, 'Without BorrowerLogs, Koha::Patron->renew_account should not have logged' );
316 t::lib::Mocks::mock_preference( 'BorrowerRenewalPeriodBase', 'combination' );
317 $expiry_date = $retrieved_patron_2->renew_account;
318 is( $expiry_date, $a_year_later, "today + 12 months must be $a_year_later" );
319 $retrieved_expiry_date = Koha::Patrons->find( $patron_2->{borrowernumber} )->dateexpiry;
320 is( dt_from_string($retrieved_expiry_date), $a_year_later, "today + 12 months must be $a_year_later" );
322 $expiry_date = $retrieved_patron_3->renew_account;
323 is( $expiry_date, $a_year_later_plus_a_month, "$a_month_later + 12 months must be $a_year_later_plus_a_month" );
324 $retrieved_expiry_date = Koha::Patrons->find( $patron_3->{borrowernumber} )->dateexpiry;
325 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" );
327 $retrieved_patron->delete;
328 $retrieved_patron_2->delete;
329 $retrieved_patron_3->delete;
334 subtest "move_to_deleted" => sub {
336 my $originally_updated_on = '2016-01-01 12:12:12';
337 my $patron = $builder->build( { source => 'Borrower',value => { updated_on => $originally_updated_on } } );
338 my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
339 is( ref( $retrieved_patron->move_to_deleted ), 'Koha::Schema::Result::Deletedborrower', 'Koha::Patron->move_to_deleted should return the Deleted patron' )
340 ; # FIXME This should be Koha::Deleted::Patron
341 my $deleted_patron = $schema->resultset('Deletedborrower')
342 ->search( { borrowernumber => $patron->{borrowernumber} }, { result_class => 'DBIx::Class::ResultClass::HashRefInflator' } )
344 ok( $retrieved_patron->updated_on, 'updated_on should be set for borrowers table' );
345 ok( $deleted_patron->{updated_on}, 'updated_on should be set for deleted_borrowers table' );
346 isnt( $deleted_patron->{updated_on}, $retrieved_patron->updated_on, 'Koha::Patron->move_to_deleted should have correctly updated the updated_on column');
347 $deleted_patron->{updated_on} = $originally_updated_on; #reset for simplicity in comparing all other fields
348 is_deeply( $deleted_patron, $patron, 'Koha::Patron->move_to_deleted should have correctly moved the patron to the deleted table' );
349 $retrieved_patron->delete( $patron->{borrowernumber} ); # Cleanup
352 subtest "delete" => sub {
354 t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
355 my $patron = $builder->build( { source => 'Borrower' } );
356 my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
357 my $hold = $builder->build(
358 { source => 'Reserve',
359 value => { borrowernumber => $patron->{borrowernumber} }
362 my $list = $builder->build(
363 { source => 'Virtualshelve',
364 value => { owner => $patron->{borrowernumber} }
368 my $deleted = $retrieved_patron->delete;
369 is( $deleted, 1, 'Koha::Patron->delete should return 1 if the patron has been correctly deleted' );
371 is( Koha::Patrons->find( $patron->{borrowernumber} ), undef, 'Koha::Patron->delete should have deleted the patron' );
373 is( Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's holds| );
375 is( Koha::Virtualshelves->search( { owner => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's lists| );
377 my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'DELETE', object => $retrieved_patron->borrowernumber } )->count;
378 is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->delete should have logged' );
381 subtest 'add_enrolment_fee_if_needed' => sub {
384 my $enrolmentfees = { K => 5, J => 10, YA => 20 };
385 foreach( keys %{$enrolmentfees} ) {
386 ( Koha::Patron::Categories->find( $_ ) // $builder->build_object({ class => 'Koha::Patron::Categories', value => { categorycode => $_ } }) )->enrolmentfee( $enrolmentfees->{$_} )->store;
388 my $enrolmentfee_K = $enrolmentfees->{K};
389 my $enrolmentfee_J = $enrolmentfees->{J};
390 my $enrolmentfee_YA = $enrolmentfees->{YA};
392 my %borrower_data = (
393 firstname => 'my firstname',
394 surname => 'my surname',
396 branchcode => $library->{branchcode},
399 my $borrowernumber = C4::Members::AddMember(%borrower_data);
400 $borrower_data{borrowernumber} = $borrowernumber;
402 my ($total) = C4::Members::GetMemberAccountRecords($borrowernumber);
403 is( $total, $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) = C4::Members::GetMemberAccountRecords($borrowernumber);
409 is( $total, $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) = C4::Members::GetMemberAccountRecords($borrowernumber);
418 is( $total, $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 my $patron = Koha::Patrons->find($borrowernumber);
422 $patron->categorycode('YA')->store;
423 my $fee = $patron->add_enrolment_fee_if_needed;
424 ($total) = C4::Members::GetMemberAccountRecords($borrowernumber);
426 $enrolmentfee_K + $enrolmentfee_J + $enrolmentfee_YA,
427 "Juvenile growing and become an young adult, they should pay " . ( $enrolmentfee_K + $enrolmentfee_J + $enrolmentfee_YA )
433 subtest 'checkouts + get_overdues' => sub {
436 my $library = $builder->build( { source => 'Branch' } );
437 my ($biblionumber_1) = AddBiblio( MARC::Record->new, '' );
438 my $item_1 = $builder->build(
442 homebranch => $library->{branchcode},
443 holdingbranch => $library->{branchcode},
444 biblionumber => $biblionumber_1
448 my $item_2 = $builder->build(
452 homebranch => $library->{branchcode},
453 holdingbranch => $library->{branchcode},
454 biblionumber => $biblionumber_1
458 my ($biblionumber_2) = AddBiblio( MARC::Record->new, '' );
459 my $item_3 = $builder->build(
463 homebranch => $library->{branchcode},
464 holdingbranch => $library->{branchcode},
465 biblionumber => $biblionumber_2
469 my $patron = $builder->build(
471 source => 'Borrower',
472 value => { branchcode => $library->{branchcode} }
476 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
477 my $checkouts = $patron->checkouts;
478 is( $checkouts->count, 0, 'checkouts should not return any issues for that patron' );
479 is( ref($checkouts), 'Koha::Checkouts', 'checkouts should return a Koha::Checkouts object' );
481 # Not sure how this is useful, but AddIssue pass this variable to different other subroutines
482 $patron = Koha::Patrons->find( $patron->borrowernumber )->unblessed;
484 my $module = new Test::MockModule('C4::Context');
485 $module->mock( 'userenv', sub { { branch => $library->{branchcode} } } );
487 AddIssue( $patron, $item_1->{barcode}, DateTime->now->subtract( days => 1 ) );
488 AddIssue( $patron, $item_2->{barcode}, DateTime->now->subtract( days => 5 ) );
489 AddIssue( $patron, $item_3->{barcode} );
491 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
492 $checkouts = $patron->checkouts;
493 is( $checkouts->count, 3, 'checkouts should return 3 issues for that patron' );
494 is( ref($checkouts), 'Koha::Checkouts', 'checkouts should return a Koha::Checkouts object' );
496 my $overdues = $patron->get_overdues;
497 is( $overdues->count, 2, 'Patron should have 2 overdues');
498 is( ref($overdues), 'Koha::Checkouts', 'Koha::Patron->get_overdues should return Koha::Checkouts' );
499 is( $overdues->next->itemnumber, $item_1->{itemnumber}, 'The issue should be returned in the same order as they have been done, first is correct' );
500 is( $overdues->next->itemnumber, $item_2->{itemnumber}, 'The issue should be returned in the same order as they have been done, second is correct' );
503 Koha::Checkouts->search( { borrowernumber => $patron->borrowernumber } )->delete;
505 $module->unmock('userenv');
508 subtest 'get_age' => sub {
511 my $patron = $builder->build( { source => 'Borrower' } );
512 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
514 my $today = dt_from_string;
516 $patron->dateofbirth( undef );
517 is( $patron->get_age, undef, 'get_age should return undef if no dateofbirth is defined' );
518 $patron->dateofbirth( $today->clone->add( years => -12, months => -6, days => -1 ) );
519 is( $patron->get_age, 12, 'Patron should be 12' );
520 $patron->dateofbirth( $today->clone->add( years => -18, months => 0, days => 1 ) );
521 is( $patron->get_age, 17, 'Patron should be 17, happy birthday tomorrow!' );
522 $patron->dateofbirth( $today->clone->add( years => -18, months => 0, days => 0 ) );
523 is( $patron->get_age, 18, 'Patron should be 18' );
524 $patron->dateofbirth( $today->clone->add( years => -18, months => -12, days => -31 ) );
525 is( $patron->get_age, 19, 'Patron should be 19' );
526 $patron->dateofbirth( $today->clone->add( years => -18, months => -12, days => -30 ) );
527 is( $patron->get_age, 19, 'Patron should be 19 again' );
528 $patron->dateofbirth( $today->clone->add( years => 0, months => -1, days => -1 ) );
529 is( $patron->get_age, 0, 'Patron is a newborn child' );
534 subtest 'account' => sub {
537 my $patron = $builder->build({source => 'Borrower'});
539 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
540 my $account = $patron->account;
541 is( ref($account), 'Koha::Account', 'account should return a Koha::Account object' );
546 subtest 'search_upcoming_membership_expires' => sub {
549 my $expiry_days = 15;
550 t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', $expiry_days );
551 my $nb_of_days_before = 1;
552 my $nb_of_days_after = 2;
554 my $builder = t::lib::TestBuilder->new();
556 my $library = $builder->build({ source => 'Branch' });
558 # before we add borrowers to this branch, add the expires we have now
559 # note that this pertains to the current mocked setting of the pref
560 # for this reason we add the new branchcode to most of the tests
561 my $nb_of_expires = Koha::Patrons->search_upcoming_membership_expires->count;
563 my $patron_1 = $builder->build({
564 source => 'Borrower',
566 branchcode => $library->{branchcode},
567 dateexpiry => dt_from_string->add( days => $expiry_days )
571 my $patron_2 = $builder->build({
572 source => 'Borrower',
574 branchcode => $library->{branchcode},
575 dateexpiry => dt_from_string->add( days => $expiry_days - $nb_of_days_before )
579 my $patron_3 = $builder->build({
580 source => 'Borrower',
582 branchcode => $library->{branchcode},
583 dateexpiry => dt_from_string->add( days => $expiry_days + $nb_of_days_after )
587 # Test without extra parameters
588 my $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires();
589 is( $upcoming_mem_expires->count, $nb_of_expires + 1, 'Get upcoming membership expires should return one new borrower.' );
592 $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode} });
593 is( $upcoming_mem_expires->count, 1, 'Test with branch parameter' );
594 my $expired = $upcoming_mem_expires->next;
595 is( $expired->surname, $patron_1->{surname}, 'Get upcoming membership expires should return the correct patron.' );
596 is( $expired->library->branchemail, $library->{branchemail}, 'Get upcoming membership expires should return the correct patron.' );
597 is( $expired->branchcode, $patron_1->{branchcode}, 'Get upcoming membership expires should return the correct patron.' );
599 t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', 0 );
600 $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode} });
601 is( $upcoming_mem_expires->count, 0, 'Get upcoming membership expires with MembershipExpiryDaysNotice==0 should not return new records.' );
603 # Test MembershipExpiryDaysNotice == undef
604 t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', undef );
605 $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode} });
606 is( $upcoming_mem_expires->count, 0, 'Get upcoming membership expires without MembershipExpiryDaysNotice should not return new records.' );
608 # Test the before parameter
609 t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', 15 );
610 $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode}, before => $nb_of_days_before });
611 is( $upcoming_mem_expires->count, 2, 'Expect two results for before');
612 # Test after parameter also
613 $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode}, before => $nb_of_days_before, after => $nb_of_days_after });
614 is( $upcoming_mem_expires->count, 3, 'Expect three results when adding after' );
615 Koha::Patrons->search({ borrowernumber => { in => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber}, $patron_3->{borrowernumber} ] } })->delete;
618 subtest 'holds and old_holds' => sub {
621 my $library = $builder->build( { source => 'Branch' } );
622 my ($biblionumber_1) = AddBiblio( MARC::Record->new, '' );
623 my $item_1 = $builder->build(
627 homebranch => $library->{branchcode},
628 holdingbranch => $library->{branchcode},
629 biblionumber => $biblionumber_1
633 my $item_2 = $builder->build(
637 homebranch => $library->{branchcode},
638 holdingbranch => $library->{branchcode},
639 biblionumber => $biblionumber_1
643 my ($biblionumber_2) = AddBiblio( MARC::Record->new, '' );
644 my $item_3 = $builder->build(
648 homebranch => $library->{branchcode},
649 holdingbranch => $library->{branchcode},
650 biblionumber => $biblionumber_2
654 my $patron = $builder->build(
656 source => 'Borrower',
657 value => { branchcode => $library->{branchcode} }
661 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
662 my $holds = $patron->holds;
663 is( ref($holds), 'Koha::Holds',
664 'Koha::Patron->holds should return a Koha::Holds objects' );
665 is( $holds->count, 0, 'There should not be holds placed by this patron yet' );
667 C4::Reserves::AddReserve( $library->{branchcode},
668 $patron->borrowernumber, $biblionumber_1 );
670 C4::Reserves::AddReserve( $library->{branchcode},
671 $patron->borrowernumber, $biblionumber_2, undef, undef, dt_from_string->add( days => 2 ) );
673 $holds = $patron->holds;
674 is( $holds->count, 2, 'There should be 2 holds placed by this patron' );
676 my $old_holds = $patron->old_holds;
677 is( ref($old_holds), 'Koha::Old::Holds',
678 'Koha::Patron->old_holds should return a Koha::Old::Holds objects' );
679 is( $old_holds->count, 0, 'There should not be any old holds yet');
681 my $hold = $holds->next;
684 $old_holds = $patron->old_holds;
685 is( $old_holds->count, 1, 'There should be 1 old (cancelled) hold');
692 subtest 'search_patrons_to_anonymise & anonymise_issue_history' => sub {
695 # TODO create a subroutine in t::lib::Mocks
696 my $branch = $builder->build({ source => 'Branch' });
697 my $userenv_patron = $builder->build({
698 source => 'Borrower',
699 value => { branchcode => $branch->{branchcode} },
701 C4::Context->_new_userenv('DUMMY SESSION');
702 C4::Context->set_userenv(
703 $userenv_patron->{borrowernumber},
704 $userenv_patron->{userid},
705 'usercnum', 'First name', 'Surname',
706 $branch->{branchcode},
707 $branch->{branchname},
710 my $anonymous = $builder->build( { source => 'Borrower', }, );
712 t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous->{borrowernumber} );
714 subtest 'patron privacy is 1 (default)' => sub {
717 t::lib::Mocks::mock_preference('IndependentBranches', 0);
718 my $patron = $builder->build(
719 { source => 'Borrower',
720 value => { privacy => 1, }
723 my $item_1 = $builder->build(
731 my $issue_1 = $builder->build(
734 borrowernumber => $patron->{borrowernumber},
735 itemnumber => $item_1->{itemnumber},
739 my $item_2 = $builder->build(
747 my $issue_2 = $builder->build(
750 borrowernumber => $patron->{borrowernumber},
751 itemnumber => $item_2->{itemnumber},
756 my ( $returned_1, undef, undef ) = C4::Circulation::AddReturn( $item_1->{barcode}, undef, undef, undef, '2010-10-10' );
757 my ( $returned_2, undef, undef ) = C4::Circulation::AddReturn( $item_2->{barcode}, undef, undef, undef, '2011-11-11' );
758 is( $returned_1 && $returned_2, 1, 'The items should have been returned' );
760 my $patrons_to_anonymise = Koha::Patrons->search_patrons_to_anonymise( { before => '2010-10-11' } )->search( { 'me.borrowernumber' => $patron->{borrowernumber} } );
761 is( ref($patrons_to_anonymise), 'Koha::Patrons', 'search_patrons_to_anonymise should return Koha::Patrons' );
763 my $rows_affected = Koha::Patrons->search_patrons_to_anonymise( { before => '2011-11-12' } )->anonymise_issue_history( { before => '2010-10-11' } );
764 ok( $rows_affected > 0, 'AnonymiseIssueHistory should affect at least 1 row' );
766 my $dbh = C4::Context->dbh;
767 my $sth = $dbh->prepare(q|SELECT borrowernumber FROM old_issues where itemnumber = ?|);
768 $sth->execute($item_1->{itemnumber});
769 my ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
770 is( $borrowernumber_used_to_anonymised, $anonymous->{borrowernumber}, 'With privacy=1, the issue should have been anonymised' );
771 $sth->execute($item_2->{itemnumber});
772 ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
773 is( $borrowernumber_used_to_anonymised, $patron->{borrowernumber}, 'The issue should not have been anonymised, the returned date is later' );
775 $rows_affected = Koha::Patrons->search_patrons_to_anonymise( { before => '2011-11-12' } )->anonymise_issue_history;
776 $sth->execute($item_2->{itemnumber});
777 ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
778 is( $borrowernumber_used_to_anonymised, $anonymous->{borrowernumber}, 'The issue should have been anonymised, the returned date is before' );
780 my $sth_reset = $dbh->prepare(q|UPDATE old_issues SET borrowernumber = ? WHERE itemnumber = ?|);
781 $sth_reset->execute( $patron->{borrowernumber}, $item_1->{itemnumber} );
782 $sth_reset->execute( $patron->{borrowernumber}, $item_2->{itemnumber} );
783 $rows_affected = Koha::Patrons->search_patrons_to_anonymise->anonymise_issue_history;
784 $sth->execute($item_1->{itemnumber});
785 ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
786 is( $borrowernumber_used_to_anonymised, $anonymous->{borrowernumber}, 'The issue 1 should have been anonymised, before parameter was not passed' );
787 $sth->execute($item_2->{itemnumber});
788 ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
789 is( $borrowernumber_used_to_anonymised, $anonymous->{borrowernumber}, 'The issue 2 should have been anonymised, before parameter was not passed' );
791 Koha::Patrons->find( $patron->{borrowernumber})->delete;
794 subtest 'patron privacy is 0 (forever)' => sub {
797 t::lib::Mocks::mock_preference('IndependentBranches', 0);
798 my $patron = $builder->build(
799 { source => 'Borrower',
800 value => { privacy => 0, }
803 my $item = $builder->build(
811 my $issue = $builder->build(
814 borrowernumber => $patron->{borrowernumber},
815 itemnumber => $item->{itemnumber},
820 my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' );
821 is( $returned, 1, 'The item should have been returned' );
822 my $rows_affected = Koha::Patrons->search_patrons_to_anonymise( { before => '2010-10-11' } )->anonymise_issue_history( { before => '2010-10-11' } );
823 ok( $rows_affected > 0, 'AnonymiseIssueHistory should not return any error if success' );
825 my $dbh = C4::Context->dbh;
826 my ($borrowernumber_used_to_anonymised) = $dbh->selectrow_array(q|
827 SELECT borrowernumber FROM old_issues where itemnumber = ?
828 |, undef, $item->{itemnumber});
829 is( $borrowernumber_used_to_anonymised, $patron->{borrowernumber}, 'With privacy=0, the issue should not be anonymised' );
830 Koha::Patrons->find( $patron->{borrowernumber})->delete;
833 t::lib::Mocks::mock_preference( 'AnonymousPatron', '' );
835 subtest 'AnonymousPatron is not defined' => sub {
838 t::lib::Mocks::mock_preference('IndependentBranches', 0);
839 my $patron = $builder->build(
840 { source => 'Borrower',
841 value => { privacy => 1, }
844 my $item = $builder->build(
852 my $issue = $builder->build(
855 borrowernumber => $patron->{borrowernumber},
856 itemnumber => $item->{itemnumber},
861 my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' );
862 is( $returned, 1, 'The item should have been returned' );
863 my $rows_affected = Koha::Patrons->search_patrons_to_anonymise( { before => '2010-10-11' } )->anonymise_issue_history( { before => '2010-10-11' } );
864 ok( $rows_affected > 0, 'AnonymiseIssueHistory should affect at least 1 row' );
866 my $dbh = C4::Context->dbh;
867 my ($borrowernumber_used_to_anonymised) = $dbh->selectrow_array(q|
868 SELECT borrowernumber FROM old_issues where itemnumber = ?
869 |, undef, $item->{itemnumber});
870 is( $borrowernumber_used_to_anonymised, undef, 'With AnonymousPatron is not defined, the issue should have been anonymised anyway' );
871 Koha::Patrons->find( $patron->{borrowernumber})->delete;
874 subtest 'Logged in librarian is not superlibrarian & IndependentBranches' => sub {
876 t::lib::Mocks::mock_preference( 'IndependentBranches', 1 );
877 my $patron = $builder->build(
878 { source => 'Borrower',
879 value => { privacy => 1 } # Another branchcode than the logged in librarian
882 my $item = $builder->build(
890 my $issue = $builder->build(
893 borrowernumber => $patron->{borrowernumber},
894 itemnumber => $item->{itemnumber},
899 my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' );
900 is( Koha::Patrons->search_patrons_to_anonymise( { before => '2010-10-11' } )->count, 0 );
901 Koha::Patrons->find( $patron->{borrowernumber})->delete;
904 Koha::Patrons->find( $anonymous->{borrowernumber})->delete;
905 Koha::Patrons->find( $userenv_patron->{borrowernumber})->delete;
908 subtest 'account_locked' => sub {
910 my $patron = $builder->build({ source => 'Borrower', value => { login_attempts => 0 } });
911 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
912 for my $value ( undef, '', 0 ) {
913 t::lib::Mocks::mock_preference('FailedloginAttempts', $value);
914 is( $patron->account_locked, 0, 'Feature is disabled, patron account should not be considered locked' );
915 $patron->login_attempts(1)->store;
916 is( $patron->account_locked, 0, 'Feature is disabled, patron account should not be considered locked' );
919 t::lib::Mocks::mock_preference('FailedloginAttempts', 3);
920 $patron->login_attempts(2)->store;
921 is( $patron->account_locked, 0, 'Patron has 2 failed attempts, account should not be considered locked yet' );
922 $patron->login_attempts(3)->store;
923 is( $patron->account_locked, 1, 'Patron has 3 failed attempts, account should be considered locked yet' );
928 $retrieved_patron_1->delete;
929 is( Koha::Patrons->search->count, $nb_of_patrons + 1, 'Delete should have deleted the patron' );
931 $schema->storage->txn_rollback;