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 => 72;
26 use_ok('C4::Members');
29 my $dbh = C4::Context->dbh;
32 $dbh->{AutoCommit} = 0;
33 $dbh->{RaiseError} = 1;
35 my $CARDNUMBER = 'TESTCARD01';
36 my $FIRSTNAME = 'Marie';
37 my $SURNAME = 'Mcknight';
38 my $CATEGORYCODE = 'S';
39 my $BRANCHCODE = 'CPL';
41 my $CHANGED_FIRSTNAME = "Marry Ann";
42 my $EMAIL = "Marie\@email.com";
43 my $EMAILPRO = "Marie\@work.com";
44 my $ETHNICITY = "German";
45 my $PHONE = "555-12123";
47 # XXX should be randomised and checked against the database
48 my $IMPOSSIBLE_CARDNUMBER = "XYZZZ999";
50 my $INDEPENDENT_BRANCHES_PREF = 'IndependentBranches';
52 # XXX make a non-commit transaction and rollback rather than insert/delete
54 #my ($usernum, $userid, $usercnum, $userfirstname, $usersurname, $userbranch, $branchname, $userflags, $emailaddress, $branchprinter)= @_;
67 C4::Context->_new_userenv ('DUMMY_SESSION_ID');
68 C4::Context->set_userenv ( @USERENV );
70 my $userenv = C4::Context->userenv
71 or BAIL_OUT("No userenv");
73 # Make a borrower for testing
75 cardnumber => $CARDNUMBER,
76 firstname => $FIRSTNAME,
78 categorycode => $CATEGORYCODE,
79 branchcode => $BRANCHCODE,
81 dateexpiry => '9999-12-31',
85 testAgeAccessors(\%data); #Age accessor tests don't touch the db so it is safe to run them with just the object.
87 my $addmem=AddMember(%data);
88 ok($addmem, "AddMember()");
90 # It's not really a Move, it's a Copy.
91 my $result = MoveMemberToDeleted($addmem);
92 ok($result,"MoveMemberToDeleted()");
94 my $sth = $dbh->prepare("SELECT * from borrowers WHERE borrowernumber=?");
95 $sth->execute($addmem);
96 my $MemberAdded = $sth->fetchrow_hashref;
98 $sth = $dbh->prepare("SELECT * from deletedborrowers WHERE borrowernumber=?");
99 $sth->execute($addmem);
100 my $MemberMoved = $sth->fetchrow_hashref;
102 is_deeply($MemberMoved,$MemberAdded,"Confirm MoveMemberToDeleted.");
104 my $member=GetMemberDetails("",$CARDNUMBER)
105 or BAIL_OUT("Cannot read member with card $CARDNUMBER");
107 ok ( $member->{firstname} eq $FIRSTNAME &&
108 $member->{surname} eq $SURNAME &&
109 $member->{categorycode} eq $CATEGORYCODE &&
110 $member->{branchcode} eq $BRANCHCODE
112 or diag("Mismatching member details: ".Dumper(\%data, $member));
114 is($member->{dateofbirth}, undef, "Empty dates handled correctly");
116 $member->{firstname} = $CHANGED_FIRSTNAME;
117 $member->{email} = $EMAIL;
118 $member->{ethnicity} = $ETHNICITY;
119 $member->{phone} = $PHONE;
120 $member->{emailpro} = $EMAILPRO;
122 my $changedmember=GetMemberDetails("",$CARDNUMBER);
123 ok ( $changedmember->{firstname} eq $CHANGED_FIRSTNAME &&
124 $changedmember->{email} eq $EMAIL &&
125 $changedmember->{ethnicity} eq $ETHNICITY &&
126 $changedmember->{phone} eq $PHONE &&
127 $changedmember->{emailpro} eq $EMAILPRO
129 or diag("Mismatching member details: ".Dumper($member, $changedmember));
131 C4::Context->set_preference( $INDEPENDENT_BRANCHES_PREF, '0' );
132 C4::Context->clear_syspref_cache();
134 my $results = Search($CARDNUMBER);
135 ok (@$results == 1, "Search cardnumber returned only one result")
136 or diag("Multiple members with Card $CARDNUMBER: ".Dumper($results));
137 ok (_find_member($results), "Search cardnumber")
138 or diag("Card $CARDNUMBER not found in the resultset: ".Dumper($results));
140 my @searchstring=($SURNAME);
141 $results = Search(\@searchstring);
142 ok (_find_member($results), "Search (arrayref)")
143 or diag("Card $CARDNUMBER not found in the resultset: ".Dumper($results));
145 $results = Search(\@searchstring,undef,undef,undef,["surname"]);
146 ok (_find_member($results), "Surname Search (arrayref)")
147 or diag("Card $CARDNUMBER not found in the resultset: ".Dumper($results));
149 $results = Search("$CHANGED_FIRSTNAME $SURNAME", "surname");
150 ok (_find_member($results), "Full name Search (string)")
151 or diag("Card $CARDNUMBER not found in the resultset: ".Dumper($results));
153 @searchstring=($PHONE);
154 $results = Search(\@searchstring,undef,undef,undef,["phone"]);
155 ok (_find_member($results), "Phone Search (arrayref)")
156 or diag("Card $CARDNUMBER not found in the resultset: ".Dumper($results));
158 $results = Search($PHONE,undef,undef,undef,["phone"]);
159 ok (_find_member($results), "Phone Search (string)")
160 or diag("Card $CARDNUMBER not found in the resultset: ".Dumper($results));
162 C4::Context->set_preference( $INDEPENDENT_BRANCHES_PREF, '1' );
163 C4::Context->clear_syspref_cache();
165 $results = Search("$CHANGED_FIRSTNAME $SURNAME", "surname");
166 ok (!_find_member($results), "Full name Search (string) for independent branches, different branch")
167 or diag("Card $CARDNUMBER found in the resultset for independent branches: ".Dumper(C4::Context->preference($INDEPENDENT_BRANCHES_PREF), $results));
169 @searchstring=($SURNAME);
170 $results = Search(\@searchstring);
171 ok (!_find_member($results), "Search (arrayref) for independent branches, different branch")
172 or diag("Card $CARDNUMBER found in the resultset for independent branches: ".Dumper(C4::Context->preference($INDEPENDENT_BRANCHES_PREF), $results));
174 $USERENV[$BRANCH_IDX] = $BRANCHCODE;
175 C4::Context->set_userenv ( @USERENV );
177 $results = Search("$CHANGED_FIRSTNAME $SURNAME", "surname");
178 ok (_find_member($results), "Full name Search (string) for independent branches, same branch")
179 or diag("Card $CARDNUMBER not found in the resultset for independent branches: ".Dumper(C4::Context->preference($INDEPENDENT_BRANCHES_PREF), $results));
181 @searchstring=($SURNAME);
182 $results = Search(\@searchstring);
183 ok (_find_member($results), "Search (arrayref) for independent branches, same branch")
184 or diag("Card $CARDNUMBER not found in the resultset for independent branches: ".Dumper(C4::Context->preference($INDEPENDENT_BRANCHES_PREF), $results));
186 C4::Context->set_preference( 'CardnumberLength', '' );
187 C4::Context->clear_syspref_cache();
189 my $checkcardnum=C4::Members::checkcardnumber($CARDNUMBER, "");
190 is ($checkcardnum, "1", "Card No. in use");
192 $checkcardnum=C4::Members::checkcardnumber($IMPOSSIBLE_CARDNUMBER, "");
193 is ($checkcardnum, "0", "Card No. not used");
195 C4::Context->set_preference( 'CardnumberLength', '4' );
196 C4::Context->clear_syspref_cache();
198 $checkcardnum=C4::Members::checkcardnumber($IMPOSSIBLE_CARDNUMBER, "");
199 is ($checkcardnum, "2", "Card number is too long");
203 C4::Context->set_preference( 'AutoEmailPrimaryAddress', 'OFF' );
204 C4::Context->clear_syspref_cache();
206 my $notice_email = GetNoticeEmailAddress($member->{'borrowernumber'});
207 is ($notice_email, $EMAIL, "GetNoticeEmailAddress returns correct value when AutoEmailPrimaryAddress is off");
209 C4::Context->set_preference( 'AutoEmailPrimaryAddress', 'emailpro' );
210 C4::Context->clear_syspref_cache();
212 $notice_email = GetNoticeEmailAddress($member->{'borrowernumber'});
213 is ($notice_email, $EMAILPRO, "GetNoticeEmailAddress returns correct value when AutoEmailPrimaryAddress is emailpro");
215 ok(!$member->{is_expired}, "GetMemberDetails() indicates that patron is not expired");
216 ModMember(borrowernumber => $member->{'borrowernumber'}, dateexpiry => '2001-01-1');
217 $member = GetMemberDetails($member->{'borrowernumber'});
218 ok($member->{is_expired}, "GetMemberDetails() indicates that patron is expired");
221 my $message_type = 'B';
222 my $message = 'my message';
223 my $messages_count = GetMessagesCount($member->{borrowernumber}, $message_type, $BRANCHCODE);
224 is( $messages_count, 0, 'GetMessagesCount returns the number of messages correclty' );
226 is( AddMessage(), undef, 'AddMessage without argument returns undef' );
227 is( AddMessage(undef, $message_type, $message, $BRANCHCODE), undef, 'AddMessage without the borrower number returns undef' );
228 is( AddMessage($member->{borrowernumber}, undef, $message, $BRANCHCODE), undef, 'AddMessage without the message type returns undef' );
229 is( AddMessage($member->{borrowernumber}, $message_type, undef, $BRANCHCODE), undef, 'AddMessage without the message returns undef' );
230 is( AddMessage($member->{borrowernumber}, $message_type, $message, undef), undef, 'AddMessage without the branch code returns undef' );
231 is( AddMessage($member->{borrowernumber}, $message_type, $message, $BRANCHCODE), 1, 'AddMessage functions correctly' );
233 $messages_count = GetMessagesCount();
234 is( $messages_count, 0, 'GetMessagesCount without argument returns 0' );
235 $messages_count = GetMessagesCount(undef, $message_type, $BRANCHCODE);
236 is( $messages_count, '0', 'GetMessagesCount without the borrower number returns the number of messages' );
237 $messages_count = GetMessagesCount($member->{borrowernumber}, undef, $BRANCHCODE);
238 is( $messages_count, '1', 'GetMessagesCount without the message type returns the total number of messages' );
239 $messages_count = GetMessagesCount($member->{borrowernumber}, $message_type, undef);
240 is( $messages_count, '1', 'GetMessagesCount without the branchcode returns the total number of messages' );
241 $messages_count = GetMessagesCount($member->{borrowernumber}, $message_type, $BRANCHCODE);
242 is( $messages_count, '1', 'GetMessagesCount returns the number of messages correctly' );
244 my $messages = GetMessages();
245 is( @$messages, 0, 'GetMessages without argument returns 0' );
246 $messages = GetMessages($member->{borrowernumber}, $message_type, $BRANCHCODE);
247 is( @$messages, 1, 'GetMessages returns the correct number of messages' );
248 is( $messages->[0]->{borrowernumber}, $member->{borrowernumber}, 'GetMessages returns the borrower number correctly' );
249 is( $messages->[0]->{message_type}, $message_type, 'GetMessages returns the message type correclty' );
250 is( $messages->[0]->{message}, $message, 'GetMessages returns the message correctly' );
251 is( $messages->[0]->{branchcode}, $BRANCHCODE, 'GetMessages returns the branch code correctly' );
253 $messages_count = GetMessagesCount($member->{borrowernumber}, $message_type, $BRANCHCODE);
254 is( $messages_count, 1, 'GetMessagesCount returns the number of messages correclty' );
257 $messages = GetMessages($member->{borrowernumber}, $message_type, $BRANCHCODE);
258 is( @$messages, 1, 'DeleteMessage without message id does not delete messages' );
259 DeleteMessage($messages->[0]->{message_id});
260 $messages = GetMessages($member->{borrowernumber}, $message_type, $BRANCHCODE);
261 is( @$messages, 0, 'DeleteMessage deletes a message correctly' );
265 DelMember($member->{borrowernumber});
266 $results = Search($CARDNUMBER,undef,undef,undef,["cardnumber"]);
267 ok (!_find_member($results), "Delete member")
268 or diag("Card $CARDNUMBER found for the deleted member in the resultset: ".Dumper($results));
272 cardnumber => "123456789",
273 firstname => "Tomasito",
278 dateexpiry => '9999-12-31',
281 my $borrowernumber = AddMember( %data );
282 is( Check_Userid( 'tomasito.non', $borrowernumber ), 1,
283 'recently created userid -> unique (borrowernumber passed)' );
284 is( Check_Userid( 'tomasitoxxx', $borrowernumber ), 1,
285 'non-existent userid -> unique (borrowernumber passed)' );
286 is( Check_Userid( 'tomasito.none', '' ), 0,
287 'userid exists (blank borrowernumber)' );
288 is( Check_Userid( 'tomasitoxxx', '' ), 1,
289 'non-existent userid -> unique (blank borrowernumber)' );
291 # Add a new borrower with the same userid but different cardnumber
292 $data{ cardnumber } = "987654321";
293 my $new_borrowernumber = AddMember( %data );
294 is( Check_Userid( 'tomasito.none', '' ), 0,
295 'userid not unique (blank borrowernumber)' );
296 is( Check_Userid( 'tomasito.none', $new_borrowernumber ), 0,
297 'userid not unique (second borrowernumber passed)' );
298 my $borrower = GetMember( borrowernumber => $new_borrowernumber );
299 ok( $borrower->{userid} ne 'tomasito', "Borrower with duplicate userid has new userid generated" );
301 $data{ cardnumber } = "234567890";
302 $data{userid} = 'a_user_id';
303 $borrowernumber = AddMember( %data );
304 $borrower = GetMember( borrowernumber => $borrowernumber );
305 is( $borrower->{userid}, $data{userid}, 'AddMember should insert the given userid' );
307 # Regression tests for BZ13502
308 ## Remove all entries with userid='' (should be only 1 max)
309 $dbh->do(q|DELETE FROM borrowers WHERE userid = ''|);
310 ## And create a patron with a userid=''
311 $borrowernumber = AddMember( categorycode => 'S', branchcode => 'MPL' );
312 $dbh->do(q|UPDATE borrowers SET userid = '' WHERE borrowernumber = ?|, undef, $borrowernumber);
313 # Create another patron and verify the userid has been generated
314 $borrowernumber = AddMember( categorycode => 'S', branchcode => 'MPL' );
315 ok( $borrowernumber > 0, 'AddMember should have inserted the patron even if no userid is given' );
316 $borrower = GetMember( borrowernumber => $borrowernumber );
317 ok( $borrower->{userid}, 'A userid should have been generated correctly' );
319 # Regression tests for BZ12226
320 is( Check_Userid( C4::Context->config('user'), '' ), 0,
321 'Check_Userid should return 0 for the DB user (Bug 12226)');
323 subtest 'GetMemberAccountBalance' => sub {
327 my $members_mock = new Test::MockModule('C4::Members');
328 $members_mock->mock( 'GetMemberAccountRecords', sub {
329 my ($borrowernumber) = @_;
330 if ($borrowernumber) {
332 { amountoutstanding => '7', accounttype => 'Rent' },
333 { amountoutstanding => '5', accounttype => 'Res' },
334 { amountoutstanding => '3', accounttype => 'Pay' } );
335 return ( 15, \@accountlines );
339 return ( 0, \@accountlines );
343 my $person = GetMemberDetails(undef,undef);
344 ok( !$person , 'Expected no member details from undef,undef' );
345 $person = GetMemberDetails(undef,'987654321');
346 is( $person->{amountoutstanding}, 15,
347 'Expected 15 outstanding for cardnumber.');
348 $borrowernumber = $person->{borrowernumber};
349 $person = GetMemberDetails($borrowernumber,undef);
350 is( $person->{amountoutstanding}, 15,
351 'Expected 15 outstanding for borrowernumber.');
352 $person = GetMemberDetails($borrowernumber,'987654321');
353 is( $person->{amountoutstanding}, 15,
354 'Expected 15 outstanding for both borrowernumber and cardnumber.');
356 # do not count holds charges
357 C4::Context->set_preference( 'HoldsInNoissuesCharge', '1' );
358 C4::Context->set_preference( 'ManInvInNoissuesCharge', '0' );
359 my ($total, $total_minus_charges,
360 $other_charges) = C4::Members::GetMemberAccountBalance(123);
361 is( $total, 15 , "Total calculated correctly");
362 is( $total_minus_charges, 15, "Holds charges are not count if HoldsInNoissuesCharge=1");
363 is( $other_charges, 0, "Holds charges are not considered if HoldsInNoissuesCharge=1");
365 C4::Context->set_preference( 'HoldsInNoissuesCharge', '0' );
366 ($total, $total_minus_charges,
367 $other_charges) = C4::Members::GetMemberAccountBalance(123);
368 is( $total, 15 , "Total calculated correctly");
369 is( $total_minus_charges, 10, "Holds charges are count if HoldsInNoissuesCharge=0");
370 is( $other_charges, 5, "Holds charges are considered if HoldsInNoissuesCharge=1");
376 my ($resultset) = @_;
377 my $found = $resultset && grep( { $_->{cardnumber} && $_->{cardnumber} eq $CARDNUMBER } @$resultset );
381 ### ------------------------------------- ###
382 ### Testing GetAge() / SetAge() functions ###
383 ### ------------------------------------- ###
384 #USES the package $member-variable to mock a koha.borrowers-object
385 sub testAgeAccessors {
387 my $original_dateofbirth = $member->{dateofbirth};
390 my $age=GetAge("1992-08-14", "2011-01-19");
391 is ($age, "18", "Age correct");
393 $age=GetAge("2011-01-19", "1992-01-19");
394 is ($age, "-19", "Birthday In the Future");
396 ##Testing SetAge() for now()
397 my $dt_now = DateTime->now();
398 $age = DateTime::Duration->new(years => 12, months => 6, days => 1);
399 C4::Members::SetAge( $member, $age );
400 $age = C4::Members::GetAge( $member->{dateofbirth} );
401 is ($age, '12', "SetAge 12 years");
403 $age = DateTime::Duration->new(years => 18, months => 12, days => 31);
404 C4::Members::SetAge( $member, $age );
405 $age = C4::Members::GetAge( $member->{dateofbirth} );
406 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.
408 $age = DateTime::Duration->new(years => 18, months => 12, days => 30);
409 C4::Members::SetAge( $member, $age );
410 $age = C4::Members::GetAge( $member->{dateofbirth} );
411 is ($age, '19', "SetAge 18 years");
413 $age = DateTime::Duration->new(years => 0, months => 1, days => 1);
414 C4::Members::SetAge( $member, $age );
415 $age = C4::Members::GetAge( $member->{dateofbirth} );
416 is ($age, '0', "SetAge 0 years");
419 C4::Members::SetAge( $member, $age );
420 $age = C4::Members::GetAge( $member->{dateofbirth} );
421 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.
424 C4::Members::SetAge( $member, $age );
425 $age = C4::Members::GetAge( $member->{dateofbirth} );
426 is ($age, '19', "SetAge ISO_Date 18 years");
429 eval { C4::Members::SetAge( $member, $age ); };
430 is ((length $@ > 1), '1', "SetAge ISO_Date $age years FAILS");
433 eval { C4::Members::SetAge( $member, $age ); };
434 is ((length $@ == 0), '1', "SetAge ISO_Date $age years succeeds");
436 ##Testing SetAge() for relative_date
437 my $relative_date = DateTime->new(year => 3010, month => 3, day => 15);
439 $age = DateTime::Duration->new(years => 10, months => 3);
440 C4::Members::SetAge( $member, $age, $relative_date );
441 $age = C4::Members::GetAge( $member->{dateofbirth}, $relative_date->ymd() );
442 is ($age, '10', "SetAge, 10 years and 3 months old person was born on ".$member->{dateofbirth}." if todays is ".$relative_date->ymd());
444 $age = DateTime::Duration->new(years => 112, months => 1, days => 1);
445 C4::Members::SetAge( $member, $age, $relative_date );
446 $age = C4::Members::GetAge( $member->{dateofbirth}, $relative_date->ymd() );
447 is ($age, '112', "SetAge, 112 years, 1 months and 1 days old person was born on ".$member->{dateofbirth}." if today is ".$relative_date->ymd());
450 C4::Members::SetAge( $member, $age, $relative_date );
451 $age = C4::Members::GetAge( $member->{dateofbirth}, $relative_date->ymd() );
452 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());
454 $member->{dateofbirth} = $original_dateofbirth; #It is polite to revert made changes in the unit tests.
455 } #sub testAgeAccessors