|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
use Modern::Perl; |
| 4 |
use Koha::DateUtils; |
| 5 |
use DateTime::Duration; |
| 6 |
use C4::Biblio; |
| 7 |
use C4::Members; |
| 8 |
use C4::Branch; |
| 9 |
use C4::Circulation; |
| 10 |
use C4::Items; |
| 11 |
use C4::Context; |
| 12 |
|
| 13 |
use Test::More tests => 16; |
| 14 |
|
| 15 |
BEGIN { |
| 16 |
use_ok('C4::Circulation'); |
| 17 |
} |
| 18 |
can_ok( |
| 19 |
'C4::Circulation', |
| 20 |
qw(AddIssue |
| 21 |
AddIssuingCharge |
| 22 |
AddRenewal |
| 23 |
AddReturn |
| 24 |
GetBiblioIssues |
| 25 |
GetIssuingCharges |
| 26 |
GetIssuingRule |
| 27 |
GetItemIssue |
| 28 |
GetItemIssues |
| 29 |
GetOpenIssue |
| 30 |
GetRenewCount |
| 31 |
GetUpcomingDueIssues |
| 32 |
) |
| 33 |
); |
| 34 |
|
| 35 |
#Start transaction |
| 36 |
my $dbh = C4::Context->dbh; |
| 37 |
$dbh->{RaiseError} = 1; |
| 38 |
$dbh->{AutoCommit} = 0; |
| 39 |
|
| 40 |
$dbh->do(q|DELETE FROM issues|); |
| 41 |
$dbh->do(q|DELETE FROM borrowers|); |
| 42 |
$dbh->do(q|DELETE FROM items|); |
| 43 |
$dbh->do(q|DELETE FROM branches|); |
| 44 |
$dbh->do(q|DELETE FROM categories|); |
| 45 |
$dbh->do(q|DELETE FROM accountlines|); |
| 46 |
|
| 47 |
#Add sample datas |
| 48 |
my @USERENV = ( 1, 'test', 'MASTERTEST', 'Test', 'Test', 't', 'Test', 0, ); |
| 49 |
|
| 50 |
C4::Context->_new_userenv('DUMMY_SESSION_ID'); |
| 51 |
C4::Context->set_userenv(@USERENV); |
| 52 |
|
| 53 |
my $userenv = C4::Context->userenv |
| 54 |
or BAIL_OUT("No userenv"); |
| 55 |
|
| 56 |
#Add Dates |
| 57 |
|
| 58 |
my $dt_today = dt_from_string; |
| 59 |
my $today = output_pref( $dt_today, 'iso', '24hr', 1 ); |
| 60 |
|
| 61 |
my $dt_today2 = dt_from_string; |
| 62 |
my $dur10 = DateTime::Duration->new( days => -10 ); |
| 63 |
$dt_today2->add_duration($dur10); |
| 64 |
my $daysago10 = output_pref( $dt_today2, 'iso', '24hr', 1 ); |
| 65 |
|
| 66 |
#Add branch and category |
| 67 |
my $samplebranch1 = { |
| 68 |
add => 1, |
| 69 |
branchcode => 'SAB1', |
| 70 |
branchname => 'Sample Branch', |
| 71 |
branchaddress1 => 'sample adr1', |
| 72 |
branchaddress2 => 'sample adr2', |
| 73 |
branchaddress3 => 'sample adr3', |
| 74 |
branchzip => 'sample zip', |
| 75 |
branchcity => 'sample city', |
| 76 |
branchstate => 'sample state', |
| 77 |
branchcountry => 'sample country', |
| 78 |
branchphone => 'sample phone', |
| 79 |
branchfax => 'sample fax', |
| 80 |
branchemail => 'sample email', |
| 81 |
branchurl => 'sample url', |
| 82 |
branchip => 'sample ip', |
| 83 |
branchprinter => undef, |
| 84 |
opac_info => 'sample opac', |
| 85 |
}; |
| 86 |
my $samplebranch2 = { |
| 87 |
add => 1, |
| 88 |
branchcode => 'SAB2', |
| 89 |
branchname => 'Sample Branch2', |
| 90 |
branchaddress1 => 'sample adr1_2', |
| 91 |
branchaddress2 => 'sample adr2_2', |
| 92 |
branchaddress3 => 'sample adr3_2', |
| 93 |
branchzip => 'sample zip2', |
| 94 |
branchcity => 'sample city2', |
| 95 |
branchstate => 'sample state2', |
| 96 |
branchcountry => 'sample country2', |
| 97 |
branchphone => 'sample phone2', |
| 98 |
branchfax => 'sample fax2', |
| 99 |
branchemail => 'sample email2', |
| 100 |
branchurl => 'sample url2', |
| 101 |
branchip => 'sample ip2', |
| 102 |
branchprinter => undef, |
| 103 |
opac_info => 'sample opac2', |
| 104 |
}; |
| 105 |
ModBranch($samplebranch1); |
| 106 |
ModBranch($samplebranch2); |
| 107 |
|
| 108 |
my $samplecat = { |
| 109 |
categorycode => 'CAT1', |
| 110 |
description => 'Description1', |
| 111 |
enrolmentperiod => 'Null', |
| 112 |
enrolmentperioddate => 'Null', |
| 113 |
dateofbirthrequired => 'Null', |
| 114 |
finetype => 'Null', |
| 115 |
bulk => 'Null', |
| 116 |
enrolmentfee => 'Null', |
| 117 |
overduenoticerequired => 'Null', |
| 118 |
issuelimit => 'Null', |
| 119 |
reservefee => 'Null', |
| 120 |
hidelostitems => 0, |
| 121 |
category_type => 'Null' |
| 122 |
}; |
| 123 |
my $query = |
| 124 |
"INSERT INTO categories (categorycode,description,enrolmentperiod,enrolmentperioddate,dateofbirthrequired ,finetype,bulk,enrolmentfee,overduenoticerequired,issuelimit ,reservefee ,hidelostitems ,category_type) VALUES( ?,?,?,?,?,?,?,?,?,?,?,?,?)"; |
| 125 |
$dbh->do( |
| 126 |
$query, {}, |
| 127 |
$samplecat->{categorycode}, $samplecat->{description}, |
| 128 |
$samplecat->{enrolmentperiod}, $samplecat->{enrolmentperioddate}, |
| 129 |
$samplecat->{dateofbirthrequired}, $samplecat->{finetype}, |
| 130 |
$samplecat->{bulk}, $samplecat->{enrolmentfee}, |
| 131 |
$samplecat->{overduenoticerequired}, $samplecat->{issuelimit}, |
| 132 |
$samplecat->{reservefee}, $samplecat->{hidelostitems}, |
| 133 |
$samplecat->{category_type} |
| 134 |
); |
| 135 |
|
| 136 |
#Add biblio and item |
| 137 |
my $record = MARC::Record->new(); |
| 138 |
$record->append_fields( |
| 139 |
MARC::Field->new( '952', '0', '0', a => $samplebranch1->{branchcode} ) ); |
| 140 |
my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '', ); |
| 141 |
|
| 142 |
my @sampleitem1 = C4::Items::AddItem( |
| 143 |
{ |
| 144 |
barcode => 1, |
| 145 |
itemcallnumber => 'callnumber1', |
| 146 |
homebranch => $samplebranch1->{branchcode}, |
| 147 |
holdingbranch => $samplebranch1->{branchcode}, |
| 148 |
issue => 1, |
| 149 |
reserve => 1 |
| 150 |
}, |
| 151 |
$biblionumber |
| 152 |
); |
| 153 |
my $item_id1 = $sampleitem1[2]; |
| 154 |
my @sampleitem2 = C4::Items::AddItem( |
| 155 |
{ |
| 156 |
barcode => 2, |
| 157 |
itemcallnumber => 'callnumber2', |
| 158 |
homebranch => $samplebranch2->{branchcode}, |
| 159 |
holdingbranch => $samplebranch2->{branchcode}, |
| 160 |
notforloan => 1, |
| 161 |
issue => 1 |
| 162 |
}, |
| 163 |
$biblionumber |
| 164 |
); |
| 165 |
my $item_id2 = $sampleitem2[2]; |
| 166 |
|
| 167 |
#Add borrower |
| 168 |
my $borrower_id1 = C4::Members::AddMember( |
| 169 |
firstname => 'firstname1', |
| 170 |
surname => 'surname1 ', |
| 171 |
categorycode => $samplecat->{categorycode}, |
| 172 |
branchcode => $samplebranch1->{branchcode}, |
| 173 |
); |
| 174 |
my $borrower_id2 = C4::Members::AddMember( |
| 175 |
firstname => 'firstname2', |
| 176 |
surname => 'surname2 ', |
| 177 |
categorycode => $samplecat->{categorycode}, |
| 178 |
branchcode => $samplebranch2->{branchcode}, |
| 179 |
); |
| 180 |
|
| 181 |
#Begin Tests |
| 182 |
|
| 183 |
#Test AddIssue |
| 184 |
$query = " SELECT count(*) FROM issues"; |
| 185 |
my $sth = $dbh->prepare($query); |
| 186 |
$sth->execute; |
| 187 |
my $countissue = $sth -> fetchrow_array; |
| 188 |
is ($countissue ,0, "there is no issue"); |
| 189 |
my $datedue1 = C4::Circulation::AddIssue( $borrower_id1, "code", $daysago10,0, $today, '' ); |
| 190 |
like( |
| 191 |
$datedue1, |
| 192 |
qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/, |
| 193 |
"AddRenewal returns a date" |
| 194 |
); |
| 195 |
my $issue_id1 = $dbh->last_insert_id( undef, undef, 'issues', undef ); |
| 196 |
|
| 197 |
my $datedue2 = C4::Circulation::AddIssue( $borrower_id1, 'Barcode2' ); |
| 198 |
is( $datedue2, undef, "AddIssue returns undef if no datedue is specified" ); |
| 199 |
my $issue_id2 = $dbh->last_insert_id( undef, undef, 'issues', undef ); |
| 200 |
|
| 201 |
$sth->execute; |
| 202 |
$countissue = $sth -> fetchrow_array; |
| 203 |
#FIXME: Currently AddIssue doesn't add correctly issues |
| 204 |
#is ($countissue,2,"2 issues have been added"); |
| 205 |
|
| 206 |
#Test AddIssuingCharge |
| 207 |
$query = " SELECT count(*) FROM accountlines"; |
| 208 |
$sth = $dbh->prepare($query); |
| 209 |
$sth->execute; |
| 210 |
my $countaccount = $sth -> fetchrow_array; |
| 211 |
is ($countaccount,0,"0 accountline exists"); |
| 212 |
is( C4::Circulation::AddIssuingCharge( $item_id1, $borrower_id1, 10 ), |
| 213 |
1, "An issuing charge has been added" ); |
| 214 |
my $account_id = $dbh->last_insert_id( undef, undef, 'accountlines', undef ); |
| 215 |
$sth->execute; |
| 216 |
$countaccount = $sth -> fetchrow_array; |
| 217 |
is ($countaccount,1,"1 accountline has been added"); |
| 218 |
|
| 219 |
#Test AddRenewal |
| 220 |
my $datedue3 = |
| 221 |
AddRenewal( $borrower_id1, $item_id1, $samplebranch1->{branchcode}, |
| 222 |
$datedue1, $daysago10 ); |
| 223 |
like( |
| 224 |
$datedue3, |
| 225 |
qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/, |
| 226 |
"AddRenewal returns a date" |
| 227 |
); |
| 228 |
|
| 229 |
#Test GetBiblioIssues |
| 230 |
is( GetBiblioIssues(), undef, "GetBiblio Issues without parameters" ); |
| 231 |
|
| 232 |
#Test GetItemIssue |
| 233 |
#FIXME : As the issues are not correctly added in the database, these tests don't work correctly |
| 234 |
is(GetItemIssue,undef,"Without parameter GetItemIssue returns undef"); |
| 235 |
#is(GetItemIssue($item_id1),{},"Item1's issues"); |
| 236 |
|
| 237 |
#Test GetItemIssues |
| 238 |
#FIXME: this routine currently doesn't work be |
| 239 |
#is_deeply (GetItemIssues,{},"Without parameter, GetItemIssue returns all the issues"); |
| 240 |
|
| 241 |
#Test GetOpenIssue |
| 242 |
is( GetOpenIssue(), undef, "Without parameter GetOpenIssue returns undef" ); |
| 243 |
is( GetOpenIssue(-1), undef, |
| 244 |
"With wrong parameter GetOpenIssue returns undef" ); |
| 245 |
my $openissue = GetOpenIssue($item_id1); |
| 246 |
|
| 247 |
#Test GetRenewCount |
| 248 |
my @renewcount = C4::Circulation::GetRenewCount(); |
| 249 |
is_deeply( |
| 250 |
\@renewcount, |
| 251 |
[ 0, 1, 1 ], |
| 252 |
"Without paramater, GetRenewCount returns renewcount0,renewsallowed = 0,renewsleft = 0" |
| 253 |
); |
| 254 |
@renewcount = C4::Circulation::GetRenewCount(-1); |
| 255 |
is_deeply( |
| 256 |
\@renewcount, |
| 257 |
[ 0, 1, 1 ], |
| 258 |
"Without wrong, GetRenewCount returns renewcount0,renewsallowed = 0,renewsleft = 0" |
| 259 |
); |
| 260 |
@renewcount = C4::Circulation::GetRenewCount($item_id1); |
| 261 |
is_deeply( \@renewcount, [ 0, 1, 1 ], "Getrenewcount of item1 returns" ); |
| 262 |
|
| 263 |
#End transaction |
| 264 |
$dbh->rollback; |
| 265 |
|