Bug 10612 - Unit 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 => 76;
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 #Regression tests for bug 10612
235 my $library3 = $builder->build({
236     source => 'Branch',
237 });
238 $builder->build({
239         source => 'Category',
240         value => {
241             categorycode         => 'STAFFER',
242             description          => 'Staff dont batch del',
243             category_type        => 'S',
244         },
245 });
246
247 $builder->build({
248         source => 'Category',
249         value => {
250             categorycode         => 'CIVILIAN',
251             description          => 'Civilian batch del',
252             category_type        => 'A',
253         },
254 });
255
256 $builder->build({
257         source => 'Category',
258         value => {
259             categorycode         => 'KIDclamp',
260             description          => 'Kid to be guaranteed',
261             category_type        => 'C',
262         },
263 });
264
265 #$builder->clear( { source => 'Borrower' } );
266 my $borrower1 = $builder->build({
267         source => 'Borrower',
268         value  => {
269             categorycode=>'STAFFER',
270             branchcode => $library3->{branchcode},
271             dateexpiry => '2015-01-01',
272         },
273 });
274 my $bor1inlist = $borrower1->{borrowernumber};
275 my $borrower2 = $builder->build({
276         source => 'Borrower',
277         value  => {
278             categorycode=>'STAFFER',
279             branchcode => $library3->{branchcode},
280             dateexpiry => '2015-01-01',
281         },
282 });
283
284 my $guarantee = $builder->build({
285         source => 'Borrower',
286         value  => {
287             categorycode=>'KIDclamp',
288             branchcode => $library3->{branchcode},
289             dateexpiry => '2015-01-01',
290         },
291 });
292
293 my $bor2inlist = $borrower2->{borrowernumber};
294
295 $builder->build({
296         source => 'OldIssue',
297         value  => {
298             borrowernumber => $bor2inlist,
299             timestamp => '2016-01-01',
300         },
301 });
302
303 my $owner = AddMember (categorycode => 'STAFFER', branchcode => $library2->{branchcode} );
304 my $list1 = AddPatronList( { name => 'Test List 1', owner => $owner } );
305 my @listpatrons = ($bor1inlist, $bor2inlist);
306 AddPatronsToList(  { list => $list1, borrowernumbers => \@listpatrons });
307 my $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id() } );
308 is( scalar(@$patstodel),0,'No staff deleted from list of all staff');
309 ModMember( borrowernumber => $bor2inlist, categorycode => 'CIVILIAN' );
310 $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id()} );
311 ok( scalar(@$patstodel)== 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Staff patron not deleted from list');
312 $patstodel = GetBorrowersToExpunge( {branchcode => $library3->{branchcode},patron_list_id => $list1->patron_list_id() } );
313 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Staff patron not deleted by branchcode and list');
314 $patstodel = GetBorrowersToExpunge( {expired_before => '2015-01-02', patron_list_id => $list1->patron_list_id() } );
315 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Staff patron not deleted by expirationdate and list');
316 $patstodel = GetBorrowersToExpunge( {not_borrowered_since => '2016-01-02', patron_list_id => $list1->patron_list_id() } );
317 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Staff patron not deleted by last issue date');
318
319 ModMember( borrowernumber => $bor1inlist, categorycode => 'CIVILIAN' );
320 ModMember( borrowernumber => $guarantee->{borrowernumber} ,guarantorid=>$bor1inlist );
321
322 $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id()} );
323 ok( scalar(@$patstodel)== 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Guarantor 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,'Guarantor 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,'Guarantor patron not deleted by expirationdate and list');
328 $patstodel = GetBorrowersToExpunge( {not_borrowered_since => '2016-01-02', patron_list_id => $list1->patron_list_id() } );
329 ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Guarantor patron not deleted by last issue date');
330 ModMember( borrowernumber => $guarantee->{borrowernumber}, guarantorid=>'' );
331
332 $builder->build({
333         source => 'Issue',
334         value  => {
335             borrowernumber => $bor2inlist,
336         },
337 });
338 $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id()} );
339 is( scalar(@$patstodel),1,'Borrower with issue not deleted from list');
340 $patstodel = GetBorrowersToExpunge( {branchcode => $library3->{branchcode},patron_list_id => $list1->patron_list_id() } );
341 is( scalar(@$patstodel),1,'Borrower with issue not deleted by branchcode and list');
342 $patstodel = GetBorrowersToExpunge( {category_code => 'CIVILIAN',patron_list_id => $list1->patron_list_id() } );
343 is( scalar(@$patstodel),1,'Borrower with issue not 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),1,'Borrower with issue not deleted by expiration_date and list');
346 $builder->clear( { source => 'Issue' } );
347 $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id()} );
348 ok( scalar(@$patstodel)== 2,'Borrowers without issue deleted from list');
349 $patstodel = GetBorrowersToExpunge( {category_code => 'CIVILIAN',patron_list_id => $list1->patron_list_id() } );
350 is( scalar(@$patstodel),2,'Borrowers without issues deleted by category_code and list');
351 $patstodel = GetBorrowersToExpunge( {expired_before => '2015-01-02',patron_list_id => $list1->patron_list_id() } );
352 is( scalar(@$patstodel),2,'Borrowers without issues deleted by expiration_date and list');
353 $patstodel = GetBorrowersToExpunge( {not_borrowered_since => '2016-01-02', patron_list_id => $list1->patron_list_id() } );
354 is( scalar(@$patstodel),2,'Borrowers without issues deleted by last issue date');
355
356
357
358
359
360 # Regression tests for BZ13502
361 ## Remove all entries with userid='' (should be only 1 max)
362 $dbh->do(q|DELETE FROM borrowers WHERE userid = ''|);
363 ## And create a patron with a userid=''
364 $borrowernumber = AddMember( categorycode => 'S', branchcode => $library2->{branchcode} );
365 $dbh->do(q|UPDATE borrowers SET userid = '' WHERE borrowernumber = ?|, undef, $borrowernumber);
366 # Create another patron and verify the userid has been generated
367 $borrowernumber = AddMember( categorycode => 'S', branchcode => $library2->{branchcode} );
368 ok( $borrowernumber > 0, 'AddMember should have inserted the patron even if no userid is given' );
369 $borrower = GetMember( borrowernumber => $borrowernumber );
370 ok( $borrower->{userid},  'A userid should have been generated correctly' );
371
372 # Regression tests for BZ12226
373 is( Check_Userid( C4::Context->config('user'), '' ), 0,
374     'Check_Userid should return 0 for the DB user (Bug 12226)');
375
376 subtest 'GetMemberAccountRecords' => sub {
377
378     plan tests => 2;
379
380     my $borrowernumber = $builder->build({ source => 'Borrower' })->{ borrowernumber };
381     my $accountline_1  = $builder->build({
382         source => 'Accountline',
383         value  => {
384             borrowernumber    => $borrowernumber,
385             amountoutstanding => 64.60
386         }
387     });
388
389     my ($total,undef,undef) = GetMemberAccountRecords( $borrowernumber );
390     is( $total , 64.60, "Rounding works correctly in total calculation (single value)" );
391
392     my $accountline_2 = $builder->build({
393         source => 'Accountline',
394         value  => {
395             borrowernumber    => $borrowernumber,
396             amountoutstanding => 10.65
397         }
398     });
399
400     ($total,undef,undef) = GetMemberAccountRecords( $borrowernumber );
401     is( $total , 75.25, "Rounding works correctly in total calculation (multiple values)" );
402
403 };
404
405 subtest 'GetMemberAccountBalance' => sub {
406
407     plan tests => 10;
408
409     my $members_mock = new Test::MockModule('C4::Members');
410     $members_mock->mock( 'GetMemberAccountRecords', sub {
411         my ($borrowernumber) = @_;
412         if ($borrowernumber) {
413             my @accountlines = (
414             { amountoutstanding => '7', accounttype => 'Rent' },
415             { amountoutstanding => '5', accounttype => 'Res' },
416             { amountoutstanding => '3', accounttype => 'Pay' } );
417             return ( 15, \@accountlines );
418         }
419         else {
420             my @accountlines;
421             return ( 0, \@accountlines );
422         }
423     });
424
425     my $person = GetMemberDetails(undef,undef);
426     ok( !$person , 'Expected no member details from undef,undef' );
427     $person = GetMemberDetails(undef,'987654321');
428     is( $person->{amountoutstanding}, 15,
429         'Expected 15 outstanding for cardnumber.');
430     $borrowernumber = $person->{borrowernumber};
431     $person = GetMemberDetails($borrowernumber,undef);
432     is( $person->{amountoutstanding}, 15,
433         'Expected 15 outstanding for borrowernumber.');
434     $person = GetMemberDetails($borrowernumber,'987654321');
435     is( $person->{amountoutstanding}, 15,
436         'Expected 15 outstanding for both borrowernumber and cardnumber.');
437
438     # do not count holds charges
439     t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', '1' );
440     t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', '0' );
441     my ($total, $total_minus_charges,
442         $other_charges) = C4::Members::GetMemberAccountBalance(123);
443     is( $total, 15 , "Total calculated correctly");
444     is( $total_minus_charges, 15, "Holds charges are not count if HoldsInNoissuesCharge=1");
445     is( $other_charges, 0, "Holds charges are not considered if HoldsInNoissuesCharge=1");
446
447     t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', '0' );
448     ($total, $total_minus_charges,
449         $other_charges) = C4::Members::GetMemberAccountBalance(123);
450     is( $total, 15 , "Total calculated correctly");
451     is( $total_minus_charges, 10, "Holds charges are count if HoldsInNoissuesCharge=0");
452     is( $other_charges, 5, "Holds charges are considered if HoldsInNoissuesCharge=1");
453 };
454
455 subtest 'purgeSelfRegistration' => sub {
456     plan tests => 2;
457
458     #purge unverified
459     my $d=360;
460     C4::Members::DeleteUnverifiedOpacRegistrations($d);
461     foreach(1..3) {
462         $dbh->do("INSERT INTO borrower_modifications (timestamp, borrowernumber, verification_token) VALUES ('2014-01-01 01:02:03',0,?)", undef, (scalar localtime)."_$_");
463     }
464     is( C4::Members::DeleteUnverifiedOpacRegistrations($d), 3, 'Test for DeleteUnverifiedOpacRegistrations' );
465
466     #purge members in temporary category
467     my $c= 'XYZ';
468     $dbh->do("INSERT IGNORE INTO categories (categorycode) VALUES ('$c')");
469     t::lib::Mocks::mock_preference('PatronSelfRegistrationDefaultCategory', $c );
470     t::lib::Mocks::mock_preference('PatronSelfRegistrationExpireTemporaryAccountsDelay', 360);
471     C4::Members::DeleteExpiredOpacRegistrations();
472     $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});
473     is( C4::Members::DeleteExpiredOpacRegistrations(), 1, 'Test for DeleteExpiredOpacRegistrations');
474 };
475
476 sub _find_member {
477     my ($resultset) = @_;
478     my $found = $resultset && grep( { $_->{cardnumber} && $_->{cardnumber} eq $CARDNUMBER } @$resultset );
479     return $found;
480 }
481
482 # Regression tests for BZ15343
483 my $password="";
484 ( $borrowernumber, $password ) = AddMember_Opac(surname=>"Dick",firstname=>'Philip',branchcode => $library2->{branchcode});
485 is( $password =~ /^[a-zA-Z]{10}$/ , 1, 'Test for autogenerated password if none submitted');
486 ( $borrowernumber, $password ) = AddMember_Opac(surname=>"Deckard",firstname=>"Rick",password=>"Nexus-6",branchcode => $library2->{branchcode});
487 is( $password eq "Nexus-6", 1, 'Test password used if submitted');
488 $borrower = GetMember(borrowernumber => $borrowernumber);
489 my $hashed_up =  Koha::AuthUtils::hash_password("Nexus-6", $borrower->{password});
490 is( $borrower->{password} eq $hashed_up, 1, 'Check password hash equals hash of submitted password' );
491
492
493
494 ### ------------------------------------- ###
495 ### Testing GetAge() / SetAge() functions ###
496 ### ------------------------------------- ###
497 #USES the package $member-variable to mock a koha.borrowers-object
498 sub testAgeAccessors {
499     my ($member) = @_;
500     my $original_dateofbirth = $member->{dateofbirth};
501
502     ##Testing GetAge()
503     my $age=GetAge("1992-08-14", "2011-01-19");
504     is ($age, "18", "Age correct");
505
506     $age=GetAge("2011-01-19", "1992-01-19");
507     is ($age, "-19", "Birthday In the Future");
508
509     ##Testing SetAge() for now()
510     my $dt_now = DateTime->now();
511     $age = DateTime::Duration->new(years => 12, months => 6, days => 1);
512     C4::Members::SetAge( $member, $age );
513     $age = C4::Members::GetAge( $member->{dateofbirth} );
514     is ($age, '12', "SetAge 12 years");
515
516     $age = DateTime::Duration->new(years => 18, months => 12, days => 31);
517     C4::Members::SetAge( $member, $age );
518     $age = C4::Members::GetAge( $member->{dateofbirth} );
519     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.
520
521     $age = DateTime::Duration->new(years => 18, months => 12, days => 30);
522     C4::Members::SetAge( $member, $age );
523     $age = C4::Members::GetAge( $member->{dateofbirth} );
524     is ($age, '19', "SetAge 18 years");
525
526     $age = DateTime::Duration->new(years => 0, months => 1, days => 1);
527     C4::Members::SetAge( $member, $age );
528     $age = C4::Members::GetAge( $member->{dateofbirth} );
529     is ($age, '0', "SetAge 0 years");
530
531     $age = '0018-12-31';
532     C4::Members::SetAge( $member, $age );
533     $age = C4::Members::GetAge( $member->{dateofbirth} );
534     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.
535
536     $age = '0018-12-30';
537     C4::Members::SetAge( $member, $age );
538     $age = C4::Members::GetAge( $member->{dateofbirth} );
539     is ($age, '19', "SetAge ISO_Date 18 years");
540
541     $age = '18-1-1';
542     eval { C4::Members::SetAge( $member, $age ); };
543     is ((length $@ > 1), '1', "SetAge ISO_Date $age years FAILS");
544
545     $age = '0018-01-01';
546     eval { C4::Members::SetAge( $member, $age ); };
547     is ((length $@ == 0), '1', "SetAge ISO_Date $age years succeeds");
548
549     ##Testing SetAge() for relative_date
550     my $relative_date = DateTime->new(year => 3010, month => 3, day => 15);
551
552     $age = DateTime::Duration->new(years => 10, months => 3);
553     C4::Members::SetAge( $member, $age, $relative_date );
554     $age = C4::Members::GetAge( $member->{dateofbirth}, $relative_date->ymd() );
555     is ($age, '10', "SetAge, 10 years and 3 months old person was born on ".$member->{dateofbirth}." if todays is ".$relative_date->ymd());
556
557     $age = DateTime::Duration->new(years => 112, months => 1, days => 1);
558     C4::Members::SetAge( $member, $age, $relative_date );
559     $age = C4::Members::GetAge( $member->{dateofbirth}, $relative_date->ymd() );
560     is ($age, '112', "SetAge, 112 years, 1 months and 1 days old person was born on ".$member->{dateofbirth}." if today is ".$relative_date->ymd());
561
562     $age = '0112-01-01';
563     C4::Members::SetAge( $member, $age, $relative_date );
564     $age = C4::Members::GetAge( $member->{dateofbirth}, $relative_date->ymd() );
565     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());
566
567     $member->{dateofbirth} = $original_dateofbirth; #It is polite to revert made changes in the unit tests.
568 } #sub testAgeAccessors
569
570 # regression test for bug 16009
571 my $patron;
572 eval {
573     my $patron = GetMember(cardnumber => undef);
574 };
575 is($@, '', 'Bug 16009: GetMember(cardnumber => undef) works');
576 is($patron, undef, 'Bug 16009: GetMember(cardnumber => undef) returns undef');
577
578 1;