3 # This file is part of Koha.
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.
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.
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>.
20 use Test::More tests => 77;
25 use Koha::List::Patron;
29 use t::lib::TestBuilder;
32 use_ok('C4::Members');
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;
41 my $library1 = $builder->build({
44 my $library2 = $builder->build({
47 my $CARDNUMBER = 'TESTCARD01';
48 my $FIRSTNAME = 'Marie';
49 my $SURNAME = 'Mcknight';
50 my $CATEGORYCODE = 'S';
51 my $BRANCHCODE = $library1->{branchcode};
53 my $CHANGED_FIRSTNAME = "Marry Ann";
54 my $EMAIL = "Marie\@email.com";
55 my $EMAILPRO = "Marie\@work.com";
56 my $PHONE = "555-12123";
58 # XXX should be randomised and checked against the database
59 my $IMPOSSIBLE_CARDNUMBER = "XYZZZ999";
61 #my ($usernum, $userid, $usercnum, $userfirstname, $usersurname, $userbranch, $branchname, $userflags, $emailaddress, $branchprinter)= @_;
74 C4::Context->_new_userenv ('DUMMY_SESSION_ID');
75 C4::Context->set_userenv ( @USERENV );
77 my $userenv = C4::Context->userenv
78 or BAIL_OUT("No userenv");
80 # Make a borrower for testing
82 cardnumber => $CARDNUMBER,
83 firstname => $FIRSTNAME,
85 categorycode => $CATEGORYCODE,
86 branchcode => $BRANCHCODE,
88 dateexpiry => '9999-12-31',
92 testAgeAccessors(\%data); #Age accessor tests don't touch the db so it is safe to run them with just the object.
94 my $addmem=AddMember(%data);
95 ok($addmem, "AddMember()");
97 # It's not really a Move, it's a Copy.
98 my $result = MoveMemberToDeleted($addmem);
99 ok($result,"MoveMemberToDeleted()");
101 my $sth = $dbh->prepare("SELECT * from borrowers WHERE borrowernumber=?");
102 $sth->execute($addmem);
103 my $MemberAdded = $sth->fetchrow_hashref;
105 $sth = $dbh->prepare("SELECT * from deletedborrowers WHERE borrowernumber=?");
106 $sth->execute($addmem);
107 my $MemberMoved = $sth->fetchrow_hashref;
109 is_deeply($MemberMoved,$MemberAdded,"Confirm MoveMemberToDeleted.");
111 my $member=GetMemberDetails("",$CARDNUMBER)
112 or BAIL_OUT("Cannot read member with card $CARDNUMBER");
114 ok ( $member->{firstname} eq $FIRSTNAME &&
115 $member->{surname} eq $SURNAME &&
116 $member->{categorycode} eq $CATEGORYCODE &&
117 $member->{branchcode} eq $BRANCHCODE
119 or diag("Mismatching member details: ".Dumper(\%data, $member));
121 is($member->{dateofbirth}, undef, "Empty dates handled correctly");
123 $member->{firstname} = $CHANGED_FIRSTNAME;
124 $member->{email} = $EMAIL;
125 $member->{phone} = $PHONE;
126 $member->{emailpro} = $EMAILPRO;
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
134 or diag("Mismatching member details: ".Dumper($member, $changedmember));
136 t::lib::Mocks::mock_preference( 'CardnumberLength', '' );
137 C4::Context->clear_syspref_cache();
139 my $checkcardnum=C4::Members::checkcardnumber($CARDNUMBER, "");
140 is ($checkcardnum, "1", "Card No. in use");
142 $checkcardnum=C4::Members::checkcardnumber($IMPOSSIBLE_CARDNUMBER, "");
143 is ($checkcardnum, "0", "Card No. not used");
145 t::lib::Mocks::mock_preference( 'CardnumberLength', '4' );
146 C4::Context->clear_syspref_cache();
148 $checkcardnum=C4::Members::checkcardnumber($IMPOSSIBLE_CARDNUMBER, "");
149 is ($checkcardnum, "2", "Card number is too long");
153 t::lib::Mocks::mock_preference( 'AutoEmailPrimaryAddress', 'OFF' );
154 C4::Context->clear_syspref_cache();
156 my $notice_email = GetNoticeEmailAddress($member->{'borrowernumber'});
157 is ($notice_email, $EMAIL, "GetNoticeEmailAddress returns correct value when AutoEmailPrimaryAddress is off");
159 t::lib::Mocks::mock_preference( 'AutoEmailPrimaryAddress', 'emailpro' );
160 C4::Context->clear_syspref_cache();
162 $notice_email = GetNoticeEmailAddress($member->{'borrowernumber'});
163 is ($notice_email, $EMAILPRO, "GetNoticeEmailAddress returns correct value when AutoEmailPrimaryAddress is emailpro");
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");
171 DelMember($member->{borrowernumber});
172 my $borrower = GetMember( cardnumber => $CARDNUMBER );
173 is( $borrower, undef, 'DelMember should remove the patron' );
177 cardnumber => "123456789",
178 firstname => "Tomasito",
181 branchcode => $library2->{branchcode},
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)' );
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');
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');
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');
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" );
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' );
234 subtest 'ModMember should not update userid if not true' => sub {
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');
247 #Regression tests for bug 10612
248 my $library3 = $builder->build({
252 source => 'Category',
254 categorycode => 'STAFFER',
255 description => 'Staff dont batch del',
256 category_type => 'S',
261 source => 'Category',
263 categorycode => 'CIVILIAN',
264 description => 'Civilian batch del',
265 category_type => 'A',
270 source => 'Category',
272 categorycode => 'KIDclamp',
273 description => 'Kid to be guaranteed',
274 category_type => 'C',
278 my $borrower1 = $builder->build({
279 source => 'Borrower',
281 categorycode=>'STAFFER',
282 branchcode => $library3->{branchcode},
283 dateexpiry => '2015-01-01',
286 my $bor1inlist = $borrower1->{borrowernumber};
287 my $borrower2 = $builder->build({
288 source => 'Borrower',
290 categorycode=>'STAFFER',
291 branchcode => $library3->{branchcode},
292 dateexpiry => '2015-01-01',
296 my $guarantee = $builder->build({
297 source => 'Borrower',
299 categorycode=>'KIDclamp',
300 branchcode => $library3->{branchcode},
301 dateexpiry => '2015-01-01',
305 my $bor2inlist = $borrower2->{borrowernumber};
308 source => 'OldIssue',
310 borrowernumber => $bor2inlist,
311 timestamp => '2016-01-01',
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');
331 ModMember( borrowernumber => $bor1inlist, categorycode => 'CIVILIAN' );
332 ModMember( borrowernumber => $guarantee->{borrowernumber} ,guarantorid=>$bor1inlist );
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=>'' );
347 borrowernumber => $bor2inlist,
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');
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' );
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)');
388 subtest 'GetMemberAccountRecords' => sub {
392 my $borrowernumber = $builder->build({ source => 'Borrower' })->{ borrowernumber };
393 my $accountline_1 = $builder->build({
394 source => 'Accountline',
396 borrowernumber => $borrowernumber,
397 amountoutstanding => 64.60
401 my ($total,undef,undef) = GetMemberAccountRecords( $borrowernumber );
402 is( $total , 64.60, "Rounding works correctly in total calculation (single value)" );
404 my $accountline_2 = $builder->build({
405 source => 'Accountline',
407 borrowernumber => $borrowernumber,
408 amountoutstanding => 10.65
412 ($total,undef,undef) = GetMemberAccountRecords( $borrowernumber );
413 is( $total , 75.25, "Rounding works correctly in total calculation (multiple values)" );
417 subtest 'GetMemberAccountBalance' => sub {
421 my $members_mock = new Test::MockModule('C4::Members');
422 $members_mock->mock( 'GetMemberAccountRecords', sub {
423 my ($borrowernumber) = @_;
424 if ($borrowernumber) {
426 { amountoutstanding => '7', accounttype => 'Rent' },
427 { amountoutstanding => '5', accounttype => 'Res' },
428 { amountoutstanding => '3', accounttype => 'Pay' } );
429 return ( 15, \@accountlines );
433 return ( 0, \@accountlines );
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.');
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");
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");
467 subtest 'purgeSelfRegistration' => sub {
472 C4::Members::DeleteUnverifiedOpacRegistrations($d);
474 $dbh->do("INSERT INTO borrower_modifications (timestamp, borrowernumber, verification_token) VALUES ('2014-01-01 01:02:03',0,?)", undef, (scalar localtime)."_$_");
476 is( C4::Members::DeleteUnverifiedOpacRegistrations($d), 3, 'Test for DeleteUnverifiedOpacRegistrations' );
478 #purge members in temporary category
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');
489 my ($resultset) = @_;
490 my $found = $resultset && grep( { $_->{cardnumber} && $_->{cardnumber} eq $CARDNUMBER } @$resultset );
494 # Regression tests for BZ15343
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' );
506 ### ------------------------------------- ###
507 ### Testing GetAge() / SetAge() functions ###
508 ### ------------------------------------- ###
509 #USES the package $member-variable to mock a koha.borrowers-object
510 sub testAgeAccessors {
512 my $original_dateofbirth = $member->{dateofbirth};
515 my $age=GetAge("1992-08-14", "2011-01-19");
516 is ($age, "18", "Age correct");
518 $age=GetAge("2011-01-19", "1992-01-19");
519 is ($age, "-19", "Birthday In the Future");
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");
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.
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");
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");
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.
549 C4::Members::SetAge( $member, $age );
550 $age = C4::Members::GetAge( $member->{dateofbirth} );
551 is ($age, '19', "SetAge ISO_Date 18 years");
554 eval { C4::Members::SetAge( $member, $age ); };
555 is ((length $@ > 1), '1', "SetAge ISO_Date $age years FAILS");
558 eval { C4::Members::SetAge( $member, $age ); };
559 is ((length $@ == 0), '1', "SetAge ISO_Date $age years succeeds");
561 ##Testing SetAge() for relative_date
562 my $relative_date = DateTime->new(year => 3010, month => 3, day => 15);
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());
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());
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());
579 $member->{dateofbirth} = $original_dateofbirth; #It is polite to revert made changes in the unit tests.
580 } #sub testAgeAccessors
582 # regression test for bug 16009
585 my $patron = GetMember(cardnumber => undef);
587 is($@, '', 'Bug 16009: GetMember(cardnumber => undef) works');
588 is($patron, undef, 'Bug 16009: GetMember(cardnumber => undef) returns undef');