Bug 15878 - Updated unit tests for hbyymmincr barcodes
[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 => 77;
21 use Test::MockModule;
22 use Data::Dumper;
23 use C4::Context;
24 use Koha::Database;
25 use Koha::List::Patron;
26
27
28 use t::lib::Mocks;
29 use t::lib::TestBuilder;
30
31 BEGIN {
32         use_ok('C4::Members');
33 }
34
35 my $schema = Koha::Database->schema;
36 $schema->storage->txn_begin;
37 my $builder = t::lib::TestBuilder->new;
38 my $dbh = C4::Context->dbh;
39 $dbh->{RaiseError} = 1;
40
41 my $library1 = $builder->build({
42     source => 'Branch',
43 });
44 my $library2 = $builder->build({
45     source => 'Branch',
46 });
47 my $CARDNUMBER   = 'TESTCARD01';
48 my $FIRSTNAME    = 'Marie';
49 my $SURNAME      = 'Mcknight';
50 my $CATEGORYCODE = 'S';
51 my $BRANCHCODE   = $library1->{branchcode};
52
53 my $CHANGED_FIRSTNAME = "Marry Ann";
54 my $EMAIL             = "Marie\@email.com";
55 my $EMAILPRO          = "Marie\@work.com";
56 my $PHONE             = "555-12123";
57
58 # XXX should be randomised and checked against the database
59 my $IMPOSSIBLE_CARDNUMBER = "XYZZZ999";
60
61 #my ($usernum, $userid, $usercnum, $userfirstname, $usersurname, $userbranch, $branchname, $userflags, $emailaddress, $branchprinter)= @_;
62 my @USERENV = (
63     1,
64     'test',
65     'MASTERTEST',
66     'Test',
67     'Test',
68     't',
69     'Test',
70     0,
71 );
72 my $BRANCH_IDX = 5;
73
74 C4::Context->_new_userenv ('DUMMY_SESSION_ID');
75 C4::Context->set_userenv ( @USERENV );
76
77 my $userenv = C4::Context->userenv
78   or BAIL_OUT("No userenv");
79
80 # Make a borrower for testing
81 my %data = (
82     cardnumber => $CARDNUMBER,
83     firstname =>  $FIRSTNAME,
84     surname => $SURNAME,
85     categorycode => $CATEGORYCODE,
86     branchcode => $BRANCHCODE,
87     dateofbirth => '',
88     dateexpiry => '9999-12-31',
89     userid => 'tomasito'
90 );
91
92 testAgeAccessors(\%data); #Age accessor tests don't touch the db so it is safe to run them with just the object.
93
94 my $addmem=AddMember(%data);
95 ok($addmem, "AddMember()");
96
97 # It's not really a Move, it's a Copy.
98 my $result = MoveMemberToDeleted($addmem);
99 ok($result,"MoveMemberToDeleted()");
100
101 my $sth = $dbh->prepare("SELECT * from borrowers WHERE borrowernumber=?");
102 $sth->execute($addmem);
103 my $MemberAdded = $sth->fetchrow_hashref;
104
105 $sth = $dbh->prepare("SELECT * from deletedborrowers WHERE borrowernumber=?");
106 $sth->execute($addmem);
107 my $MemberMoved = $sth->fetchrow_hashref;
108
109 is_deeply($MemberMoved,$MemberAdded,"Confirm MoveMemberToDeleted.");
110
111 my $member=GetMemberDetails("",$CARDNUMBER)
112   or BAIL_OUT("Cannot read member with card $CARDNUMBER");
113
114 ok ( $member->{firstname}    eq $FIRSTNAME    &&
115      $member->{surname}      eq $SURNAME      &&
116      $member->{categorycode} eq $CATEGORYCODE &&
117      $member->{branchcode}   eq $BRANCHCODE
118      , "Got member")
119   or diag("Mismatching member details: ".Dumper(\%data, $member));
120
121 is($member->{dateofbirth}, undef, "Empty dates handled correctly");
122
123 $member->{firstname} = $CHANGED_FIRSTNAME;
124 $member->{email}     = $EMAIL;
125 $member->{phone}     = $PHONE;
126 $member->{emailpro}  = $EMAILPRO;
127 ModMember(%$member);
128 my $changedmember=GetMemberDetails("",$CARDNUMBER);
129 ok ( $changedmember->{firstname} eq $CHANGED_FIRSTNAME &&
130      $changedmember->{email}     eq $EMAIL             &&
131      $changedmember->{phone}     eq $PHONE             &&
132      $changedmember->{emailpro}  eq $EMAILPRO
133      , "Member Changed")
134   or diag("Mismatching member details: ".Dumper($member, $changedmember));
135
136 t::lib::Mocks::mock_preference( 'CardnumberLength', '' );
137 C4::Context->clear_syspref_cache();
138
139 my $checkcardnum=C4::Members::checkcardnumber($CARDNUMBER, "");
140 is ($checkcardnum, "1", "Card No. in use");
141
142 $checkcardnum=C4::Members::checkcardnumber($IMPOSSIBLE_CARDNUMBER, "");
143 is ($checkcardnum, "0", "Card No. not used");
144
145 t::lib::Mocks::mock_preference( 'CardnumberLength', '4' );
146 C4::Context->clear_syspref_cache();
147
148 $checkcardnum=C4::Members::checkcardnumber($IMPOSSIBLE_CARDNUMBER, "");
149 is ($checkcardnum, "2", "Card number is too long");
150
151
152
153 t::lib::Mocks::mock_preference( 'AutoEmailPrimaryAddress', 'OFF' );
154 C4::Context->clear_syspref_cache();
155
156 my $notice_email = GetNoticeEmailAddress($member->{'borrowernumber'});
157 is ($notice_email, $EMAIL, "GetNoticeEmailAddress returns correct value when AutoEmailPrimaryAddress is off");
158
159 t::lib::Mocks::mock_preference( 'AutoEmailPrimaryAddress', 'emailpro' );
160 C4::Context->clear_syspref_cache();
161
162 $notice_email = GetNoticeEmailAddress($member->{'borrowernumber'});
163 is ($notice_email, $EMAILPRO, "GetNoticeEmailAddress returns correct value when AutoEmailPrimaryAddress is emailpro");
164
165 ok(!$member->{is_expired}, "GetMemberDetails() indicates that patron is not expired");
166 ModMember(borrowernumber => $member->{'borrowernumber'}, dateexpiry => '2001-01-1');
167 $member = GetMemberDetails($member->{'borrowernumber'});
168 ok($member->{is_expired}, "GetMemberDetails() indicates that patron is expired");
169
170 # clean up 
171 DelMember($member->{borrowernumber});
172 my $borrower = GetMember( cardnumber => $CARDNUMBER );
173 is( $borrower, undef, 'DelMember should remove the patron' );
174
175 # Check_Userid tests
176 %data = (
177     cardnumber   => "123456789",
178     firstname    => "Tomasito",
179     surname      => "None",
180     categorycode => "S",
181     branchcode   => $library2->{branchcode},
182     dateofbirth  => '',
183     debarred     => '',
184     dateexpiry   => '',
185     dateenrolled => '',
186 );
187 # Add a new borrower
188 my $borrowernumber = AddMember( %data );
189 is( Check_Userid( 'tomasito.non', $borrowernumber ), 1,
190     'recently created userid -> unique (borrowernumber passed)' );
191 is( Check_Userid( 'tomasitoxxx', $borrowernumber ), 1,
192     'non-existent userid -> unique (borrowernumber passed)' );
193 is( Check_Userid( 'tomasito.none', '' ), 0,
194     'userid exists (blank borrowernumber)' );
195 is( Check_Userid( 'tomasitoxxx', '' ), 1,
196     'non-existent userid -> unique (blank borrowernumber)' );
197
198 $borrower = GetMember( borrowernumber => $borrowernumber );
199 is( $borrower->{dateofbirth}, undef, 'AddMember should undef dateofbirth if empty string is given');
200 is( $borrower->{debarred}, undef, 'AddMember should undef debarred if empty string is given');
201 isnt( $borrower->{dateexpiry}, '0000-00-00', 'AddMember should not set dateexpiry to 0000-00-00 if empty string is given');
202 isnt( $borrower->{dateenrolled}, '0000-00-00', 'AddMember should not set dateenrolled to 0000-00-00 if empty string is given');
203
204 ModMember( borrowernumber => $borrowernumber, dateofbirth => '', debarred => '', dateexpiry => '', dateenrolled => '' );
205 $borrower = GetMember( borrowernumber => $borrowernumber );
206 is( $borrower->{dateofbirth}, undef, 'ModMember should undef dateofbirth if empty string is given');
207 is( $borrower->{debarred}, undef, 'ModMember should undef debarred if empty string is given');
208 isnt( $borrower->{dateexpiry}, '0000-00-00', 'ModMember should not set dateexpiry to 0000-00-00 if empty string is given');
209 isnt( $borrower->{dateenrolled}, '0000-00-00', 'ModMember should not set dateenrolled to 0000-00-00 if empty string is given');
210
211 ModMember( borrowernumber => $borrowernumber, dateofbirth => '1970-01-01', debarred => '2042-01-01', dateexpiry => '9999-12-31', dateenrolled => '2015-09-06' );
212 $borrower = GetMember( borrowernumber => $borrowernumber );
213 is( $borrower->{dateofbirth}, '1970-01-01', 'ModMember should correctly set dateofbirth if a valid date is given');
214 is( $borrower->{debarred}, '2042-01-01', 'ModMember should correctly set debarred if a valid date is given');
215 is( $borrower->{dateexpiry}, '9999-12-31', 'ModMember should correctly set dateexpiry if a valid date is given');
216 is( $borrower->{dateenrolled}, '2015-09-06', 'ModMember should correctly set dateenrolled if a valid date is given');
217
218 # Add a new borrower with the same userid but different cardnumber
219 $data{ cardnumber } = "987654321";
220 my $new_borrowernumber = AddMember( %data );
221 is( Check_Userid( 'tomasito.none', '' ), 0,
222     'userid not unique (blank borrowernumber)' );
223 is( Check_Userid( 'tomasito.none', $new_borrowernumber ), 0,
224     'userid not unique (second borrowernumber passed)' );
225 $borrower = GetMember( borrowernumber => $new_borrowernumber );
226 ok( $borrower->{userid} ne 'tomasito', "Borrower with duplicate userid has new userid generated" );
227
228 $data{ cardnumber } = "234567890";
229 $data{userid} = 'a_user_id';
230 $borrowernumber = AddMember( %data );
231 $borrower = GetMember( borrowernumber => $borrowernumber );
232 is( $borrower->{userid}, $data{userid}, 'AddMember should insert the given userid' );
233
234 subtest 'ModMember should not update userid if not true' => sub {
235     plan tests => 3;
236     ModMember( borrowernumber => $borrowernumber, firstname => 'Tomas', userid => '' );
237     $borrower = GetMember( borrowernumber => $borrowernumber );
238     is ( $borrower->{userid}, $data{userid}, 'ModMember should not update the userid with an empty string' );
239     ModMember( borrowernumber => $borrowernumber, firstname => 'Tomas', userid => 0 );
240     $borrower = GetMember( borrowernumber => $borrowernumber );
241     is ( $borrower->{userid}, $data{userid}, 'ModMember should not update the userid with an 0');
242     ModMember( borrowernumber => $borrowernumber, firstname => 'Tomas', userid => undef );
243     $borrower = GetMember( borrowernumber => $borrowernumber );
244     is ( $borrower->{userid}, $data{userid}, 'ModMember should not update the userid with an undefined value');
245 };
246
247 #Regression tests for bug 10612
248 my $library3 = $builder->build({
249     source => 'Branch',
250 });
251 $builder->build({
252         source => 'Category',
253         value => {
254             categorycode         => 'STAFFER',
255             description          => 'Staff dont batch del',
256             category_type        => 'S',
257         },
258 });
259
260 $builder->build({
261         source => 'Category',
262         value => {
263             categorycode         => 'CIVILIAN',
264             description          => 'Civilian batch del',
265             category_type        => 'A',
266         },
267 });
268
269 $builder->build({
270         source => 'Category',
271         value => {
272             categorycode         => 'KIDclamp',
273             description          => 'Kid to be guaranteed',
274             category_type        => 'C',
275         },
276 });
277
278 my $borrower1 = $builder->build({
279         source => 'Borrower',
280         value  => {
281             categorycode=>'STAFFER',
282             branchcode => $library3->{branchcode},
283             dateexpiry => '2015-01-01',
284         },
285 });
286 my $bor1inlist = $borrower1->{borrowernumber};
287 my $borrower2 = $builder->build({
288         source => 'Borrower',
289         value  => {
290             categorycode=>'STAFFER',
291             branchcode => $library3->{branchcode},
292             dateexpiry => '2015-01-01',
293         },
294 });
295
296 my $guarantee = $builder->build({
297         source => 'Borrower',
298         value  => {
299             categorycode=>'KIDclamp',
300             branchcode => $library3->{branchcode},
301             dateexpiry => '2015-01-01',
302         },
303 });
304
305 my $bor2inlist = $borrower2->{borrowernumber};
306
307 $builder->build({
308         source => 'OldIssue',
309         value  => {
310             borrowernumber => $bor2inlist,
311             timestamp => '2016-01-01',
312         },
313 });
314
315 my $owner = AddMember (categorycode => 'STAFFER', branchcode => $library2->{branchcode} );
316 my $list1 = AddPatronList( { name => 'Test List 1', owner => $owner } );
317 my @listpatrons = ($bor1inlist, $bor2inlist);
318 AddPatronsToList(  { list => $list1, borrowernumbers => \@listpatrons });
319 my $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id() } );
320 is( scalar(@$patstodel),0,'No staff deleted from list of all staff');
321 ModMember( borrowernumber => $bor2inlist, categorycode => 'CIVILIAN' );
322 $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id()} );
323 ok( scalar(@$patstodel)== 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Staff patron not deleted from list');
324 $patstodel = GetBorrowersToExpunge( {branchcode => $library3->{branchcode},patron_list_id => $list1->patron_list_id() } );
325 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Staff patron not deleted by branchcode and list');
326 $patstodel = GetBorrowersToExpunge( {expired_before => '2015-01-02', patron_list_id => $list1->patron_list_id() } );
327 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Staff patron not deleted by expirationdate and list');
328 $patstodel = GetBorrowersToExpunge( {not_borrowed_since => '2016-01-02', patron_list_id => $list1->patron_list_id() } );
329 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Staff patron not deleted by last issue date');
330
331 ModMember( borrowernumber => $bor1inlist, categorycode => 'CIVILIAN' );
332 ModMember( borrowernumber => $guarantee->{borrowernumber} ,guarantorid=>$bor1inlist );
333
334 $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id()} );
335 ok( scalar(@$patstodel)== 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Guarantor patron not deleted from list');
336 $patstodel = GetBorrowersToExpunge( {branchcode => $library3->{branchcode},patron_list_id => $list1->patron_list_id() } );
337 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Guarantor patron not deleted by branchcode and list');
338 $patstodel = GetBorrowersToExpunge( {expired_before => '2015-01-02', patron_list_id => $list1->patron_list_id() } );
339 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Guarantor patron not deleted by expirationdate and list');
340 $patstodel = GetBorrowersToExpunge( {not_borrowed_since => '2016-01-02', patron_list_id => $list1->patron_list_id() } );
341 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Guarantor patron not deleted by last issue date');
342 ModMember( borrowernumber => $guarantee->{borrowernumber}, guarantorid=>'' );
343
344 $builder->build({
345         source => 'Issue',
346         value  => {
347             borrowernumber => $bor2inlist,
348         },
349 });
350 $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id()} );
351 is( scalar(@$patstodel),1,'Borrower with issue not deleted from list');
352 $patstodel = GetBorrowersToExpunge( {branchcode => $library3->{branchcode},patron_list_id => $list1->patron_list_id() } );
353 is( scalar(@$patstodel),1,'Borrower with issue not deleted by branchcode and list');
354 $patstodel = GetBorrowersToExpunge( {category_code => 'CIVILIAN',patron_list_id => $list1->patron_list_id() } );
355 is( scalar(@$patstodel),1,'Borrower with issue not deleted by category_code and list');
356 $patstodel = GetBorrowersToExpunge( {expired_before => '2015-01-02',patron_list_id => $list1->patron_list_id() } );
357 is( scalar(@$patstodel),1,'Borrower with issue not deleted by expiration_date and list');
358 $builder->schema->resultset( 'Issue' )->delete_all;
359 $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id()} );
360 ok( scalar(@$patstodel)== 2,'Borrowers without issue deleted from list');
361 $patstodel = GetBorrowersToExpunge( {category_code => 'CIVILIAN',patron_list_id => $list1->patron_list_id() } );
362 is( scalar(@$patstodel),2,'Borrowers without issues deleted by category_code and list');
363 $patstodel = GetBorrowersToExpunge( {expired_before => '2015-01-02',patron_list_id => $list1->patron_list_id() } );
364 is( scalar(@$patstodel),2,'Borrowers without issues deleted by expiration_date and list');
365 $patstodel = GetBorrowersToExpunge( {not_borrowed_since => '2016-01-02', patron_list_id => $list1->patron_list_id() } );
366 is( scalar(@$patstodel),2,'Borrowers without issues deleted by last issue date');
367
368
369
370
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 => 10;
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     my $person = GetMemberDetails(undef,undef);
438     ok( !$person , 'Expected no member details from undef,undef' );
439     $person = GetMemberDetails(undef,'987654321');
440     is( $person->{amountoutstanding}, 15,
441         'Expected 15 outstanding for cardnumber.');
442     $borrowernumber = $person->{borrowernumber};
443     $person = GetMemberDetails($borrowernumber,undef);
444     is( $person->{amountoutstanding}, 15,
445         'Expected 15 outstanding for borrowernumber.');
446     $person = GetMemberDetails($borrowernumber,'987654321');
447     is( $person->{amountoutstanding}, 15,
448         'Expected 15 outstanding for both borrowernumber and cardnumber.');
449
450     # do not count holds charges
451     t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', '1' );
452     t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', '0' );
453     my ($total, $total_minus_charges,
454         $other_charges) = C4::Members::GetMemberAccountBalance(123);
455     is( $total, 15 , "Total calculated correctly");
456     is( $total_minus_charges, 15, "Holds charges are not count if HoldsInNoissuesCharge=1");
457     is( $other_charges, 0, "Holds charges are not considered if HoldsInNoissuesCharge=1");
458
459     t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', '0' );
460     ($total, $total_minus_charges,
461         $other_charges) = C4::Members::GetMemberAccountBalance(123);
462     is( $total, 15 , "Total calculated correctly");
463     is( $total_minus_charges, 10, "Holds charges are count if HoldsInNoissuesCharge=0");
464     is( $other_charges, 5, "Holds charges are considered if HoldsInNoissuesCharge=1");
465 };
466
467 subtest 'purgeSelfRegistration' => sub {
468     plan tests => 2;
469
470     #purge unverified
471     my $d=360;
472     C4::Members::DeleteUnverifiedOpacRegistrations($d);
473     foreach(1..3) {
474         $dbh->do("INSERT INTO borrower_modifications (timestamp, borrowernumber, verification_token) VALUES ('2014-01-01 01:02:03',0,?)", undef, (scalar localtime)."_$_");
475     }
476     is( C4::Members::DeleteUnverifiedOpacRegistrations($d), 3, 'Test for DeleteUnverifiedOpacRegistrations' );
477
478     #purge members in temporary category
479     my $c= 'XYZ';
480     $dbh->do("INSERT IGNORE INTO categories (categorycode) VALUES ('$c')");
481     t::lib::Mocks::mock_preference('PatronSelfRegistrationDefaultCategory', $c );
482     t::lib::Mocks::mock_preference('PatronSelfRegistrationExpireTemporaryAccountsDelay', 360);
483     C4::Members::DeleteExpiredOpacRegistrations();
484     $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});
485     is( C4::Members::DeleteExpiredOpacRegistrations(), 1, 'Test for DeleteExpiredOpacRegistrations');
486 };
487
488 sub _find_member {
489     my ($resultset) = @_;
490     my $found = $resultset && grep( { $_->{cardnumber} && $_->{cardnumber} eq $CARDNUMBER } @$resultset );
491     return $found;
492 }
493
494 # Regression tests for BZ15343
495 my $password="";
496 ( $borrowernumber, $password ) = AddMember_Opac(surname=>"Dick",firstname=>'Philip',branchcode => $library2->{branchcode});
497 is( $password =~ /^[a-zA-Z]{10}$/ , 1, 'Test for autogenerated password if none submitted');
498 ( $borrowernumber, $password ) = AddMember_Opac(surname=>"Deckard",firstname=>"Rick",password=>"Nexus-6",branchcode => $library2->{branchcode});
499 is( $password eq "Nexus-6", 1, 'Test password used if submitted');
500 $borrower = GetMember(borrowernumber => $borrowernumber);
501 my $hashed_up =  Koha::AuthUtils::hash_password("Nexus-6", $borrower->{password});
502 is( $borrower->{password} eq $hashed_up, 1, 'Check password hash equals hash of submitted password' );
503
504
505
506 ### ------------------------------------- ###
507 ### Testing GetAge() / SetAge() functions ###
508 ### ------------------------------------- ###
509 #USES the package $member-variable to mock a koha.borrowers-object
510 sub testAgeAccessors {
511     my ($member) = @_;
512     my $original_dateofbirth = $member->{dateofbirth};
513
514     ##Testing GetAge()
515     my $age=GetAge("1992-08-14", "2011-01-19");
516     is ($age, "18", "Age correct");
517
518     $age=GetAge("2011-01-19", "1992-01-19");
519     is ($age, "-19", "Birthday In the Future");
520
521     ##Testing SetAge() for now()
522     my $dt_now = DateTime->now();
523     $age = DateTime::Duration->new(years => 12, months => 6, days => 1);
524     C4::Members::SetAge( $member, $age );
525     $age = C4::Members::GetAge( $member->{dateofbirth} );
526     is ($age, '12', "SetAge 12 years");
527
528     $age = DateTime::Duration->new(years => 18, months => 12, days => 31);
529     C4::Members::SetAge( $member, $age );
530     $age = C4::Members::GetAge( $member->{dateofbirth} );
531     is ($age, '19', "SetAge 18+1 years"); #This is a special case, where months=>12 and days=>31 constitute one full year, hence we get age 19 instead of 18.
532
533     $age = DateTime::Duration->new(years => 18, months => 12, days => 30);
534     C4::Members::SetAge( $member, $age );
535     $age = C4::Members::GetAge( $member->{dateofbirth} );
536     is ($age, '19', "SetAge 18 years");
537
538     $age = DateTime::Duration->new(years => 0, months => 1, days => 1);
539     C4::Members::SetAge( $member, $age );
540     $age = C4::Members::GetAge( $member->{dateofbirth} );
541     is ($age, '0', "SetAge 0 years");
542
543     $age = '0018-12-31';
544     C4::Members::SetAge( $member, $age );
545     $age = C4::Members::GetAge( $member->{dateofbirth} );
546     is ($age, '19', "SetAge ISO_Date 18+1 years"); #This is a special case, where months=>12 and days=>31 constitute one full year, hence we get age 19 instead of 18.
547
548     $age = '0018-12-30';
549     C4::Members::SetAge( $member, $age );
550     $age = C4::Members::GetAge( $member->{dateofbirth} );
551     is ($age, '19', "SetAge ISO_Date 18 years");
552
553     $age = '18-1-1';
554     eval { C4::Members::SetAge( $member, $age ); };
555     is ((length $@ > 1), '1', "SetAge ISO_Date $age years FAILS");
556
557     $age = '0018-01-01';
558     eval { C4::Members::SetAge( $member, $age ); };
559     is ((length $@ == 0), '1', "SetAge ISO_Date $age years succeeds");
560
561     ##Testing SetAge() for relative_date
562     my $relative_date = DateTime->new(year => 3010, month => 3, day => 15);
563
564     $age = DateTime::Duration->new(years => 10, months => 3);
565     C4::Members::SetAge( $member, $age, $relative_date );
566     $age = C4::Members::GetAge( $member->{dateofbirth}, $relative_date->ymd() );
567     is ($age, '10', "SetAge, 10 years and 3 months old person was born on ".$member->{dateofbirth}." if todays is ".$relative_date->ymd());
568
569     $age = DateTime::Duration->new(years => 112, months => 1, days => 1);
570     C4::Members::SetAge( $member, $age, $relative_date );
571     $age = C4::Members::GetAge( $member->{dateofbirth}, $relative_date->ymd() );
572     is ($age, '112', "SetAge, 112 years, 1 months and 1 days old person was born on ".$member->{dateofbirth}." if today is ".$relative_date->ymd());
573
574     $age = '0112-01-01';
575     C4::Members::SetAge( $member, $age, $relative_date );
576     $age = C4::Members::GetAge( $member->{dateofbirth}, $relative_date->ymd() );
577     is ($age, '112', "SetAge ISO_Date, 112 years, 1 months and 1 days old person was born on ".$member->{dateofbirth}." if today is ".$relative_date->ymd());
578
579     $member->{dateofbirth} = $original_dateofbirth; #It is polite to revert made changes in the unit tests.
580 } #sub testAgeAccessors
581
582 # regression test for bug 16009
583 my $patron;
584 eval {
585     my $patron = GetMember(cardnumber => undef);
586 };
587 is($@, '', 'Bug 16009: GetMember(cardnumber => undef) works');
588 is($patron, undef, 'Bug 16009: GetMember(cardnumber => undef) returns undef');
589
590 1;