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
=> 74;
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 $PHONE = "555-12123";
46 # XXX should be randomised and checked against the database
47 my $IMPOSSIBLE_CARDNUMBER = "XYZZZ999";
49 # XXX make a non-commit transaction and rollback rather than insert/delete
51 #my ($usernum, $userid, $usercnum, $userfirstname, $usersurname, $userbranch, $branchname, $userflags, $emailaddress, $branchprinter)= @_;
64 C4
::Context
->_new_userenv ('DUMMY_SESSION_ID');
65 C4
::Context
->set_userenv ( @USERENV );
67 my $userenv = C4
::Context
->userenv
68 or BAIL_OUT
("No userenv");
70 # Make a borrower for testing
72 cardnumber
=> $CARDNUMBER,
73 firstname
=> $FIRSTNAME,
75 categorycode
=> $CATEGORYCODE,
76 branchcode
=> $BRANCHCODE,
78 dateexpiry
=> '9999-12-31',
82 testAgeAccessors
(\
%data); #Age accessor tests don't touch the db so it is safe to run them with just the object.
84 my $addmem=AddMember
(%data);
85 ok
($addmem, "AddMember()");
87 # It's not really a Move, it's a Copy.
88 my $result = MoveMemberToDeleted
($addmem);
89 ok
($result,"MoveMemberToDeleted()");
91 my $sth = $dbh->prepare("SELECT * from borrowers WHERE borrowernumber=?");
92 $sth->execute($addmem);
93 my $MemberAdded = $sth->fetchrow_hashref;
95 $sth = $dbh->prepare("SELECT * from deletedborrowers WHERE borrowernumber=?");
96 $sth->execute($addmem);
97 my $MemberMoved = $sth->fetchrow_hashref;
99 is_deeply
($MemberMoved,$MemberAdded,"Confirm MoveMemberToDeleted.");
101 my $member=GetMemberDetails
("",$CARDNUMBER)
102 or BAIL_OUT
("Cannot read member with card $CARDNUMBER");
104 ok
( $member->{firstname
} eq $FIRSTNAME &&
105 $member->{surname
} eq $SURNAME &&
106 $member->{categorycode
} eq $CATEGORYCODE &&
107 $member->{branchcode
} eq $BRANCHCODE
109 or diag
("Mismatching member details: ".Dumper
(\
%data, $member));
111 is
($member->{dateofbirth
}, undef, "Empty dates handled correctly");
113 $member->{firstname
} = $CHANGED_FIRSTNAME;
114 $member->{email
} = $EMAIL;
115 $member->{phone
} = $PHONE;
116 $member->{emailpro
} = $EMAILPRO;
118 my $changedmember=GetMemberDetails
("",$CARDNUMBER);
119 ok
( $changedmember->{firstname
} eq $CHANGED_FIRSTNAME &&
120 $changedmember->{email
} eq $EMAIL &&
121 $changedmember->{phone
} eq $PHONE &&
122 $changedmember->{emailpro
} eq $EMAILPRO
124 or diag
("Mismatching member details: ".Dumper
($member, $changedmember));
126 C4
::Context
->set_preference( 'CardnumberLength', '' );
127 C4
::Context
->clear_syspref_cache();
129 my $checkcardnum=C4
::Members
::checkcardnumber
($CARDNUMBER, "");
130 is
($checkcardnum, "1", "Card No. in use");
132 $checkcardnum=C4
::Members
::checkcardnumber
($IMPOSSIBLE_CARDNUMBER, "");
133 is
($checkcardnum, "0", "Card No. not used");
135 C4
::Context
->set_preference( 'CardnumberLength', '4' );
136 C4
::Context
->clear_syspref_cache();
138 $checkcardnum=C4
::Members
::checkcardnumber
($IMPOSSIBLE_CARDNUMBER, "");
139 is
($checkcardnum, "2", "Card number is too long");
143 C4
::Context
->set_preference( 'AutoEmailPrimaryAddress', 'OFF' );
144 C4
::Context
->clear_syspref_cache();
146 my $notice_email = GetNoticeEmailAddress
($member->{'borrowernumber'});
147 is
($notice_email, $EMAIL, "GetNoticeEmailAddress returns correct value when AutoEmailPrimaryAddress is off");
149 C4
::Context
->set_preference( 'AutoEmailPrimaryAddress', 'emailpro' );
150 C4
::Context
->clear_syspref_cache();
152 $notice_email = GetNoticeEmailAddress
($member->{'borrowernumber'});
153 is
($notice_email, $EMAILPRO, "GetNoticeEmailAddress returns correct value when AutoEmailPrimaryAddress is emailpro");
155 ok
(!$member->{is_expired
}, "GetMemberDetails() indicates that patron is not expired");
156 ModMember
(borrowernumber
=> $member->{'borrowernumber'}, dateexpiry
=> '2001-01-1');
157 $member = GetMemberDetails
($member->{'borrowernumber'});
158 ok
($member->{is_expired
}, "GetMemberDetails() indicates that patron is expired");
161 my $message_type = 'B';
162 my $message = 'my message';
163 my $messages_count = GetMessagesCount
($member->{borrowernumber
}, $message_type, $BRANCHCODE);
164 is
( $messages_count, 0, 'GetMessagesCount returns the number of messages correclty' );
166 is
( AddMessage
(), undef, 'AddMessage without argument returns undef' );
167 is
( AddMessage
(undef, $message_type, $message, $BRANCHCODE), undef, 'AddMessage without the borrower number returns undef' );
168 is
( AddMessage
($member->{borrowernumber
}, undef, $message, $BRANCHCODE), undef, 'AddMessage without the message type returns undef' );
169 is
( AddMessage
($member->{borrowernumber
}, $message_type, undef, $BRANCHCODE), undef, 'AddMessage without the message returns undef' );
170 is
( AddMessage
($member->{borrowernumber
}, $message_type, $message, undef), undef, 'AddMessage without the branch code returns undef' );
171 is
( AddMessage
($member->{borrowernumber
}, $message_type, $message, $BRANCHCODE), 1, 'AddMessage functions correctly' );
173 $messages_count = GetMessagesCount
();
174 is
( $messages_count, 0, 'GetMessagesCount without argument returns 0' );
175 $messages_count = GetMessagesCount
(undef, $message_type, $BRANCHCODE);
176 is
( $messages_count, '0', 'GetMessagesCount without the borrower number returns the number of messages' );
177 $messages_count = GetMessagesCount
($member->{borrowernumber
}, undef, $BRANCHCODE);
178 is
( $messages_count, '1', 'GetMessagesCount without the message type returns the total number of messages' );
179 $messages_count = GetMessagesCount
($member->{borrowernumber
}, $message_type, undef);
180 is
( $messages_count, '1', 'GetMessagesCount without the branchcode returns the total number of messages' );
181 $messages_count = GetMessagesCount
($member->{borrowernumber
}, $message_type, $BRANCHCODE);
182 is
( $messages_count, '1', 'GetMessagesCount returns the number of messages correctly' );
184 my $messages = GetMessages
();
185 is
( @
$messages, 0, 'GetMessages without argument returns 0' );
186 $messages = GetMessages
($member->{borrowernumber
}, $message_type, $BRANCHCODE);
187 is
( @
$messages, 1, 'GetMessages returns the correct number of messages' );
188 is
( $messages->[0]->{borrowernumber
}, $member->{borrowernumber
}, 'GetMessages returns the borrower number correctly' );
189 is
( $messages->[0]->{message_type
}, $message_type, 'GetMessages returns the message type correclty' );
190 is
( $messages->[0]->{message
}, $message, 'GetMessages returns the message correctly' );
191 is
( $messages->[0]->{branchcode
}, $BRANCHCODE, 'GetMessages returns the branch code correctly' );
193 $messages_count = GetMessagesCount
($member->{borrowernumber
}, $message_type, $BRANCHCODE);
194 is
( $messages_count, 1, 'GetMessagesCount returns the number of messages correclty' );
197 $messages = GetMessages
($member->{borrowernumber
}, $message_type, $BRANCHCODE);
198 is
( @
$messages, 1, 'DeleteMessage without message id does not delete messages' );
199 DeleteMessage
($messages->[0]->{message_id
});
200 $messages = GetMessages
($member->{borrowernumber
}, $message_type, $BRANCHCODE);
201 is
( @
$messages, 0, 'DeleteMessage deletes a message correctly' );
205 DelMember
($member->{borrowernumber
});
206 my $borrower = GetMember
( cardnumber
=> $CARDNUMBER );
207 is
( $borrower, undef, 'DelMember should remove the patron' );
211 cardnumber
=> "123456789",
212 firstname
=> "Tomasito",
222 my $borrowernumber = AddMember
( %data );
223 is
( Check_Userid
( 'tomasito.non', $borrowernumber ), 1,
224 'recently created userid -> unique (borrowernumber passed)' );
225 is
( Check_Userid
( 'tomasitoxxx', $borrowernumber ), 1,
226 'non-existent userid -> unique (borrowernumber passed)' );
227 is
( Check_Userid
( 'tomasito.none', '' ), 0,
228 'userid exists (blank borrowernumber)' );
229 is
( Check_Userid
( 'tomasitoxxx', '' ), 1,
230 'non-existent userid -> unique (blank borrowernumber)' );
232 $borrower = GetMember
( borrowernumber
=> $borrowernumber );
233 is
( $borrower->{dateofbirth
}, undef, 'AddMember should undef dateofbirth if empty string is given');
234 is
( $borrower->{debarred
}, undef, 'AddMember should undef debarred if empty string is given');
235 isnt
( $borrower->{dateexpiry
}, '0000-00-00', 'AddMember should not set dateexpiry to 0000-00-00 if empty string is given');
236 isnt
( $borrower->{dateenrolled
}, '0000-00-00', 'AddMember should not set dateenrolled to 0000-00-00 if empty string is given');
238 ModMember
( borrowernumber
=> $borrowernumber, dateofbirth
=> '', debarred
=> '', dateexpiry
=> '', dateenrolled
=> '' );
239 $borrower = GetMember
( borrowernumber
=> $borrowernumber );
240 is
( $borrower->{dateofbirth
}, undef, 'ModMember should undef dateofbirth if empty string is given');
241 is
( $borrower->{debarred
}, undef, 'ModMember should undef debarred if empty string is given');
242 isnt
( $borrower->{dateexpiry
}, '0000-00-00', 'ModMember should not set dateexpiry to 0000-00-00 if empty string is given');
243 isnt
( $borrower->{dateenrolled
}, '0000-00-00', 'ModMember should not set dateenrolled to 0000-00-00 if empty string is given');
245 ModMember
( borrowernumber
=> $borrowernumber, dateofbirth
=> '1970-01-01', debarred
=> '2042-01-01', dateexpiry
=> '9999-12-31', dateenrolled
=> '2015-09-06' );
246 $borrower = GetMember
( borrowernumber
=> $borrowernumber );
247 is
( $borrower->{dateofbirth
}, '1970-01-01', 'ModMember should correctly set dateofbirth if a valid date is given');
248 is
( $borrower->{debarred
}, '2042-01-01', 'ModMember should correctly set debarred if a valid date is given');
249 is
( $borrower->{dateexpiry
}, '9999-12-31', 'ModMember should correctly set dateexpiry if a valid date is given');
250 is
( $borrower->{dateenrolled
}, '2015-09-06', 'ModMember should correctly set dateenrolled if a valid date is given');
252 # Add a new borrower with the same userid but different cardnumber
253 $data{ cardnumber
} = "987654321";
254 my $new_borrowernumber = AddMember
( %data );
255 is
( Check_Userid
( 'tomasito.none', '' ), 0,
256 'userid not unique (blank borrowernumber)' );
257 is
( Check_Userid
( 'tomasito.none', $new_borrowernumber ), 0,
258 'userid not unique (second borrowernumber passed)' );
259 $borrower = GetMember
( borrowernumber
=> $new_borrowernumber );
260 ok
( $borrower->{userid
} ne 'tomasito', "Borrower with duplicate userid has new userid generated" );
262 $data{ cardnumber
} = "234567890";
263 $data{userid
} = 'a_user_id';
264 $borrowernumber = AddMember
( %data );
265 $borrower = GetMember
( borrowernumber
=> $borrowernumber );
266 is
( $borrower->{userid
}, $data{userid
}, 'AddMember should insert the given userid' );
268 # Regression tests for BZ13502
269 ## Remove all entries with userid='' (should be only 1 max)
270 $dbh->do(q
|DELETE FROM borrowers WHERE userid
= ''|);
271 ## And create a patron with a userid=''
272 $borrowernumber = AddMember
( categorycode
=> 'S', branchcode
=> 'MPL' );
273 $dbh->do(q
|UPDATE borrowers SET userid
= '' WHERE borrowernumber
= ?
|, undef, $borrowernumber);
274 # Create another patron and verify the userid has been generated
275 $borrowernumber = AddMember
( categorycode
=> 'S', branchcode
=> 'MPL' );
276 ok
( $borrowernumber > 0, 'AddMember should have inserted the patron even if no userid is given' );
277 $borrower = GetMember
( borrowernumber
=> $borrowernumber );
278 ok
( $borrower->{userid
}, 'A userid should have been generated correctly' );
280 # Regression tests for BZ12226
281 is
( Check_Userid
( C4
::Context
->config('user'), '' ), 0,
282 'Check_Userid should return 0 for the DB user (Bug 12226)');
284 subtest
'GetMemberAccountBalance' => sub {
288 my $members_mock = new Test
::MockModule
('C4::Members');
289 $members_mock->mock( 'GetMemberAccountRecords', sub {
290 my ($borrowernumber) = @_;
291 if ($borrowernumber) {
293 { amountoutstanding
=> '7', accounttype
=> 'Rent' },
294 { amountoutstanding
=> '5', accounttype
=> 'Res' },
295 { amountoutstanding
=> '3', accounttype
=> 'Pay' } );
296 return ( 15, \
@accountlines );
300 return ( 0, \
@accountlines );
304 my $person = GetMemberDetails
(undef,undef);
305 ok
( !$person , 'Expected no member details from undef,undef' );
306 $person = GetMemberDetails
(undef,'987654321');
307 is
( $person->{amountoutstanding
}, 15,
308 'Expected 15 outstanding for cardnumber.');
309 $borrowernumber = $person->{borrowernumber
};
310 $person = GetMemberDetails
($borrowernumber,undef);
311 is
( $person->{amountoutstanding
}, 15,
312 'Expected 15 outstanding for borrowernumber.');
313 $person = GetMemberDetails
($borrowernumber,'987654321');
314 is
( $person->{amountoutstanding
}, 15,
315 'Expected 15 outstanding for both borrowernumber and cardnumber.');
317 # do not count holds charges
318 C4
::Context
->set_preference( 'HoldsInNoissuesCharge', '1' );
319 C4
::Context
->set_preference( 'ManInvInNoissuesCharge', '0' );
320 my ($total, $total_minus_charges,
321 $other_charges) = C4
::Members
::GetMemberAccountBalance
(123);
322 is
( $total, 15 , "Total calculated correctly");
323 is
( $total_minus_charges, 15, "Holds charges are not count if HoldsInNoissuesCharge=1");
324 is
( $other_charges, 0, "Holds charges are not considered if HoldsInNoissuesCharge=1");
326 C4
::Context
->set_preference( 'HoldsInNoissuesCharge', '0' );
327 ($total, $total_minus_charges,
328 $other_charges) = C4
::Members
::GetMemberAccountBalance
(123);
329 is
( $total, 15 , "Total calculated correctly");
330 is
( $total_minus_charges, 10, "Holds charges are count if HoldsInNoissuesCharge=0");
331 is
( $other_charges, 5, "Holds charges are considered if HoldsInNoissuesCharge=1");
336 subtest
'purgeSelfRegistration' => sub {
341 C4
::Members
::DeleteUnverifiedOpacRegistrations
($d);
343 $dbh->do("INSERT INTO borrower_modifications (timestamp, borrowernumber, verification_token) VALUES ('2014-01-01 01:02:03',0,?)", undef, (scalar localtime)."_$_");
345 is
( C4
::Members
::DeleteUnverifiedOpacRegistrations
($d), 3, 'Test for DeleteUnverifiedOpacRegistrations' );
347 #purge members in temporary category
349 $dbh->do("INSERT IGNORE INTO categories (categorycode) VALUES ('$c')");
350 C4
::Context
->set_preference('PatronSelfRegistrationDefaultCategory', $c );
351 C4
::Context
->set_preference('PatronSelfRegistrationExpireTemporaryAccountsDelay', 360);
352 C4
::Members
::DeleteExpiredOpacRegistrations
();
353 $dbh->do("INSERT INTO borrowers (surname, address, city, branchcode, categorycode, dateenrolled) VALUES ('Testaabbcc', 'Street 1', 'CITY', 'CPL', '$c', '2014-01-01 01:02:03')");
354 is
( C4
::Members
::DeleteExpiredOpacRegistrations
(), 1, 'Test for DeleteExpiredOpacRegistrations');
359 my ($resultset) = @_;
360 my $found = $resultset && grep( { $_->{cardnumber
} && $_->{cardnumber
} eq $CARDNUMBER } @
$resultset );
364 ### ------------------------------------- ###
365 ### Testing GetAge() / SetAge() functions ###
366 ### ------------------------------------- ###
367 #USES the package $member-variable to mock a koha.borrowers-object
368 sub testAgeAccessors
{
370 my $original_dateofbirth = $member->{dateofbirth
};
373 my $age=GetAge
("1992-08-14", "2011-01-19");
374 is
($age, "18", "Age correct");
376 $age=GetAge
("2011-01-19", "1992-01-19");
377 is
($age, "-19", "Birthday In the Future");
379 ##Testing SetAge() for now()
380 my $dt_now = DateTime
->now();
381 $age = DateTime
::Duration
->new(years
=> 12, months
=> 6, days
=> 1);
382 C4
::Members
::SetAge
( $member, $age );
383 $age = C4
::Members
::GetAge
( $member->{dateofbirth
} );
384 is
($age, '12', "SetAge 12 years");
386 $age = DateTime
::Duration
->new(years
=> 18, months
=> 12, days
=> 31);
387 C4
::Members
::SetAge
( $member, $age );
388 $age = C4
::Members
::GetAge
( $member->{dateofbirth
} );
389 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.
391 $age = DateTime
::Duration
->new(years
=> 18, months
=> 12, days
=> 30);
392 C4
::Members
::SetAge
( $member, $age );
393 $age = C4
::Members
::GetAge
( $member->{dateofbirth
} );
394 is
($age, '19', "SetAge 18 years");
396 $age = DateTime
::Duration
->new(years
=> 0, months
=> 1, days
=> 1);
397 C4
::Members
::SetAge
( $member, $age );
398 $age = C4
::Members
::GetAge
( $member->{dateofbirth
} );
399 is
($age, '0', "SetAge 0 years");
402 C4
::Members
::SetAge
( $member, $age );
403 $age = C4
::Members
::GetAge
( $member->{dateofbirth
} );
404 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.
407 C4
::Members
::SetAge
( $member, $age );
408 $age = C4
::Members
::GetAge
( $member->{dateofbirth
} );
409 is
($age, '19', "SetAge ISO_Date 18 years");
412 eval { C4
::Members
::SetAge
( $member, $age ); };
413 is
((length $@
> 1), '1', "SetAge ISO_Date $age years FAILS");
416 eval { C4
::Members
::SetAge
( $member, $age ); };
417 is
((length $@
== 0), '1', "SetAge ISO_Date $age years succeeds");
419 ##Testing SetAge() for relative_date
420 my $relative_date = DateTime
->new(year
=> 3010, month
=> 3, day
=> 15);
422 $age = DateTime
::Duration
->new(years
=> 10, months
=> 3);
423 C4
::Members
::SetAge
( $member, $age, $relative_date );
424 $age = C4
::Members
::GetAge
( $member->{dateofbirth
}, $relative_date->ymd() );
425 is
($age, '10', "SetAge, 10 years and 3 months old person was born on ".$member->{dateofbirth
}." if todays is ".$relative_date->ymd());
427 $age = DateTime
::Duration
->new(years
=> 112, months
=> 1, days
=> 1);
428 C4
::Members
::SetAge
( $member, $age, $relative_date );
429 $age = C4
::Members
::GetAge
( $member->{dateofbirth
}, $relative_date->ymd() );
430 is
($age, '112', "SetAge, 112 years, 1 months and 1 days old person was born on ".$member->{dateofbirth
}." if today is ".$relative_date->ymd());
433 C4
::Members
::SetAge
( $member, $age, $relative_date );
434 $age = C4
::Members
::GetAge
( $member->{dateofbirth
}, $relative_date->ymd() );
435 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());
437 $member->{dateofbirth
} = $original_dateofbirth; #It is polite to revert made changes in the unit tests.
438 } #sub testAgeAccessors