3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Test::More tests => 53;
24 use Data::Dumper qw/Dumper/;
28 use Koha::List::Patron qw( AddPatronList AddPatronsToList );
30 use Koha::Patron::Relationship;
33 use t::lib::TestBuilder;
36 use_ok('C4::Members', qw( checkcardnumber GetBorrowersToExpunge DeleteUnverifiedOpacRegistrations DeleteExpiredOpacRegistrations ));
39 my $schema = Koha::Database->schema;
40 $schema->storage->txn_begin;
41 my $builder = t::lib::TestBuilder->new;
42 my $dbh = C4::Context->dbh;
44 my $library1 = $builder->build({
47 my $library2 = $builder->build({
50 my $patron_category = $builder->build({ source => 'Category' });
51 my $CARDNUMBER = 'TESTCARD01';
52 my $FIRSTNAME = 'Marie';
53 my $SURNAME = 'Mcknight';
54 my $BRANCHCODE = $library1->{branchcode};
56 my $CHANGED_FIRSTNAME = "Marry Ann";
57 my $EMAIL = "Marie\@email.com";
58 my $EMAILPRO = "Marie\@work.com";
59 my $PHONE = "555-12123";
61 # XXX should be randomised and checked against the database
62 my $IMPOSSIBLE_CARDNUMBER = "XYZZZ999";
64 t::lib::Mocks::mock_userenv();
66 # Make a borrower for testing
68 cardnumber => $CARDNUMBER,
69 firstname => $FIRSTNAME . q{ },
71 categorycode => $patron_category->{categorycode},
72 branchcode => $BRANCHCODE,
74 dateexpiry => '9999-12-31',
78 my $addmem=Koha::Patron->new(\%data)->store->borrowernumber;
79 ok($addmem, "Koha::Patron->store()");
81 my $patron = Koha::Patrons->find( { cardnumber => $CARDNUMBER } )
82 or BAIL_OUT("Cannot read member with card $CARDNUMBER");
83 my $member = $patron->unblessed;
85 ok ( $member->{firstname} eq $FIRSTNAME &&
86 $member->{surname} eq $SURNAME &&
87 $member->{categorycode} eq $patron_category->{categorycode} &&
88 $member->{branchcode} eq $BRANCHCODE
90 or diag("Mismatching member details: ".Dumper(\%data, $member));
92 is($member->{dateofbirth}, undef, "Empty dates handled correctly");
94 $member->{firstname} = $CHANGED_FIRSTNAME . q{ };
95 $member->{email} = $EMAIL;
96 $member->{phone} = $PHONE;
97 $member->{emailpro} = $EMAILPRO;
98 $patron->set($member)->store;
99 my $changedmember = Koha::Patrons->find( { cardnumber => $CARDNUMBER } )->unblessed;
100 ok ( $changedmember->{firstname} eq $CHANGED_FIRSTNAME &&
101 $changedmember->{email} eq $EMAIL &&
102 $changedmember->{phone} eq $PHONE &&
103 $changedmember->{emailpro} eq $EMAILPRO
105 or diag("Mismatching member details: ".Dumper($member, $changedmember));
107 t::lib::Mocks::mock_preference( 'CardnumberLength', '' );
108 C4::Context->clear_syspref_cache();
110 my $checkcardnum=C4::Members::checkcardnumber($CARDNUMBER, "");
111 is ($checkcardnum, "1", "Card No. in use");
113 $checkcardnum=C4::Members::checkcardnumber($IMPOSSIBLE_CARDNUMBER, "");
114 is ($checkcardnum, "0", "Card No. not used");
116 t::lib::Mocks::mock_preference( 'CardnumberLength', '4' );
117 C4::Context->clear_syspref_cache();
119 $checkcardnum=C4::Members::checkcardnumber($IMPOSSIBLE_CARDNUMBER, "");
120 is ($checkcardnum, "2", "Card number is too long");
125 cardnumber => "123456789",
126 firstname => "Tomasito",
128 categorycode => $patron_category->{categorycode},
129 branchcode => $library2->{branchcode},
135 my $borrowernumber = Koha::Patron->new( \%data )->store->borrowernumber;
136 $patron = Koha::Patrons->find( $borrowernumber );
137 my $borrower = $patron->unblessed;
138 is( $borrower->{dateofbirth}, undef, 'Koha::Patron->store should undef dateofbirth if empty string is given');
139 is( $borrower->{debarred}, undef, 'Koha::Patron->store should undef debarred if empty string is given');
140 isnt( $borrower->{dateexpiry}, '0000-00-00', 'Koha::Patron->store should not set dateexpiry to 0000-00-00 if empty string is given');
141 isnt( $borrower->{dateenrolled}, '0000-00-00', 'Koha::Patron->store should not set dateenrolled to 0000-00-00 if empty string is given');
143 $patron->set({ dateofbirth => '', debarred => '', dateexpiry => '', dateenrolled => '' })->store;
144 $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
145 is( $borrower->{dateofbirth}, undef, 'Koha::Patron->store should undef dateofbirth if empty string is given');
146 is( $borrower->{debarred}, undef, 'Koha::Patron->store should undef debarred if empty string is given');
147 isnt( $borrower->{dateexpiry}, '0000-00-00', 'Koha::Patron->store should not set dateexpiry to 0000-00-00 if empty string is given');
148 isnt( $borrower->{dateenrolled}, '0000-00-00', 'Koha::Patron->store should not set dateenrolled to 0000-00-00 if empty string is given');
150 $patron->set({ dateofbirth => '1970-01-01', debarred => '2042-01-01', dateexpiry => '9999-12-31', dateenrolled => '2015-09-06' })->store;
151 $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
152 is( $borrower->{dateofbirth}, '1970-01-01', 'Koha::Patron->store should correctly set dateofbirth if a valid date is given');
153 is( $borrower->{debarred}, '2042-01-01', 'Koha::Patron->store should correctly set debarred if a valid date is given');
154 is( $borrower->{dateexpiry}, '9999-12-31', 'Koha::Patron->store should correctly set dateexpiry if a valid date is given');
155 is( $borrower->{dateenrolled}, '2015-09-06', 'Koha::Patron->store should correctly set dateenrolled if a valid date is given');
157 #Regression tests for bug 10612
158 my $library3 = $builder->build({
162 source => 'Category',
164 categorycode => 'STAFFER',
165 description => 'Staff dont batch del',
166 category_type => 'S',
171 source => 'Category',
173 categorycode => 'CIVILIAN',
174 description => 'Civilian batch del',
175 category_type => 'A',
180 source => 'Category',
182 categorycode => 'KIDclamp',
183 description => 'Kid to be guaranteed',
184 category_type => 'C',
188 my $borrower1 = $builder->build({
189 source => 'Borrower',
191 categorycode=>'STAFFER',
192 branchcode => $library3->{branchcode},
193 dateexpiry => '2015-01-01',
197 my $bor1inlist = $borrower1->{borrowernumber};
198 my $borrower2 = $builder->build({
199 source => 'Borrower',
201 categorycode=>'STAFFER',
202 branchcode => $library3->{branchcode},
203 dateexpiry => '2015-01-01',
208 my $guarantee = $builder->build({
209 source => 'Borrower',
211 categorycode=>'KIDclamp',
212 branchcode => $library3->{branchcode},
213 dateexpiry => '2015-01-01',
218 my $bor2inlist = $borrower2->{borrowernumber};
221 source => 'OldIssue',
223 borrowernumber => $bor2inlist,
224 timestamp => '2016-01-01',
228 # The following calls to GetBorrowersToExpunge are assuming that the pref
229 # IndependentBranches is off.
230 t::lib::Mocks::mock_preference('IndependentBranches', 0);
232 my $anonymous_patron = Koha::Patron->new({ categorycode => 'CIVILIAN', branchcode => $library2->{branchcode} })->store->borrowernumber;
233 t::lib::Mocks::mock_preference('AnonymousPatron', $anonymous_patron);
235 my $owner = Koha::Patron->new({ categorycode => 'STAFFER', branchcode => $library2->{branchcode} })->store->borrowernumber;
236 my $list1 = AddPatronList( { name => 'Test List 1', owner => $owner } );
238 AddPatronsToList( { list => $list1, borrowernumbers => [$anonymous_patron] } );
239 my $patstodel = GetBorrowersToExpunge( { patron_list_id => $list1->patron_list_id() } );
240 is( scalar(@$patstodel), 0, 'Anonymous Patron not deleted from list' );
242 my @listpatrons = ($bor1inlist, $bor2inlist);
243 AddPatronsToList( { list => $list1, borrowernumbers => \@listpatrons });
244 $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id() } );
245 is( scalar(@$patstodel),0,'No staff deleted from list of all staff');
246 Koha::Patrons->find($bor2inlist)->set({ categorycode => 'CIVILIAN' })->store;
247 $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id()} );
248 ok( scalar(@$patstodel)== 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Staff patron not deleted from list');
249 $patstodel = GetBorrowersToExpunge( {branchcode => $library3->{branchcode},patron_list_id => $list1->patron_list_id() } );
250 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Staff patron not deleted by branchcode and list');
251 $patstodel = GetBorrowersToExpunge( {expired_before => '2015-01-02', patron_list_id => $list1->patron_list_id() } );
252 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Staff patron not deleted by expirationdate and list');
253 $patstodel = GetBorrowersToExpunge( {not_borrowed_since => '2016-01-02', patron_list_id => $list1->patron_list_id() } );
254 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Staff patron not deleted by last issue date');
256 Koha::Patrons->find($bor1inlist)->set({ categorycode => 'CIVILIAN' })->store;
258 t::lib::Mocks::mock_preference( 'borrowerRelationship', 'test' );
260 my $relationship = Koha::Patron::Relationship->new( { guarantor_id => $bor1inlist, guarantee_id => $guarantee->{borrowernumber}, relationship => 'test' } )->store();
262 $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id()} );
263 ok( scalar(@$patstodel)== 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Guarantor patron not deleted from list');
264 $patstodel = GetBorrowersToExpunge( {branchcode => $library3->{branchcode},patron_list_id => $list1->patron_list_id() } );
265 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Guarantor patron not deleted by branchcode and list');
266 $patstodel = GetBorrowersToExpunge( {expired_before => '2015-01-02', patron_list_id => $list1->patron_list_id() } );
267 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Guarantor patron not deleted by expirationdate and list');
268 $patstodel = GetBorrowersToExpunge( {not_borrowed_since => '2016-01-02', patron_list_id => $list1->patron_list_id() } );
269 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Guarantor patron not deleted by last issue date');
270 $relationship->delete();
275 borrowernumber => $bor2inlist,
278 $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id()} );
279 is( scalar(@$patstodel),1,'Borrower with issue not deleted from list');
280 $patstodel = GetBorrowersToExpunge( {branchcode => $library3->{branchcode},patron_list_id => $list1->patron_list_id() } );
281 is( scalar(@$patstodel),1,'Borrower with issue not deleted by branchcode and list');
282 $patstodel = GetBorrowersToExpunge( {category_code => 'CIVILIAN',patron_list_id => $list1->patron_list_id() } );
283 is( scalar(@$patstodel),1,'Borrower with issue not deleted by category_code and list');
284 $patstodel = GetBorrowersToExpunge( {category_code => [], patron_list_id => $list1->patron_list_id() } );
285 is( scalar(@$patstodel),1,'category_code can contain an empty arrayref');
286 $patstodel = GetBorrowersToExpunge( {category_code => ['CIVILIAN','STAFFER'],patron_list_id => $list1->patron_list_id() } );
287 is( scalar(@$patstodel),1,'Borrower with issue not deleted by multiple category_code and list');
288 $patstodel = GetBorrowersToExpunge( {expired_before => '2015-01-02',patron_list_id => $list1->patron_list_id() } );
289 is( scalar(@$patstodel),1,'Borrower with issue not deleted by expiration_date and list');
290 $builder->schema->resultset( 'Issue' )->delete_all;
291 $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id()} );
292 ok( scalar(@$patstodel)== 2,'Borrowers without issue deleted from list');
293 $patstodel = GetBorrowersToExpunge( {category_code => 'CIVILIAN',patron_list_id => $list1->patron_list_id() } );
294 is( scalar(@$patstodel),2,'Borrowers without issues deleted by category_code and list');
295 $patstodel = GetBorrowersToExpunge( {category_code => ['CIVILIAN','STAFFER'],patron_list_id => $list1->patron_list_id() } );
296 is( scalar(@$patstodel),2,'Borrowers without issues deleted by multiple category_code and list');
297 $patstodel = GetBorrowersToExpunge( {expired_before => '2015-01-02',patron_list_id => $list1->patron_list_id() } );
298 is( scalar(@$patstodel),2,'Borrowers without issues deleted by expiration_date and list');
299 $patstodel = GetBorrowersToExpunge( {not_borrowed_since => '2016-01-02', patron_list_id => $list1->patron_list_id() } );
300 is( scalar(@$patstodel),2,'Borrowers without issues deleted by last issue date');
302 # Test GetBorrowersToExpunge and TrackLastPatronActivity
303 $dbh->do(q|UPDATE borrowers SET lastseen=NULL|);
305 source => 'Borrower',
307 lastseen => '2016-01-01 01:01:01',
308 categorycode => 'CIVILIAN',
313 source => 'Borrower',
315 lastseen => '2016-02-02 02:02:02',
316 categorycode => 'CIVILIAN',
321 source => 'Borrower',
323 lastseen => '2016-03-03 03:03:03',
324 categorycode => 'CIVILIAN',
328 $patstodel = GetBorrowersToExpunge( { last_seen => '1999-12-12' });
329 is( scalar @$patstodel, 0, 'TrackLastPatronActivity - 0 patrons must be deleted' );
330 $patstodel = GetBorrowersToExpunge( { last_seen => '2016-02-15' });
331 is( scalar @$patstodel, 2, 'TrackLastPatronActivity - 2 patrons must be deleted' );
332 $patstodel = GetBorrowersToExpunge( { last_seen => '2016-04-04' });
333 is( scalar @$patstodel, 3, 'TrackLastPatronActivity - 3 patrons must be deleted' );
334 my $patron2 = $builder->build({
335 source => 'Borrower',
341 t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '0' );
342 Koha::Patrons->find( $patron2->{borrowernumber} )->track_login;
343 is( Koha::Patrons->find( $patron2->{borrowernumber} )->lastseen, undef, 'Lastseen should not be changed' );
344 Koha::Patrons->find( $patron2->{borrowernumber} )->track_login({ force => 1 });
345 isnt( Koha::Patrons->find( $patron2->{borrowernumber} )->lastseen, undef, 'Lastseen should be changed now' );
347 # Test GetBorrowersToExpunge and regular patron with permission
349 source => 'Category',
351 categorycode => 'SMALLSTAFF',
352 description => 'Small staff',
353 category_type => 'A',
356 $borrowernumber = Koha::Patron->new({
357 categorycode => 'SMALLSTAFF',
358 branchcode => $library2->{branchcode},
360 })->store->borrowernumber;
361 $patron = Koha::Patrons->find( $borrowernumber );
362 $patstodel = GetBorrowersToExpunge( {category_code => 'SMALLSTAFF' } );
363 is( scalar @$patstodel, 1, 'Regular patron with flags=undef can be deleted' );
364 $patron->set({ flags => 0 })->store;
365 $patstodel = GetBorrowersToExpunge( {category_code => 'SMALLSTAFF' } );
366 is( scalar @$patstodel, 1, 'Regular patron with flags=0 can be deleted' );
367 $patron->set({ flags => 4 })->store;
368 $patstodel = GetBorrowersToExpunge( {category_code => 'SMALLSTAFF' } );
369 is( scalar @$patstodel, 0, 'Regular patron with flags>0 can not be deleted' );
371 # Regression tests for BZ13502
372 ## Remove all entries with userid='' (should be only 1 max)
373 $dbh->do(q|DELETE FROM borrowers WHERE userid = ''|);
374 ## And create a patron with a userid=''
375 $borrowernumber = Koha::Patron->new({ categorycode => $patron_category->{categorycode}, branchcode => $library2->{branchcode} })->store->borrowernumber;
376 $dbh->do(q|UPDATE borrowers SET userid = '' WHERE borrowernumber = ?|, undef, $borrowernumber);
377 # Create another patron and verify the userid has been generated
378 $borrowernumber = Koha::Patron->new({ categorycode => $patron_category->{categorycode}, branchcode => $library2->{branchcode} })->store->borrowernumber;
379 ok( $borrowernumber > 0, 'Koha::Patron->store should have inserted the patron even if no userid is given' );
380 $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
381 ok( $borrower->{userid}, 'A userid should have been generated correctly' );
383 subtest 'purgeSelfRegistration' => sub {
388 C4::Members::DeleteUnverifiedOpacRegistrations($d);
390 $dbh->do("INSERT INTO borrower_modifications (timestamp, borrowernumber, verification_token, changed_fields) VALUES ('2014-01-01 01:02:03',0,?,'firstname,surname')", undef, (scalar localtime)."_$_");
392 # Add a record with a borrowernumber which should not be deleted by DeleteUnverifiedOpacRegistrations
393 # NOTE: We are using the borrowernumber from the last test outside this subtest
394 $dbh->do( "INSERT INTO borrower_modifications (timestamp, borrowernumber, verification_token, changed_fields) VALUES ('2014-01-01 01:02:03', ?, '', 'firstname,surname' )", undef, $borrowernumber );
395 is( C4::Members::DeleteUnverifiedOpacRegistrations($d), 3, 'Test for DeleteUnverifiedOpacRegistrations' );
397 #purge members in temporary category
399 $dbh->do("INSERT IGNORE INTO categories (categorycode) VALUES ('$c')");
400 t::lib::Mocks::mock_preference('PatronSelfRegistrationDefaultCategory', $c );
401 C4::Members::DeleteExpiredOpacRegistrations();
402 my $self_reg = $builder->build_object({
403 class => 'Koha::Patrons',
405 dateenrolled => '2014-01-01 01:02:03',
410 # First test if empty PatronSelfRegistrationExpireTemporaryAccountsDelay returns zero
411 t::lib::Mocks::mock_preference('PatronSelfRegistrationExpireTemporaryAccountsDelay', q{} );
412 is( C4::Members::DeleteExpiredOpacRegistrations(), 0, "DeleteExpiredOpacRegistrations with empty delay" );
414 t::lib::Mocks::mock_preference('PatronSelfRegistrationExpireTemporaryAccountsDelay', 0 );
415 is( C4::Members::DeleteExpiredOpacRegistrations(), 0, "DeleteExpiredOpacRegistrations with delay 0" );
416 # Also check empty category
417 t::lib::Mocks::mock_preference('PatronSelfRegistrationDefaultCategory', q{} );
418 t::lib::Mocks::mock_preference('PatronSelfRegistrationExpireTemporaryAccountsDelay', 360 );
419 is( C4::Members::DeleteExpiredOpacRegistrations(), 0, "DeleteExpiredOpacRegistrations with empty category" );
420 t::lib::Mocks::mock_preference('PatronSelfRegistrationDefaultCategory', $c );
422 my $checkout = $builder->build_object({
423 class=>'Koha::Checkouts',
425 borrowernumber=>$self_reg->borrowernumber
428 is( C4::Members::DeleteExpiredOpacRegistrations(), 0, "DeleteExpiredOpacRegistrations doesn't delete borrower with checkout");
430 my $account_line = $builder->build_object(
432 class => 'Koha::Account::Lines',
434 borrowernumber => $self_reg->borrowernumber,
435 amountoutstanding => 5,
439 is( C4::Members::DeleteExpiredOpacRegistrations(), 0, "DeleteExpiredOpacRegistrations doesn't delete borrower with checkout and fine");
442 is( C4::Members::DeleteExpiredOpacRegistrations(), 0, "DeleteExpiredOpacRegistrations doesn't delete borrower with fine and no checkout");
444 $account_line->delete;
445 is( C4::Members::DeleteExpiredOpacRegistrations(), 1, "DeleteExpiredOpacRegistrations does delete borrower with no fines and no checkouts");
450 my ($resultset) = @_;
451 my $found = $resultset && grep( { $_->{cardnumber} && $_->{cardnumber} eq $CARDNUMBER } @$resultset );
455 $schema->storage->txn_rollback;
457 subtest 'Koha::Patron->store (invalid categorycode) tests' => sub {
459 # TODO Move this to t/db_dependent/Koha/Patrons.t subtest ->store
461 $schema->storage->txn_begin;
463 my $category = $builder->build_object({ class => 'Koha::Patron::Categories' });
464 my $category_id = $category->id;
465 # Remove category to make sure the id is not on the DB
469 categorycode => $category_id
473 { Koha::Patron->new( $patron_data )->store; }
474 'Koha::Exceptions::Object::FKConstraint',
475 'AddMember raises an exception on invalid categorycode';
477 $schema->storage->txn_rollback;