Bug 17583: Mock the pref before tests
[koha.git] / t / db_dependent / Koha / Patrons.t
blob2c71d213ad12b4c775c41abb4fed09c9636f9d80
1 #!/usr/bin/perl
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>.
20 use Modern::Perl;
22 use Test::More tests => 13;
23 use Test::Warn;
25 use C4::Members;
27 use Koha::Holds;
28 use Koha::Patron;
29 use Koha::Patrons;
30 use Koha::Database;
31 use Koha::DateUtils;
32 use Koha::Virtualshelves;
34 use t::lib::TestBuilder;
35 use t::lib::Mocks;
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',
52 )->store;
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',
61 )->store;
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 {
72 plan tests => 8;
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 'category' => sub {
93 plan tests => 2;
94 my $patron_category = $new_patron_1->category;
95 is( ref( $patron_category), 'Koha::Patron::Category', );
96 is( $patron_category->categorycode, $category->{categorycode}, );
99 subtest 'siblings' => sub {
100 plan tests => 7;
101 my $siblings = $new_patron_1->siblings;
102 is( $siblings, undef, 'Koha::Patron->siblings should not crashed if the patron has no guarantor' );
103 my $guarantee_1 = $builder->build( { source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber } } );
104 my $retrieved_guarantee_1 = Koha::Patrons->find($guarantee_1);
105 $siblings = $retrieved_guarantee_1->siblings;
106 is( ref($siblings), 'Koha::Patrons', 'Koha::Patron->siblings should return a Koha::Patrons result set in a scalar context' );
107 my @siblings = $retrieved_guarantee_1->siblings;
108 is( ref( \@siblings ), 'ARRAY', 'Koha::Patron->siblings should return an array in a list context' );
109 is( $siblings->count, 0, 'guarantee_1 should not have siblings yet' );
110 my $guarantee_2 = $builder->build( { source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber } } );
111 my $guarantee_3 = $builder->build( { source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber } } );
112 $siblings = $retrieved_guarantee_1->siblings;
113 is( $siblings->count, 2, 'guarantee_1 should have 2 siblings' );
114 is( $guarantee_2->{borrowernumber}, $siblings->next->borrowernumber, 'guarantee_2 should exist in the guarantees' );
115 is( $guarantee_3->{borrowernumber}, $siblings->next->borrowernumber, 'guarantee_3 should exist in the guarantees' );
116 $_->delete for $retrieved_guarantee_1->siblings;
117 $retrieved_guarantee_1->delete;
120 subtest 'has_overdues' => sub {
121 plan tests => 3;
123 my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
124 my $item_1 = $builder->build(
125 { source => 'Item',
126 value => {
127 homebranch => $library->{branchcode},
128 holdingbranch => $library->{branchcode},
129 notforloan => 0,
130 itemlost => 0,
131 withdrawn => 0,
132 biblionumber => $biblioitem_1->{biblionumber}
136 my $retrieved_patron = Koha::Patrons->find( $new_patron_1->borrowernumber );
137 is( $retrieved_patron->has_overdues, 0, );
139 my $tomorrow = DateTime->today( time_zone => C4::Context->tz() )->add( days => 1 );
140 my $issue = Koha::Issue->new({ borrowernumber => $new_patron_1->id, itemnumber => $item_1->{itemnumber}, date_due => $tomorrow, branchcode => $library->{branchcode} })->store();
141 is( $retrieved_patron->has_overdues, 0, );
142 $issue->delete();
143 my $yesterday = DateTime->today(time_zone => C4::Context->tz())->add( days => -1 );
144 $issue = Koha::Issue->new({ borrowernumber => $new_patron_1->id, itemnumber => $item_1->{itemnumber}, date_due => $yesterday, branchcode => $library->{branchcode} })->store();
145 $retrieved_patron = Koha::Patrons->find( $new_patron_1->borrowernumber );
146 is( $retrieved_patron->has_overdues, 1, );
147 $issue->delete();
150 subtest 'update_password' => sub {
151 plan tests => 7;
153 t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
154 my $original_userid = $new_patron_1->userid;
155 my $original_password = $new_patron_1->password;
156 warning_like { $retrieved_patron_1->update_password( $new_patron_2->userid, 'another_password' ) }
157 qr{Duplicate entry},
158 'Koha::Patron->update_password should warn if the userid is already used by another patron';
159 is( Koha::Patrons->find( $new_patron_1->borrowernumber )->userid, $original_userid, 'Koha::Patron->update_password should not have updated the userid' );
160 is( Koha::Patrons->find( $new_patron_1->borrowernumber )->password, $original_password, 'Koha::Patron->update_password should not have updated the userid' );
162 $retrieved_patron_1->update_password( 'another_nonexistent_userid_1', 'another_password' );
163 is( Koha::Patrons->find( $new_patron_1->borrowernumber )->userid, 'another_nonexistent_userid_1', 'Koha::Patron->update_password should have updated the userid' );
164 is( Koha::Patrons->find( $new_patron_1->borrowernumber )->password, 'another_password', 'Koha::Patron->update_password should have updated the password' );
166 my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'CHANGE PASS', object => $new_patron_1->borrowernumber } )->count;
167 is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->update_password should have logged' );
169 t::lib::Mocks::mock_preference( 'BorrowersLog', 0 );
170 $retrieved_patron_1->update_password( 'yet_another_nonexistent_userid_1', 'another_password' );
171 $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'CHANGE PASS', object => $new_patron_1->borrowernumber } )->count;
172 is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->update_password should not have logged' );
175 subtest 'is_expired' => sub {
176 plan tests => 5;
177 my $patron = $builder->build({ source => 'Borrower' });
178 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
179 $patron->dateexpiry( undef )->store->discard_changes;
180 is( $patron->is_expired, 0, 'Patron should not be considered expired if dateexpiry is not set');
181 $patron->dateexpiry( '0000-00-00' )->store->discard_changes;
182 is( $patron->is_expired, 0, 'Patron should not be considered expired if dateexpiry is not 0000-00-00');
183 $patron->dateexpiry( dt_from_string )->store->discard_changes;
184 is( $patron->is_expired, 0, 'Patron should not be considered expired if dateexpiry is today');
185 $patron->dateexpiry( dt_from_string->add( days => 1 ) )->store->discard_changes;
186 is( $patron->is_expired, 0, 'Patron should not be considered expired if dateexpiry is tomorrow');
187 $patron->dateexpiry( dt_from_string->add( days => -1 ) )->store->discard_changes;
188 is( $patron->is_expired, 1, 'Patron should be considered expired if dateexpiry is yesterday');
190 $patron->delete;
193 subtest 'is_going_to_expired' => sub {
194 plan tests => 9;
195 my $patron = $builder->build({ source => 'Borrower' });
196 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
197 $patron->dateexpiry( undef )->store->discard_changes;
198 is( $patron->is_going_to_expired, 0, 'Patron should not be considered going to expire if dateexpiry is not set');
199 $patron->dateexpiry( '0000-00-00' )->store->discard_changes;
200 is( $patron->is_going_to_expired, 0, 'Patron should not be considered going to expire if dateexpiry is not 0000-00-00');
202 t::lib::Mocks::mock_preference('NotifyBorrowerDeparture', 0);
203 $patron->dateexpiry( dt_from_string )->store->discard_changes;
204 is( $patron->is_going_to_expired, 0, 'Patron should not be considered going to expire if dateexpiry is today');
206 $patron->dateexpiry( dt_from_string )->store->discard_changes;
207 is( $patron->is_going_to_expired, 0, 'Patron should not be considered going to expire if dateexpiry is today and pref is 0');
209 t::lib::Mocks::mock_preference('NotifyBorrowerDeparture', 10);
210 $patron->dateexpiry( dt_from_string->add( days => 11 ) )->store->discard_changes;
211 is( $patron->is_going_to_expired, 0, 'Patron should not be considered going to expire if dateexpiry is 11 days ahead and pref is 10');
213 t::lib::Mocks::mock_preference('NotifyBorrowerDeparture', 0);
214 $patron->dateexpiry( dt_from_string->add( days => 10 ) )->store->discard_changes;
215 is( $patron->is_going_to_expired, 0, 'Patron should not be considered going to expire if dateexpiry is 10 days ahead and pref is 0');
217 t::lib::Mocks::mock_preference('NotifyBorrowerDeparture', 10);
218 $patron->dateexpiry( dt_from_string->add( days => 10 ) )->store->discard_changes;
219 is( $patron->is_going_to_expired, 0, 'Patron should not be considered going to expire if dateexpiry is 10 days ahead and pref is 10');
220 $patron->delete;
222 t::lib::Mocks::mock_preference('NotifyBorrowerDeparture', 10);
223 $patron->dateexpiry( dt_from_string->add( days => 20 ) )->store->discard_changes;
224 is( $patron->is_going_to_expired, 0, 'Patron should not be considered going to expire if dateexpiry is 20 days ahead and pref is 10');
226 t::lib::Mocks::mock_preference('NotifyBorrowerDeparture', 20);
227 $patron->dateexpiry( dt_from_string->add( days => 10 ) )->store->discard_changes;
228 is( $patron->is_going_to_expired, 1, 'Patron should be considered going to expire if dateexpiry is 10 days ahead and pref is 20');
230 $patron->delete;
234 subtest 'renew_account' => sub {
235 plan tests => 10;
236 my $a_month_ago = dt_from_string->add( months => -1 )->truncate( to => 'day' );
237 my $a_year_later = dt_from_string->add( months => 12 )->truncate( to => 'day' );
238 my $a_year_later_minus_a_month = dt_from_string->add( months => 11 )->truncate( to => 'day' );
239 my $a_month_later = dt_from_string->add( months => 1 )->truncate( to => 'day' );
240 my $a_year_later_plus_a_month = dt_from_string->add( months => 13 )->truncate( to => 'day' );
241 my $patron_category = $builder->build(
242 { source => 'Category',
243 value => {
244 enrolmentperiod => 12,
245 enrolmentperioddate => undef,
249 my $patron = $builder->build(
250 { source => 'Borrower',
251 value => {
252 dateexpiry => $a_month_ago,
253 categorycode => $patron_category->{categorycode},
257 my $patron_2 = $builder->build(
258 { source => 'Borrower',
259 value => {
260 dateexpiry => $a_month_ago,
261 categorycode => $patron_category->{categorycode},
265 my $patron_3 = $builder->build(
266 { source => 'Borrower',
267 value => {
268 dateexpiry => $a_month_later,
269 categorycode => $patron_category->{categorycode},
273 my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
274 my $retrieved_patron_2 = Koha::Patrons->find( $patron_2->{borrowernumber} );
275 my $retrieved_patron_3 = Koha::Patrons->find( $patron_3->{borrowernumber} );
277 t::lib::Mocks::mock_preference( 'BorrowerRenewalPeriodBase', 'dateexpiry' );
278 t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
279 my $expiry_date = $retrieved_patron->renew_account;
280 is( $expiry_date, $a_year_later_minus_a_month, );
281 my $retrieved_expiry_date = Koha::Patrons->find( $patron->{borrowernumber} )->dateexpiry;
282 is( dt_from_string($retrieved_expiry_date), $a_year_later_minus_a_month );
283 my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'RENEW', object => $retrieved_patron->borrowernumber } )->count;
284 is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->renew_account should have logged' );
286 t::lib::Mocks::mock_preference( 'BorrowerRenewalPeriodBase', 'now' );
287 t::lib::Mocks::mock_preference( 'BorrowersLog', 0 );
288 $expiry_date = $retrieved_patron->renew_account;
289 is( $expiry_date, $a_year_later, );
290 $retrieved_expiry_date = Koha::Patrons->find( $patron->{borrowernumber} )->dateexpiry;
291 is( dt_from_string($retrieved_expiry_date), $a_year_later );
292 $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'RENEW', object => $retrieved_patron->borrowernumber } )->count;
293 is( $number_of_logs, 1, 'Without BorrowerLogs, Koha::Patron->renew_account should not have logged' );
295 t::lib::Mocks::mock_preference( 'BorrowerRenewalPeriodBase', 'combination' );
296 $expiry_date = $retrieved_patron_2->renew_account;
297 is( $expiry_date, $a_year_later );
298 $retrieved_expiry_date = Koha::Patrons->find( $patron_2->{borrowernumber} )->dateexpiry;
299 is( dt_from_string($retrieved_expiry_date), $a_year_later );
301 $expiry_date = $retrieved_patron_3->renew_account;
302 is( $expiry_date, $a_year_later_plus_a_month );
303 $retrieved_expiry_date = Koha::Patrons->find( $patron_3->{borrowernumber} )->dateexpiry;
304 is( dt_from_string($retrieved_expiry_date), $a_year_later_plus_a_month );
306 $retrieved_patron->delete;
307 $retrieved_patron_2->delete;
308 $retrieved_patron_3->delete;
311 subtest "move_to_deleted" => sub {
312 plan tests => 2;
313 my $patron = $builder->build( { source => 'Borrower' } );
314 my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
315 is( ref( $retrieved_patron->move_to_deleted ), 'Koha::Schema::Result::Deletedborrower', 'Koha::Patron->move_to_deleted should return the Deleted patron' )
316 ; # FIXME This should be Koha::Deleted::Patron
317 my $deleted_patron = $schema->resultset('Deletedborrower')
318 ->search( { borrowernumber => $patron->{borrowernumber} }, { result_class => 'DBIx::Class::ResultClass::HashRefInflator' } )
319 ->next;
320 is_deeply( $deleted_patron, $patron, 'Koha::Patron->move_to_deleted should have correctly moved the patron to the deleted table' );
321 $retrieved_patron->delete( $patron->{borrowernumber} ); # Cleanup
324 subtest "delete" => sub {
325 plan tests => 5;
326 t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
327 my $patron = $builder->build( { source => 'Borrower' } );
328 my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
329 my $hold = $builder->build(
330 { source => 'Reserve',
331 value => { borrowernumber => $patron->{borrowernumber} }
334 my $list = $builder->build(
335 { source => 'Virtualshelve',
336 value => { owner => $patron->{borrowernumber} }
340 my $deleted = $retrieved_patron->delete;
341 is( $deleted, 1, 'Koha::Patron->delete should return 1 if the patron has been correctly deleted' );
343 is( Koha::Patrons->find( $patron->{borrowernumber} ), undef, 'Koha::Patron->delete should have deleted the patron' );
345 is( Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's holds| );
347 is( Koha::Virtualshelves->search( { owner => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's lists| );
349 my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'DELETE', object => $retrieved_patron->borrowernumber } )->count;
350 is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->delete should have logged' );
353 subtest 'add_enrolment_fee_if_needed' => sub {
354 plan tests => 4;
356 my $enrolmentfee_K = 5;
357 my $enrolmentfee_J = 10;
358 my $enrolmentfee_YA = 20;
360 my $dbh = C4::Context->dbh;
361 $dbh->do(q|UPDATE categories set enrolmentfee=? where categorycode=?|, undef, $enrolmentfee_K, 'K');
362 $dbh->do(q|UPDATE categories set enrolmentfee=? where categorycode=?|, undef, $enrolmentfee_J, 'J');
363 $dbh->do(q|UPDATE categories set enrolmentfee=? where categorycode=?|, undef, $enrolmentfee_YA, 'YA');
365 my %borrower_data = (
366 firstname => 'my firstname',
367 surname => 'my surname',
368 categorycode => 'K',
369 branchcode => $library->{branchcode},
372 my $borrowernumber = C4::Members::AddMember(%borrower_data);
373 $borrower_data{borrowernumber} = $borrowernumber;
375 my ($total) = C4::Members::GetMemberAccountRecords($borrowernumber);
376 is( $total, $enrolmentfee_K, "New kid pay $enrolmentfee_K" );
378 t::lib::Mocks::mock_preference( 'FeeOnChangePatronCategory', 0 );
379 $borrower_data{categorycode} = 'J';
380 C4::Members::ModMember(%borrower_data);
381 ($total) = C4::Members::GetMemberAccountRecords($borrowernumber);
382 is( $total, $enrolmentfee_K, "Kid growing and become a juvenile, but shouldn't pay for the upgrade " );
384 $borrower_data{categorycode} = 'K';
385 C4::Members::ModMember(%borrower_data);
386 t::lib::Mocks::mock_preference( 'FeeOnChangePatronCategory', 1 );
388 $borrower_data{categorycode} = 'J';
389 C4::Members::ModMember(%borrower_data);
390 ($total) = C4::Members::GetMemberAccountRecords($borrowernumber);
391 is( $total, $enrolmentfee_K + $enrolmentfee_J, "Kid growing and become a juvenile, he should pay " . ( $enrolmentfee_K + $enrolmentfee_J ) );
393 # Check with calling directly Koha::Patron->get_enrolment_fee_if_needed
394 my $patron = Koha::Patrons->find($borrowernumber);
395 $patron->categorycode('YA')->store;
396 my $fee = $patron->add_enrolment_fee_if_needed;
397 ($total) = C4::Members::GetMemberAccountRecords($borrowernumber);
398 is( $total,
399 $enrolmentfee_K + $enrolmentfee_J + $enrolmentfee_YA,
400 "Juvenile growing and become an young adult, he should pay " . ( $enrolmentfee_K + $enrolmentfee_J + $enrolmentfee_YA )
403 $patron->delete;
406 $retrieved_patron_1->delete;
407 is( Koha::Patrons->search->count, $nb_of_patrons + 1, 'Delete should have deleted the patron' );
409 $schema->storage->txn_rollback;