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 => 11;
32 use Koha::Virtualshelves;
34 use t::lib::TestBuilder;
37 my $schema = Koha::Database->new->schema;
38 $schema->storage->txn_begin;
40 my $builder = t::lib::TestBuilder->new;
41 my $library = $builder->build({source => 'Branch' });
42 my $category = $builder->build({source => 'Category' });
43 my $nb_of_patrons = Koha::Patrons->search->count;
44 my $new_patron_1 = Koha::Patron->new(
45 { cardnumber => 'test_cn_1',
46 branchcode => $library->{branchcode},
47 categorycode => $category->{categorycode},
48 surname => 'surname for patron1',
49 firstname => 'firstname for patron1',
50 userid => 'a_nonexistent_userid_1',
53 my $new_patron_2 = Koha::Patron->new(
54 { cardnumber => 'test_cn_2',
55 branchcode => $library->{branchcode},
56 categorycode => $category->{categorycode},
57 surname => 'surname for patron2',
58 firstname => 'firstname for patron2',
59 userid => 'a_nonexistent_userid_2',
63 C4::Context->_new_userenv('xxx');
64 C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, 'Midway Public Library', '', '', '');
66 is( Koha::Patrons->search->count, $nb_of_patrons + 2, 'The 2 patrons should have been added' );
68 my $retrieved_patron_1 = Koha::Patrons->find( $new_patron_1->borrowernumber );
69 is( $retrieved_patron_1->cardnumber, $new_patron_1->cardnumber, 'Find a patron by borrowernumber should return the correct patron' );
71 subtest 'guarantees' => sub {
73 my $guarantees = $new_patron_1->guarantees;
74 is( ref($guarantees), 'Koha::Patrons', 'Koha::Patron->guarantees should return a Koha::Patrons result set in a scalar context' );
75 is( $guarantees->count, 0, 'new_patron_1 should have 0 guarantee' );
76 my @guarantees = $new_patron_1->guarantees;
77 is( ref(\@guarantees), 'ARRAY', 'Koha::Patron->guarantees should return an array in a list context' );
78 is( scalar(@guarantees), 0, 'new_patron_1 should have 0 guarantee' );
80 my $guarantee_1 = $builder->build({ source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber }});
81 my $guarantee_2 = $builder->build({ source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber }});
83 $guarantees = $new_patron_1->guarantees;
84 is( ref($guarantees), 'Koha::Patrons', 'Koha::Patron->guarantees should return a Koha::Patrons result set in a scalar context' );
85 is( $guarantees->count, 2, 'new_patron_1 should have 2 guarantees' );
86 @guarantees = $new_patron_1->guarantees;
87 is( ref(\@guarantees), 'ARRAY', 'Koha::Patron->guarantees should return an array in a list context' );
88 is( scalar(@guarantees), 2, 'new_patron_1 should have 2 guarantees' );
89 $_->delete for @guarantees;
92 subtest 'siblings' => sub {
94 my $siblings = $new_patron_1->siblings;
95 is( $siblings, undef, 'Koha::Patron->siblings should not crashed if the patron has no guarantor' );
96 my $guarantee_1 = $builder->build( { source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber } } );
97 my $retrieved_guarantee_1 = Koha::Patrons->find($guarantee_1);
98 $siblings = $retrieved_guarantee_1->siblings;
99 is( ref($siblings), 'Koha::Patrons', 'Koha::Patron->siblings should return a Koha::Patrons result set in a scalar context' );
100 my @siblings = $retrieved_guarantee_1->siblings;
101 is( ref( \@siblings ), 'ARRAY', 'Koha::Patron->siblings should return an array in a list context' );
102 is( $siblings->count, 0, 'guarantee_1 should not have siblings yet' );
103 my $guarantee_2 = $builder->build( { source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber } } );
104 my $guarantee_3 = $builder->build( { source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber } } );
105 $siblings = $retrieved_guarantee_1->siblings;
106 is( $siblings->count, 2, 'guarantee_1 should have 2 siblings' );
107 is( $guarantee_2->{borrowernumber}, $siblings->next->borrowernumber, 'guarantee_2 should exist in the guarantees' );
108 is( $guarantee_3->{borrowernumber}, $siblings->next->borrowernumber, 'guarantee_3 should exist in the guarantees' );
109 $_->delete for $retrieved_guarantee_1->siblings;
110 $retrieved_guarantee_1->delete;
113 subtest 'has_overdues' => sub {
116 my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
117 my $item_1 = $builder->build(
120 homebranch => $library->{branchcode},
121 holdingbranch => $library->{branchcode},
125 biblionumber => $biblioitem_1->{biblionumber}
129 my $retrieved_patron = Koha::Patrons->find( $new_patron_1->borrowernumber );
130 is( $retrieved_patron->has_overdues, 0, );
132 my $tomorrow = DateTime->today( time_zone => C4::Context->tz() )->add( days => 1 );
133 my $issue = Koha::Issue->new({ borrowernumber => $new_patron_1->id, itemnumber => $item_1->{itemnumber}, date_due => $tomorrow, branchcode => $library->{branchcode} })->store();
134 is( $retrieved_patron->has_overdues, 0, );
136 my $yesterday = DateTime->today(time_zone => C4::Context->tz())->add( days => -1 );
137 $issue = Koha::Issue->new({ borrowernumber => $new_patron_1->id, itemnumber => $item_1->{itemnumber}, date_due => $yesterday, branchcode => $library->{branchcode} })->store();
138 $retrieved_patron = Koha::Patrons->find( $new_patron_1->borrowernumber );
139 is( $retrieved_patron->has_overdues, 1, );
143 subtest 'update_password' => sub {
146 t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
147 my $original_userid = $new_patron_1->userid;
148 my $original_password = $new_patron_1->password;
149 warning_like { $retrieved_patron_1->update_password( $new_patron_2->userid, 'another_password' ) }
151 'Koha::Patron->update_password should warn if the userid is already used by another patron';
152 is( Koha::Patrons->find( $new_patron_1->borrowernumber )->userid, $original_userid, 'Koha::Patron->update_password should not have updated the userid' );
153 is( Koha::Patrons->find( $new_patron_1->borrowernumber )->password, $original_password, 'Koha::Patron->update_password should not have updated the userid' );
155 $retrieved_patron_1->update_password( 'another_nonexistent_userid_1', 'another_password' );
156 is( Koha::Patrons->find( $new_patron_1->borrowernumber )->userid, 'another_nonexistent_userid_1', 'Koha::Patron->update_password should have updated the userid' );
157 is( Koha::Patrons->find( $new_patron_1->borrowernumber )->password, 'another_password', 'Koha::Patron->update_password should have updated the password' );
159 my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'CHANGE PASS', object => $new_patron_1->borrowernumber } )->count;
160 is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->update_password should have logged' );
162 t::lib::Mocks::mock_preference( 'BorrowersLog', 0 );
163 $retrieved_patron_1->update_password( 'yet_another_nonexistent_userid_1', 'another_password' );
164 $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'CHANGE PASS', object => $new_patron_1->borrowernumber } )->count;
165 is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->update_password should not have logged' );
168 subtest 'renew_account' => sub {
170 my $a_month_ago = dt_from_string->add( months => -1 )->truncate( to => 'day' );
171 my $a_year_later = dt_from_string->add( months => 12 )->truncate( to => 'day' );
172 my $a_year_later_minus_a_month = dt_from_string->add( months => 11 )->truncate( to => 'day' );
173 my $patron_category = $builder->build(
174 { source => 'Category',
176 enrolmentperiod => 12,
177 enrolmentperioddate => undef,
181 my $patron = $builder->build(
182 { source => 'Borrower',
184 dateexpiry => $a_month_ago,
185 categorycode => $patron_category->{categorycode},
189 my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
191 t::lib::Mocks::mock_preference( 'BorrowerRenewalPeriodBase', 'dateexpiry' );
192 t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
193 my $expiry_date = $retrieved_patron->renew_account;
194 is( $expiry_date, $a_year_later_minus_a_month, );
195 my $retrieved_expiry_date = Koha::Patrons->find( $patron->{borrowernumber} )->dateexpiry;
196 is( dt_from_string($retrieved_expiry_date), $a_year_later_minus_a_month );
197 my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'RENEW', object => $retrieved_patron->borrowernumber } )->count;
198 is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->renew_account should have logged' );
200 t::lib::Mocks::mock_preference( 'BorrowerRenewalPeriodBase', 'now' );
201 t::lib::Mocks::mock_preference( 'BorrowersLog', 0 );
202 $expiry_date = $retrieved_patron->renew_account;
203 is( $expiry_date, $a_year_later, );
204 $retrieved_expiry_date = Koha::Patrons->find( $patron->{borrowernumber} )->dateexpiry;
205 is( dt_from_string($retrieved_expiry_date), $a_year_later );
206 $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'RENEW', object => $retrieved_patron->borrowernumber } )->count;
207 is( $number_of_logs, 1, 'Without BorrowerLogs, Koha::Patron->renew_account should not have logged' );
209 $retrieved_patron->delete;
212 subtest "move_to_deleted" => sub {
214 my $patron = $builder->build( { source => 'Borrower' } );
215 my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
216 is( ref( $retrieved_patron->move_to_deleted ), 'Koha::Schema::Result::Deletedborrower', 'Koha::Patron->move_to_deleted should return the Deleted patron' )
217 ; # FIXME This should be Koha::Deleted::Patron
218 my $deleted_patron = $schema->resultset('Deletedborrower')
219 ->search( { borrowernumber => $patron->{borrowernumber} }, { result_class => 'DBIx::Class::ResultClass::HashRefInflator' } )
221 is_deeply( $deleted_patron, $patron, 'Koha::Patron->move_to_deleted should have correctly moved the patron to the deleted table' );
222 $retrieved_patron->delete( $patron->{borrowernumber} ); # Cleanup
225 subtest "delete" => sub {
227 t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
228 my $patron = $builder->build( { source => 'Borrower' } );
229 my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
230 my $hold = $builder->build(
231 { source => 'Reserve',
232 value => { borrowernumber => $patron->{borrowernumber} }
235 my $list = $builder->build(
236 { source => 'Virtualshelve',
237 value => { owner => $patron->{borrowernumber} }
241 my $deleted = $retrieved_patron->delete;
242 is( $deleted, 1, 'Koha::Patron->delete should return 1 if the patron has been correctly deleted' );
244 is( Koha::Patrons->find( $patron->{borrowernumber} ), undef, 'Koha::Patron->delete should have deleted the patron' );
246 is( Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's holds| );
248 is( Koha::Virtualshelves->search( { owner => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's lists| );
250 my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'DELETE', object => $retrieved_patron->borrowernumber } )->count;
251 is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->delete should have logged' );
254 subtest 'add_enrolment_fee_if_needed' => sub {
257 my $enrolmentfee_K = 5;
258 my $enrolmentfee_J = 10;
259 my $enrolmentfee_YA = 20;
261 $builder->build( { source => 'Category', value => { categorycode => 'K', enrolmentfee => $enrolmentfee_K } } );
262 $builder->build( { source => 'Category', value => { categorycode => 'J', enrolmentfee => $enrolmentfee_J } } );
263 $builder->build( { source => 'Category', value => { categorycode => 'YA', enrolmentfee => $enrolmentfee_YA } } );
265 my %borrower_data = (
266 firstname => 'my firstname',
267 surname => 'my surname',
269 branchcode => $library->{branchcode},
272 my $borrowernumber = C4::Members::AddMember(%borrower_data);
273 $borrower_data{borrowernumber} = $borrowernumber;
275 my ($total) = C4::Members::GetMemberAccountRecords($borrowernumber);
276 is( $total, $enrolmentfee_K, "New kid pay $enrolmentfee_K" );
278 t::lib::Mocks::mock_preference( 'FeeOnChangePatronCategory', 0 );
279 $borrower_data{categorycode} = 'J';
280 C4::Members::ModMember(%borrower_data);
281 ($total) = C4::Members::GetMemberAccountRecords($borrowernumber);
282 is( $total, $enrolmentfee_K, "Kid growing and become a juvenile, but shouldn't pay for the upgrade " );
284 $borrower_data{categorycode} = 'K';
285 C4::Members::ModMember(%borrower_data);
286 t::lib::Mocks::mock_preference( 'FeeOnChangePatronCategory', 1 );
288 $borrower_data{categorycode} = 'J';
289 C4::Members::ModMember(%borrower_data);
290 ($total) = C4::Members::GetMemberAccountRecords($borrowernumber);
291 is( $total, $enrolmentfee_K + $enrolmentfee_J, "Kid growing and become a juvenile, he should pay " . ( $enrolmentfee_K + $enrolmentfee_J ) );
293 # Check with calling directly Koha::Patron->get_enrolment_fee_if_needed
294 my $patron = Koha::Patrons->find($borrowernumber);
295 $patron->categorycode('YA')->store;
296 my $fee = $patron->add_enrolment_fee_if_needed;
297 ($total) = C4::Members::GetMemberAccountRecords($borrowernumber);
299 $enrolmentfee_K + $enrolmentfee_J + $enrolmentfee_YA,
300 "Juvenile growing and become an young adult, he should pay " . ( $enrolmentfee_K + $enrolmentfee_J + $enrolmentfee_YA )
306 $retrieved_patron_1->delete;
307 is( Koha::Patrons->search->count, $nb_of_patrons + 1, 'Delete should have deleted the patron' );
309 $schema->storage->txn_rollback;