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