Bug 31196: Remove 'default_value_for_mod_marc-' clear_from_cache calls
[koha.git] / t / db_dependent / Members.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 54;
21 use Test::MockModule;
22 use Test::Exception;
23
24 use Data::Dumper qw/Dumper/;
25 use C4::Context;
26 use Koha::Database;
27 use Koha::Holds;
28 use Koha::List::Patron qw( AddPatronList AddPatronsToList );
29 use Koha::Patrons;
30 use Koha::Patron::Relationship;
31
32 use t::lib::Mocks;
33 use t::lib::TestBuilder;
34
35 BEGIN {
36         use_ok('C4::Members', qw( checkcardnumber GetBorrowersToExpunge DeleteUnverifiedOpacRegistrations DeleteExpiredOpacRegistrations ));
37 }
38
39 my $schema = Koha::Database->schema;
40 $schema->storage->txn_begin;
41 my $builder = t::lib::TestBuilder->new;
42 my $dbh = C4::Context->dbh;
43
44 my $library1 = $builder->build({
45     source => 'Branch',
46 });
47 my $library2 = $builder->build({
48     source => 'Branch',
49 });
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};
55
56 my $CHANGED_FIRSTNAME = "Marry Ann";
57 my $EMAIL             = "Marie\@email.com";
58 my $EMAILPRO          = "Marie\@work.com";
59 my $PHONE             = "555-12123";
60
61 # XXX should be randomised and checked against the database
62 my $IMPOSSIBLE_CARDNUMBER = "XYZZZ999";
63
64 t::lib::Mocks::mock_userenv();
65
66 # Make a borrower for testing
67 my %data = (
68     cardnumber => $CARDNUMBER,
69     firstname =>  $FIRSTNAME . q{ },
70     surname => $SURNAME,
71     categorycode => $patron_category->{categorycode},
72     branchcode => $BRANCHCODE,
73     dateofbirth => '',
74     dateexpiry => '9999-12-31',
75     userid => 'tomasito'
76 );
77
78 my $addmem=Koha::Patron->new(\%data)->store->borrowernumber;
79 ok($addmem, "Koha::Patron->store()");
80
81 my $patron = Koha::Patrons->find( { cardnumber => $CARDNUMBER } )
82   or BAIL_OUT("Cannot read member with card $CARDNUMBER");
83 my $member = $patron->unblessed;
84
85 ok ( $member->{firstname}    eq $FIRSTNAME    &&
86      $member->{surname}      eq $SURNAME      &&
87      $member->{categorycode} eq $patron_category->{categorycode} &&
88      $member->{branchcode}   eq $BRANCHCODE
89      , "Got member")
90   or diag("Mismatching member details: ".Dumper(\%data, $member));
91
92 is($member->{dateofbirth}, undef, "Empty dates handled correctly");
93
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
104      , "Member Changed")
105   or diag("Mismatching member details: ".Dumper($member, $changedmember));
106
107 t::lib::Mocks::mock_preference( 'CardnumberLength', '' );
108 C4::Context->clear_syspref_cache();
109
110 my $checkcardnum=C4::Members::checkcardnumber($CARDNUMBER, "");
111 is ($checkcardnum, "1", "Card No. in use");
112
113 $checkcardnum=C4::Members::checkcardnumber($IMPOSSIBLE_CARDNUMBER, "");
114 is ($checkcardnum, "0", "Card No. not used");
115
116 t::lib::Mocks::mock_preference( 'CardnumberLength', '4' );
117 C4::Context->clear_syspref_cache();
118
119 $checkcardnum=C4::Members::checkcardnumber($IMPOSSIBLE_CARDNUMBER, "");
120 is ($checkcardnum, "2", "Card number is too long");
121
122
123 # Add a new borrower
124 %data = (
125     cardnumber   => "123456789",
126     firstname    => "Tomasito",
127     surname      => "None",
128     categorycode => $patron_category->{categorycode},
129     branchcode   => $library2->{branchcode},
130     dateofbirth  => '',
131     debarred     => '',
132     dateexpiry   => '',
133     dateenrolled => '',
134 );
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');
142
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');
149
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');
156
157 subtest 'Koha::Patron->store should not update userid if not true' => sub {
158     plan tests => 3;
159
160     # TODO Move this to t/db_dependent/Koha/Patrons.t subtest ->store
161
162     $data{ cardnumber } = "234567890";
163     $data{userid} = 'a_user_id';
164     $borrowernumber = Koha::Patron->new( \%data )->store->borrowernumber;
165     my $patron = Koha::Patrons->find( $borrowernumber );
166     my $borrower = $patron->unblessed;
167
168     $patron->set( { firstname => 'Tomas', userid => '' } )->store;
169     $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
170     is ( $borrower->{userid}, $data{userid}, 'Koha::Patron->store should not update the userid with an empty string' );
171     $patron->set( { firstname => 'Tomas', userid => 0 } )->store;
172     $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
173     is ( $borrower->{userid}, $data{userid}, 'Koha::Patron->store should not update the userid with an 0');
174     $patron->set( { firstname => 'Tomas', userid => undef } )->store;
175     $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
176     is ( $borrower->{userid}, $data{userid}, 'Koha::Patron->store should not update the userid with an undefined value');
177 };
178
179 #Regression tests for bug 10612
180 my $library3 = $builder->build({
181     source => 'Branch',
182 });
183 $builder->build({
184         source => 'Category',
185         value => {
186             categorycode         => 'STAFFER',
187             description          => 'Staff dont batch del',
188             category_type        => 'S',
189         },
190 });
191
192 $builder->build({
193         source => 'Category',
194         value => {
195             categorycode         => 'CIVILIAN',
196             description          => 'Civilian batch del',
197             category_type        => 'A',
198         },
199 });
200
201 $builder->build({
202         source => 'Category',
203         value => {
204             categorycode         => 'KIDclamp',
205             description          => 'Kid to be guaranteed',
206             category_type        => 'C',
207         },
208 });
209
210 my $borrower1 = $builder->build({
211         source => 'Borrower',
212         value  => {
213             categorycode=>'STAFFER',
214             branchcode => $library3->{branchcode},
215             dateexpiry => '2015-01-01',
216             flags => undef,
217         },
218 });
219 my $bor1inlist = $borrower1->{borrowernumber};
220 my $borrower2 = $builder->build({
221         source => 'Borrower',
222         value  => {
223             categorycode=>'STAFFER',
224             branchcode => $library3->{branchcode},
225             dateexpiry => '2015-01-01',
226             flags => undef,
227         },
228 });
229
230 my $guarantee = $builder->build({
231         source => 'Borrower',
232         value  => {
233             categorycode=>'KIDclamp',
234             branchcode => $library3->{branchcode},
235             dateexpiry => '2015-01-01',
236             flags => undef,
237         },
238 });
239
240 my $bor2inlist = $borrower2->{borrowernumber};
241
242 $builder->build({
243         source => 'OldIssue',
244         value  => {
245             borrowernumber => $bor2inlist,
246             timestamp => '2016-01-01',
247         },
248 });
249
250 # The following calls to GetBorrowersToExpunge are assuming that the pref
251 # IndependentBranches is off.
252 t::lib::Mocks::mock_preference('IndependentBranches', 0);
253
254 my $anonymous_patron = Koha::Patron->new({ categorycode => 'CIVILIAN', branchcode => $library2->{branchcode} })->store->borrowernumber;
255 t::lib::Mocks::mock_preference('AnonymousPatron', $anonymous_patron);
256
257 my $owner = Koha::Patron->new({ categorycode => 'STAFFER', branchcode => $library2->{branchcode} })->store->borrowernumber;
258 my $list1 = AddPatronList( { name => 'Test List 1', owner => $owner } );
259
260 AddPatronsToList( { list => $list1, borrowernumbers => [$anonymous_patron] } );
261 my $patstodel = GetBorrowersToExpunge( { patron_list_id => $list1->patron_list_id() } );
262 is( scalar(@$patstodel), 0, 'Anonymous Patron not deleted from list' );
263
264 my @listpatrons = ($bor1inlist, $bor2inlist);
265 AddPatronsToList(  { list => $list1, borrowernumbers => \@listpatrons });
266 $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id() } );
267 is( scalar(@$patstodel),0,'No staff deleted from list of all staff');
268 Koha::Patrons->find($bor2inlist)->set({ categorycode => 'CIVILIAN' })->store;
269 $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id()} );
270 ok( scalar(@$patstodel)== 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Staff patron not deleted from list');
271 $patstodel = GetBorrowersToExpunge( {branchcode => $library3->{branchcode},patron_list_id => $list1->patron_list_id() } );
272 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Staff patron not deleted by branchcode and list');
273 $patstodel = GetBorrowersToExpunge( {expired_before => '2015-01-02', patron_list_id => $list1->patron_list_id() } );
274 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Staff patron not deleted by expirationdate and list');
275 $patstodel = GetBorrowersToExpunge( {not_borrowed_since => '2016-01-02', patron_list_id => $list1->patron_list_id() } );
276 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Staff patron not deleted by last issue date');
277
278 Koha::Patrons->find($bor1inlist)->set({ categorycode => 'CIVILIAN' })->store;
279
280 t::lib::Mocks::mock_preference( 'borrowerRelationship', 'test' );
281
282 my $relationship = Koha::Patron::Relationship->new( { guarantor_id => $bor1inlist, guarantee_id => $guarantee->{borrowernumber}, relationship => 'test' } )->store();
283
284 $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id()} );
285 ok( scalar(@$patstodel)== 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Guarantor patron not deleted from list');
286 $patstodel = GetBorrowersToExpunge( {branchcode => $library3->{branchcode},patron_list_id => $list1->patron_list_id() } );
287 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Guarantor patron not deleted by branchcode and list');
288 $patstodel = GetBorrowersToExpunge( {expired_before => '2015-01-02', patron_list_id => $list1->patron_list_id() } );
289 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Guarantor patron not deleted by expirationdate and list');
290 $patstodel = GetBorrowersToExpunge( {not_borrowed_since => '2016-01-02', patron_list_id => $list1->patron_list_id() } );
291 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Guarantor patron not deleted by last issue date');
292 $relationship->delete();
293
294 $builder->build({
295         source => 'Issue',
296         value  => {
297             borrowernumber => $bor2inlist,
298         },
299 });
300 $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id()} );
301 is( scalar(@$patstodel),1,'Borrower with issue not deleted from list');
302 $patstodel = GetBorrowersToExpunge( {branchcode => $library3->{branchcode},patron_list_id => $list1->patron_list_id() } );
303 is( scalar(@$patstodel),1,'Borrower with issue not deleted by branchcode and list');
304 $patstodel = GetBorrowersToExpunge( {category_code => 'CIVILIAN',patron_list_id => $list1->patron_list_id() } );
305 is( scalar(@$patstodel),1,'Borrower with issue not deleted by category_code and list');
306 $patstodel = GetBorrowersToExpunge( {category_code => [], patron_list_id => $list1->patron_list_id() } );
307 is( scalar(@$patstodel),1,'category_code can contain an empty arrayref');
308 $patstodel = GetBorrowersToExpunge( {category_code => ['CIVILIAN','STAFFER'],patron_list_id => $list1->patron_list_id() } );
309 is( scalar(@$patstodel),1,'Borrower with issue not deleted by multiple category_code and list');
310 $patstodel = GetBorrowersToExpunge( {expired_before => '2015-01-02',patron_list_id => $list1->patron_list_id() } );
311 is( scalar(@$patstodel),1,'Borrower with issue not deleted by expiration_date and list');
312 $builder->schema->resultset( 'Issue' )->delete_all;
313 $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id()} );
314 ok( scalar(@$patstodel)== 2,'Borrowers without issue deleted from list');
315 $patstodel = GetBorrowersToExpunge( {category_code => 'CIVILIAN',patron_list_id => $list1->patron_list_id() } );
316 is( scalar(@$patstodel),2,'Borrowers without issues deleted by category_code and list');
317 $patstodel = GetBorrowersToExpunge( {category_code => ['CIVILIAN','STAFFER'],patron_list_id => $list1->patron_list_id() } );
318 is( scalar(@$patstodel),2,'Borrowers without issues deleted by multiple category_code and list');
319 $patstodel = GetBorrowersToExpunge( {expired_before => '2015-01-02',patron_list_id => $list1->patron_list_id() } );
320 is( scalar(@$patstodel),2,'Borrowers without issues deleted by expiration_date and list');
321 $patstodel = GetBorrowersToExpunge( {not_borrowed_since => '2016-01-02', patron_list_id => $list1->patron_list_id() } );
322 is( scalar(@$patstodel),2,'Borrowers without issues deleted by last issue date');
323
324 # Test GetBorrowersToExpunge and TrackLastPatronActivity
325 $dbh->do(q|UPDATE borrowers SET lastseen=NULL|);
326 $builder->build({
327     source => 'Borrower',
328     value => {
329         lastseen => '2016-01-01 01:01:01',
330         categorycode => 'CIVILIAN',
331         flags => undef,
332     }
333 });
334 $builder->build({
335     source => 'Borrower',
336     value => {
337         lastseen => '2016-02-02 02:02:02',
338         categorycode => 'CIVILIAN',
339         flags => undef,
340     }
341 });
342 $builder->build({
343     source => 'Borrower',
344     value => {
345         lastseen => '2016-03-03 03:03:03',
346         categorycode => 'CIVILIAN',
347         flags => undef,
348     }
349 });
350 $patstodel = GetBorrowersToExpunge( { last_seen => '1999-12-12' });
351 is( scalar @$patstodel, 0, 'TrackLastPatronActivity - 0 patrons must be deleted' );
352 $patstodel = GetBorrowersToExpunge( { last_seen => '2016-02-15' });
353 is( scalar @$patstodel, 2, 'TrackLastPatronActivity - 2 patrons must be deleted' );
354 $patstodel = GetBorrowersToExpunge( { last_seen => '2016-04-04' });
355 is( scalar @$patstodel, 3, 'TrackLastPatronActivity - 3 patrons must be deleted' );
356 my $patron2 = $builder->build({
357     source => 'Borrower',
358     value => {
359         lastseen => undef,
360         flags => undef,
361     }
362 });
363 t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '0' );
364 Koha::Patrons->find( $patron2->{borrowernumber} )->track_login;
365 is( Koha::Patrons->find( $patron2->{borrowernumber} )->lastseen, undef, 'Lastseen should not be changed' );
366 Koha::Patrons->find( $patron2->{borrowernumber} )->track_login({ force => 1 });
367 isnt( Koha::Patrons->find( $patron2->{borrowernumber} )->lastseen, undef, 'Lastseen should be changed now' );
368
369 # Test GetBorrowersToExpunge and regular patron with permission
370 $builder->build({
371         source => 'Category',
372         value => {
373             categorycode         => 'SMALLSTAFF',
374             description          => 'Small staff',
375             category_type        => 'A',
376         },
377 });
378 $borrowernumber = Koha::Patron->new({
379     categorycode => 'SMALLSTAFF',
380     branchcode => $library2->{branchcode},
381     flags => undef,
382 })->store->borrowernumber;
383 $patron = Koha::Patrons->find( $borrowernumber );
384 $patstodel = GetBorrowersToExpunge( {category_code => 'SMALLSTAFF' } );
385 is( scalar @$patstodel, 1, 'Regular patron with flags=undef can be deleted' );
386 $patron->set({ flags => 0 })->store;
387 $patstodel = GetBorrowersToExpunge( {category_code => 'SMALLSTAFF' } );
388 is( scalar @$patstodel, 1, 'Regular patron with flags=0 can be deleted' );
389 $patron->set({ flags => 4 })->store;
390 $patstodel = GetBorrowersToExpunge( {category_code => 'SMALLSTAFF' } );
391 is( scalar @$patstodel, 0, 'Regular patron with flags>0 can not be deleted' );
392
393 # Regression tests for BZ13502
394 ## Remove all entries with userid='' (should be only 1 max)
395 $dbh->do(q|DELETE FROM borrowers WHERE userid = ''|);
396 ## And create a patron with a userid=''
397 $borrowernumber = Koha::Patron->new({ categorycode => $patron_category->{categorycode}, branchcode => $library2->{branchcode} })->store->borrowernumber;
398 $dbh->do(q|UPDATE borrowers SET userid = '' WHERE borrowernumber = ?|, undef, $borrowernumber);
399 # Create another patron and verify the userid has been generated
400 $borrowernumber = Koha::Patron->new({ categorycode => $patron_category->{categorycode}, branchcode => $library2->{branchcode} })->store->borrowernumber;
401 ok( $borrowernumber > 0, 'Koha::Patron->store should have inserted the patron even if no userid is given' );
402 $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
403 ok( $borrower->{userid},  'A userid should have been generated correctly' );
404
405 subtest 'purgeSelfRegistration' => sub {
406     plan tests => 8;
407
408     #purge unverified
409     my $d=360;
410     C4::Members::DeleteUnverifiedOpacRegistrations($d);
411     foreach(1..3) {
412         $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)."_$_");
413     }
414     # Add a record with a borrowernumber which should not be deleted by DeleteUnverifiedOpacRegistrations
415     # NOTE: We are using the borrowernumber from the last test outside this subtest
416     $dbh->do( "INSERT INTO borrower_modifications (timestamp, borrowernumber, verification_token, changed_fields) VALUES ('2014-01-01 01:02:03', ?, '', 'firstname,surname' )", undef, $borrowernumber );
417     is( C4::Members::DeleteUnverifiedOpacRegistrations($d), 3, 'Test for DeleteUnverifiedOpacRegistrations' );
418
419     #purge members in temporary category
420     my $c= 'XYZ';
421     $dbh->do("INSERT IGNORE INTO categories (categorycode) VALUES ('$c')");
422     t::lib::Mocks::mock_preference('PatronSelfRegistrationDefaultCategory', $c );
423     C4::Members::DeleteExpiredOpacRegistrations();
424     my $self_reg = $builder->build_object({
425         class => 'Koha::Patrons',
426         value => {
427             dateenrolled => '2014-01-01 01:02:03',
428             categorycode => $c
429         }
430     });
431
432     # First test if empty PatronSelfRegistrationExpireTemporaryAccountsDelay returns zero
433     t::lib::Mocks::mock_preference('PatronSelfRegistrationExpireTemporaryAccountsDelay', q{} );
434     is( C4::Members::DeleteExpiredOpacRegistrations(), 0, "DeleteExpiredOpacRegistrations with empty delay" );
435     # Test zero too
436     t::lib::Mocks::mock_preference('PatronSelfRegistrationExpireTemporaryAccountsDelay', 0 );
437     is( C4::Members::DeleteExpiredOpacRegistrations(), 0, "DeleteExpiredOpacRegistrations with delay 0" );
438     # Also check empty category
439     t::lib::Mocks::mock_preference('PatronSelfRegistrationDefaultCategory', q{} );
440     t::lib::Mocks::mock_preference('PatronSelfRegistrationExpireTemporaryAccountsDelay', 360 );
441     is( C4::Members::DeleteExpiredOpacRegistrations(), 0, "DeleteExpiredOpacRegistrations with empty category" );
442     t::lib::Mocks::mock_preference('PatronSelfRegistrationDefaultCategory', $c );
443
444     my $checkout     = $builder->build_object({
445         class=>'Koha::Checkouts',
446         value=>{
447             borrowernumber=>$self_reg->borrowernumber
448         }
449     });
450     is( C4::Members::DeleteExpiredOpacRegistrations(), 0, "DeleteExpiredOpacRegistrations doesn't delete borrower with checkout");
451
452     my $account_line = $builder->build_object(
453         {
454             class => 'Koha::Account::Lines',
455             value => {
456                 borrowernumber    => $self_reg->borrowernumber,
457                 amountoutstanding => 5,
458                 acocunttype       => undef
459             }
460         }
461     );
462     is( C4::Members::DeleteExpiredOpacRegistrations(), 0, "DeleteExpiredOpacRegistrations doesn't delete borrower with checkout and fine");
463
464     $checkout->delete;
465     is( C4::Members::DeleteExpiredOpacRegistrations(), 0, "DeleteExpiredOpacRegistrations doesn't delete borrower with fine and no checkout");
466
467     $account_line->delete;
468     is( C4::Members::DeleteExpiredOpacRegistrations(), 1, "DeleteExpiredOpacRegistrations does delete borrower with no fines and no checkouts");
469
470 };
471
472 sub _find_member {
473     my ($resultset) = @_;
474     my $found = $resultset && grep( { $_->{cardnumber} && $_->{cardnumber} eq $CARDNUMBER } @$resultset );
475     return $found;
476 }
477
478 $schema->storage->txn_rollback;
479
480 subtest 'Koha::Patron->store (invalid categorycode) tests' => sub {
481     plan tests => 1;
482     # TODO Move this to t/db_dependent/Koha/Patrons.t subtest ->store
483
484     $schema->storage->txn_begin;
485
486     my $category    = $builder->build_object({ class => 'Koha::Patron::Categories' });
487     my $category_id = $category->id;
488     # Remove category to make sure the id is not on the DB
489     $category->delete;
490
491     my $patron_data = {
492         categorycode => $category_id
493     };
494
495     throws_ok
496         { Koha::Patron->new( $patron_data )->store; }
497         'Koha::Exceptions::Object::FKConstraint',
498         'AddMember raises an exception on invalid categorycode';
499
500     $schema->storage->txn_rollback;
501 };