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