|
Line 0
Link Here
|
|
|
1 |
#!/usr/bin/env perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
use Data::Dumper; |
| 6 |
use DateTime; |
| 7 |
use DateTime::Duration; |
| 8 |
use Test::More tests => 13; |
| 9 |
use Test::MockModule; |
| 10 |
use DBD::Mock; |
| 11 |
use Koha::DateUtils; |
| 12 |
use C4::Calendar qw(getOpeningHours); |
| 13 |
use C4::Circulation qw(CalcDateDue); |
| 14 |
use C4::Overdues qw(_get_chargeable_units); |
| 15 |
|
| 16 |
BEGIN { |
| 17 |
die "DBD::Mock v$DBD::Mock::VERSION is too old. This test requires v1.45 or higher.", 33 |
| 18 |
unless $DBD::Mock::VERSION >= 1.45; |
| 19 |
|
| 20 |
use_ok('Koha::Calendar'); |
| 21 |
use_ok('C4::Calendar'); |
| 22 |
use_ok('C4::Circulation'); |
| 23 |
use_ok('C4::Overdues'); |
| 24 |
} |
| 25 |
|
| 26 |
my $module_context = new Test::MockModule('C4::Context'); |
| 27 |
$module_context->mock( |
| 28 |
'_new_dbh', |
| 29 |
sub { |
| 30 |
my $dbh = DBI->connect( 'DBI:Mock:', '', '' ) |
| 31 |
|| die "Cannot create handle: $DBI::errstr\n"; |
| 32 |
return $dbh; |
| 33 |
} |
| 34 |
); |
| 35 |
|
| 36 |
# Initialize the global $dbh variable |
| 37 |
my $dbh = C4::Context->dbh(); |
| 38 |
|
| 39 |
{ #C4::Calendar |
| 40 |
my $sessionCal = DBD::Mock::Session->new('sessionCal' => ( |
| 41 |
{ #Opening hours by branch |
| 42 |
statement => "SELECT * FROM openinghours WHERE branchcode = ?", |
| 43 |
bound_params => ['BIB'], |
| 44 |
results => [ |
| 45 |
['branchcode', 'weekcode', 'openhour', 'closehour'], |
| 46 |
['BIB', 0, '09:00:00', '17:00:00'], |
| 47 |
['BIB', 1, '09:00:00', '17:00:00'], |
| 48 |
['BIB', 2, '09:00:00', '17:00:00'], |
| 49 |
['BIB', 3, '09:00:00', '17:00:00'], |
| 50 |
['BIB', 4, '09:00:00', '17:00:00'], |
| 51 |
['BIB', 5, '09:00:00', '17:00:00'], |
| 52 |
['BIB', 6, '09:00:00', '17:00:00'] |
| 53 |
] |
| 54 |
} |
| 55 |
)); |
| 56 |
|
| 57 |
$dbh->{ mock_session } = $sessionCal; |
| 58 |
|
| 59 |
print "\nTesting C4::Calendar\n"; |
| 60 |
can_ok('C4::Calendar', ('getOpeningHours')); |
| 61 |
can_ok('C4::Calendar', ('setOpeningHours')); |
| 62 |
getOpeningHours('BIB'); |
| 63 |
} |
| 64 |
|
| 65 |
print "\nTesting C4::Circulation\n"; |
| 66 |
{ #C4::Circulation : Calendar Mode |
| 67 |
my $sessionCircCal = DBD::Mock::Session->new('sessionCircCal' => ( |
| 68 |
# Mock queries for test 7 |
| 69 |
{ |
| 70 |
statement => qr/\s*SELECT issuelength, lengthunit, renewalperiod/, |
| 71 |
results => [ |
| 72 |
[ 'issuelength', 'lengthunit', 'renewalperiod' ], |
| 73 |
[ 5, 'hours' , 0 ] |
| 74 |
] |
| 75 |
}, |
| 76 |
{ |
| 77 |
statement => 'SELECT weekday FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NOT NULL', |
| 78 |
results => DBD::Mock->NULL_RESULTSET |
| 79 |
}, |
| 80 |
{ |
| 81 |
statement => 'SELECT day, month FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NULL', |
| 82 |
results => [ |
| 83 |
['day', 'month'], |
| 84 |
[24,6] |
| 85 |
] |
| 86 |
}, |
| 87 |
{ |
| 88 |
statement => 'SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 1', |
| 89 |
results => DBD::Mock->NULL_RESULTSET |
| 90 |
}, |
| 91 |
{ |
| 92 |
statement => 'SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 0', |
| 93 |
results => DBD::Mock->NULL_RESULTSET |
| 94 |
}, |
| 95 |
{ #GetIssuingRules called in GetHardDueDate |
| 96 |
statement => qr/select \* from issuingrules/, |
| 97 |
results => [ |
| 98 |
['hardduedate', 'hardduedatecompare'], |
| 99 |
[undef,-1] |
| 100 |
] |
| 101 |
}, |
| 102 |
{ #Opening hours |
| 103 |
statement => 'SELECT * FROM openinghours ', |
| 104 |
results => [ |
| 105 |
['branchcode', 'weekcode', 'openhour', 'closehour'], |
| 106 |
['BIB', 0, '09:00:00', '17:00:00'], |
| 107 |
['BIB', 1, '09:00:00', '17:00:00'], |
| 108 |
['BIB', 2, '09:00:00', '17:00:00'], |
| 109 |
['BIB', 3, '09:00:00', '17:00:00'], |
| 110 |
['BIB', 4, '09:00:00', '17:00:00'], |
| 111 |
['BIB', 5, '09:00:00', '17:00:00'], |
| 112 |
['BIB', 6, '09:00:00', '17:00:00'] |
| 113 |
] |
| 114 |
}, |
| 115 |
# Mock Queries for test 8 |
| 116 |
{ |
| 117 |
statement => qr/\s*SELECT issuelength, lengthunit, renewalperiod/, |
| 118 |
results => [ |
| 119 |
[ 'issuelength', 'lengthunit', 'renewalperiod' ], |
| 120 |
[ 1, 'days' , 0 ] |
| 121 |
] |
| 122 |
}, |
| 123 |
{ |
| 124 |
statement => 'SELECT weekday FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NOT NULL', |
| 125 |
results => DBD::Mock->NULL_RESULTSET |
| 126 |
}, |
| 127 |
{ |
| 128 |
statement => 'SELECT day, month FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NULL', |
| 129 |
results => [ |
| 130 |
['day', 'month'], |
| 131 |
[24,6] |
| 132 |
] |
| 133 |
}, |
| 134 |
{ #GetIssuingRules called in GetHardDueDate |
| 135 |
statement => qr/select \* from issuingrules/, |
| 136 |
results => [ |
| 137 |
['hardduedate', 'hardduedatecompare'], |
| 138 |
[undef,-1] |
| 139 |
] |
| 140 |
}, |
| 141 |
# Mock queries for test 9 |
| 142 |
{ |
| 143 |
statement => qr/\s*SELECT issuelength, lengthunit, renewalperiod/, |
| 144 |
results => [ |
| 145 |
[ 'issuelength', 'lengthunit', 'renewalperiod' ], |
| 146 |
[ 5, 'days' , 0 ] |
| 147 |
] |
| 148 |
}, |
| 149 |
{ |
| 150 |
statement => 'SELECT weekday FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NOT NULL', |
| 151 |
results => DBD::Mock->NULL_RESULTSET |
| 152 |
}, |
| 153 |
{ |
| 154 |
statement => 'SELECT day, month FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NULL', |
| 155 |
results => [ |
| 156 |
['day', 'month'], |
| 157 |
[24,6] |
| 158 |
] |
| 159 |
}, |
| 160 |
{ #GetIssuingRules called in GetHardDueDate |
| 161 |
statement => qr/select \* from issuingrules/, |
| 162 |
results => [ |
| 163 |
['hardduedate', 'hardduedatecompare'], |
| 164 |
[undef,-1] |
| 165 |
] |
| 166 |
} |
| 167 |
)); |
| 168 |
$dbh->{ mock_session } = $sessionCircCal; |
| 169 |
|
| 170 |
$module_context->mock('preference',sub {return 'Calendar';}); |
| 171 |
print " useDaysMode set to 'Calendar'\n"; |
| 172 |
|
| 173 |
#7 Testing 5 hours loan |
| 174 |
is(CalcDateDue(dt_from_string('2014-06-23T14:00:00', 'iso'), 0, 'BIB',{categorycode => 0, dateexpiry => '2020'}, 0), dt_from_string('2014-06-23T17:00:00', 'iso'), "Testing hourly loans. Due at close time."); |
| 175 |
#8 Testing single day loan |
| 176 |
is(CalcDateDue(dt_from_string('2014-06-23T12:00:00', 'iso'), 0, 'BIB',{categorycode => 0, dateexpiry => '2020'}, 0), dt_from_string('2014-06-25T23:59:00', 'iso'), "Testing single day loan due the day after the holiday."); |
| 177 |
#9 Testing 5 days loan |
| 178 |
is(CalcDateDue(dt_from_string('2014-06-23T12:00:00', 'iso'), 0, 'BIB',{categorycode => 0, dateexpiry => '2020'}, 0), dt_from_string('2014-06-29T23:59:00', 'iso'), "Testing 5 days loan. Loan period is extended by 1 because of the holiday."); |
| 179 |
|
| 180 |
#C4::Circulation : DateDue Mode |
| 181 |
|
| 182 |
my $sessionCircDate = DBD::Mock::Session->new('sessionCircDate' => ( |
| 183 |
# Mock queries for test 10 |
| 184 |
{ |
| 185 |
statement => qr/\s*SELECT issuelength, lengthunit, renewalperiod/, |
| 186 |
results => [ |
| 187 |
[ 'issuelength', 'lengthunit', 'renewalperiod' ], |
| 188 |
[ 5, 'hours' , 0 ] |
| 189 |
] |
| 190 |
}, |
| 191 |
{ |
| 192 |
statement => 'SELECT weekday FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NOT NULL', |
| 193 |
results => DBD::Mock->NULL_RESULTSET |
| 194 |
}, |
| 195 |
{ |
| 196 |
statement => 'SELECT day, month FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NULL', |
| 197 |
results => [ |
| 198 |
['day', 'month'], |
| 199 |
[24,6] |
| 200 |
] |
| 201 |
}, |
| 202 |
{ #GetIssuingRules called in GetHardDueDate |
| 203 |
statement => qr/select \* from issuingrules/, |
| 204 |
results => [ |
| 205 |
['hardduedate', 'hardduedatecompare'], |
| 206 |
[undef,-1] |
| 207 |
] |
| 208 |
}, |
| 209 |
{ #Opening hours |
| 210 |
statement => 'SELECT * FROM openinghours ', |
| 211 |
results => [ |
| 212 |
['branchcode', 'weekcode', 'openhour', 'closehour'], |
| 213 |
['BIB', 0, '09:00:00', '17:00:00'], |
| 214 |
['BIB', 1, '09:00:00', '17:00:00'], |
| 215 |
['BIB', 2, '09:00:00', '17:00:00'], |
| 216 |
['BIB', 3, '09:00:00', '17:00:00'], |
| 217 |
['BIB', 4, '09:00:00', '17:00:00'], |
| 218 |
['BIB', 5, '09:00:00', '17:00:00'], |
| 219 |
['BIB', 6, '09:00:00', '17:00:00'] |
| 220 |
] |
| 221 |
}, |
| 222 |
# Mock Queries for test 11 |
| 223 |
{ |
| 224 |
statement => qr/\s*SELECT issuelength, lengthunit, renewalperiod/, |
| 225 |
results => [ |
| 226 |
[ 'issuelength', 'lengthunit', 'renewalperiod' ], |
| 227 |
[ 24, 'hours' , 0 ] |
| 228 |
] |
| 229 |
}, |
| 230 |
{ |
| 231 |
statement => 'SELECT weekday FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NOT NULL', |
| 232 |
results => DBD::Mock->NULL_RESULTSET |
| 233 |
}, |
| 234 |
{ |
| 235 |
statement => 'SELECT day, month FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NULL', |
| 236 |
results => [ |
| 237 |
['day', 'month'], |
| 238 |
[24,6] |
| 239 |
] |
| 240 |
}, |
| 241 |
{ #GetIssuingRules called in GetHardDueDate |
| 242 |
statement => qr/select \* from issuingrules/, |
| 243 |
results => [ |
| 244 |
['hardduedate', 'hardduedatecompare'], |
| 245 |
[undef,-1] |
| 246 |
] |
| 247 |
}, |
| 248 |
{ #Opening hours |
| 249 |
statement => 'SELECT * FROM openinghours ', |
| 250 |
results => [ |
| 251 |
['branchcode', 'weekcode', 'openhour', 'closehour'], |
| 252 |
['BIB', 0, '09:00:00', '17:00:00'], |
| 253 |
['BIB', 1, '09:00:00', '17:00:00'], |
| 254 |
['BIB', 2, '09:00:00', '17:00:00'], |
| 255 |
['BIB', 3, '09:00:00', '17:00:00'], |
| 256 |
['BIB', 4, '09:00:00', '17:00:00'], |
| 257 |
['BIB', 5, '09:00:00', '17:00:00'], |
| 258 |
['BIB', 6, '09:00:00', '17:00:00'] |
| 259 |
] |
| 260 |
}, |
| 261 |
# Mock queries for test 12 |
| 262 |
{ |
| 263 |
statement => qr/\s*SELECT issuelength, lengthunit, renewalperiod/, |
| 264 |
results => [ |
| 265 |
[ 'issuelength', 'lengthunit', 'renewalperiod' ], |
| 266 |
[ 5, 'days' , 0 ] |
| 267 |
] |
| 268 |
}, |
| 269 |
{ |
| 270 |
statement => 'SELECT weekday FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NOT NULL', |
| 271 |
results => DBD::Mock->NULL_RESULTSET |
| 272 |
}, |
| 273 |
{ |
| 274 |
statement => 'SELECT day, month FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NULL', |
| 275 |
results => [ |
| 276 |
['day', 'month'], |
| 277 |
[24,6] |
| 278 |
] |
| 279 |
}, |
| 280 |
{ #GetIssuingRules called in GetHardDueDate |
| 281 |
statement => qr/select \* from issuingrules/, |
| 282 |
results => [ |
| 283 |
['hardduedate', 'hardduedatecompare'], |
| 284 |
[undef,-1] |
| 285 |
] |
| 286 |
} |
| 287 |
)); |
| 288 |
$module_context->mock('preference',sub {return 'DateDue';}); |
| 289 |
print " useDaysMode set to 'DateDue'\n"; |
| 290 |
|
| 291 |
$dbh->{ mock_session } = $sessionCircDate; |
| 292 |
|
| 293 |
#10 Testing 5 hours loan |
| 294 |
is(CalcDateDue(dt_from_string('2014-06-23T14:00:00', 'iso'), 0, 'BIB',{categorycode => 0, dateexpiry => '2020'}, 0), dt_from_string('2014-06-23T17:00:00', 'iso'), "Testing hourly loans. Due at close time."); |
| 295 |
#11 Testing single day loan |
| 296 |
is(CalcDateDue(dt_from_string('2014-06-23T12:00:00', 'iso'), 0, 'BIB',{categorycode => 0, dateexpiry => '2020'}, 0), dt_from_string('2014-06-25T10:00:00', 'iso'), "Testing single day loan due the day after the holiday."); |
| 297 |
#12 Testing 5 days loan |
| 298 |
is(CalcDateDue(dt_from_string('2014-06-23T12:00:00', 'iso'), 0, 'BIB',{categorycode => 0, dateexpiry => '2020'}, 0), dt_from_string('2014-06-28T23:59:00', 'iso'), "Testing 5 days loan. Unaffected by the single day holiday."); |
| 299 |
} |
| 300 |
|
| 301 |
{ #C4::Overdues |
| 302 |
print "\nTesting C4::Overdues\n"; |
| 303 |
$module_context->mock('preference',sub {return 'noFinesWhenClosed';}); |
| 304 |
print " finesCalendar syspref set to 'noFinesWhenClosed'\n"; |
| 305 |
my $sessionOver = DBD::Mock::Session->new('sessionOver' =>( |
| 306 |
{ #Opening hours |
| 307 |
statement => 'SELECT * FROM openinghours ', |
| 308 |
results => [ |
| 309 |
['branchcode', 'weekcode', 'openhour', 'closehour'], |
| 310 |
['BIB', 0, '09:00:00', '17:00:00'], |
| 311 |
['BIB', 1, '09:00:00', '17:00:00'], |
| 312 |
['BIB', 2, '09:00:00', '17:00:00'], |
| 313 |
['BIB', 3, '09:00:00', '17:00:00'], |
| 314 |
['BIB', 4, '09:00:00', '17:00:00'], |
| 315 |
['BIB', 5, '09:00:00', '17:00:00'], |
| 316 |
['BIB', 6, '09:00:00', '17:00:00'] |
| 317 |
] |
| 318 |
}, |
| 319 |
{ |
| 320 |
statement => 'SELECT weekday FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NOT NULL', |
| 321 |
results => DBD::Mock->NULL_RESULTSET |
| 322 |
}, |
| 323 |
{ |
| 324 |
statement => 'SELECT day, month FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NULL', |
| 325 |
results => [ |
| 326 |
['day', 'month'], |
| 327 |
[24,6] |
| 328 |
] |
| 329 |
} |
| 330 |
)); |
| 331 |
$dbh->{ mock_session } = $sessionOver; |
| 332 |
is(_get_chargeable_units('hours', dt_from_string('2014-06-23T12:00:00', 'iso'), dt_from_string('2014-06-25T10:00:00', 'iso'), 'BIB', getOpeningHours()), 6, "Test if _get_chargeable_units takes opening hours and holidays into account."); |
| 333 |
} |