Bug 18420: Fix Members.t when no patron category 'S' exists
[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 => 65;
21 use Test::MockModule;
22 use Data::Dumper qw/Dumper/;
23 use C4::Context;
24 use Koha::Database;
25 use Koha::Holds;
26 use Koha::List::Patron;
27 use Koha::Patrons;
28
29 use t::lib::Mocks;
30 use t::lib::TestBuilder;
31
32 BEGIN {
33         use_ok('C4::Members');
34 }
35
36 my $schema = Koha::Database->schema;
37 $schema->storage->txn_begin;
38 my $builder = t::lib::TestBuilder->new;
39 my $dbh = C4::Context->dbh;
40
41 # Remove invalid guarantorid's as long as we have no FK
42 $dbh->do("UPDATE borrowers b1 LEFT JOIN borrowers b2 ON b2.borrowernumber=b1.guarantorid SET b1.guarantorid=NULL where b1.guarantorid IS NOT NULL AND b2.borrowernumber IS NULL");
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 #my ($usernum, $userid, $usercnum, $userfirstname, $usersurname, $userbranch, $branchname, $userflags, $emailaddress, $branchprinter)= @_;
65 my @USERENV = (
66     1,
67     'test',
68     'MASTERTEST',
69     'Test',
70     'Test',
71     't',
72     'Test',
73     0,
74 );
75 my $BRANCH_IDX = 5;
76
77 C4::Context->_new_userenv ('DUMMY_SESSION_ID');
78 C4::Context->set_userenv ( @USERENV );
79
80 my $userenv = C4::Context->userenv
81   or BAIL_OUT("No userenv");
82
83 # Make a borrower for testing
84 my %data = (
85     cardnumber => $CARDNUMBER,
86     firstname =>  $FIRSTNAME,
87     surname => $SURNAME,
88     categorycode => $patron_category->{categorycode},
89     branchcode => $BRANCHCODE,
90     dateofbirth => '',
91     dateexpiry => '9999-12-31',
92     userid => 'tomasito'
93 );
94
95 my $addmem=AddMember(%data);
96 ok($addmem, "AddMember()");
97
98 my $member = GetMember( cardnumber => $CARDNUMBER )
99   or BAIL_OUT("Cannot read member with card $CARDNUMBER");
100
101 ok ( $member->{firstname}    eq $FIRSTNAME    &&
102      $member->{surname}      eq $SURNAME      &&
103      $member->{categorycode} eq $patron_category->{categorycode} &&
104      $member->{branchcode}   eq $BRANCHCODE
105      , "Got member")
106   or diag("Mismatching member details: ".Dumper(\%data, $member));
107
108 is($member->{dateofbirth}, undef, "Empty dates handled correctly");
109
110 $member->{firstname} = $CHANGED_FIRSTNAME;
111 $member->{email}     = $EMAIL;
112 $member->{phone}     = $PHONE;
113 $member->{emailpro}  = $EMAILPRO;
114 ModMember(%$member);
115 my $changedmember = GetMember( cardnumber => $CARDNUMBER );
116 ok ( $changedmember->{firstname} eq $CHANGED_FIRSTNAME &&
117      $changedmember->{email}     eq $EMAIL             &&
118      $changedmember->{phone}     eq $PHONE             &&
119      $changedmember->{emailpro}  eq $EMAILPRO
120      , "Member Changed")
121   or diag("Mismatching member details: ".Dumper($member, $changedmember));
122
123 t::lib::Mocks::mock_preference( 'CardnumberLength', '' );
124 C4::Context->clear_syspref_cache();
125
126 my $checkcardnum=C4::Members::checkcardnumber($CARDNUMBER, "");
127 is ($checkcardnum, "1", "Card No. in use");
128
129 $checkcardnum=C4::Members::checkcardnumber($IMPOSSIBLE_CARDNUMBER, "");
130 is ($checkcardnum, "0", "Card No. not used");
131
132 t::lib::Mocks::mock_preference( 'CardnumberLength', '4' );
133 C4::Context->clear_syspref_cache();
134
135 $checkcardnum=C4::Members::checkcardnumber($IMPOSSIBLE_CARDNUMBER, "");
136 is ($checkcardnum, "2", "Card number is too long");
137
138
139
140 t::lib::Mocks::mock_preference( 'AutoEmailPrimaryAddress', 'OFF' );
141 C4::Context->clear_syspref_cache();
142
143 my $notice_email = GetNoticeEmailAddress($member->{'borrowernumber'});
144 is ($notice_email, $EMAIL, "GetNoticeEmailAddress returns correct value when AutoEmailPrimaryAddress is off");
145
146 t::lib::Mocks::mock_preference( 'AutoEmailPrimaryAddress', 'emailpro' );
147 C4::Context->clear_syspref_cache();
148
149 $notice_email = GetNoticeEmailAddress($member->{'borrowernumber'});
150 is ($notice_email, $EMAILPRO, "GetNoticeEmailAddress returns correct value when AutoEmailPrimaryAddress is emailpro");
151
152 # Check_Userid tests
153 %data = (
154     cardnumber   => "123456789",
155     firstname    => "Tomasito",
156     surname      => "None",
157     categorycode => $patron_category->{categorycode},
158     branchcode   => $library2->{branchcode},
159     dateofbirth  => '',
160     debarred     => '',
161     dateexpiry   => '',
162     dateenrolled => '',
163 );
164 # Add a new borrower
165 my $borrowernumber = AddMember( %data );
166 is( Check_Userid( 'tomasito.non', $borrowernumber ), 1,
167     'recently created userid -> unique (borrowernumber passed)' );
168 is( Check_Userid( 'tomasitoxxx', $borrowernumber ), 1,
169     'non-existent userid -> unique (borrowernumber passed)' );
170 is( Check_Userid( 'tomasito.none', '' ), 0,
171     'userid exists (blank borrowernumber)' );
172 is( Check_Userid( 'tomasitoxxx', '' ), 1,
173     'non-existent userid -> unique (blank borrowernumber)' );
174
175 my $borrower = GetMember( borrowernumber => $borrowernumber );
176 is( $borrower->{dateofbirth}, undef, 'AddMember should undef dateofbirth if empty string is given');
177 is( $borrower->{debarred}, undef, 'AddMember should undef debarred if empty string is given');
178 isnt( $borrower->{dateexpiry}, '0000-00-00', 'AddMember should not set dateexpiry to 0000-00-00 if empty string is given');
179 isnt( $borrower->{dateenrolled}, '0000-00-00', 'AddMember should not set dateenrolled to 0000-00-00 if empty string is given');
180
181 ModMember( borrowernumber => $borrowernumber, dateofbirth => '', debarred => '', dateexpiry => '', dateenrolled => '' );
182 $borrower = GetMember( borrowernumber => $borrowernumber );
183 is( $borrower->{dateofbirth}, undef, 'ModMember should undef dateofbirth if empty string is given');
184 is( $borrower->{debarred}, undef, 'ModMember should undef debarred if empty string is given');
185 isnt( $borrower->{dateexpiry}, '0000-00-00', 'ModMember should not set dateexpiry to 0000-00-00 if empty string is given');
186 isnt( $borrower->{dateenrolled}, '0000-00-00', 'ModMember should not set dateenrolled to 0000-00-00 if empty string is given');
187
188 ModMember( borrowernumber => $borrowernumber, dateofbirth => '1970-01-01', debarred => '2042-01-01', dateexpiry => '9999-12-31', dateenrolled => '2015-09-06' );
189 $borrower = GetMember( borrowernumber => $borrowernumber );
190 is( $borrower->{dateofbirth}, '1970-01-01', 'ModMember should correctly set dateofbirth if a valid date is given');
191 is( $borrower->{debarred}, '2042-01-01', 'ModMember should correctly set debarred if a valid date is given');
192 is( $borrower->{dateexpiry}, '9999-12-31', 'ModMember should correctly set dateexpiry if a valid date is given');
193 is( $borrower->{dateenrolled}, '2015-09-06', 'ModMember should correctly set dateenrolled if a valid date is given');
194
195 # Add a new borrower with the same userid but different cardnumber
196 $data{ cardnumber } = "987654321";
197 my $new_borrowernumber = AddMember( %data );
198 is( Check_Userid( 'tomasito.none', '' ), 0,
199     'userid not unique (blank borrowernumber)' );
200 is( Check_Userid( 'tomasito.none', $new_borrowernumber ), 0,
201     'userid not unique (second borrowernumber passed)' );
202 $borrower = GetMember( borrowernumber => $new_borrowernumber );
203 ok( $borrower->{userid} ne 'tomasito', "Borrower with duplicate userid has new userid generated" );
204
205 $data{ cardnumber } = "234567890";
206 $data{userid} = 'a_user_id';
207 $borrowernumber = AddMember( %data );
208 $borrower = GetMember( borrowernumber => $borrowernumber );
209 is( $borrower->{userid}, $data{userid}, 'AddMember should insert the given userid' );
210
211 subtest 'ModMember should not update userid if not true' => sub {
212     plan tests => 3;
213     ModMember( borrowernumber => $borrowernumber, firstname => 'Tomas', userid => '' );
214     $borrower = GetMember( borrowernumber => $borrowernumber );
215     is ( $borrower->{userid}, $data{userid}, 'ModMember should not update the userid with an empty string' );
216     ModMember( borrowernumber => $borrowernumber, firstname => 'Tomas', userid => 0 );
217     $borrower = GetMember( borrowernumber => $borrowernumber );
218     is ( $borrower->{userid}, $data{userid}, 'ModMember should not update the userid with an 0');
219     ModMember( borrowernumber => $borrowernumber, firstname => 'Tomas', userid => undef );
220     $borrower = GetMember( borrowernumber => $borrowernumber );
221     is ( $borrower->{userid}, $data{userid}, 'ModMember should not update the userid with an undefined value');
222 };
223
224 #Regression tests for bug 10612
225 my $library3 = $builder->build({
226     source => 'Branch',
227 });
228 $builder->build({
229         source => 'Category',
230         value => {
231             categorycode         => 'STAFFER',
232             description          => 'Staff dont batch del',
233             category_type        => 'S',
234         },
235 });
236
237 $builder->build({
238         source => 'Category',
239         value => {
240             categorycode         => 'CIVILIAN',
241             description          => 'Civilian batch del',
242             category_type        => 'A',
243         },
244 });
245
246 $builder->build({
247         source => 'Category',
248         value => {
249             categorycode         => 'KIDclamp',
250             description          => 'Kid to be guaranteed',
251             category_type        => 'C',
252         },
253 });
254
255 my $borrower1 = $builder->build({
256         source => 'Borrower',
257         value  => {
258             categorycode=>'STAFFER',
259             branchcode => $library3->{branchcode},
260             dateexpiry => '2015-01-01',
261             guarantorid=> undef,
262         },
263 });
264 my $bor1inlist = $borrower1->{borrowernumber};
265 my $borrower2 = $builder->build({
266         source => 'Borrower',
267         value  => {
268             categorycode=>'STAFFER',
269             branchcode => $library3->{branchcode},
270             dateexpiry => '2015-01-01',
271             guarantorid=> undef,
272         },
273 });
274
275 my $guarantee = $builder->build({
276         source => 'Borrower',
277         value  => {
278             categorycode=>'KIDclamp',
279             branchcode => $library3->{branchcode},
280             dateexpiry => '2015-01-01',
281             guarantorid=> undef, # will be filled later
282         },
283 });
284
285 my $bor2inlist = $borrower2->{borrowernumber};
286
287 $builder->build({
288         source => 'OldIssue',
289         value  => {
290             borrowernumber => $bor2inlist,
291             timestamp => '2016-01-01',
292         },
293 });
294
295 my $owner = AddMember (categorycode => 'STAFFER', branchcode => $library2->{branchcode} );
296 my $list1 = AddPatronList( { name => 'Test List 1', owner => $owner } );
297 my @listpatrons = ($bor1inlist, $bor2inlist);
298 AddPatronsToList(  { list => $list1, borrowernumbers => \@listpatrons });
299 my $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id() } );
300 is( scalar(@$patstodel),0,'No staff deleted from list of all staff');
301 ModMember( borrowernumber => $bor2inlist, categorycode => 'CIVILIAN' );
302 $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id()} );
303 ok( scalar(@$patstodel)== 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Staff patron not deleted from list');
304 $patstodel = GetBorrowersToExpunge( {branchcode => $library3->{branchcode},patron_list_id => $list1->patron_list_id() } );
305 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Staff patron not deleted by branchcode and list');
306 $patstodel = GetBorrowersToExpunge( {expired_before => '2015-01-02', patron_list_id => $list1->patron_list_id() } );
307 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Staff patron not deleted by expirationdate and list');
308 $patstodel = GetBorrowersToExpunge( {not_borrowed_since => '2016-01-02', patron_list_id => $list1->patron_list_id() } );
309 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Staff patron not deleted by last issue date');
310
311 ModMember( borrowernumber => $bor1inlist, categorycode => 'CIVILIAN' );
312 ModMember( borrowernumber => $guarantee->{borrowernumber} ,guarantorid=>$bor1inlist );
313
314 $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id()} );
315 ok( scalar(@$patstodel)== 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Guarantor patron not deleted from list');
316 $patstodel = GetBorrowersToExpunge( {branchcode => $library3->{branchcode},patron_list_id => $list1->patron_list_id() } );
317 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Guarantor patron not deleted by branchcode and list');
318 $patstodel = GetBorrowersToExpunge( {expired_before => '2015-01-02', patron_list_id => $list1->patron_list_id() } );
319 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Guarantor patron not deleted by expirationdate and list');
320 $patstodel = GetBorrowersToExpunge( {not_borrowed_since => '2016-01-02', patron_list_id => $list1->patron_list_id() } );
321 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Guarantor patron not deleted by last issue date');
322 ModMember( borrowernumber => $guarantee->{borrowernumber}, guarantorid=>'' );
323
324 $builder->build({
325         source => 'Issue',
326         value  => {
327             borrowernumber => $bor2inlist,
328         },
329 });
330 $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id()} );
331 is( scalar(@$patstodel),1,'Borrower with issue not deleted from list');
332 $patstodel = GetBorrowersToExpunge( {branchcode => $library3->{branchcode},patron_list_id => $list1->patron_list_id() } );
333 is( scalar(@$patstodel),1,'Borrower with issue not deleted by branchcode and list');
334 $patstodel = GetBorrowersToExpunge( {category_code => 'CIVILIAN',patron_list_id => $list1->patron_list_id() } );
335 is( scalar(@$patstodel),1,'Borrower with issue not deleted by category_code and list');
336 $patstodel = GetBorrowersToExpunge( {expired_before => '2015-01-02',patron_list_id => $list1->patron_list_id() } );
337 is( scalar(@$patstodel),1,'Borrower with issue not deleted by expiration_date and list');
338 $builder->schema->resultset( 'Issue' )->delete_all;
339 $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id()} );
340 ok( scalar(@$patstodel)== 2,'Borrowers without issue deleted from list');
341 $patstodel = GetBorrowersToExpunge( {category_code => 'CIVILIAN',patron_list_id => $list1->patron_list_id() } );
342 is( scalar(@$patstodel),2,'Borrowers without issues deleted by category_code and list');
343 $patstodel = GetBorrowersToExpunge( {expired_before => '2015-01-02',patron_list_id => $list1->patron_list_id() } );
344 is( scalar(@$patstodel),2,'Borrowers without issues deleted by expiration_date and list');
345 $patstodel = GetBorrowersToExpunge( {not_borrowed_since => '2016-01-02', patron_list_id => $list1->patron_list_id() } );
346 is( scalar(@$patstodel),2,'Borrowers without issues deleted by last issue date');
347
348 # Test GetBorrowersToExpunge and TrackLastPatronActivity
349 $dbh->do(q|UPDATE borrowers SET lastseen=NULL|);
350 $builder->build({ source => 'Borrower', value => { lastseen => '2016-01-01 01:01:01', categorycode => 'CIVILIAN', guarantorid => undef } } );
351 $builder->build({ source => 'Borrower', value => { lastseen => '2016-02-02 02:02:02', categorycode => 'CIVILIAN', guarantorid => undef } } );
352 $builder->build({ source => 'Borrower', value => { lastseen => '2016-03-03 03:03:03', categorycode => 'CIVILIAN', guarantorid => undef } } );
353 $patstodel = GetBorrowersToExpunge( { last_seen => '1999-12-12' });
354 is( scalar @$patstodel, 0, 'TrackLastPatronActivity - 0 patrons must be deleted' );
355 $patstodel = GetBorrowersToExpunge( { last_seen => '2016-02-15' });
356 is( scalar @$patstodel, 2, 'TrackLastPatronActivity - 2 patrons must be deleted' );
357 $patstodel = GetBorrowersToExpunge( { last_seen => '2016-04-04' });
358 is( scalar @$patstodel, 3, 'TrackLastPatronActivity - 3 patrons must be deleted' );
359 my $patron2 = $builder->build({ source => 'Borrower', value => { lastseen => undef } });
360 t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '0' );
361 Koha::Patrons->find( $patron2->{borrowernumber} )->track_login;
362 is( Koha::Patrons->find( $patron2->{borrowernumber} )->lastseen, undef, 'Lastseen should not be changed' );
363 Koha::Patrons->find( $patron2->{borrowernumber} )->track_login({ force => 1 });
364 isnt( Koha::Patrons->find( $patron2->{borrowernumber} )->lastseen, undef, 'Lastseen should be changed now' );
365
366 # Regression tests for BZ13502
367 ## Remove all entries with userid='' (should be only 1 max)
368 $dbh->do(q|DELETE FROM borrowers WHERE userid = ''|);
369 ## And create a patron with a userid=''
370 $borrowernumber = AddMember( categorycode => $patron_category->{categorycode}, branchcode => $library2->{branchcode} );
371 $dbh->do(q|UPDATE borrowers SET userid = '' WHERE borrowernumber = ?|, undef, $borrowernumber);
372 # Create another patron and verify the userid has been generated
373 $borrowernumber = AddMember( categorycode => $patron_category->{categorycode}, branchcode => $library2->{branchcode} );
374 ok( $borrowernumber > 0, 'AddMember should have inserted the patron even if no userid is given' );
375 $borrower = GetMember( borrowernumber => $borrowernumber );
376 ok( $borrower->{userid},  'A userid should have been generated correctly' );
377
378 # Regression tests for BZ12226
379 is( Check_Userid( C4::Context->config('user'), '' ), 0,
380     'Check_Userid should return 0 for the DB user (Bug 12226)');
381
382 subtest 'GetMemberAccountRecords' => sub {
383
384     plan tests => 2;
385
386     my $borrowernumber = $builder->build({ source => 'Borrower' })->{ borrowernumber };
387     my $accountline_1  = $builder->build({
388         source => 'Accountline',
389         value  => {
390             borrowernumber    => $borrowernumber,
391             amountoutstanding => 64.60
392         }
393     });
394
395     my ($total,undef,undef) = GetMemberAccountRecords( $borrowernumber );
396     is( $total , 64.60, "Rounding works correctly in total calculation (single value)" );
397
398     my $accountline_2 = $builder->build({
399         source => 'Accountline',
400         value  => {
401             borrowernumber    => $borrowernumber,
402             amountoutstanding => 10.65
403         }
404     });
405
406     ($total,undef,undef) = GetMemberAccountRecords( $borrowernumber );
407     is( $total , 75.25, "Rounding works correctly in total calculation (multiple values)" );
408
409 };
410
411 subtest 'GetMemberAccountBalance' => sub {
412
413     plan tests => 6;
414
415     my $members_mock = new Test::MockModule('C4::Members');
416     $members_mock->mock( 'GetMemberAccountRecords', sub {
417         my ($borrowernumber) = @_;
418         if ($borrowernumber) {
419             my @accountlines = (
420             { amountoutstanding => '7', accounttype => 'Rent' },
421             { amountoutstanding => '5', accounttype => 'Res' },
422             { amountoutstanding => '3', accounttype => 'Pay' } );
423             return ( 15, \@accountlines );
424         }
425         else {
426             my @accountlines;
427             return ( 0, \@accountlines );
428         }
429     });
430
431     # do not count holds charges
432     t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', '1' );
433     t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', '0' );
434     my ($total, $total_minus_charges,
435         $other_charges) = C4::Members::GetMemberAccountBalance(123);
436     is( $total, 15 , "Total calculated correctly");
437     is( $total_minus_charges, 15, "Holds charges are not count if HoldsInNoissuesCharge=1");
438     is( $other_charges, 0, "Holds charges are not considered if HoldsInNoissuesCharge=1");
439
440     t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', '0' );
441     ($total, $total_minus_charges,
442         $other_charges) = C4::Members::GetMemberAccountBalance(123);
443     is( $total, 15 , "Total calculated correctly");
444     is( $total_minus_charges, 10, "Holds charges are count if HoldsInNoissuesCharge=0");
445     is( $other_charges, 5, "Holds charges are considered if HoldsInNoissuesCharge=1");
446 };
447
448 subtest 'purgeSelfRegistration' => sub {
449     plan tests => 2;
450
451     #purge unverified
452     my $d=360;
453     C4::Members::DeleteUnverifiedOpacRegistrations($d);
454     foreach(1..3) {
455         $dbh->do("INSERT INTO borrower_modifications (timestamp, borrowernumber, verification_token) VALUES ('2014-01-01 01:02:03',0,?)", undef, (scalar localtime)."_$_");
456     }
457     is( C4::Members::DeleteUnverifiedOpacRegistrations($d), 3, 'Test for DeleteUnverifiedOpacRegistrations' );
458
459     #purge members in temporary category
460     my $c= 'XYZ';
461     $dbh->do("INSERT IGNORE INTO categories (categorycode) VALUES ('$c')");
462     t::lib::Mocks::mock_preference('PatronSelfRegistrationDefaultCategory', $c );
463     t::lib::Mocks::mock_preference('PatronSelfRegistrationExpireTemporaryAccountsDelay', 360);
464     C4::Members::DeleteExpiredOpacRegistrations();
465     $dbh->do("INSERT INTO borrowers (surname, address, city, branchcode, categorycode, dateenrolled) VALUES ('Testaabbcc', 'Street 1', 'CITY', ?, '$c', '2014-01-01 01:02:03')", undef, $library1->{branchcode});
466     is( C4::Members::DeleteExpiredOpacRegistrations(), 1, 'Test for DeleteExpiredOpacRegistrations');
467 };
468
469 sub _find_member {
470     my ($resultset) = @_;
471     my $found = $resultset && grep( { $_->{cardnumber} && $_->{cardnumber} eq $CARDNUMBER } @$resultset );
472     return $found;
473 }
474
475 # Regression tests for BZ15343
476 my $password="";
477 ( $borrowernumber, $password ) = AddMember_Opac(surname=>"Dick",firstname=>'Philip',branchcode => $library2->{branchcode});
478 is( $password =~ /^[a-zA-Z]{10}$/ , 1, 'Test for autogenerated password if none submitted');
479 ( $borrowernumber, $password ) = AddMember_Opac(surname=>"Deckard",firstname=>"Rick",password=>"Nexus-6",branchcode => $library2->{branchcode});
480 is( $password eq "Nexus-6", 1, 'Test password used if submitted');
481 $borrower = GetMember(borrowernumber => $borrowernumber);
482 my $hashed_up =  Koha::AuthUtils::hash_password("Nexus-6", $borrower->{password});
483 is( $borrower->{password} eq $hashed_up, 1, 'Check password hash equals hash of submitted password' );
484
485 # regression test for bug 16009
486 my $patron;
487 eval {
488     my $patron = GetMember(cardnumber => undef);
489 };
490 is($@, '', 'Bug 16009: GetMember(cardnumber => undef) works');
491 is($patron, undef, 'Bug 16009: GetMember(cardnumber => undef) returns undef');
492
493 subtest 'Trivial test for AddMember_Auto' => sub {
494     plan tests => 3;
495     my $members_mock = Test::MockModule->new( 'C4::Members' );
496     $members_mock->mock( 'fixup_cardnumber', sub { 12345; } );
497     my $library = $builder->build({ source => 'Branch' });
498     my $category = $builder->build({ source => 'Category' });
499     my %borr = AddMember_Auto( surname=> 'Dick3', firstname => 'Philip', branchcode => $library->{branchcode}, categorycode => $category->{categorycode}, password => '34567890' );
500     ok( $borr{borrowernumber}, 'Borrower hash contains borrowernumber' );
501     is( $borr{cardnumber}, 12345, 'Borrower hash contains cardnumber' );
502     $patron = Koha::Patrons->find( $borr{borrowernumber} );
503     isnt( $patron, undef, 'Patron found' );
504 };
505
506 $schema->storage->txn_rollback;