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;
34 use Koha
::Patron
::Categories
;
37 use Koha
::Virtualshelves
;
39 use t
::lib
::TestBuilder
;
42 my $schema = Koha
::Database
->new->schema;
43 $schema->storage->txn_begin;
45 my $builder = t
::lib
::TestBuilder
->new;
46 my $library = $builder->build({source
=> 'Branch' });
47 my $category = $builder->build({source
=> 'Category' });
48 my $nb_of_patrons = Koha
::Patrons
->search->count;
49 my $new_patron_1 = Koha
::Patron
->new(
50 { cardnumber
=> 'test_cn_1',
51 branchcode
=> $library->{branchcode
},
52 categorycode
=> $category->{categorycode
},
53 surname
=> 'surname for patron1',
54 firstname
=> 'firstname for patron1',
55 userid
=> 'a_nonexistent_userid_1',
58 my $new_patron_2 = Koha
::Patron
->new(
59 { cardnumber
=> 'test_cn_2',
60 branchcode
=> $library->{branchcode
},
61 categorycode
=> $category->{categorycode
},
62 surname
=> 'surname for patron2',
63 firstname
=> 'firstname for patron2',
64 userid
=> 'a_nonexistent_userid_2',
68 C4
::Context
->_new_userenv('xxx');
69 C4
::Context
->set_userenv(0,0,0,'firstname','surname', $library->{branchcode
}, 'Midway Public Library', '', '', '');
71 is
( Koha
::Patrons
->search->count, $nb_of_patrons + 2, 'The 2 patrons should have been added' );
73 my $retrieved_patron_1 = Koha
::Patrons
->find( $new_patron_1->borrowernumber );
74 is
( $retrieved_patron_1->cardnumber, $new_patron_1->cardnumber, 'Find a patron by borrowernumber should return the correct patron' );
76 subtest
'library' => sub {
78 is
( $retrieved_patron_1->library->branchcode, $library->{branchcode
}, 'Koha::Patron->library should return the correct library' );
79 is
( ref($retrieved_patron_1->library), 'Koha::Library', 'Koha::Patron->library should return a Koha::Library object' );
82 subtest
'guarantees' => sub {
84 my $guarantees = $new_patron_1->guarantees;
85 is
( ref($guarantees), 'Koha::Patrons', 'Koha::Patron->guarantees should return a Koha::Patrons result set in a scalar context' );
86 is
( $guarantees->count, 0, 'new_patron_1 should have 0 guarantee' );
87 my @guarantees = $new_patron_1->guarantees;
88 is
( ref(\
@guarantees), 'ARRAY', 'Koha::Patron->guarantees should return an array in a list context' );
89 is
( scalar(@guarantees), 0, 'new_patron_1 should have 0 guarantee' );
91 my $guarantee_1 = $builder->build({ source
=> 'Borrower', value
=> { guarantorid
=> $new_patron_1->borrowernumber }});
92 my $guarantee_2 = $builder->build({ source
=> 'Borrower', value
=> { guarantorid
=> $new_patron_1->borrowernumber }});
94 $guarantees = $new_patron_1->guarantees;
95 is
( ref($guarantees), 'Koha::Patrons', 'Koha::Patron->guarantees should return a Koha::Patrons result set in a scalar context' );
96 is
( $guarantees->count, 2, 'new_patron_1 should have 2 guarantees' );
97 @guarantees = $new_patron_1->guarantees;
98 is
( ref(\
@guarantees), 'ARRAY', 'Koha::Patron->guarantees should return an array in a list context' );
99 is
( scalar(@guarantees), 2, 'new_patron_1 should have 2 guarantees' );
100 $_->delete for @guarantees;
102 #Test return order of guarantees BZ 18635
103 my $categorycode = $builder->build({ source
=> 'Category' })->{categorycode
};
104 my $branchcode = $builder->build({ source
=> 'Branch' })->{branchcode
};
106 my $guarantor = $builder->build_object( { class => 'Koha::Patrons' } );
108 my $order_guarantee1 = $builder->build_object( { class => 'Koha::Patrons' , value
=> {
110 guarantorid
=> $guarantor->borrowernumber
114 my $order_guarantee2 = $builder->build_object( { class => 'Koha::Patrons' , value
=> {
116 guarantorid
=> $guarantor->borrowernumber
120 my $order_guarantee3 = $builder->build_object( { class => 'Koha::Patrons' , value
=> {
122 firstname
=> 'Walrus',
123 guarantorid
=> $guarantor->borrowernumber
127 my $order_guarantee4 = $builder->build_object( { class => 'Koha::Patrons' , value
=> {
129 firstname
=> 'Vulture',
130 guarantorid
=> $guarantor->borrowernumber
134 my $order_guarantee5 = $builder->build_object( { class => 'Koha::Patrons' , value
=> {
136 firstname
=> 'Unicorn',
137 guarantorid
=> $guarantor->borrowernumber
141 $guarantees = $guarantor->guarantees();
143 is
( $guarantees->next()->borrowernumber, $order_guarantee5, "Return first guarantor alphabetically" );
144 is
( $guarantees->next()->borrowernumber, $order_guarantee4, "Return second guarantor alphabetically" );
145 is
( $guarantees->next()->borrowernumber, $order_guarantee3, "Return third guarantor alphabetically" );
146 is
( $guarantees->next()->borrowernumber, $order_guarantee2, "Return fourth guarantor alphabetically" );
147 is
( $guarantees->next()->borrowernumber, $order_guarantee1, "Return fifth guarantor alphabetically" );
150 subtest
'category' => sub {
152 my $patron_category = $new_patron_1->category;
153 is
( ref( $patron_category), 'Koha::Patron::Category', );
154 is
( $patron_category->categorycode, $category->{categorycode
}, );
157 subtest
'siblings' => sub {
159 my $siblings = $new_patron_1->siblings;
160 is
( $siblings, undef, 'Koha::Patron->siblings should not crashed if the patron has no guarantor' );
161 my $guarantee_1 = $builder->build( { source
=> 'Borrower', value
=> { guarantorid
=> $new_patron_1->borrowernumber } } );
162 my $retrieved_guarantee_1 = Koha
::Patrons
->find($guarantee_1);
163 $siblings = $retrieved_guarantee_1->siblings;
164 is
( ref($siblings), 'Koha::Patrons', 'Koha::Patron->siblings should return a Koha::Patrons result set in a scalar context' );
165 my @siblings = $retrieved_guarantee_1->siblings;
166 is
( ref( \
@siblings ), 'ARRAY', 'Koha::Patron->siblings should return an array in a list context' );
167 is
( $siblings->count, 0, 'guarantee_1 should not have siblings yet' );
168 my $guarantee_2 = $builder->build( { source
=> 'Borrower', value
=> { guarantorid
=> $new_patron_1->borrowernumber } } );
169 my $guarantee_3 = $builder->build( { source
=> 'Borrower', value
=> { guarantorid
=> $new_patron_1->borrowernumber } } );
170 $siblings = $retrieved_guarantee_1->siblings;
171 is
( $siblings->count, 2, 'guarantee_1 should have 2 siblings' );
172 is
( $guarantee_2->{borrowernumber
}, $siblings->next->borrowernumber, 'guarantee_2 should exist in the guarantees' );
173 is
( $guarantee_3->{borrowernumber
}, $siblings->next->borrowernumber, 'guarantee_3 should exist in the guarantees' );
174 $_->delete for $retrieved_guarantee_1->siblings;
175 $retrieved_guarantee_1->delete;
178 subtest
'has_overdues' => sub {
181 my $biblioitem_1 = $builder->build( { source
=> 'Biblioitem' } );
182 my $item_1 = $builder->build(
185 homebranch
=> $library->{branchcode
},
186 holdingbranch
=> $library->{branchcode
},
190 biblionumber
=> $biblioitem_1->{biblionumber
}
194 my $retrieved_patron = Koha
::Patrons
->find( $new_patron_1->borrowernumber );
195 is
( $retrieved_patron->has_overdues, 0, );
197 my $tomorrow = DateTime
->today( time_zone
=> C4
::Context
->tz() )->add( days
=> 1 );
198 my $issue = Koha
::Checkout
->new({ borrowernumber
=> $new_patron_1->id, itemnumber
=> $item_1->{itemnumber
}, date_due
=> $tomorrow, branchcode
=> $library->{branchcode
} })->store();
199 is
( $retrieved_patron->has_overdues, 0, );
201 my $yesterday = DateTime
->today(time_zone
=> C4
::Context
->tz())->add( days
=> -1 );
202 $issue = Koha
::Checkout
->new({ borrowernumber
=> $new_patron_1->id, itemnumber
=> $item_1->{itemnumber
}, date_due
=> $yesterday, branchcode
=> $library->{branchcode
} })->store();
203 $retrieved_patron = Koha
::Patrons
->find( $new_patron_1->borrowernumber );
204 is
( $retrieved_patron->has_overdues, 1, );
208 subtest
'update_password' => sub {
211 t
::lib
::Mocks
::mock_preference
( 'BorrowersLog', 1 );
212 my $original_userid = $new_patron_1->userid;
213 my $original_password = $new_patron_1->password;
214 warning_like
{ $retrieved_patron_1->update_password( $new_patron_2->userid, 'another_password' ) }
216 'Koha::Patron->update_password should warn if the userid is already used by another patron';
217 is
( Koha
::Patrons
->find( $new_patron_1->borrowernumber )->userid, $original_userid, 'Koha::Patron->update_password should not have updated the userid' );
218 is
( Koha
::Patrons
->find( $new_patron_1->borrowernumber )->password, $original_password, 'Koha::Patron->update_password should not have updated the userid' );
220 $retrieved_patron_1->update_password( 'another_nonexistent_userid_1', 'another_password' );
221 is
( Koha
::Patrons
->find( $new_patron_1->borrowernumber )->userid, 'another_nonexistent_userid_1', 'Koha::Patron->update_password should have updated the userid' );
222 is
( Koha
::Patrons
->find( $new_patron_1->borrowernumber )->password, 'another_password', 'Koha::Patron->update_password should have updated the password' );
224 my $number_of_logs = $schema->resultset('ActionLog')->search( { module
=> 'MEMBERS', action
=> 'CHANGE PASS', object
=> $new_patron_1->borrowernumber } )->count;
225 is
( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->update_password should have logged' );
227 t
::lib
::Mocks
::mock_preference
( 'BorrowersLog', 0 );
228 $retrieved_patron_1->update_password( 'yet_another_nonexistent_userid_1', 'another_password' );
229 $number_of_logs = $schema->resultset('ActionLog')->search( { module
=> 'MEMBERS', action
=> 'CHANGE PASS', object
=> $new_patron_1->borrowernumber } )->count;
230 is
( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->update_password should not have logged' );
233 subtest
'is_expired' => sub {
235 my $patron = $builder->build({ source
=> 'Borrower' });
236 $patron = Koha
::Patrons
->find( $patron->{borrowernumber
} );
237 $patron->dateexpiry( undef )->store->discard_changes;
238 is
( $patron->is_expired, 0, 'Patron should not be considered expired if dateexpiry is not set');
239 $patron->dateexpiry( dt_from_string
)->store->discard_changes;
240 is
( $patron->is_expired, 0, 'Patron should not be considered expired if dateexpiry is today');
241 $patron->dateexpiry( dt_from_string
->add( days
=> 1 ) )->store->discard_changes;
242 is
( $patron->is_expired, 0, 'Patron should not be considered expired if dateexpiry is tomorrow');
243 $patron->dateexpiry( dt_from_string
->add( days
=> -1 ) )->store->discard_changes;
244 is
( $patron->is_expired, 1, 'Patron should be considered expired if dateexpiry is yesterday');
249 subtest
'is_going_to_expire' => sub {
251 my $patron = $builder->build({ source
=> 'Borrower' });
252 $patron = Koha
::Patrons
->find( $patron->{borrowernumber
} );
253 $patron->dateexpiry( undef )->store->discard_changes;
254 is
( $patron->is_going_to_expire, 0, 'Patron should not be considered going to expire if dateexpiry is not set');
256 t
::lib
::Mocks
::mock_preference
('NotifyBorrowerDeparture', 0);
257 $patron->dateexpiry( dt_from_string
)->store->discard_changes;
258 is
( $patron->is_going_to_expire, 0, 'Patron should not be considered going to expire if dateexpiry is today');
260 $patron->dateexpiry( dt_from_string
)->store->discard_changes;
261 is
( $patron->is_going_to_expire, 0, 'Patron should not be considered going to expire if dateexpiry is today and pref is 0');
263 t
::lib
::Mocks
::mock_preference
('NotifyBorrowerDeparture', 10);
264 $patron->dateexpiry( dt_from_string
->add( days
=> 11 ) )->store->discard_changes;
265 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');
267 t
::lib
::Mocks
::mock_preference
('NotifyBorrowerDeparture', 0);
268 $patron->dateexpiry( dt_from_string
->add( days
=> 10 ) )->store->discard_changes;
269 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');
271 t
::lib
::Mocks
::mock_preference
('NotifyBorrowerDeparture', 10);
272 $patron->dateexpiry( dt_from_string
->add( days
=> 10 ) )->store->discard_changes;
273 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');
276 t
::lib
::Mocks
::mock_preference
('NotifyBorrowerDeparture', 10);
277 $patron->dateexpiry( dt_from_string
->add( days
=> 20 ) )->store->discard_changes;
278 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');
280 t
::lib
::Mocks
::mock_preference
('NotifyBorrowerDeparture', 20);
281 $patron->dateexpiry( dt_from_string
->add( days
=> 10 ) )->store->discard_changes;
282 is
( $patron->is_going_to_expire, 1, 'Patron should be considered going to expire if dateexpiry is 10 days ahead and pref is 20');
288 subtest
'renew_account' => sub {
291 for my $date ( '2016-03-31', '2016-11-30', '2019-01-31', dt_from_string
() ) {
292 my $dt = dt_from_string
( $date, 'iso' );
293 Time
::Fake
->offset( $dt->epoch );
294 my $a_month_ago = $dt->clone->subtract( months
=> 1, end_of_month
=> 'limit' )->truncate( to
=> 'day' );
295 my $a_year_later = $dt->clone->add( months
=> 12, end_of_month
=> 'limit' )->truncate( to
=> 'day' );
296 my $a_year_later_minus_a_month = $a_month_ago->clone->add( months
=> 12, end_of_month
=> 'limit' )->truncate( to
=> 'day' );
297 my $a_month_later = $dt->clone->add( months
=> 1 , end_of_month
=> 'limit' )->truncate( to
=> 'day' );
298 my $a_year_later_plus_a_month = $a_month_later->clone->add( months
=> 12, end_of_month
=> 'limit' )->truncate( to
=> 'day' );
299 my $patron_category = $builder->build(
300 { source
=> 'Category',
302 enrolmentperiod
=> 12,
303 enrolmentperioddate
=> undef,
307 my $patron = $builder->build(
308 { source
=> 'Borrower',
310 dateexpiry
=> $a_month_ago,
311 categorycode
=> $patron_category->{categorycode
},
312 date_renewed
=> undef, # Force builder to not populate the column for new patron
316 my $patron_2 = $builder->build(
317 { source
=> 'Borrower',
319 dateexpiry
=> $a_month_ago,
320 categorycode
=> $patron_category->{categorycode
},
324 my $patron_3 = $builder->build(
325 { source
=> 'Borrower',
327 dateexpiry
=> $a_month_later,
328 categorycode
=> $patron_category->{categorycode
},
332 my $retrieved_patron = Koha
::Patrons
->find( $patron->{borrowernumber
} );
333 my $retrieved_patron_2 = Koha
::Patrons
->find( $patron_2->{borrowernumber
} );
334 my $retrieved_patron_3 = Koha
::Patrons
->find( $patron_3->{borrowernumber
} );
336 is
( $retrieved_patron->date_renewed, undef, "Date renewed is not set for patrons that have never been renewed" );
338 t
::lib
::Mocks
::mock_preference
( 'BorrowerRenewalPeriodBase', 'dateexpiry' );
339 t
::lib
::Mocks
::mock_preference
( 'BorrowersLog', 1 );
340 my $expiry_date = $retrieved_patron->renew_account;
341 is
( $expiry_date, $a_year_later_minus_a_month, "$a_month_ago + 12 months must be $a_year_later_minus_a_month" );
342 my $retrieved_expiry_date = Koha
::Patrons
->find( $patron->{borrowernumber
} )->dateexpiry;
343 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" );
344 my $number_of_logs = $schema->resultset('ActionLog')->search( { module
=> 'MEMBERS', action
=> 'RENEW', object
=> $retrieved_patron->borrowernumber } )->count;
345 is
( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->renew_account should have logged' );
347 t
::lib
::Mocks
::mock_preference
( 'BorrowerRenewalPeriodBase', 'now' );
348 t
::lib
::Mocks
::mock_preference
( 'BorrowersLog', 0 );
349 $expiry_date = $retrieved_patron->renew_account;
350 is
( $expiry_date, $a_year_later, "today + 12 months must be $a_year_later" );
351 $retrieved_patron = Koha
::Patrons
->find( $patron->{borrowernumber
} );
352 is
( $retrieved_patron->date_renewed, output_pref
({ dt
=> $dt, dateformat
=> 'iso', dateonly
=> 1 }), "Date renewed is set when calling renew_account" );
353 $retrieved_expiry_date = $retrieved_patron->dateexpiry;
354 is
( dt_from_string
($retrieved_expiry_date), $a_year_later, "today + 12 months must be $a_year_later" );
355 $number_of_logs = $schema->resultset('ActionLog')->search( { module
=> 'MEMBERS', action
=> 'RENEW', object
=> $retrieved_patron->borrowernumber } )->count;
356 is
( $number_of_logs, 1, 'Without BorrowerLogs, Koha::Patron->renew_account should not have logged' );
358 t
::lib
::Mocks
::mock_preference
( 'BorrowerRenewalPeriodBase', 'combination' );
359 $expiry_date = $retrieved_patron_2->renew_account;
360 is
( $expiry_date, $a_year_later, "today + 12 months must be $a_year_later" );
361 $retrieved_expiry_date = Koha
::Patrons
->find( $patron_2->{borrowernumber
} )->dateexpiry;
362 is
( dt_from_string
($retrieved_expiry_date), $a_year_later, "today + 12 months must be $a_year_later" );
364 $expiry_date = $retrieved_patron_3->renew_account;
365 is
( $expiry_date, $a_year_later_plus_a_month, "$a_month_later + 12 months must be $a_year_later_plus_a_month" );
366 $retrieved_expiry_date = Koha
::Patrons
->find( $patron_3->{borrowernumber
} )->dateexpiry;
367 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" );
369 $retrieved_patron->delete;
370 $retrieved_patron_2->delete;
371 $retrieved_patron_3->delete;
376 subtest
"move_to_deleted" => sub {
378 my $originally_updated_on = '2016-01-01 12:12:12';
379 my $patron = $builder->build( { source
=> 'Borrower',value
=> { updated_on
=> $originally_updated_on } } );
380 my $retrieved_patron = Koha
::Patrons
->find( $patron->{borrowernumber
} );
381 is
( ref( $retrieved_patron->move_to_deleted ), 'Koha::Schema::Result::Deletedborrower', 'Koha::Patron->move_to_deleted should return the Deleted patron' )
382 ; # FIXME This should be Koha::Deleted::Patron
383 my $deleted_patron = $schema->resultset('Deletedborrower')
384 ->search( { borrowernumber
=> $patron->{borrowernumber
} }, { result_class
=> 'DBIx::Class::ResultClass::HashRefInflator' } )
386 ok
( $retrieved_patron->updated_on, 'updated_on should be set for borrowers table' );
387 ok
( $deleted_patron->{updated_on
}, 'updated_on should be set for deleted_borrowers table' );
388 isnt
( $deleted_patron->{updated_on
}, $retrieved_patron->updated_on, 'Koha::Patron->move_to_deleted should have correctly updated the updated_on column');
389 $deleted_patron->{updated_on
} = $originally_updated_on; #reset for simplicity in comparing all other fields
390 is_deeply
( $deleted_patron, $patron, 'Koha::Patron->move_to_deleted should have correctly moved the patron to the deleted table' );
391 $retrieved_patron->delete( $patron->{borrowernumber
} ); # Cleanup
394 subtest
"delete" => sub {
396 t
::lib
::Mocks
::mock_preference
( 'BorrowersLog', 1 );
397 my $patron = $builder->build( { source
=> 'Borrower' } );
398 my $retrieved_patron = Koha
::Patrons
->find( $patron->{borrowernumber
} );
399 my $hold = $builder->build(
400 { source
=> 'Reserve',
401 value
=> { borrowernumber
=> $patron->{borrowernumber
} }
404 my $list = $builder->build(
405 { source
=> 'Virtualshelve',
406 value
=> { owner
=> $patron->{borrowernumber
} }
410 my $deleted = $retrieved_patron->delete;
411 is
( $deleted, 1, 'Koha::Patron->delete should return 1 if the patron has been correctly deleted' );
413 is
( Koha
::Patrons
->find( $patron->{borrowernumber
} ), undef, 'Koha::Patron->delete should have deleted the patron' );
415 is
( Koha
::Holds
->search( { borrowernumber
=> $patron->{borrowernumber
} } )->count, 0, q
|Koha
::Patron
->delete should have deleted patron
's holds| );
417 is( Koha::Virtualshelves->search( { owner => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's lists
| );
419 my $number_of_logs = $schema->resultset('ActionLog')->search( { module
=> 'MEMBERS', action
=> 'DELETE', object
=> $retrieved_patron->borrowernumber } )->count;
420 is
( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->delete should have logged' );
423 subtest
'add_enrolment_fee_if_needed' => sub {
426 my $enrolmentfees = { K
=> 5, J
=> 10, YA
=> 20 };
427 foreach( keys %{$enrolmentfees} ) {
428 ( Koha
::Patron
::Categories
->find( $_ ) // $builder->build_object({ class => 'Koha::Patron::Categories', value
=> { categorycode
=> $_ } }) )->enrolmentfee( $enrolmentfees->{$_} )->store;
430 my $enrolmentfee_K = $enrolmentfees->{K
};
431 my $enrolmentfee_J = $enrolmentfees->{J
};
432 my $enrolmentfee_YA = $enrolmentfees->{YA
};
434 my %borrower_data = (
435 firstname
=> 'my firstname',
436 surname
=> 'my surname',
438 branchcode
=> $library->{branchcode
},
441 my $borrowernumber = C4
::Members
::AddMember
(%borrower_data);
442 $borrower_data{borrowernumber
} = $borrowernumber;
444 my ($total) = C4
::Members
::GetMemberAccountRecords
($borrowernumber);
445 is
( $total, $enrolmentfee_K, "New kid pay $enrolmentfee_K" );
447 t
::lib
::Mocks
::mock_preference
( 'FeeOnChangePatronCategory', 0 );
448 $borrower_data{categorycode
} = 'J';
449 C4
::Members
::ModMember
(%borrower_data);
450 ($total) = C4
::Members
::GetMemberAccountRecords
($borrowernumber);
451 is
( $total, $enrolmentfee_K, "Kid growing and become a juvenile, but shouldn't pay for the upgrade " );
453 $borrower_data{categorycode
} = 'K';
454 C4
::Members
::ModMember
(%borrower_data);
455 t
::lib
::Mocks
::mock_preference
( 'FeeOnChangePatronCategory', 1 );
457 $borrower_data{categorycode
} = 'J';
458 C4
::Members
::ModMember
(%borrower_data);
459 ($total) = C4
::Members
::GetMemberAccountRecords
($borrowernumber);
460 is
( $total, $enrolmentfee_K + $enrolmentfee_J, "Kid growing and become a juvenile, they should pay " . ( $enrolmentfee_K + $enrolmentfee_J ) );
462 # Check with calling directly Koha::Patron->get_enrolment_fee_if_needed
463 my $patron = Koha
::Patrons
->find($borrowernumber);
464 $patron->categorycode('YA')->store;
465 my $fee = $patron->add_enrolment_fee_if_needed;
466 ($total) = C4
::Members
::GetMemberAccountRecords
($borrowernumber);
468 $enrolmentfee_K + $enrolmentfee_J + $enrolmentfee_YA,
469 "Juvenile growing and become an young adult, they should pay " . ( $enrolmentfee_K + $enrolmentfee_J + $enrolmentfee_YA )
475 subtest
'checkouts + get_overdues + old_checkouts' => sub {
478 my $library = $builder->build( { source
=> 'Branch' } );
479 my ($biblionumber_1) = AddBiblio
( MARC
::Record
->new, '' );
480 my $item_1 = $builder->build(
484 homebranch
=> $library->{branchcode
},
485 holdingbranch
=> $library->{branchcode
},
486 biblionumber
=> $biblionumber_1,
492 my $item_2 = $builder->build(
496 homebranch
=> $library->{branchcode
},
497 holdingbranch
=> $library->{branchcode
},
498 biblionumber
=> $biblionumber_1,
504 my ($biblionumber_2) = AddBiblio
( MARC
::Record
->new, '' );
505 my $item_3 = $builder->build(
509 homebranch
=> $library->{branchcode
},
510 holdingbranch
=> $library->{branchcode
},
511 biblionumber
=> $biblionumber_2,
517 my $patron = $builder->build(
519 source
=> 'Borrower',
520 value
=> { branchcode
=> $library->{branchcode
} }
524 $patron = Koha
::Patrons
->find( $patron->{borrowernumber
} );
525 my $checkouts = $patron->checkouts;
526 is
( $checkouts->count, 0, 'checkouts should not return any issues for that patron' );
527 is
( ref($checkouts), 'Koha::Checkouts', 'checkouts should return a Koha::Checkouts object' );
528 my $old_checkouts = $patron->old_checkouts;
529 is
( $old_checkouts->count, 0, 'old_checkouts should not return any issues for that patron' );
530 is
( ref($old_checkouts), 'Koha::Old::Checkouts', 'old_checkouts should return a Koha::Old::Checkouts object' );
532 # Not sure how this is useful, but AddIssue pass this variable to different other subroutines
533 $patron = Koha
::Patrons
->find( $patron->borrowernumber )->unblessed;
535 my $module = new Test
::MockModule
('C4::Context');
536 $module->mock( 'userenv', sub { { branch
=> $library->{branchcode
} } } );
538 AddIssue
( $patron, $item_1->{barcode
}, DateTime
->now->subtract( days
=> 1 ) );
539 AddIssue
( $patron, $item_2->{barcode
}, DateTime
->now->subtract( days
=> 5 ) );
540 AddIssue
( $patron, $item_3->{barcode
} );
542 $patron = Koha
::Patrons
->find( $patron->{borrowernumber
} );
543 $checkouts = $patron->checkouts;
544 is
( $checkouts->count, 3, 'checkouts should return 3 issues for that patron' );
545 is
( ref($checkouts), 'Koha::Checkouts', 'checkouts should return a Koha::Checkouts object' );
547 my $overdues = $patron->get_overdues;
548 is
( $overdues->count, 2, 'Patron should have 2 overdues');
549 is
( ref($overdues), 'Koha::Checkouts', 'Koha::Patron->get_overdues should return Koha::Checkouts' );
550 is
( $overdues->next->itemnumber, $item_1->{itemnumber
}, 'The issue should be returned in the same order as they have been done, first is correct' );
551 is
( $overdues->next->itemnumber, $item_2->{itemnumber
}, 'The issue should be returned in the same order as they have been done, second is correct' );
554 C4
::Circulation
::AddReturn
( $item_1->{barcode
} );
555 C4
::Circulation
::AddReturn
( $item_2->{barcode
} );
556 $old_checkouts = $patron->old_checkouts;
557 is
( $old_checkouts->count, 2, 'old_checkouts should return 2 old checkouts that patron' );
558 is
( ref($old_checkouts), 'Koha::Old::Checkouts', 'old_checkouts should return a Koha::Old::Checkouts object' );
561 Koha
::Checkouts
->search( { borrowernumber
=> $patron->borrowernumber } )->delete;
563 $module->unmock('userenv');
566 subtest
'get_age' => sub {
569 my $patron = $builder->build( { source
=> 'Borrower' } );
570 $patron = Koha
::Patrons
->find( $patron->{borrowernumber
} );
572 my $today = dt_from_string
;
574 $patron->dateofbirth( undef );
575 is
( $patron->get_age, undef, 'get_age should return undef if no dateofbirth is defined' );
576 $patron->dateofbirth( $today->clone->add( years
=> -12, months
=> -6, days
=> -1, end_of_month
=> 'limit' ) );
577 is
( $patron->get_age, 12, 'Patron should be 12' );
578 $patron->dateofbirth( $today->clone->add( years
=> -18, months
=> 0, days
=> 1, end_of_month
=> 'limit' ) );
579 is
( $patron->get_age, 17, 'Patron should be 17, happy birthday tomorrow!' );
580 $patron->dateofbirth( $today->clone->add( years
=> -18, months
=> 0, days
=> 0, end_of_month
=> 'limit' ) );
581 is
( $patron->get_age, 18, 'Patron should be 18' );
582 $patron->dateofbirth( $today->clone->add( years
=> -18, months
=> -12, days
=> -31, end_of_month
=> 'limit' ) );
583 is
( $patron->get_age, 19, 'Patron should be 19' );
584 $patron->dateofbirth( $today->clone->add( years
=> -18, months
=> -12, days
=> -30, end_of_month
=> 'limit' ) );
585 is
( $patron->get_age, 19, 'Patron should be 19 again' );
586 $patron->dateofbirth( $today->clone->add( years
=> 0, months
=> -1, days
=> -1, end_of_month
=> 'limit' ) );
587 is
( $patron->get_age, 0, 'Patron is a newborn child' );
592 subtest
'account' => sub {
595 my $patron = $builder->build({source
=> 'Borrower'});
597 $patron = Koha
::Patrons
->find( $patron->{borrowernumber
} );
598 my $account = $patron->account;
599 is
( ref($account), 'Koha::Account', 'account should return a Koha::Account object' );
604 subtest
'search_upcoming_membership_expires' => sub {
607 my $expiry_days = 15;
608 t
::lib
::Mocks
::mock_preference
( 'MembershipExpiryDaysNotice', $expiry_days );
609 my $nb_of_days_before = 1;
610 my $nb_of_days_after = 2;
612 my $builder = t
::lib
::TestBuilder
->new();
614 my $library = $builder->build({ source
=> 'Branch' });
616 # before we add borrowers to this branch, add the expires we have now
617 # note that this pertains to the current mocked setting of the pref
618 # for this reason we add the new branchcode to most of the tests
619 my $nb_of_expires = Koha
::Patrons
->search_upcoming_membership_expires->count;
621 my $patron_1 = $builder->build({
622 source
=> 'Borrower',
624 branchcode
=> $library->{branchcode
},
625 dateexpiry
=> dt_from_string
->add( days
=> $expiry_days )
629 my $patron_2 = $builder->build({
630 source
=> 'Borrower',
632 branchcode
=> $library->{branchcode
},
633 dateexpiry
=> dt_from_string
->add( days
=> $expiry_days - $nb_of_days_before )
637 my $patron_3 = $builder->build({
638 source
=> 'Borrower',
640 branchcode
=> $library->{branchcode
},
641 dateexpiry
=> dt_from_string
->add( days
=> $expiry_days + $nb_of_days_after )
645 # Test without extra parameters
646 my $upcoming_mem_expires = Koha
::Patrons
->search_upcoming_membership_expires();
647 is
( $upcoming_mem_expires->count, $nb_of_expires + 1, 'Get upcoming membership expires should return one new borrower.' );
650 $upcoming_mem_expires = Koha
::Patrons
->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode
} });
651 is
( $upcoming_mem_expires->count, 1, 'Test with branch parameter' );
652 my $expired = $upcoming_mem_expires->next;
653 is
( $expired->surname, $patron_1->{surname
}, 'Get upcoming membership expires should return the correct patron.' );
654 is
( $expired->library->branchemail, $library->{branchemail
}, 'Get upcoming membership expires should return the correct patron.' );
655 is
( $expired->branchcode, $patron_1->{branchcode
}, 'Get upcoming membership expires should return the correct patron.' );
657 t
::lib
::Mocks
::mock_preference
( 'MembershipExpiryDaysNotice', 0 );
658 $upcoming_mem_expires = Koha
::Patrons
->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode
} });
659 is
( $upcoming_mem_expires->count, 0, 'Get upcoming membership expires with MembershipExpiryDaysNotice==0 should not return new records.' );
661 # Test MembershipExpiryDaysNotice == undef
662 t
::lib
::Mocks
::mock_preference
( 'MembershipExpiryDaysNotice', undef );
663 $upcoming_mem_expires = Koha
::Patrons
->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode
} });
664 is
( $upcoming_mem_expires->count, 0, 'Get upcoming membership expires without MembershipExpiryDaysNotice should not return new records.' );
666 # Test the before parameter
667 t
::lib
::Mocks
::mock_preference
( 'MembershipExpiryDaysNotice', 15 );
668 $upcoming_mem_expires = Koha
::Patrons
->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode
}, before
=> $nb_of_days_before });
669 is
( $upcoming_mem_expires->count, 2, 'Expect two results for before');
670 # Test after parameter also
671 $upcoming_mem_expires = Koha
::Patrons
->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode
}, before
=> $nb_of_days_before, after
=> $nb_of_days_after });
672 is
( $upcoming_mem_expires->count, 3, 'Expect three results when adding after' );
673 Koha
::Patrons
->search({ borrowernumber
=> { in => [ $patron_1->{borrowernumber
}, $patron_2->{borrowernumber
}, $patron_3->{borrowernumber
} ] } })->delete;
676 subtest
'holds and old_holds' => sub {
679 my $library = $builder->build( { source
=> 'Branch' } );
680 my ($biblionumber_1) = AddBiblio
( MARC
::Record
->new, '' );
681 my $item_1 = $builder->build(
685 homebranch
=> $library->{branchcode
},
686 holdingbranch
=> $library->{branchcode
},
687 biblionumber
=> $biblionumber_1
691 my $item_2 = $builder->build(
695 homebranch
=> $library->{branchcode
},
696 holdingbranch
=> $library->{branchcode
},
697 biblionumber
=> $biblionumber_1
701 my ($biblionumber_2) = AddBiblio
( MARC
::Record
->new, '' );
702 my $item_3 = $builder->build(
706 homebranch
=> $library->{branchcode
},
707 holdingbranch
=> $library->{branchcode
},
708 biblionumber
=> $biblionumber_2
712 my $patron = $builder->build(
714 source
=> 'Borrower',
715 value
=> { branchcode
=> $library->{branchcode
} }
719 $patron = Koha
::Patrons
->find( $patron->{borrowernumber
} );
720 my $holds = $patron->holds;
721 is
( ref($holds), 'Koha::Holds',
722 'Koha::Patron->holds should return a Koha::Holds objects' );
723 is
( $holds->count, 0, 'There should not be holds placed by this patron yet' );
725 C4
::Reserves
::AddReserve
( $library->{branchcode
},
726 $patron->borrowernumber, $biblionumber_1 );
728 C4
::Reserves
::AddReserve
( $library->{branchcode
},
729 $patron->borrowernumber, $biblionumber_2, undef, undef, dt_from_string
->add( days
=> 2 ) );
731 $holds = $patron->holds;
732 is
( $holds->count, 2, 'There should be 2 holds placed by this patron' );
734 my $old_holds = $patron->old_holds;
735 is
( ref($old_holds), 'Koha::Old::Holds',
736 'Koha::Patron->old_holds should return a Koha::Old::Holds objects' );
737 is
( $old_holds->count, 0, 'There should not be any old holds yet');
739 my $hold = $holds->next;
742 $old_holds = $patron->old_holds;
743 is
( $old_holds->count, 1, 'There should be 1 old (cancelled) hold');
750 subtest
'search_patrons_to_anonymise & anonymise_issue_history' => sub {
753 # TODO create a subroutine in t::lib::Mocks
754 my $branch = $builder->build({ source
=> 'Branch' });
755 my $userenv_patron = $builder->build({
756 source
=> 'Borrower',
757 value
=> { branchcode
=> $branch->{branchcode
} },
759 C4
::Context
->_new_userenv('DUMMY SESSION');
760 C4
::Context
->set_userenv(
761 $userenv_patron->{borrowernumber
},
762 $userenv_patron->{userid
},
763 'usercnum', 'First name', 'Surname',
764 $branch->{branchcode
},
765 $branch->{branchname
},
768 my $anonymous = $builder->build( { source
=> 'Borrower', }, );
770 t
::lib
::Mocks
::mock_preference
( 'AnonymousPatron', $anonymous->{borrowernumber
} );
772 subtest
'patron privacy is 1 (default)' => sub {
775 t
::lib
::Mocks
::mock_preference
('IndependentBranches', 0);
776 my $patron = $builder->build(
777 { source
=> 'Borrower',
778 value
=> { privacy
=> 1, }
781 my $item_1 = $builder->build(
789 my $issue_1 = $builder->build(
792 borrowernumber
=> $patron->{borrowernumber
},
793 itemnumber
=> $item_1->{itemnumber
},
797 my $item_2 = $builder->build(
805 my $issue_2 = $builder->build(
808 borrowernumber
=> $patron->{borrowernumber
},
809 itemnumber
=> $item_2->{itemnumber
},
814 my ( $returned_1, undef, undef ) = C4
::Circulation
::AddReturn
( $item_1->{barcode
}, undef, undef, undef, '2010-10-10' );
815 my ( $returned_2, undef, undef ) = C4
::Circulation
::AddReturn
( $item_2->{barcode
}, undef, undef, undef, '2011-11-11' );
816 is
( $returned_1 && $returned_2, 1, 'The items should have been returned' );
818 my $patrons_to_anonymise = Koha
::Patrons
->search_patrons_to_anonymise( { before
=> '2010-10-11' } )->search( { 'me.borrowernumber' => $patron->{borrowernumber
} } );
819 is
( ref($patrons_to_anonymise), 'Koha::Patrons', 'search_patrons_to_anonymise should return Koha::Patrons' );
821 my $rows_affected = Koha
::Patrons
->search_patrons_to_anonymise( { before
=> '2011-11-12' } )->anonymise_issue_history( { before
=> '2010-10-11' } );
822 ok
( $rows_affected > 0, 'AnonymiseIssueHistory should affect at least 1 row' );
824 my $dbh = C4
::Context
->dbh;
825 my $sth = $dbh->prepare(q
|SELECT borrowernumber FROM old_issues where itemnumber
= ?
|);
826 $sth->execute($item_1->{itemnumber
});
827 my ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
828 is
( $borrowernumber_used_to_anonymised, $anonymous->{borrowernumber
}, 'With privacy=1, the issue should have been anonymised' );
829 $sth->execute($item_2->{itemnumber
});
830 ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
831 is
( $borrowernumber_used_to_anonymised, $patron->{borrowernumber
}, 'The issue should not have been anonymised, the returned date is later' );
833 $rows_affected = Koha
::Patrons
->search_patrons_to_anonymise( { before
=> '2011-11-12' } )->anonymise_issue_history;
834 $sth->execute($item_2->{itemnumber
});
835 ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
836 is
( $borrowernumber_used_to_anonymised, $anonymous->{borrowernumber
}, 'The issue should have been anonymised, the returned date is before' );
838 my $sth_reset = $dbh->prepare(q
|UPDATE old_issues SET borrowernumber
= ? WHERE itemnumber
= ?
|);
839 $sth_reset->execute( $patron->{borrowernumber
}, $item_1->{itemnumber
} );
840 $sth_reset->execute( $patron->{borrowernumber
}, $item_2->{itemnumber
} );
841 $rows_affected = Koha
::Patrons
->search_patrons_to_anonymise->anonymise_issue_history;
842 $sth->execute($item_1->{itemnumber
});
843 ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
844 is
( $borrowernumber_used_to_anonymised, $anonymous->{borrowernumber
}, 'The issue 1 should have been anonymised, before parameter was not passed' );
845 $sth->execute($item_2->{itemnumber
});
846 ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
847 is
( $borrowernumber_used_to_anonymised, $anonymous->{borrowernumber
}, 'The issue 2 should have been anonymised, before parameter was not passed' );
849 Koha
::Patrons
->find( $patron->{borrowernumber
})->delete;
852 subtest
'patron privacy is 0 (forever)' => sub {
855 t
::lib
::Mocks
::mock_preference
('IndependentBranches', 0);
856 my $patron = $builder->build(
857 { source
=> 'Borrower',
858 value
=> { privacy
=> 0, }
861 my $item = $builder->build(
869 my $issue = $builder->build(
872 borrowernumber
=> $patron->{borrowernumber
},
873 itemnumber
=> $item->{itemnumber
},
878 my ( $returned, undef, undef ) = C4
::Circulation
::AddReturn
( $item->{barcode
}, undef, undef, undef, '2010-10-10' );
879 is
( $returned, 1, 'The item should have been returned' );
880 my $rows_affected = Koha
::Patrons
->search_patrons_to_anonymise( { before
=> '2010-10-11' } )->anonymise_issue_history( { before
=> '2010-10-11' } );
881 ok
( $rows_affected > 0, 'AnonymiseIssueHistory should not return any error if success' );
883 my $dbh = C4
::Context
->dbh;
884 my ($borrowernumber_used_to_anonymised) = $dbh->selectrow_array(q
|
885 SELECT borrowernumber FROM old_issues where itemnumber
= ?
886 |, undef, $item->{itemnumber
});
887 is
( $borrowernumber_used_to_anonymised, $patron->{borrowernumber
}, 'With privacy=0, the issue should not be anonymised' );
888 Koha
::Patrons
->find( $patron->{borrowernumber
})->delete;
891 t
::lib
::Mocks
::mock_preference
( 'AnonymousPatron', '' );
893 subtest
'AnonymousPatron is not defined' => sub {
896 t
::lib
::Mocks
::mock_preference
('IndependentBranches', 0);
897 my $patron = $builder->build(
898 { source
=> 'Borrower',
899 value
=> { privacy
=> 1, }
902 my $item = $builder->build(
910 my $issue = $builder->build(
913 borrowernumber
=> $patron->{borrowernumber
},
914 itemnumber
=> $item->{itemnumber
},
919 my ( $returned, undef, undef ) = C4
::Circulation
::AddReturn
( $item->{barcode
}, undef, undef, undef, '2010-10-10' );
920 is
( $returned, 1, 'The item should have been returned' );
921 my $rows_affected = Koha
::Patrons
->search_patrons_to_anonymise( { before
=> '2010-10-11' } )->anonymise_issue_history( { before
=> '2010-10-11' } );
922 ok
( $rows_affected > 0, 'AnonymiseIssueHistory should affect at least 1 row' );
924 my $dbh = C4
::Context
->dbh;
925 my ($borrowernumber_used_to_anonymised) = $dbh->selectrow_array(q
|
926 SELECT borrowernumber FROM old_issues where itemnumber
= ?
927 |, undef, $item->{itemnumber
});
928 is
( $borrowernumber_used_to_anonymised, undef, 'With AnonymousPatron is not defined, the issue should have been anonymised anyway' );
929 Koha
::Patrons
->find( $patron->{borrowernumber
})->delete;
932 subtest
'Logged in librarian is not superlibrarian & IndependentBranches' => sub {
934 t
::lib
::Mocks
::mock_preference
( 'IndependentBranches', 1 );
935 my $patron = $builder->build(
936 { source
=> 'Borrower',
937 value
=> { privacy
=> 1 } # Another branchcode than the logged in librarian
940 my $item = $builder->build(
948 my $issue = $builder->build(
951 borrowernumber
=> $patron->{borrowernumber
},
952 itemnumber
=> $item->{itemnumber
},
957 my ( $returned, undef, undef ) = C4
::Circulation
::AddReturn
( $item->{barcode
}, undef, undef, undef, '2010-10-10' );
958 is
( Koha
::Patrons
->search_patrons_to_anonymise( { before
=> '2010-10-11' } )->count, 0 );
959 Koha
::Patrons
->find( $patron->{borrowernumber
})->delete;
962 Koha
::Patrons
->find( $anonymous->{borrowernumber
})->delete;
963 Koha
::Patrons
->find( $userenv_patron->{borrowernumber
})->delete;
966 subtest
'account_locked' => sub {
968 my $patron = $builder->build({ source
=> 'Borrower', value
=> { login_attempts
=> 0 } });
969 $patron = Koha
::Patrons
->find( $patron->{borrowernumber
} );
970 for my $value ( undef, '', 0 ) {
971 t
::lib
::Mocks
::mock_preference
('FailedloginAttempts', $value);
972 is
( $patron->account_locked, 0, 'Feature is disabled, patron account should not be considered locked' );
973 $patron->login_attempts(1)->store;
974 is
( $patron->account_locked, 0, 'Feature is disabled, patron account should not be considered locked' );
977 t
::lib
::Mocks
::mock_preference
('FailedloginAttempts', 3);
978 $patron->login_attempts(2)->store;
979 is
( $patron->account_locked, 0, 'Patron has 2 failed attempts, account should not be considered locked yet' );
980 $patron->login_attempts(3)->store;
981 is
( $patron->account_locked, 1, 'Patron has 3 failed attempts, account should be considered locked yet' );
986 $nb_of_patrons = Koha
::Patrons
->search->count;
987 $retrieved_patron_1->delete;
988 is
( Koha
::Patrons
->search->count, $nb_of_patrons - 1, 'Delete should have deleted the patron' );
990 $schema->storage->txn_rollback;