Bug 18292: Remove return 1 statements in tests
[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 => 63;
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 . q{ },
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 = Koha::Patrons->find( { cardnumber => $CARDNUMBER } )
99   or BAIL_OUT("Cannot read member with card $CARDNUMBER");
100 $member = $member->unblessed;
101
102 ok ( $member->{firstname}    eq $FIRSTNAME    &&
103      $member->{surname}      eq $SURNAME      &&
104      $member->{categorycode} eq $patron_category->{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 . q{ };
112 $member->{email}     = $EMAIL;
113 $member->{phone}     = $PHONE;
114 $member->{emailpro}  = $EMAILPRO;
115 ModMember(%$member);
116 my $changedmember = Koha::Patrons->find( { cardnumber => $CARDNUMBER } )->unblessed;
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 # Check_Userid tests
154 %data = (
155     cardnumber   => "123456789",
156     firstname    => "Tomasito",
157     surname      => "None",
158     categorycode => $patron_category->{categorycode},
159     branchcode   => $library2->{branchcode},
160     dateofbirth  => '',
161     debarred     => '',
162     dateexpiry   => '',
163     dateenrolled => '',
164 );
165 # Add a new borrower
166 my $borrowernumber = AddMember( %data );
167 is( Check_Userid( 'tomasito.non', $borrowernumber ), 1,
168     'recently created userid -> unique (borrowernumber passed)' );
169 is( Check_Userid( 'tomasitoxxx', $borrowernumber ), 1,
170     'non-existent userid -> unique (borrowernumber passed)' );
171 is( Check_Userid( 'tomasito.none', '' ), 0,
172     'userid exists (blank borrowernumber)' );
173 is( Check_Userid( 'tomasitoxxx', '' ), 1,
174     'non-existent userid -> unique (blank borrowernumber)' );
175
176 my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
177 is( $borrower->{dateofbirth}, undef, 'AddMember should undef dateofbirth if empty string is given');
178 is( $borrower->{debarred}, undef, 'AddMember should undef debarred if empty string is given');
179 isnt( $borrower->{dateexpiry}, '0000-00-00', 'AddMember should not set dateexpiry to 0000-00-00 if empty string is given');
180 isnt( $borrower->{dateenrolled}, '0000-00-00', 'AddMember should not set dateenrolled to 0000-00-00 if empty string is given');
181
182 ModMember( borrowernumber => $borrowernumber, dateofbirth => '', debarred => '', dateexpiry => '', dateenrolled => '' );
183 $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
184 is( $borrower->{dateofbirth}, undef, 'ModMember should undef dateofbirth if empty string is given');
185 is( $borrower->{debarred}, undef, 'ModMember should undef debarred if empty string is given');
186 isnt( $borrower->{dateexpiry}, '0000-00-00', 'ModMember should not set dateexpiry to 0000-00-00 if empty string is given');
187 isnt( $borrower->{dateenrolled}, '0000-00-00', 'ModMember should not set dateenrolled to 0000-00-00 if empty string is given');
188
189 ModMember( borrowernumber => $borrowernumber, dateofbirth => '1970-01-01', debarred => '2042-01-01', dateexpiry => '9999-12-31', dateenrolled => '2015-09-06' );
190 $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
191 is( $borrower->{dateofbirth}, '1970-01-01', 'ModMember should correctly set dateofbirth if a valid date is given');
192 is( $borrower->{debarred}, '2042-01-01', 'ModMember should correctly set debarred if a valid date is given');
193 is( $borrower->{dateexpiry}, '9999-12-31', 'ModMember should correctly set dateexpiry if a valid date is given');
194 is( $borrower->{dateenrolled}, '2015-09-06', 'ModMember should correctly set dateenrolled if a valid date is given');
195
196 # Add a new borrower with the same userid but different cardnumber
197 $data{ cardnumber } = "987654321";
198 my $new_borrowernumber = AddMember( %data );
199 is( Check_Userid( 'tomasito.none', '' ), 0,
200     'userid not unique (blank borrowernumber)' );
201 is( Check_Userid( 'tomasito.none', $new_borrowernumber ), 0,
202     'userid not unique (second borrowernumber passed)' );
203 $borrower = Koha::Patrons->find( $new_borrowernumber )->unblessed;
204 ok( $borrower->{userid} ne 'tomasito', "Borrower with duplicate userid has new userid generated" );
205
206 $data{ cardnumber } = "234567890";
207 $data{userid} = 'a_user_id';
208 $borrowernumber = AddMember( %data );
209 $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
210 is( $borrower->{userid}, $data{userid}, 'AddMember should insert the given userid' );
211
212 subtest 'ModMember should not update userid if not true' => sub {
213     plan tests => 3;
214     ModMember( borrowernumber => $borrowernumber, firstname => 'Tomas', userid => '' );
215     $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
216     is ( $borrower->{userid}, $data{userid}, 'ModMember should not update the userid with an empty string' );
217     ModMember( borrowernumber => $borrowernumber, firstname => 'Tomas', userid => 0 );
218     $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
219     is ( $borrower->{userid}, $data{userid}, 'ModMember should not update the userid with an 0');
220     ModMember( borrowernumber => $borrowernumber, firstname => 'Tomas', userid => undef );
221     $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
222     is ( $borrower->{userid}, $data{userid}, 'ModMember should not update the userid with an undefined value');
223 };
224
225 #Regression tests for bug 10612
226 my $library3 = $builder->build({
227     source => 'Branch',
228 });
229 $builder->build({
230         source => 'Category',
231         value => {
232             categorycode         => 'STAFFER',
233             description          => 'Staff dont batch del',
234             category_type        => 'S',
235         },
236 });
237
238 $builder->build({
239         source => 'Category',
240         value => {
241             categorycode         => 'CIVILIAN',
242             description          => 'Civilian batch del',
243             category_type        => 'A',
244         },
245 });
246
247 $builder->build({
248         source => 'Category',
249         value => {
250             categorycode         => 'KIDclamp',
251             description          => 'Kid to be guaranteed',
252             category_type        => 'C',
253         },
254 });
255
256 my $borrower1 = $builder->build({
257         source => 'Borrower',
258         value  => {
259             categorycode=>'STAFFER',
260             branchcode => $library3->{branchcode},
261             dateexpiry => '2015-01-01',
262             guarantorid=> undef,
263         },
264 });
265 my $bor1inlist = $borrower1->{borrowernumber};
266 my $borrower2 = $builder->build({
267         source => 'Borrower',
268         value  => {
269             categorycode=>'STAFFER',
270             branchcode => $library3->{branchcode},
271             dateexpiry => '2015-01-01',
272             guarantorid=> undef,
273         },
274 });
275
276 my $guarantee = $builder->build({
277         source => 'Borrower',
278         value  => {
279             categorycode=>'KIDclamp',
280             branchcode => $library3->{branchcode},
281             dateexpiry => '2015-01-01',
282             guarantorid=> undef, # will be filled later
283         },
284 });
285
286 my $bor2inlist = $borrower2->{borrowernumber};
287
288 $builder->build({
289         source => 'OldIssue',
290         value  => {
291             borrowernumber => $bor2inlist,
292             timestamp => '2016-01-01',
293         },
294 });
295
296 my $owner = AddMember (categorycode => 'STAFFER', branchcode => $library2->{branchcode} );
297 my $list1 = AddPatronList( { name => 'Test List 1', owner => $owner } );
298 my @listpatrons = ($bor1inlist, $bor2inlist);
299 AddPatronsToList(  { list => $list1, borrowernumbers => \@listpatrons });
300 my $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id() } );
301 is( scalar(@$patstodel),0,'No staff deleted from list of all staff');
302 ModMember( borrowernumber => $bor2inlist, categorycode => 'CIVILIAN' );
303 $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id()} );
304 ok( scalar(@$patstodel)== 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Staff patron not deleted from list');
305 $patstodel = GetBorrowersToExpunge( {branchcode => $library3->{branchcode},patron_list_id => $list1->patron_list_id() } );
306 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Staff patron not deleted by branchcode and list');
307 $patstodel = GetBorrowersToExpunge( {expired_before => '2015-01-02', patron_list_id => $list1->patron_list_id() } );
308 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Staff patron not deleted by expirationdate and list');
309 $patstodel = GetBorrowersToExpunge( {not_borrowed_since => '2016-01-02', patron_list_id => $list1->patron_list_id() } );
310 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Staff patron not deleted by last issue date');
311
312 ModMember( borrowernumber => $bor1inlist, categorycode => 'CIVILIAN' );
313 ModMember( borrowernumber => $guarantee->{borrowernumber} ,guarantorid=>$bor1inlist );
314
315 $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id()} );
316 ok( scalar(@$patstodel)== 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Guarantor patron not deleted from list');
317 $patstodel = GetBorrowersToExpunge( {branchcode => $library3->{branchcode},patron_list_id => $list1->patron_list_id() } );
318 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Guarantor patron not deleted by branchcode and list');
319 $patstodel = GetBorrowersToExpunge( {expired_before => '2015-01-02', patron_list_id => $list1->patron_list_id() } );
320 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Guarantor patron not deleted by expirationdate and list');
321 $patstodel = GetBorrowersToExpunge( {not_borrowed_since => '2016-01-02', patron_list_id => $list1->patron_list_id() } );
322 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Guarantor patron not deleted by last issue date');
323 ModMember( borrowernumber => $guarantee->{borrowernumber}, guarantorid=>'' );
324
325 $builder->build({
326         source => 'Issue',
327         value  => {
328             borrowernumber => $bor2inlist,
329         },
330 });
331 $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id()} );
332 is( scalar(@$patstodel),1,'Borrower with issue not deleted from list');
333 $patstodel = GetBorrowersToExpunge( {branchcode => $library3->{branchcode},patron_list_id => $list1->patron_list_id() } );
334 is( scalar(@$patstodel),1,'Borrower with issue not deleted by branchcode and list');
335 $patstodel = GetBorrowersToExpunge( {category_code => 'CIVILIAN',patron_list_id => $list1->patron_list_id() } );
336 is( scalar(@$patstodel),1,'Borrower with issue not deleted by category_code and list');
337 $patstodel = GetBorrowersToExpunge( {expired_before => '2015-01-02',patron_list_id => $list1->patron_list_id() } );
338 is( scalar(@$patstodel),1,'Borrower with issue not deleted by expiration_date and list');
339 $builder->schema->resultset( 'Issue' )->delete_all;
340 $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id()} );
341 ok( scalar(@$patstodel)== 2,'Borrowers without issue deleted from list');
342 $patstodel = GetBorrowersToExpunge( {category_code => 'CIVILIAN',patron_list_id => $list1->patron_list_id() } );
343 is( scalar(@$patstodel),2,'Borrowers without issues deleted by category_code and list');
344 $patstodel = GetBorrowersToExpunge( {expired_before => '2015-01-02',patron_list_id => $list1->patron_list_id() } );
345 is( scalar(@$patstodel),2,'Borrowers without issues deleted by expiration_date and list');
346 $patstodel = GetBorrowersToExpunge( {not_borrowed_since => '2016-01-02', patron_list_id => $list1->patron_list_id() } );
347 is( scalar(@$patstodel),2,'Borrowers without issues deleted by last issue date');
348
349 # Test GetBorrowersToExpunge and TrackLastPatronActivity
350 $dbh->do(q|UPDATE borrowers SET lastseen=NULL|);
351 $builder->build({ source => 'Borrower', value => { lastseen => '2016-01-01 01:01:01', categorycode => 'CIVILIAN', guarantorid => undef } } );
352 $builder->build({ source => 'Borrower', value => { lastseen => '2016-02-02 02:02:02', categorycode => 'CIVILIAN', guarantorid => undef } } );
353 $builder->build({ source => 'Borrower', value => { lastseen => '2016-03-03 03:03:03', categorycode => 'CIVILIAN', guarantorid => undef } } );
354 $patstodel = GetBorrowersToExpunge( { last_seen => '1999-12-12' });
355 is( scalar @$patstodel, 0, 'TrackLastPatronActivity - 0 patrons must be deleted' );
356 $patstodel = GetBorrowersToExpunge( { last_seen => '2016-02-15' });
357 is( scalar @$patstodel, 2, 'TrackLastPatronActivity - 2 patrons must be deleted' );
358 $patstodel = GetBorrowersToExpunge( { last_seen => '2016-04-04' });
359 is( scalar @$patstodel, 3, 'TrackLastPatronActivity - 3 patrons must be deleted' );
360 my $patron2 = $builder->build({ source => 'Borrower', value => { lastseen => undef } });
361 t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '0' );
362 Koha::Patrons->find( $patron2->{borrowernumber} )->track_login;
363 is( Koha::Patrons->find( $patron2->{borrowernumber} )->lastseen, undef, 'Lastseen should not be changed' );
364 Koha::Patrons->find( $patron2->{borrowernumber} )->track_login({ force => 1 });
365 isnt( Koha::Patrons->find( $patron2->{borrowernumber} )->lastseen, undef, 'Lastseen should be changed now' );
366
367 # Regression tests for BZ13502
368 ## Remove all entries with userid='' (should be only 1 max)
369 $dbh->do(q|DELETE FROM borrowers WHERE userid = ''|);
370 ## And create a patron with a userid=''
371 $borrowernumber = AddMember( categorycode => $patron_category->{categorycode}, branchcode => $library2->{branchcode} );
372 $dbh->do(q|UPDATE borrowers SET userid = '' WHERE borrowernumber = ?|, undef, $borrowernumber);
373 # Create another patron and verify the userid has been generated
374 $borrowernumber = AddMember( categorycode => $patron_category->{categorycode}, branchcode => $library2->{branchcode} );
375 ok( $borrowernumber > 0, 'AddMember should have inserted the patron even if no userid is given' );
376 $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
377 ok( $borrower->{userid},  'A userid should have been generated correctly' );
378
379 # Regression tests for BZ12226
380 is( Check_Userid( C4::Context->config('user'), '' ), 0,
381     'Check_Userid should return 0 for the DB user (Bug 12226)');
382
383 subtest 'GetMemberAccountRecords' => sub {
384
385     plan tests => 2;
386
387     my $borrowernumber = $builder->build({ source => 'Borrower' })->{ borrowernumber };
388     my $accountline_1  = $builder->build({
389         source => 'Accountline',
390         value  => {
391             borrowernumber    => $borrowernumber,
392             amountoutstanding => 64.60
393         }
394     });
395
396     my ($total,undef,undef) = GetMemberAccountRecords( $borrowernumber );
397     is( $total , 64.60, "Rounding works correctly in total calculation (single value)" );
398
399     my $accountline_2 = $builder->build({
400         source => 'Accountline',
401         value  => {
402             borrowernumber    => $borrowernumber,
403             amountoutstanding => 10.65
404         }
405     });
406
407     ($total,undef,undef) = GetMemberAccountRecords( $borrowernumber );
408     is( $total , 75.25, "Rounding works correctly in total calculation (multiple values)" );
409
410 };
411
412 subtest 'GetMemberAccountBalance' => sub {
413
414     plan tests => 6;
415
416     my $members_mock = new Test::MockModule('C4::Members');
417     $members_mock->mock( 'GetMemberAccountRecords', sub {
418         my ($borrowernumber) = @_;
419         if ($borrowernumber) {
420             my @accountlines = (
421             { amountoutstanding => '7', accounttype => 'Rent' },
422             { amountoutstanding => '5', accounttype => 'Res' },
423             { amountoutstanding => '3', accounttype => 'Pay' } );
424             return ( 15, \@accountlines );
425         }
426         else {
427             my @accountlines;
428             return ( 0, \@accountlines );
429         }
430     });
431
432     # do not count holds charges
433     t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', '1' );
434     t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', '0' );
435     my ($total, $total_minus_charges,
436         $other_charges) = C4::Members::GetMemberAccountBalance(123);
437     is( $total, 15 , "Total calculated correctly");
438     is( $total_minus_charges, 15, "Holds charges are not count if HoldsInNoissuesCharge=1");
439     is( $other_charges, 0, "Holds charges are not considered if HoldsInNoissuesCharge=1");
440
441     t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', '0' );
442     ($total, $total_minus_charges,
443         $other_charges) = C4::Members::GetMemberAccountBalance(123);
444     is( $total, 15 , "Total calculated correctly");
445     is( $total_minus_charges, 10, "Holds charges are count if HoldsInNoissuesCharge=0");
446     is( $other_charges, 5, "Holds charges are considered if HoldsInNoissuesCharge=1");
447 };
448
449 subtest 'purgeSelfRegistration' => sub {
450     plan tests => 2;
451
452     #purge unverified
453     my $d=360;
454     C4::Members::DeleteUnverifiedOpacRegistrations($d);
455     foreach(1..3) {
456         $dbh->do("INSERT INTO borrower_modifications (timestamp, borrowernumber, verification_token) VALUES ('2014-01-01 01:02:03',0,?)", undef, (scalar localtime)."_$_");
457     }
458     is( C4::Members::DeleteUnverifiedOpacRegistrations($d), 3, 'Test for DeleteUnverifiedOpacRegistrations' );
459
460     #purge members in temporary category
461     my $c= 'XYZ';
462     $dbh->do("INSERT IGNORE INTO categories (categorycode) VALUES ('$c')");
463     t::lib::Mocks::mock_preference('PatronSelfRegistrationDefaultCategory', $c );
464     t::lib::Mocks::mock_preference('PatronSelfRegistrationExpireTemporaryAccountsDelay', 360);
465     C4::Members::DeleteExpiredOpacRegistrations();
466     $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});
467     is( C4::Members::DeleteExpiredOpacRegistrations(), 1, 'Test for DeleteExpiredOpacRegistrations');
468 };
469
470 sub _find_member {
471     my ($resultset) = @_;
472     my $found = $resultset && grep( { $_->{cardnumber} && $_->{cardnumber} eq $CARDNUMBER } @$resultset );
473     return $found;
474 }
475
476 # Regression tests for BZ15343
477 my $password="";
478 ( $borrowernumber, $password ) = AddMember_Opac(surname=>"Dick",firstname=>'Philip',branchcode => $library2->{branchcode});
479 is( $password =~ /^[a-zA-Z]{10}$/ , 1, 'Test for autogenerated password if none submitted');
480 ( $borrowernumber, $password ) = AddMember_Opac(surname=>"Deckard",firstname=>"Rick",password=>"Nexus-6",branchcode => $library2->{branchcode});
481 is( $password eq "Nexus-6", 1, 'Test password used if submitted');
482 $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
483 my $hashed_up =  Koha::AuthUtils::hash_password("Nexus-6", $borrower->{password});
484 is( $borrower->{password} eq $hashed_up, 1, 'Check password hash equals hash of submitted password' );
485
486 subtest 'Trivial test for AddMember_Auto' => sub {
487     plan tests => 3;
488     my $members_mock = Test::MockModule->new( 'C4::Members' );
489     $members_mock->mock( 'fixup_cardnumber', sub { 12345; } );
490     my $library = $builder->build({ source => 'Branch' });
491     my $category = $builder->build({ source => 'Category' });
492     my %borr = AddMember_Auto( surname=> 'Dick3', firstname => 'Philip', branchcode => $library->{branchcode}, categorycode => $category->{categorycode}, password => '34567890' );
493     ok( $borr{borrowernumber}, 'Borrower hash contains borrowernumber' );
494     is( $borr{cardnumber}, 12345, 'Borrower hash contains cardnumber' );
495     my $patron = Koha::Patrons->find( $borr{borrowernumber} );
496     isnt( $patron, undef, 'Patron found' );
497 };
498
499 $schema->storage->txn_rollback;