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 => 50;
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 TrackLastPatronActivity
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, 'TrackLastPatronActivity - 0 patrons must be deleted' );
316 $patstodel = GetBorrowersToExpunge( { category_code => $new_category->categorycode, last_seen => '2016-02-15' });
317 is( scalar @$patstodel, 2, 'TrackLastPatronActivity - 2 patrons must be deleted' );
318 $patstodel = GetBorrowersToExpunge( { category_code => $new_category->categorycode, last_seen => '2016-04-04' });
319 is( scalar @$patstodel, 3, 'TrackLastPatronActivity - 3 patrons must be deleted' );
320 my $patron2 = $builder->build({
321 source => 'Borrower',
327 t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '0' );
328 Koha::Patrons->find( $patron2->{borrowernumber} )->track_login;
329 is( Koha::Patrons->find( $patron2->{borrowernumber} )->lastseen, undef, 'Lastseen should not be changed' );
330 Koha::Patrons->find( $patron2->{borrowernumber} )->track_login({ force => 1 });
331 isnt( Koha::Patrons->find( $patron2->{borrowernumber} )->lastseen, undef, 'Lastseen should be changed now' );
333 # Test GetBorrowersToExpunge and regular patron with permission
335 source => 'Category',
337 categorycode => 'SMALLSTAFF',
338 description => 'Small staff',
339 category_type => 'A',
342 $borrowernumber = Koha::Patron->new({
343 categorycode => 'SMALLSTAFF',
344 branchcode => $library2->{branchcode},
346 })->store->borrowernumber;
347 $patron = Koha::Patrons->find( $borrowernumber );
348 $patstodel = GetBorrowersToExpunge( {category_code => 'SMALLSTAFF' } );
349 is( scalar @$patstodel, 1, 'Regular patron with flags=undef can be deleted' );
350 $patron->set({ flags => 0 })->store;
351 $patstodel = GetBorrowersToExpunge( {category_code => 'SMALLSTAFF' } );
352 is( scalar @$patstodel, 1, 'Regular patron with flags=0 can be deleted' );
353 $patron->set({ flags => 4 })->store;
354 $patstodel = GetBorrowersToExpunge( {category_code => 'SMALLSTAFF' } );
355 is( scalar @$patstodel, 0, 'Regular patron with flags>0 can not be deleted' );
357 # Regression tests for BZ13502
358 ## Remove all entries with userid='' (should be only 1 max)
359 $dbh->do(q|DELETE FROM borrowers WHERE userid = ''|);
360 ## And create a patron with a userid=''
361 $borrowernumber = Koha::Patron->new({ categorycode => $patron_category->{categorycode}, branchcode => $library2->{branchcode} })->store->borrowernumber;
362 $dbh->do(q|UPDATE borrowers SET userid = '' WHERE borrowernumber = ?|, undef, $borrowernumber);
363 # Create another patron and verify the userid has been generated
364 $borrowernumber = Koha::Patron->new({ categorycode => $patron_category->{categorycode}, branchcode => $library2->{branchcode} })->store->borrowernumber;
365 ok( $borrowernumber > 0, 'Koha::Patron->store should have inserted the patron even if no userid is given' );
366 $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
367 ok( $borrower->{userid}, 'A userid should have been generated correctly' );
369 subtest 'purgeSelfRegistration' => sub {
374 C4::Members::DeleteUnverifiedOpacRegistrations($d);
376 $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)."_$_");
378 # Add a record with a borrowernumber which should not be deleted by DeleteUnverifiedOpacRegistrations
379 # NOTE: We are using the borrowernumber from the last test outside this subtest
380 $dbh->do( "INSERT INTO borrower_modifications (timestamp, borrowernumber, verification_token, changed_fields) VALUES ('2014-01-01 01:02:03', ?, '', 'firstname,surname' )", undef, $borrowernumber );
381 is( C4::Members::DeleteUnverifiedOpacRegistrations($d), 3, 'Test for DeleteUnverifiedOpacRegistrations' );
383 #purge members in temporary category
385 $dbh->do("INSERT IGNORE INTO categories (categorycode) VALUES ('$c')");
386 t::lib::Mocks::mock_preference('PatronSelfRegistrationDefaultCategory', $c );
387 C4::Members::DeleteExpiredOpacRegistrations();
388 my $self_reg = $builder->build_object({
389 class => 'Koha::Patrons',
391 dateenrolled => '2014-01-01 01:02:03',
396 # First test if empty PatronSelfRegistrationExpireTemporaryAccountsDelay returns zero
397 t::lib::Mocks::mock_preference('PatronSelfRegistrationExpireTemporaryAccountsDelay', q{} );
398 is( C4::Members::DeleteExpiredOpacRegistrations(), 0, "DeleteExpiredOpacRegistrations with empty delay" );
400 t::lib::Mocks::mock_preference('PatronSelfRegistrationExpireTemporaryAccountsDelay', 0 );
401 is( C4::Members::DeleteExpiredOpacRegistrations(), 0, "DeleteExpiredOpacRegistrations with delay 0" );
402 # Also check empty category
403 t::lib::Mocks::mock_preference('PatronSelfRegistrationDefaultCategory', q{} );
404 t::lib::Mocks::mock_preference('PatronSelfRegistrationExpireTemporaryAccountsDelay', 360 );
405 is( C4::Members::DeleteExpiredOpacRegistrations(), 0, "DeleteExpiredOpacRegistrations with empty category" );
406 t::lib::Mocks::mock_preference('PatronSelfRegistrationDefaultCategory', $c );
408 my $checkout = $builder->build_object({
409 class=>'Koha::Checkouts',
411 borrowernumber=>$self_reg->borrowernumber
414 is( C4::Members::DeleteExpiredOpacRegistrations(), 0, "DeleteExpiredOpacRegistrations doesn't delete borrower with checkout");
416 my $account_line = $builder->build_object(
418 class => 'Koha::Account::Lines',
420 borrowernumber => $self_reg->borrowernumber,
421 amountoutstanding => 5,
425 is( C4::Members::DeleteExpiredOpacRegistrations(), 0, "DeleteExpiredOpacRegistrations doesn't delete borrower with checkout and fine");
428 is( C4::Members::DeleteExpiredOpacRegistrations(), 0, "DeleteExpiredOpacRegistrations doesn't delete borrower with fine and no checkout");
430 $account_line->delete;
431 is( C4::Members::DeleteExpiredOpacRegistrations(), 1, "DeleteExpiredOpacRegistrations does delete borrower with no fines and no checkouts");
436 my ($resultset) = @_;
437 my $found = $resultset && grep( { $_->{cardnumber} && $_->{cardnumber} eq $CARDNUMBER } @$resultset );
441 $schema->storage->txn_rollback;
443 subtest 'Koha::Patron->store (invalid categorycode) tests' => sub {
445 # TODO Move this to t/db_dependent/Koha/Patrons.t subtest ->store
447 $schema->storage->txn_begin;
449 my $category = $builder->build_object({ class => 'Koha::Patron::Categories' });
450 my $category_id = $category->id;
451 # Remove category to make sure the id is not on the DB
455 categorycode => $category_id
459 { Koha::Patron->new( $patron_data )->store; }
460 'Koha::Exceptions::Object::FKConstraint',
461 'AddMember raises an exception on invalid categorycode';
463 $schema->storage->txn_rollback;