|
Line 0
Link Here
|
| 0 |
- |
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 => 7; |
| 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 Getoverdues); |
| 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::Circulation |
| 40 |
$module_context->mock('preference',sub {return 'Calendar';}); |
| 41 |
|
| 42 |
my $sessionCirc = DBD::Mock::Session->new('sessionCirc' => ( |
| 43 |
{ |
| 44 |
statement => qr/\s*SELECT issuelength, lengthunit, renewalperiod/, |
| 45 |
results => [ |
| 46 |
[ 'issuelength', 'lengthunit', 'renewalperiod' ], |
| 47 |
[ 30, 'minutes' , 0 ] |
| 48 |
] |
| 49 |
}, |
| 50 |
{ |
| 51 |
statement => 'SELECT weekday FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NOT NULL', |
| 52 |
results => DBD::Mock->NULL_RESULTSET |
| 53 |
}, |
| 54 |
{ |
| 55 |
statement => 'SELECT day, month FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NULL', |
| 56 |
results => [ |
| 57 |
['day', 'month'], |
| 58 |
[24,6] |
| 59 |
] |
| 60 |
}, |
| 61 |
{ |
| 62 |
statement => 'SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 1', |
| 63 |
results => DBD::Mock->NULL_RESULTSET |
| 64 |
}, |
| 65 |
{ |
| 66 |
statement => 'SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 0', |
| 67 |
results => DBD::Mock->NULL_RESULTSET |
| 68 |
}, |
| 69 |
{ #GetIssuingRules called in GetHardDueDate |
| 70 |
statement => qr/select \* from issuingrules/, |
| 71 |
results => [ |
| 72 |
['hardduedate', 'hardduedatecompare'], |
| 73 |
[undef,-1] |
| 74 |
] |
| 75 |
}, |
| 76 |
{ #Opening hours |
| 77 |
statement => 'SELECT * FROM openinghours ', |
| 78 |
results => [ |
| 79 |
['branchcode', 'weekcode', 'openhour', 'closehour'], |
| 80 |
['BIB', 0, '09:00:00', '17:00:00'], |
| 81 |
['BIB', 1, '09:00:00', '17:00:00'], |
| 82 |
['BIB', 2, '09:00:00', '17:00:00'], |
| 83 |
['BIB', 3, '09:00:00', '17:00:00'], |
| 84 |
['BIB', 4, '09:00:00', '17:00:00'], |
| 85 |
['BIB', 5, '09:00:00', '17:00:00'], |
| 86 |
['BIB', 6, '09:00:00', '17:00:00'] |
| 87 |
] |
| 88 |
} |
| 89 |
)); |
| 90 |
$dbh->{ mock_session } = $sessionCirc; |
| 91 |
print "\nTesting C4::Circulation\n"; |
| 92 |
is(CalcDateDue(dt_from_string('2014-06-23T16:40:00', 'iso'), 0, 'BIB',{categorycode => 0, dateexpiry => '2020'}, 0), dt_from_string('2014-06-23T17:00:00', 'iso'), "Testing minute loans. Due time capped at close time."); |
| 93 |
} |
| 94 |
|
| 95 |
{ #C4::Overdues |
| 96 |
print "\nTesting C4::Overdues\n"; |
| 97 |
$module_context->mock('preference',sub {return 'noFinesWhenClosed';}); |
| 98 |
print " finesCalendar syspref set to 'noFinesWhenClosed'\n"; |
| 99 |
my $sessionOver = DBD::Mock::Session->new('sessionOver' =>( |
| 100 |
{ #Opening hours TEST #6 |
| 101 |
statement => 'SELECT * FROM openinghours ', |
| 102 |
results => [ |
| 103 |
['branchcode', 'weekcode', 'openhour', 'closehour'], |
| 104 |
['BIB', 0, '09:00:00', '17:00:00'], |
| 105 |
['BIB', 1, '09:00:00', '17:00:00'], |
| 106 |
['BIB', 2, '09:00:00', '17:00:00'], |
| 107 |
['BIB', 3, '09:00:00', '17:00:00'], |
| 108 |
['BIB', 4, '09:00:00', '17:00:00'], |
| 109 |
['BIB', 5, '09:00:00', '17:00:00'], |
| 110 |
['BIB', 6, '09:00:00', '17:00:00'] |
| 111 |
] |
| 112 |
}, |
| 113 |
{ |
| 114 |
statement => 'SELECT weekday FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NOT NULL', |
| 115 |
results => DBD::Mock->NULL_RESULTSET |
| 116 |
}, |
| 117 |
{ |
| 118 |
statement => 'SELECT day, month FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NULL', |
| 119 |
results => [ |
| 120 |
['day', 'month'], |
| 121 |
[24,6] |
| 122 |
] |
| 123 |
}, |
| 124 |
{ #Opening hours TEST #7 |
| 125 |
statement => 'SELECT * FROM openinghours ', |
| 126 |
results => [ |
| 127 |
['branchcode', 'weekcode', 'openhour', 'closehour'], |
| 128 |
['BIB', 0, '09:00:00', '17:00:00'], |
| 129 |
['BIB', 1, '09:00:00', '17:00:00'], |
| 130 |
['BIB', 2, '09:00:00', '17:00:00'], |
| 131 |
['BIB', 3, '09:00:00', '17:00:00'], |
| 132 |
['BIB', 4, '09:00:00', '17:00:00'], |
| 133 |
['BIB', 5, '09:00:00', '17:00:00'], |
| 134 |
['BIB', 6, '09:00:00', '17:00:00'] |
| 135 |
] |
| 136 |
}, |
| 137 |
{ |
| 138 |
statement => qr/select \* from issuingrules/, |
| 139 |
results => [ |
| 140 |
[ 'issuelength', 'lengthunit', 'fine', 'chargeperiod', 'firstremind'], |
| 141 |
[ 30, 'minutes' , 0.25, 1, 0 ] |
| 142 |
] |
| 143 |
}, |
| 144 |
{ |
| 145 |
statement => 'SELECT weekday FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NOT NULL', |
| 146 |
results => DBD::Mock->NULL_RESULTSET |
| 147 |
}, |
| 148 |
{ |
| 149 |
statement => 'SELECT day, month FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NULL', |
| 150 |
results => DBD::Mock->NULL_RESULTSET |
| 151 |
}, |
| 152 |
{ |
| 153 |
statement => 'SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 1', |
| 154 |
results => DBD::Mock->NULL_RESULTSET |
| 155 |
}, |
| 156 |
{ |
| 157 |
statement => 'SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 0', |
| 158 |
results => DBD::Mock->NULL_RESULTSET |
| 159 |
} |
| 160 |
)); |
| 161 |
$dbh->{ mock_session } = $sessionOver; |
| 162 |
is(_get_chargeable_units('minutes', dt_from_string('2014-06-23T12:00:00', 'iso'), dt_from_string('2014-06-25T10:00:00', 'iso'), 'BIB', getOpeningHours()), 360, "Test if _get_chargeable_units takes opening hours and holidays into account."); |
| 163 |
|
| 164 |
my ($fine) = CalcFine( {itemtype=>'item'}, 0, 'BIB' ,dt_from_string('2014-6-23T12:00:00', 'iso'), dt_from_string('2014-6-23T13:00:00', 'iso'), getOpeningHours()); |
| 165 |
is($fine, 15, 'Fines are calculated correctly for minute loans'); |
| 166 |
} |