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 => 15;
35 use Koha::Virtualshelves;
37 use t::lib::TestBuilder;
40 my $schema = Koha::Database->new->schema;
41 $schema->storage->txn_begin;
43 my $builder = t::lib::TestBuilder->new;
44 my $library = $builder->build({source => 'Branch' });
45 my $category = $builder->build({source => 'Category' });
46 my $nb_of_patrons = Koha::Patrons->search->count;
47 my $new_patron_1 = Koha::Patron->new(
48 { cardnumber => 'test_cn_1',
49 branchcode => $library->{branchcode},
50 categorycode => $category->{categorycode},
51 surname => 'surname for patron1',
52 firstname => 'firstname for patron1',
53 userid => 'a_nonexistent_userid_1',
56 my $new_patron_2 = Koha::Patron->new(
57 { cardnumber => 'test_cn_2',
58 branchcode => $library->{branchcode},
59 categorycode => $category->{categorycode},
60 surname => 'surname for patron2',
61 firstname => 'firstname for patron2',
62 userid => 'a_nonexistent_userid_2',
66 C4::Context->_new_userenv('xxx');
67 C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, 'Midway Public Library', '', '', '');
69 is( Koha::Patrons->search->count, $nb_of_patrons + 2, 'The 2 patrons should have been added' );
71 my $retrieved_patron_1 = Koha::Patrons->find( $new_patron_1->borrowernumber );
72 is( $retrieved_patron_1->cardnumber, $new_patron_1->cardnumber, 'Find a patron by borrowernumber should return the correct patron' );
74 subtest 'guarantees' => sub {
76 my $guarantees = $new_patron_1->guarantees;
77 is( ref($guarantees), 'Koha::Patrons', 'Koha::Patron->guarantees should return a Koha::Patrons result set in a scalar context' );
78 is( $guarantees->count, 0, 'new_patron_1 should have 0 guarantee' );
79 my @guarantees = $new_patron_1->guarantees;
80 is( ref(\@guarantees), 'ARRAY', 'Koha::Patron->guarantees should return an array in a list context' );
81 is( scalar(@guarantees), 0, 'new_patron_1 should have 0 guarantee' );
83 my $guarantee_1 = $builder->build({ source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber }});
84 my $guarantee_2 = $builder->build({ source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber }});
86 $guarantees = $new_patron_1->guarantees;
87 is( ref($guarantees), 'Koha::Patrons', 'Koha::Patron->guarantees should return a Koha::Patrons result set in a scalar context' );
88 is( $guarantees->count, 2, 'new_patron_1 should have 2 guarantees' );
89 @guarantees = $new_patron_1->guarantees;
90 is( ref(\@guarantees), 'ARRAY', 'Koha::Patron->guarantees should return an array in a list context' );
91 is( scalar(@guarantees), 2, 'new_patron_1 should have 2 guarantees' );
92 $_->delete for @guarantees;
95 subtest 'category' => sub {
97 my $patron_category = $new_patron_1->category;
98 is( ref( $patron_category), 'Koha::Patron::Category', );
99 is( $patron_category->categorycode, $category->{categorycode}, );
102 subtest 'siblings' => sub {
104 my $siblings = $new_patron_1->siblings;
105 is( $siblings, undef, 'Koha::Patron->siblings should not crashed if the patron has no guarantor' );
106 my $guarantee_1 = $builder->build( { source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber } } );
107 my $retrieved_guarantee_1 = Koha::Patrons->find($guarantee_1);
108 $siblings = $retrieved_guarantee_1->siblings;
109 is( ref($siblings), 'Koha::Patrons', 'Koha::Patron->siblings should return a Koha::Patrons result set in a scalar context' );
110 my @siblings = $retrieved_guarantee_1->siblings;
111 is( ref( \@siblings ), 'ARRAY', 'Koha::Patron->siblings should return an array in a list context' );
112 is( $siblings->count, 0, 'guarantee_1 should not have siblings yet' );
113 my $guarantee_2 = $builder->build( { source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber } } );
114 my $guarantee_3 = $builder->build( { source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber } } );
115 $siblings = $retrieved_guarantee_1->siblings;
116 is( $siblings->count, 2, 'guarantee_1 should have 2 siblings' );
117 is( $guarantee_2->{borrowernumber}, $siblings->next->borrowernumber, 'guarantee_2 should exist in the guarantees' );
118 is( $guarantee_3->{borrowernumber}, $siblings->next->borrowernumber, 'guarantee_3 should exist in the guarantees' );
119 $_->delete for $retrieved_guarantee_1->siblings;
120 $retrieved_guarantee_1->delete;
123 subtest 'has_overdues' => sub {
126 my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
127 my $item_1 = $builder->build(
130 homebranch => $library->{branchcode},
131 holdingbranch => $library->{branchcode},
135 biblionumber => $biblioitem_1->{biblionumber}
139 my $retrieved_patron = Koha::Patrons->find( $new_patron_1->borrowernumber );
140 is( $retrieved_patron->has_overdues, 0, );
142 my $tomorrow = DateTime->today( time_zone => C4::Context->tz() )->add( days => 1 );
143 my $issue = Koha::Issue->new({ borrowernumber => $new_patron_1->id, itemnumber => $item_1->{itemnumber}, date_due => $tomorrow, branchcode => $library->{branchcode} })->store();
144 is( $retrieved_patron->has_overdues, 0, );
146 my $yesterday = DateTime->today(time_zone => C4::Context->tz())->add( days => -1 );
147 $issue = Koha::Issue->new({ borrowernumber => $new_patron_1->id, itemnumber => $item_1->{itemnumber}, date_due => $yesterday, branchcode => $library->{branchcode} })->store();
148 $retrieved_patron = Koha::Patrons->find( $new_patron_1->borrowernumber );
149 is( $retrieved_patron->has_overdues, 1, );
153 subtest 'update_password' => sub {
156 t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
157 my $original_userid = $new_patron_1->userid;
158 my $original_password = $new_patron_1->password;
159 warning_like { $retrieved_patron_1->update_password( $new_patron_2->userid, 'another_password' ) }
161 'Koha::Patron->update_password should warn if the userid is already used by another patron';
162 is( Koha::Patrons->find( $new_patron_1->borrowernumber )->userid, $original_userid, 'Koha::Patron->update_password should not have updated the userid' );
163 is( Koha::Patrons->find( $new_patron_1->borrowernumber )->password, $original_password, 'Koha::Patron->update_password should not have updated the userid' );
165 $retrieved_patron_1->update_password( 'another_nonexistent_userid_1', 'another_password' );
166 is( Koha::Patrons->find( $new_patron_1->borrowernumber )->userid, 'another_nonexistent_userid_1', 'Koha::Patron->update_password should have updated the userid' );
167 is( Koha::Patrons->find( $new_patron_1->borrowernumber )->password, 'another_password', 'Koha::Patron->update_password should have updated the password' );
169 my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'CHANGE PASS', object => $new_patron_1->borrowernumber } )->count;
170 is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->update_password should have logged' );
172 t::lib::Mocks::mock_preference( 'BorrowersLog', 0 );
173 $retrieved_patron_1->update_password( 'yet_another_nonexistent_userid_1', 'another_password' );
174 $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'CHANGE PASS', object => $new_patron_1->borrowernumber } )->count;
175 is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->update_password should not have logged' );
178 subtest 'is_expired' => sub {
180 my $patron = $builder->build({ source => 'Borrower' });
181 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
182 $patron->dateexpiry( undef )->store->discard_changes;
183 is( $patron->is_expired, 0, 'Patron should not be considered expired if dateexpiry is not set');
184 $patron->dateexpiry( '0000-00-00' )->store->discard_changes;
185 is( $patron->is_expired, 0, 'Patron should not be considered expired if dateexpiry is not 0000-00-00');
186 $patron->dateexpiry( dt_from_string )->store->discard_changes;
187 is( $patron->is_expired, 0, 'Patron should not be considered expired if dateexpiry is today');
188 $patron->dateexpiry( dt_from_string->add( days => 1 ) )->store->discard_changes;
189 is( $patron->is_expired, 0, 'Patron should not be considered expired if dateexpiry is tomorrow');
190 $patron->dateexpiry( dt_from_string->add( days => -1 ) )->store->discard_changes;
191 is( $patron->is_expired, 1, 'Patron should be considered expired if dateexpiry is yesterday');
196 subtest 'is_going_to_expire' => sub {
198 my $patron = $builder->build({ source => 'Borrower' });
199 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
200 $patron->dateexpiry( undef )->store->discard_changes;
201 is( $patron->is_going_to_expire, 0, 'Patron should not be considered going to expire if dateexpiry is not set');
202 $patron->dateexpiry( '0000-00-00' )->store->discard_changes;
203 is( $patron->is_going_to_expire, 0, 'Patron should not be considered going to expire if dateexpiry is not 0000-00-00');
205 t::lib::Mocks::mock_preference('NotifyBorrowerDeparture', 0);
206 $patron->dateexpiry( dt_from_string )->store->discard_changes;
207 is( $patron->is_going_to_expire, 0, 'Patron should not be considered going to expire if dateexpiry is today');
209 $patron->dateexpiry( dt_from_string )->store->discard_changes;
210 is( $patron->is_going_to_expire, 0, 'Patron should not be considered going to expire if dateexpiry is today and pref is 0');
212 t::lib::Mocks::mock_preference('NotifyBorrowerDeparture', 10);
213 $patron->dateexpiry( dt_from_string->add( days => 11 ) )->store->discard_changes;
214 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');
216 t::lib::Mocks::mock_preference('NotifyBorrowerDeparture', 0);
217 $patron->dateexpiry( dt_from_string->add( days => 10 ) )->store->discard_changes;
218 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');
220 t::lib::Mocks::mock_preference('NotifyBorrowerDeparture', 10);
221 $patron->dateexpiry( dt_from_string->add( days => 10 ) )->store->discard_changes;
222 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');
225 t::lib::Mocks::mock_preference('NotifyBorrowerDeparture', 10);
226 $patron->dateexpiry( dt_from_string->add( days => 20 ) )->store->discard_changes;
227 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');
229 t::lib::Mocks::mock_preference('NotifyBorrowerDeparture', 20);
230 $patron->dateexpiry( dt_from_string->add( days => 10 ) )->store->discard_changes;
231 is( $patron->is_going_to_expire, 1, 'Patron should be considered going to expire if dateexpiry is 10 days ahead and pref is 20');
237 subtest 'renew_account' => sub {
239 my $a_month_ago = dt_from_string->add( months => -1 )->truncate( to => 'day' );
240 my $a_year_later = dt_from_string->add( months => 12 )->truncate( to => 'day' );
241 my $a_year_later_minus_a_month = dt_from_string->add( months => 11 )->truncate( to => 'day' );
242 my $a_month_later = dt_from_string->add( months => 1 )->truncate( to => 'day' );
243 my $a_year_later_plus_a_month = dt_from_string->add( months => 13 )->truncate( to => 'day' );
244 my $patron_category = $builder->build(
245 { source => 'Category',
247 enrolmentperiod => 12,
248 enrolmentperioddate => undef,
252 my $patron = $builder->build(
253 { source => 'Borrower',
255 dateexpiry => $a_month_ago,
256 categorycode => $patron_category->{categorycode},
260 my $patron_2 = $builder->build(
261 { source => 'Borrower',
263 dateexpiry => $a_month_ago,
264 categorycode => $patron_category->{categorycode},
268 my $patron_3 = $builder->build(
269 { source => 'Borrower',
271 dateexpiry => $a_month_later,
272 categorycode => $patron_category->{categorycode},
276 my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
277 my $retrieved_patron_2 = Koha::Patrons->find( $patron_2->{borrowernumber} );
278 my $retrieved_patron_3 = Koha::Patrons->find( $patron_3->{borrowernumber} );
280 t::lib::Mocks::mock_preference( 'BorrowerRenewalPeriodBase', 'dateexpiry' );
281 t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
282 my $expiry_date = $retrieved_patron->renew_account;
283 is( $expiry_date, $a_year_later_minus_a_month, );
284 my $retrieved_expiry_date = Koha::Patrons->find( $patron->{borrowernumber} )->dateexpiry;
285 is( dt_from_string($retrieved_expiry_date), $a_year_later_minus_a_month );
286 my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'RENEW', object => $retrieved_patron->borrowernumber } )->count;
287 is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->renew_account should have logged' );
289 t::lib::Mocks::mock_preference( 'BorrowerRenewalPeriodBase', 'now' );
290 t::lib::Mocks::mock_preference( 'BorrowersLog', 0 );
291 $expiry_date = $retrieved_patron->renew_account;
292 is( $expiry_date, $a_year_later, );
293 $retrieved_expiry_date = Koha::Patrons->find( $patron->{borrowernumber} )->dateexpiry;
294 is( dt_from_string($retrieved_expiry_date), $a_year_later );
295 $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'RENEW', object => $retrieved_patron->borrowernumber } )->count;
296 is( $number_of_logs, 1, 'Without BorrowerLogs, Koha::Patron->renew_account should not have logged' );
298 t::lib::Mocks::mock_preference( 'BorrowerRenewalPeriodBase', 'combination' );
299 $expiry_date = $retrieved_patron_2->renew_account;
300 is( $expiry_date, $a_year_later );
301 $retrieved_expiry_date = Koha::Patrons->find( $patron_2->{borrowernumber} )->dateexpiry;
302 is( dt_from_string($retrieved_expiry_date), $a_year_later );
304 $expiry_date = $retrieved_patron_3->renew_account;
305 is( $expiry_date, $a_year_later_plus_a_month );
306 $retrieved_expiry_date = Koha::Patrons->find( $patron_3->{borrowernumber} )->dateexpiry;
307 is( dt_from_string($retrieved_expiry_date), $a_year_later_plus_a_month );
309 $retrieved_patron->delete;
310 $retrieved_patron_2->delete;
311 $retrieved_patron_3->delete;
314 subtest "move_to_deleted" => sub {
316 my $patron = $builder->build( { source => 'Borrower' } );
317 my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
318 is( ref( $retrieved_patron->move_to_deleted ), 'Koha::Schema::Result::Deletedborrower', 'Koha::Patron->move_to_deleted should return the Deleted patron' )
319 ; # FIXME This should be Koha::Deleted::Patron
320 my $deleted_patron = $schema->resultset('Deletedborrower')
321 ->search( { borrowernumber => $patron->{borrowernumber} }, { result_class => 'DBIx::Class::ResultClass::HashRefInflator' } )
323 is_deeply( $deleted_patron, $patron, 'Koha::Patron->move_to_deleted should have correctly moved the patron to the deleted table' );
324 $retrieved_patron->delete( $patron->{borrowernumber} ); # Cleanup
327 subtest "delete" => sub {
329 t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
330 my $patron = $builder->build( { source => 'Borrower' } );
331 my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
332 my $hold = $builder->build(
333 { source => 'Reserve',
334 value => { borrowernumber => $patron->{borrowernumber} }
337 my $list = $builder->build(
338 { source => 'Virtualshelve',
339 value => { owner => $patron->{borrowernumber} }
343 my $deleted = $retrieved_patron->delete;
344 is( $deleted, 1, 'Koha::Patron->delete should return 1 if the patron has been correctly deleted' );
346 is( Koha::Patrons->find( $patron->{borrowernumber} ), undef, 'Koha::Patron->delete should have deleted the patron' );
348 is( Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's holds| );
350 is( Koha::Virtualshelves->search( { owner => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's lists| );
352 my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'DELETE', object => $retrieved_patron->borrowernumber } )->count;
353 is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->delete should have logged' );
356 subtest 'add_enrolment_fee_if_needed' => sub {
359 my $enrolmentfee_K = 5;
360 my $enrolmentfee_J = 10;
361 my $enrolmentfee_YA = 20;
363 my $dbh = C4::Context->dbh;
364 $dbh->do(q|UPDATE categories set enrolmentfee=? where categorycode=?|, undef, $enrolmentfee_K, 'K');
365 $dbh->do(q|UPDATE categories set enrolmentfee=? where categorycode=?|, undef, $enrolmentfee_J, 'J');
366 $dbh->do(q|UPDATE categories set enrolmentfee=? where categorycode=?|, undef, $enrolmentfee_YA, 'YA');
368 my %borrower_data = (
369 firstname => 'my firstname',
370 surname => 'my surname',
372 branchcode => $library->{branchcode},
375 my $borrowernumber = C4::Members::AddMember(%borrower_data);
376 $borrower_data{borrowernumber} = $borrowernumber;
378 my ($total) = C4::Members::GetMemberAccountRecords($borrowernumber);
379 is( $total, $enrolmentfee_K, "New kid pay $enrolmentfee_K" );
381 t::lib::Mocks::mock_preference( 'FeeOnChangePatronCategory', 0 );
382 $borrower_data{categorycode} = 'J';
383 C4::Members::ModMember(%borrower_data);
384 ($total) = C4::Members::GetMemberAccountRecords($borrowernumber);
385 is( $total, $enrolmentfee_K, "Kid growing and become a juvenile, but shouldn't pay for the upgrade " );
387 $borrower_data{categorycode} = 'K';
388 C4::Members::ModMember(%borrower_data);
389 t::lib::Mocks::mock_preference( 'FeeOnChangePatronCategory', 1 );
391 $borrower_data{categorycode} = 'J';
392 C4::Members::ModMember(%borrower_data);
393 ($total) = C4::Members::GetMemberAccountRecords($borrowernumber);
394 is( $total, $enrolmentfee_K + $enrolmentfee_J, "Kid growing and become a juvenile, he should pay " . ( $enrolmentfee_K + $enrolmentfee_J ) );
396 # Check with calling directly Koha::Patron->get_enrolment_fee_if_needed
397 my $patron = Koha::Patrons->find($borrowernumber);
398 $patron->categorycode('YA')->store;
399 my $fee = $patron->add_enrolment_fee_if_needed;
400 ($total) = C4::Members::GetMemberAccountRecords($borrowernumber);
402 $enrolmentfee_K + $enrolmentfee_J + $enrolmentfee_YA,
403 "Juvenile growing and become an young adult, he should pay " . ( $enrolmentfee_K + $enrolmentfee_J + $enrolmentfee_YA )
409 subtest 'get_overdues' => sub {
412 my $library = $builder->build( { source => 'Branch' } );
413 my ($biblionumber_1) = AddBiblio( MARC::Record->new, '' );
414 my $item_1 = $builder->build(
418 homebranch => $library->{branchcode},
419 holdingbranch => $library->{branchcode},
420 biblionumber => $biblionumber_1
424 my $item_2 = $builder->build(
428 homebranch => $library->{branchcode},
429 holdingbranch => $library->{branchcode},
430 biblionumber => $biblionumber_1
434 my ($biblionumber_2) = AddBiblio( MARC::Record->new, '' );
435 my $item_3 = $builder->build(
439 homebranch => $library->{branchcode},
440 holdingbranch => $library->{branchcode},
441 biblionumber => $biblionumber_2
445 my $patron = $builder->build(
447 source => 'Borrower',
448 value => { branchcode => $library->{branchcode} }
452 # Not sure how this is useful, but AddIssue pass this variable to different other subroutines
453 $patron = GetMember( borrowernumber => $patron->{borrowernumber} );
455 my $module = new Test::MockModule('C4::Context');
456 $module->mock( 'userenv', sub { { branch => $library->{branchcode} } } );
458 AddIssue( $patron, $item_1->{barcode}, DateTime->now->subtract( days => 1 ) );
459 AddIssue( $patron, $item_2->{barcode}, DateTime->now->subtract( days => 5 ) );
460 AddIssue( $patron, $item_3->{barcode} );
462 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
463 my $overdues = $patron->get_overdues;
464 is( $overdues->count, 2, 'Patron should have 2 overdues');
465 is( ref($overdues), 'Koha::Issues', 'Koha::Patron->get_overdues should return Koha::Issues' );
466 is( $overdues->next->itemnumber, $item_1->{itemnumber}, 'The issue should be returned in the same order as they have been done, first is correct' );
467 is( $overdues->next->itemnumber, $item_2->{itemnumber}, 'The issue should be returned in the same order as they have been done, second is correct' );
470 Koha::Issues->search( { borrowernumber => $patron->borrowernumber } )->delete;
474 $retrieved_patron_1->delete;
475 is( Koha::Patrons->search->count, $nb_of_patrons + 1, 'Delete should have deleted the patron' );
477 $schema->storage->txn_rollback;