Lines 17-23
Link Here
|
17 |
|
17 |
|
18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
19 |
|
19 |
|
20 |
use Test::More tests => 3; |
20 |
use Test::More tests => 4; |
|
|
21 |
use Time::Fake; |
21 |
use t::lib::TestBuilder; |
22 |
use t::lib::TestBuilder; |
22 |
|
23 |
|
23 |
use DateTime; |
24 |
use DateTime; |
Lines 91-94
subtest 'crossing_DST' => sub {
Link Here
|
91 |
); |
92 |
); |
92 |
}; |
93 |
}; |
93 |
|
94 |
|
|
|
95 |
subtest 'hours_between' => sub { |
96 |
|
97 |
plan tests => 2; |
98 |
|
99 |
# November 2019 |
100 |
# Su Mo Tu We Th Fr Sa |
101 |
# 1 2 |
102 |
# 3 4 *5* 6 7 8 9 |
103 |
# 10 11 12 13 14 15 16 |
104 |
# 17 18 19 20 21 22 23 |
105 |
# 24 25 26 27 28 29 30 |
106 |
|
107 |
my $now = dt_from_string('2019-11-05 12:34:56'); # Today is 2019 Nov 5th |
108 |
my $nov_6 = dt_from_string('2019-11-06 12:34:56'); |
109 |
my $nov_7 = dt_from_string('2019-11-07 12:34:56'); |
110 |
my $nov_12 = dt_from_string('2019-11-12 12:34:56'); |
111 |
my $nov_13 = dt_from_string('2019-11-13 12:34:56'); |
112 |
my $nov_15 = dt_from_string('2019-11-15 12:34:56'); |
113 |
Time::Fake->offset($now->epoch); |
114 |
|
115 |
subtest 'No holiday' => sub { |
116 |
|
117 |
plan tests => 2; |
118 |
|
119 |
my $library = $builder->build_object({ class => 'Koha::Libraries' }); |
120 |
my $calendar = Koha::Calendar->new( branchcode => $library->branchcode ); |
121 |
|
122 |
subtest 'Same hours' => sub { |
123 |
|
124 |
plan tests => 4; |
125 |
|
126 |
# Between 5th and 6th |
127 |
my $diff_hours = $calendar->hours_between( $now, $nov_6 )->hours; |
128 |
is( $diff_hours, 1 * 24, '' ); |
129 |
|
130 |
# Between 5th and 7th |
131 |
$diff_hours = $calendar->hours_between( $now, $nov_7 )->hours; |
132 |
is( $diff_hours, 2 * 24, '' ); |
133 |
|
134 |
# Between 5th and 12th |
135 |
$diff_hours = $calendar->hours_between( $now, $nov_12 )->hours; |
136 |
is( $diff_hours, 7 * 24, '' ); |
137 |
|
138 |
# Between 5th and 15th |
139 |
$diff_hours = $calendar->hours_between( $now, $nov_15 )->hours; |
140 |
is( $diff_hours, 10 * 24, '' ); |
141 |
}; |
142 |
|
143 |
subtest 'Different hours' => sub { |
144 |
|
145 |
plan tests => 4; |
146 |
|
147 |
# Between 5th and 6th |
148 |
my $diff_hours = $calendar->hours_between( $now, $nov_6->clone->subtract(hours => 3) )->hours; |
149 |
is( $diff_hours, 1 * 24 - 3, '' ); |
150 |
|
151 |
# Between 5th and 7th |
152 |
$diff_hours = $calendar->hours_between( $now, $nov_7->clone->subtract(hours => 3) )->hours; |
153 |
is( $diff_hours, 2 * 24 - 3, '' ); |
154 |
|
155 |
# Between 5th and 12th |
156 |
$diff_hours = $calendar->hours_between( $now, $nov_12->clone->subtract(hours => 3) )->hours; |
157 |
is( $diff_hours, 7 * 24 - 3, '' ); |
158 |
|
159 |
# Between 5th and 15th |
160 |
$diff_hours = $calendar->hours_between( $now, $nov_15->clone->subtract(hours => 3) )->hours; |
161 |
is( $diff_hours, 10 * 24 - 3, '' ); |
162 |
}; |
163 |
}; |
164 |
|
165 |
subtest 'With holiday' => sub { |
166 |
plan tests => 2; |
167 |
|
168 |
my $library = $builder->build_object({ class => 'Koha::Libraries' }); |
169 |
|
170 |
# Wednesdays are closed |
171 |
my $dbh = C4::Context->dbh; |
172 |
$dbh->do(q| |
173 |
INSERT INTO repeatable_holidays (branchcode,weekday,day,month,title,description) |
174 |
VALUES ( ?, ?, NULL, NULL, ?, '' ) |
175 |
|, undef, $library->branchcode, 3, 'Closed on Wednesday'); |
176 |
|
177 |
my $calendar = Koha::Calendar->new( branchcode => $library->branchcode ); |
178 |
|
179 |
subtest 'Same hours' => sub { |
180 |
plan tests => 5; |
181 |
|
182 |
my $diff_hours; |
183 |
|
184 |
# Between 5th and 6th |
185 |
$diff_hours = $calendar->hours_between( $now, $nov_6 )->hours; |
186 |
is( $diff_hours, 0 * 24, '' ); # FIXME Is this really should be 0? |
187 |
|
188 |
# Between 5th and 7th |
189 |
$diff_hours = $calendar->hours_between( $now, $nov_7 )->hours; |
190 |
is( $diff_hours, 2 * 24 - 1 * 24, '' ); |
191 |
|
192 |
# Between 5th and 12th |
193 |
$diff_hours = $calendar->hours_between( $now, $nov_12 )->hours; |
194 |
is( $diff_hours, 7 * 24 - 1 * 24, '' ); |
195 |
|
196 |
# Between 5th and 13th |
197 |
$diff_hours = $calendar->hours_between( $now, $nov_13 )->hours; |
198 |
is( $diff_hours, 8 * 24 - 2 * 24, '' ); |
199 |
|
200 |
# Between 5th and 15th |
201 |
$diff_hours = $calendar->hours_between( $now, $nov_15 )->hours; |
202 |
is( $diff_hours, 10 * 24 - 2 * 24, '' ); |
203 |
}; |
204 |
|
205 |
subtest 'Different hours' => sub { |
206 |
plan tests => 6; |
207 |
|
208 |
my $diff_hours; |
209 |
|
210 |
# Between 5th and 6th |
211 |
my $duration = $calendar->hours_between( $now, $nov_6->clone->subtract(hours => 3) ); |
212 |
is( $duration->hours, abs(0 * 24 - 3), '' ); # FIXME $duration->hours always return a abs |
213 |
is( $duration->is_negative, 1, ); # FIXME Do really test for that case in our calls to hours_between? |
214 |
|
215 |
# Between 5th and 7th |
216 |
$diff_hours = $calendar->hours_between( $now, $nov_7->clone->subtract(hours => 3) )->hours; |
217 |
is( $diff_hours, 2 * 24 - 1 * 24 - 3, '' ); |
218 |
|
219 |
# Between 5th and 12th |
220 |
$diff_hours = $calendar->hours_between( $now, $nov_12->clone->subtract(hours => 3) )->hours; |
221 |
is( $diff_hours, 7 * 24 - 1 * 24 - 3, '' ); |
222 |
|
223 |
# Between 5th and 13th |
224 |
$diff_hours = $calendar->hours_between( $now, $nov_13->clone->subtract(hours => 3) )->hours; |
225 |
is( $diff_hours, 8 * 24 - 2 * 24 - 3, '' ); |
226 |
|
227 |
# Between 5th and 15th |
228 |
$diff_hours = $calendar->hours_between( $now, $nov_15->clone->subtract(hours => 3) )->hours; |
229 |
is( $diff_hours, 10 * 24 - 2 * 24 - 3, '' ); |
230 |
}; |
231 |
|
232 |
}; |
233 |
|
234 |
}; |
235 |
|
94 |
$schema->storage->txn_rollback(); |
236 |
$schema->storage->txn_rollback(); |
95 |
- |
|
|