Bug 23974: Add new tests for hours_between
[srvgit] / t / db_dependent / Calendar.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 4;
21 use Time::Fake;
22 use t::lib::TestBuilder;
23
24 use DateTime;
25 use Koha::Caches;
26 use Koha::DateUtils;
27
28 use_ok('Koha::Calendar');
29
30 my $schema  = Koha::Database->new->schema;
31 $schema->storage->txn_begin;
32
33 my $today = dt_from_string();
34 my $holiday_dt = $today->clone;
35 $holiday_dt->add(days => 3);
36
37 Koha::Caches->get_instance()->flush_all();
38
39 my $builder = t::lib::TestBuilder->new();
40 my $library = $builder->build_object({ class => 'Koha::Libraries' });
41 my $holiday = $builder->build(
42     {
43         source => 'SpecialHoliday',
44         value  => {
45             branchcode  => $library->branchcode,
46             day         => $holiday_dt->day,
47             month       => $holiday_dt->month,
48             year        => $holiday_dt->year,
49             title       => 'My holiday',
50             isexception => 0
51         },
52     }
53 );
54
55 my $calendar = Koha::Calendar->new( branchcode => $library->branchcode );
56
57 subtest 'days_forward' => sub {
58
59     plan tests => 4;
60     my $forwarded_dt = $calendar->days_forward( $today, 2 );
61     my $expected = $today->clone->add( days => 2 );
62     is( $forwarded_dt->ymd, $expected->ymd, 'With no holiday on the perioddays_forward should add 2 days' );
63
64     $forwarded_dt = $calendar->days_forward( $today, 5 );
65     $expected = $today->clone->add( days => 6 );
66     is( $forwarded_dt->ymd, $expected->ymd, 'With holiday on the perioddays_forward should add 5 days + 1 day for holiday'
67     );
68
69     $forwarded_dt = $calendar->days_forward( $today, 0 );
70     is( $forwarded_dt->ymd, $today->ymd, '0 day should return start dt' );
71
72     $forwarded_dt = $calendar->days_forward( $today, -2 );
73     is( $forwarded_dt->ymd, $today->ymd, 'negative day should return start dt' );
74 };
75
76 subtest 'crossing_DST' => sub {
77
78     plan tests => 3;
79
80     my $tz = DateTime::TimeZone->new( name => 'America/New_York' );
81     my $start_date = dt_from_string( "2016-03-09 02:29:00", undef, $tz );
82     my $end_date   = dt_from_string( "2017-01-01 00:00:00", undef, $tz );
83     my $days_between = $calendar->days_between( $start_date, $end_date );
84     is( $days_between->delta_days, 298, "Days calculated correctly" );
85     $days_between = $calendar->days_between( $end_date, $start_date );
86     is( $days_between->delta_days, 298, "Swapping returns the same" );
87     my $hours_between = $calendar->hours_between( $start_date, $end_date );
88     is(
89         $hours_between->delta_minutes,
90         298 * 24 * 60 - 149,
91         "Hours (in minutes) calculated correctly"
92     );
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
236 $schema->storage->txn_rollback();