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 => 51;
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( 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 t::lib::Mocks::mock_userenv();
63 # Make a borrower for testing
65 cardnumber => $CARDNUMBER,
66 firstname => $FIRSTNAME . q{ },
68 categorycode => $patron_category->{categorycode},
69 branchcode => $BRANCHCODE,
71 dateexpiry => '9999-12-31',
75 my $addmem=Koha::Patron->new(\%data)->store->borrowernumber;
76 ok($addmem, "Koha::Patron->store()");
78 my $patron = Koha::Patrons->find( { cardnumber => $CARDNUMBER } )
79 or BAIL_OUT("Cannot read member with card $CARDNUMBER");
80 my $member = $patron->unblessed;
82 ok ( $member->{firstname} eq $FIRSTNAME &&
83 $member->{surname} eq $SURNAME &&
84 $member->{categorycode} eq $patron_category->{categorycode} &&
85 $member->{branchcode} eq $BRANCHCODE
87 or diag("Mismatching member details: ".Dumper(\%data, $member));
89 is($member->{dateofbirth}, undef, "Empty dates handled correctly");
91 $member->{firstname} = $CHANGED_FIRSTNAME . q{ };
92 $member->{email} = $EMAIL;
93 $member->{phone} = $PHONE;
94 $member->{emailpro} = $EMAILPRO;
95 $patron->set($member)->store;
96 my $changedmember = Koha::Patrons->find( { cardnumber => $CARDNUMBER } )->unblessed;
97 ok ( $changedmember->{firstname} eq $CHANGED_FIRSTNAME &&
98 $changedmember->{email} eq $EMAIL &&
99 $changedmember->{phone} eq $PHONE &&
100 $changedmember->{emailpro} eq $EMAILPRO
102 or diag("Mismatching member details: ".Dumper($member, $changedmember));
106 cardnumber => "123456789",
107 firstname => "Tomasito",
109 categorycode => $patron_category->{categorycode},
110 branchcode => $library2->{branchcode},
116 my $borrowernumber = Koha::Patron->new( \%data )->store->borrowernumber;
117 $patron = Koha::Patrons->find( $borrowernumber );
118 my $borrower = $patron->unblessed;
119 is( $borrower->{dateofbirth}, undef, 'Koha::Patron->store should undef dateofbirth if empty string is given');
120 is( $borrower->{debarred}, undef, 'Koha::Patron->store should undef debarred if empty string is given');
121 isnt( $borrower->{dateexpiry}, '0000-00-00', 'Koha::Patron->store should not set dateexpiry to 0000-00-00 if empty string is given');
122 isnt( $borrower->{dateenrolled}, '0000-00-00', 'Koha::Patron->store should not set dateenrolled to 0000-00-00 if empty string is given');
124 $patron->set({ dateofbirth => '', debarred => '', dateexpiry => '', dateenrolled => '' })->store;
125 $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
126 is( $borrower->{dateofbirth}, undef, 'Koha::Patron->store should undef dateofbirth if empty string is given');
127 is( $borrower->{debarred}, undef, 'Koha::Patron->store should undef debarred if empty string is given');
128 isnt( $borrower->{dateexpiry}, '0000-00-00', 'Koha::Patron->store should not set dateexpiry to 0000-00-00 if empty string is given');
129 isnt( $borrower->{dateenrolled}, '0000-00-00', 'Koha::Patron->store should not set dateenrolled to 0000-00-00 if empty string is given');
131 $patron->set({ dateofbirth => '1970-01-01', debarred => '2042-01-01', dateexpiry => '9999-12-31', dateenrolled => '2015-09-06' })->store;
132 $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
133 is( $borrower->{dateofbirth}, '1970-01-01', 'Koha::Patron->store should correctly set dateofbirth if a valid date is given');
134 is( $borrower->{debarred}, '2042-01-01', 'Koha::Patron->store should correctly set debarred if a valid date is given');
135 is( $borrower->{dateexpiry}, '9999-12-31', 'Koha::Patron->store should correctly set dateexpiry if a valid date is given');
136 is( $borrower->{dateenrolled}, '2015-09-06', 'Koha::Patron->store should correctly set dateenrolled if a valid date is given');
138 #Regression tests for bug 10612
139 my $library3 = $builder->build({
143 source => 'Category',
145 categorycode => 'STAFFER',
146 description => 'Staff dont batch del',
147 category_type => 'S',
152 source => 'Category',
154 categorycode => 'CIVILIAN',
155 description => 'Civilian batch del',
156 category_type => 'A',
161 source => 'Category',
163 categorycode => 'KIDclamp',
164 description => 'Kid to be guaranteed',
165 category_type => 'C',
169 my $borrower1 = $builder->build({
170 source => 'Borrower',
172 categorycode=>'STAFFER',
173 branchcode => $library3->{branchcode},
174 dateexpiry => '2015-01-01',
178 my $bor1inlist = $borrower1->{borrowernumber};
179 my $borrower2 = $builder->build({
180 source => 'Borrower',
182 categorycode=>'STAFFER',
183 branchcode => $library3->{branchcode},
184 dateexpiry => '2015-01-01',
189 my $guarantee = $builder->build({
190 source => 'Borrower',
192 categorycode=>'KIDclamp',
193 branchcode => $library3->{branchcode},
194 dateexpiry => '2015-01-01',
199 my $bor2inlist = $borrower2->{borrowernumber};
202 source => 'OldIssue',
204 borrowernumber => $bor2inlist,
205 timestamp => '2016-01-01',
209 # The following calls to GetBorrowersToExpunge are assuming that the pref
210 # IndependentBranches is off.
211 t::lib::Mocks::mock_preference('IndependentBranches', 0);
213 my $anonymous_patron = Koha::Patron->new({ categorycode => 'CIVILIAN', branchcode => $library2->{branchcode} })->store->borrowernumber;
214 t::lib::Mocks::mock_preference('AnonymousPatron', $anonymous_patron);
216 my $owner = Koha::Patron->new({ categorycode => 'STAFFER', branchcode => $library2->{branchcode} })->store->borrowernumber;
217 my $list1 = AddPatronList( { name => 'Test List 1', owner => $owner } );
219 AddPatronsToList( { list => $list1, borrowernumbers => [$anonymous_patron] } );
220 my $patstodel = GetBorrowersToExpunge( { patron_list_id => $list1->patron_list_id() } );
221 is( scalar(@$patstodel), 0, 'Anonymous Patron not deleted from list' );
223 my @listpatrons = ($bor1inlist, $bor2inlist);
224 AddPatronsToList( { list => $list1, borrowernumbers => \@listpatrons });
225 $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id() } );
226 is( scalar(@$patstodel),0,'No staff deleted from list of all staff');
227 Koha::Patrons->find($bor2inlist)->set({ categorycode => 'CIVILIAN' })->store;
228 $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id()} );
229 ok( scalar(@$patstodel)== 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Staff patron not deleted from list');
230 $patstodel = GetBorrowersToExpunge( {branchcode => $library3->{branchcode},patron_list_id => $list1->patron_list_id() } );
231 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Staff patron not deleted by branchcode and list');
232 $patstodel = GetBorrowersToExpunge( {expired_before => '2015-01-02', patron_list_id => $list1->patron_list_id() } );
233 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Staff patron not deleted by expirationdate and list');
234 $patstodel = GetBorrowersToExpunge( {not_borrowed_since => '2016-01-02', patron_list_id => $list1->patron_list_id() } );
235 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Staff patron not deleted by last issue date');
237 Koha::Patrons->find($bor1inlist)->set({ categorycode => 'CIVILIAN' })->store;
239 t::lib::Mocks::mock_preference( 'borrowerRelationship', 'test' );
241 my $relationship = Koha::Patron::Relationship->new( { guarantor_id => $bor1inlist, guarantee_id => $guarantee->{borrowernumber}, relationship => 'test' } )->store();
243 $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id()} );
244 ok( scalar(@$patstodel)== 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Guarantor patron not deleted from list');
245 $patstodel = GetBorrowersToExpunge( {branchcode => $library3->{branchcode},patron_list_id => $list1->patron_list_id() } );
246 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Guarantor patron not deleted by branchcode and list');
247 $patstodel = GetBorrowersToExpunge( {expired_before => '2015-01-02', patron_list_id => $list1->patron_list_id() } );
248 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Guarantor patron not deleted by expirationdate and list');
249 $patstodel = GetBorrowersToExpunge( {not_borrowed_since => '2016-01-02', patron_list_id => $list1->patron_list_id() } );
250 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Guarantor patron not deleted by last issue date');
251 $relationship->delete();
256 borrowernumber => $bor2inlist,
259 $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id()} );
260 is( scalar(@$patstodel),1,'Borrower with issue not deleted from list');
261 $patstodel = GetBorrowersToExpunge( {branchcode => $library3->{branchcode},patron_list_id => $list1->patron_list_id() } );
262 is( scalar(@$patstodel),1,'Borrower with issue not deleted by branchcode and list');
263 $patstodel = GetBorrowersToExpunge( {category_code => 'CIVILIAN',patron_list_id => $list1->patron_list_id() } );
264 is( scalar(@$patstodel),1,'Borrower with issue not deleted by category_code and list');
265 $patstodel = GetBorrowersToExpunge( {category_code => [], patron_list_id => $list1->patron_list_id() } );
266 is( scalar(@$patstodel),1,'category_code can contain an empty arrayref');
267 $patstodel = GetBorrowersToExpunge( {category_code => ['CIVILIAN','STAFFER'],patron_list_id => $list1->patron_list_id() } );
268 is( scalar(@$patstodel),1,'Borrower with issue not deleted by multiple category_code and list');
269 $patstodel = GetBorrowersToExpunge( {expired_before => '2015-01-02',patron_list_id => $list1->patron_list_id() } );
270 is( scalar(@$patstodel),1,'Borrower with issue not deleted by expiration_date and list');
271 $builder->schema->resultset( 'Issue' )->delete_all;
272 $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id()} );
273 ok( scalar(@$patstodel)== 2,'Borrowers without issue deleted from list');
274 $patstodel = GetBorrowersToExpunge( {category_code => 'CIVILIAN',patron_list_id => $list1->patron_list_id() } );
275 is( scalar(@$patstodel),2,'Borrowers without issues deleted by category_code and list');
276 $patstodel = GetBorrowersToExpunge( {category_code => ['CIVILIAN','STAFFER'],patron_list_id => $list1->patron_list_id() } );
277 is( scalar(@$patstodel),2,'Borrowers without issues deleted by multiple category_code and list');
278 $patstodel = GetBorrowersToExpunge( {expired_before => '2015-01-02',patron_list_id => $list1->patron_list_id() } );
279 is( scalar(@$patstodel),2,'Borrowers without issues deleted by expiration_date and list');
280 $patstodel = GetBorrowersToExpunge( {not_borrowed_since => '2016-01-02', patron_list_id => $list1->patron_list_id() } );
281 is( scalar(@$patstodel),2,'Borrowers without issues deleted by last issue date');
283 # Test GetBorrowersToExpunge and TrackLastPatronActivityTriggers
284 my $new_category = $builder->build_object(
286 class => 'Koha::Patron::Categories',
287 value => { category_type => 'A' }, # should not be S for GetBorrowersToExpunge
291 source => 'Borrower',
293 lastseen => '2016-01-01 01:01:01',
294 categorycode => $new_category->categorycode,
299 source => 'Borrower',
301 lastseen => '2016-02-02 02:02:02',
302 categorycode => $new_category->categorycode,
307 source => 'Borrower',
309 lastseen => '2016-03-03 03:03:03',
310 categorycode => $new_category->categorycode,
314 $patstodel = GetBorrowersToExpunge( { category_code => $new_category->categorycode, last_seen => '1999-12-12' });
315 is( scalar @$patstodel, 0, 'TrackLastPatronActivityTriggers - 0 patrons must be deleted' );
316 $patstodel = GetBorrowersToExpunge( { category_code => $new_category->categorycode, last_seen => '2016-02-15' });
317 is( scalar @$patstodel, 2, 'TrackLastPatronActivityTriggers - 2 patrons must be deleted' );
318 $patstodel = GetBorrowersToExpunge( { category_code => $new_category->categorycode, last_seen => '2016-04-04' });
319 is( scalar @$patstodel, 3, 'TrackLastPatronActivityTriggers - 3 patrons must be deleted' );
320 my $patron2 = $builder->build({
321 source => 'Borrower',
327 t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', 'connection' );
328 Koha::Patrons->find( $patron2->{borrowernumber} )->update_lastseen('connection');
329 isnt( Koha::Patrons->find( $patron2->{borrowernumber} )->lastseen, undef, 'Lastseen should be changed now' );
331 # Test GetBorrowersToExpunge and regular patron with permission
333 source => 'Category',
335 categorycode => 'SMALLSTAFF',
336 description => 'Small staff',
337 category_type => 'A',
340 $borrowernumber = Koha::Patron->new({
341 categorycode => 'SMALLSTAFF',
342 branchcode => $library2->{branchcode},
344 })->store->borrowernumber;
345 $patron = Koha::Patrons->find( $borrowernumber );
346 $patstodel = GetBorrowersToExpunge( {category_code => 'SMALLSTAFF' } );
347 is( scalar @$patstodel, 1, 'Regular patron with flags=undef can be deleted' );
348 $patron->set({ flags => 0 })->store;
349 $patstodel = GetBorrowersToExpunge( {category_code => 'SMALLSTAFF' } );
350 is( scalar @$patstodel, 1, 'Regular patron with flags=0 can be deleted' );
351 $patron->set({ flags => 4 })->store;
352 $patstodel = GetBorrowersToExpunge( {category_code => 'SMALLSTAFF' } );
353 is( scalar @$patstodel, 0, 'Regular patron with flags>0 can not be deleted' );
355 # Test GetBorrowersToExpunge and patrons with "protected" status (borrowers.protected = 1)
358 source => 'Category',
360 categorycode => 'PROTECTED',
361 description => 'Protected',
362 category_type => 'A',
366 $borrowernumber = Koha::Patron->new(
368 categorycode => 'PROTECTED',
369 branchcode => $library2->{branchcode},
371 )->store->borrowernumber;
372 $patron = Koha::Patrons->find($borrowernumber);
373 $patstodel = GetBorrowersToExpunge( { category_code => 'PROTECTED' } );
374 is( scalar @$patstodel, 1, 'Patron with default protected status can be deleted' );
375 $patron->set( { protected => 1 } )->store;
376 $patstodel = GetBorrowersToExpunge( { category_code => 'PROTECTED' } );
377 is( scalar @$patstodel, 0, 'Patron with protected status set can not be deleted' );
379 # Regression tests for BZ13502
380 ## Remove all entries with userid='' (should be only 1 max)
381 $dbh->do(q|DELETE FROM borrowers WHERE userid = ''|);
382 ## And create a patron with a userid=''
383 $borrowernumber = Koha::Patron->new({ categorycode => $patron_category->{categorycode}, branchcode => $library2->{branchcode} })->store->borrowernumber;
384 $dbh->do(q|UPDATE borrowers SET userid = '' WHERE borrowernumber = ?|, undef, $borrowernumber);
385 # Create another patron and verify the userid has been generated
386 $borrowernumber = Koha::Patron->new({ categorycode => $patron_category->{categorycode}, branchcode => $library2->{branchcode} })->store->borrowernumber;
387 ok( $borrowernumber > 0, 'Koha::Patron->store should have inserted the patron even if no userid is given' );
388 $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
389 ok( $borrower->{userid}, 'A userid should have been generated correctly' );
391 subtest 'purgeSelfRegistration' => sub {
396 C4::Members::DeleteUnverifiedOpacRegistrations($d);
398 $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)."_$_");
400 # Add a record with a borrowernumber which should not be deleted by DeleteUnverifiedOpacRegistrations
401 # NOTE: We are using the borrowernumber from the last test outside this subtest
402 $dbh->do( "INSERT INTO borrower_modifications (timestamp, borrowernumber, verification_token, changed_fields) VALUES ('2014-01-01 01:02:03', ?, '', 'firstname,surname' )", undef, $borrowernumber );
403 is( C4::Members::DeleteUnverifiedOpacRegistrations($d), 3, 'Test for DeleteUnverifiedOpacRegistrations' );
405 #purge members in temporary category
407 $dbh->do("INSERT IGNORE INTO categories (categorycode) VALUES ('$c')");
408 t::lib::Mocks::mock_preference('PatronSelfRegistrationDefaultCategory', $c );
409 C4::Members::DeleteExpiredOpacRegistrations();
410 my $self_reg = $builder->build_object({
411 class => 'Koha::Patrons',
413 dateenrolled => '2014-01-01 01:02:03',
418 # First test if empty PatronSelfRegistrationExpireTemporaryAccountsDelay returns zero
419 t::lib::Mocks::mock_preference('PatronSelfRegistrationExpireTemporaryAccountsDelay', q{} );
420 is( C4::Members::DeleteExpiredOpacRegistrations(), 0, "DeleteExpiredOpacRegistrations with empty delay" );
422 t::lib::Mocks::mock_preference('PatronSelfRegistrationExpireTemporaryAccountsDelay', 0 );
423 is( C4::Members::DeleteExpiredOpacRegistrations(), 0, "DeleteExpiredOpacRegistrations with delay 0" );
424 # Also check empty category
425 t::lib::Mocks::mock_preference('PatronSelfRegistrationDefaultCategory', q{} );
426 t::lib::Mocks::mock_preference('PatronSelfRegistrationExpireTemporaryAccountsDelay', 360 );
427 is( C4::Members::DeleteExpiredOpacRegistrations(), 0, "DeleteExpiredOpacRegistrations with empty category" );
428 t::lib::Mocks::mock_preference('PatronSelfRegistrationDefaultCategory', $c );
430 my $checkout = $builder->build_object({
431 class=>'Koha::Checkouts',
433 borrowernumber=>$self_reg->borrowernumber
436 is( C4::Members::DeleteExpiredOpacRegistrations(), 0, "DeleteExpiredOpacRegistrations doesn't delete borrower with checkout");
438 my $account_line = $builder->build_object(
440 class => 'Koha::Account::Lines',
442 borrowernumber => $self_reg->borrowernumber,
443 amountoutstanding => 5,
447 is( C4::Members::DeleteExpiredOpacRegistrations(), 0, "DeleteExpiredOpacRegistrations doesn't delete borrower with checkout and fine");
450 is( C4::Members::DeleteExpiredOpacRegistrations(), 0, "DeleteExpiredOpacRegistrations doesn't delete borrower with fine and no checkout");
452 $account_line->delete;
453 is( C4::Members::DeleteExpiredOpacRegistrations(), 1, "DeleteExpiredOpacRegistrations does delete borrower with no fines and no checkouts");
458 my ($resultset) = @_;
459 my $found = $resultset && grep( { $_->{cardnumber} && $_->{cardnumber} eq $CARDNUMBER } @$resultset );
463 $schema->storage->txn_rollback;
465 subtest 'Koha::Patron->store (invalid categorycode) tests' => sub {
467 # TODO Move this to t/db_dependent/Koha/Patrons.t subtest ->store
469 $schema->storage->txn_begin;
471 my $category = $builder->build_object({ class => 'Koha::Patron::Categories' });
472 my $category_id = $category->id;
473 # Remove category to make sure the id is not on the DB
477 categorycode => $category_id
481 { Koha::Patron->new( $patron_data )->store; }
482 'Koha::Exceptions::Object::FKConstraint',
483 'AddMember raises an exception on invalid categorycode';
485 $schema->storage->txn_rollback;