Merge branch 'new/security-release-19.11.09' into 19.11.x
[koha.git] / t / db_dependent / Holidays.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 => 17;
21
22 use DateTime;
23 use DateTime::TimeZone;
24
25 use t::lib::TestBuilder;
26 use C4::Context;
27 use Koha::Database;
28 use Koha::DateUtils;
29
30
31 BEGIN {
32     use_ok('Koha::Calendar');
33     use_ok('C4::Calendar');
34 }
35
36 my $schema = Koha::Database->new->schema;
37 my $dbh = C4::Context->dbh;
38 my $builder = t::lib::TestBuilder->new;
39
40 subtest 'exception_holidays() tests' => sub {
41
42     plan tests => 2;
43
44     $schema->storage->txn_begin;
45
46     my $branch = $builder->build( { source => 'Branch' } )->{branchcode};
47     my $branch2 = $builder->build( { source => 'Branch' } )->{branchcode};
48
49     $dbh->do("DELETE FROM special_holidays");
50     # Clear cache
51     Koha::Caches->get_instance->flush_all;
52
53     my $holiday2add = dt_from_string("2030-07-07");
54     C4::Calendar->new( branchcode => $branch )->insert_day_month_holiday(
55         day         => $holiday2add->day(),
56         month       => $holiday2add->month(),
57         year        => $holiday2add->year(),
58         title       => 'A holiday',
59         description => "This is a holiday, for now",
60     );
61
62     C4::Calendar->new( branchcode => $branch )->insert_exception_holiday(
63         day         => 7,
64         month       => 7,
65         year        => 2020,
66         title       => 'Not a holiday',
67         description => 'This date should not be a holiday',
68     );
69
70     my $calendar = Koha::Calendar->new( branchcode => $branch2 );
71     $calendar->exception_holidays(); #This sets exception holiday in cache
72     $calendar = Koha::Calendar->new( branchcode => $branch );
73     is( $calendar->is_holiday( dt_from_string('2020-07-07') ), 0, "The date is not a holiday");
74
75     $dbh->do("DELETE FROM special_holidays");
76     # Clear cache
77     Koha::Caches->get_instance->flush_all;
78
79     # Artificially set timezone
80     my $timezone = 'America/Santiago';
81     $ENV{TZ} = $timezone;
82     use POSIX qw(tzset);
83     tzset;
84
85     $calendar = Koha::Calendar->new( branchcode => $branch );
86
87     C4::Calendar->new( branchcode => $branch )->insert_exception_holiday(
88         day         => 6,
89         month       => 9,
90         year        => 2015,
91         title       => 'Invalid date',
92         description => 'Invalid date description',
93     );
94
95     my $exception_holiday = $calendar->exception_holidays->iterator->next;
96     my $now_dt            = DateTime->now;
97
98     my $diff;
99     eval { $diff = $calendar->days_between( $now_dt, $exception_holiday ) };
100     unlike(
101         $@,
102         qr/Invalid local time for date in time zone: America\/Santiago/,
103         'Avoid invalid datetime due to DST'
104     );
105
106     $schema->storage->txn_rollback;
107 };
108
109 $schema->storage->txn_begin;
110
111 # Create two fresh branches for the tests
112 my $branch_1 = $builder->build({ source => 'Branch' })->{ branchcode };
113 my $branch_2 = $builder->build({ source => 'Branch' })->{ branchcode };
114
115 C4::Calendar->new( branchcode => $branch_1 )->insert_week_day_holiday(
116     weekday     => 0,
117     title       => '',
118     description => 'Sundays',
119 );
120
121 my $holiday2add = dt_from_string("2015-01-01");
122 C4::Calendar->new( branchcode => $branch_1 )->insert_day_month_holiday(
123     day         => $holiday2add->day(),
124     month       => $holiday2add->month(),
125     year        => $holiday2add->year(),
126     title       => '',
127     description => "New Year's Day",
128 );
129 $holiday2add = dt_from_string("2014-12-25");
130 C4::Calendar->new( branchcode => $branch_1 )->insert_day_month_holiday(
131     day         => $holiday2add->day(),
132     month       => $holiday2add->month(),
133     year        => $holiday2add->year(),
134     title       => '',
135     description => 'Christmas',
136 );
137
138 my $koha_calendar = Koha::Calendar->new( branchcode => $branch_1 );
139 my $c4_calendar = C4::Calendar->new( branchcode => $branch_1 );
140
141 isa_ok( $koha_calendar, 'Koha::Calendar', 'Koha::Calendar class returned' );
142 isa_ok( $c4_calendar,   'C4::Calendar',   'C4::Calendar class returned' );
143
144 my $sunday = DateTime->new(
145     year  => 2011,
146     month => 6,
147     day   => 26,
148 );
149 my $monday = DateTime->new(
150     year  => 2011,
151     month => 6,
152     day   => 27,
153 );
154 my $christmas = DateTime->new(
155     year  => 2032,
156     month => 12,
157     day   => 25,
158 );
159 my $newyear = DateTime->new(
160     year  => 2035,
161     month => 1,
162     day   => 1,
163 );
164
165 is( $koha_calendar->is_holiday($sunday),    1, 'Sunday is a closed day' );
166 is( $koha_calendar->is_holiday($monday),    0, 'Monday is not a closed day' );
167 is( $koha_calendar->is_holiday($christmas), 1, 'Christmas is a closed day' );
168 is( $koha_calendar->is_holiday($newyear),   1, 'New Years day is a closed day' );
169
170 $dbh->do("DELETE FROM repeatable_holidays");
171 $dbh->do("DELETE FROM special_holidays");
172
173 my $custom_holiday = DateTime->new(
174     year  => 2013,
175     month => 11,
176     day   => 12,
177 );
178
179 my $today = dt_from_string();
180 C4::Calendar->new( branchcode => $branch_2 )->insert_single_holiday(
181     day         => $today->day(),
182     month       => $today->month(),
183     year        => $today->year(),
184     title       => "$today",
185     description => "$today",
186 );
187
188 is( Koha::Calendar->new( branchcode => $branch_2 )->is_holiday( $today ), 1, "Today is a holiday for $branch_2" );
189 is( Koha::Calendar->new( branchcode => $branch_1 )->is_holiday( $today ), 0, "Today is not a holiday for $branch_1");
190
191 # Few tests for exception holidays
192 my ( $diff, $cal, $special );
193 $dbh->do("DELETE FROM special_holidays");
194 _add_exception( $today, $branch_1, 'Today' );
195 $cal = Koha::Calendar->new( branchcode => $branch_1 );
196 $special = $cal->exception_holidays;
197 is( $special->count, 1, 'One exception holiday added' );
198
199 my $tomorrow= dt_from_string();
200 $tomorrow->add_duration( DateTime::Duration->new(days => 1) );
201 _add_exception( $tomorrow, $branch_1, 'Tomorrow' );
202 $cal = Koha::Calendar->new( branchcode => $branch_1 );
203 $special = $cal->exception_holidays;
204 is( $special->count, 2, 'Set of exception holidays contains two dates' );
205
206 $diff = $today->delta_days( $special->min )->in_units('days');
207 is( $diff, 0, 'Lowest exception holiday is today' );
208 $diff = $tomorrow->delta_days( $special->max )->in_units('days');
209 is( $diff, 0, 'Highest exception holiday is tomorrow' );
210
211 C4::Calendar->new( branchcode => $branch_1 )->delete_holiday(
212     weekday => $tomorrow->day_of_week,
213     day     => $tomorrow->day,
214     month   => $tomorrow->month,
215     year    => $tomorrow->year,
216 );
217 $cal = Koha::Calendar->new( branchcode => $branch_1 );
218 $special = $cal->exception_holidays;
219 is( $special->count, 1, 'Set of exception holidays back to one' );
220
221 sub _add_exception {
222     my ( $dt, $branch, $descr ) = @_;
223     C4::Calendar->new( branchcode => $branch )->insert_exception_holiday(
224         day         => $dt->day,
225         month       => $dt->month,
226         year        => $dt->year,
227         title       => $descr,
228         description => $descr,
229     );
230 }
231
232 $schema->storage->txn_rollback;
233
234 subtest 'copy_to_branch' => sub {
235
236     plan tests => 8;
237
238     $schema->storage->txn_begin;
239
240     my $branch1 = $builder->build( { source => 'Branch' } )->{ branchcode };
241     my $calendar1 = C4::Calendar->new( branchcode => $branch1 );
242     my $sunday = dt_from_string("2020-03-15");
243     $calendar1->insert_week_day_holiday(
244         weekday     => 0,
245         title       => '',
246         description => 'Sundays',
247     );
248
249     my $day_month = dt_from_string("2020-03-17");
250     $calendar1->insert_day_month_holiday(
251         day         => $day_month->day(),
252         month       => $day_month->month(),
253         year        => $day_month->year(),
254         title       => '',
255         description => "",
256     );
257
258     my $future_date = dt_from_string("9999-12-31");
259     $calendar1->insert_single_holiday(
260         day         => $future_date->day(),
261         month       => $future_date->month(),
262         year        => $future_date->year(),
263         title       => "",
264         description => "",
265     );
266
267     my $future_exception = dt_from_string("9999-12-30");
268     $calendar1->insert_exception_holiday(
269         day         => $future_exception->day(),
270         month       => $future_exception->month(),
271         year        => $future_exception->year(),
272         title       => "",
273         description => "",
274     );
275
276     my $past_date = dt_from_string("2019-11-20");
277     $calendar1->insert_single_holiday(
278         day         => $past_date->day(),
279         month       => $past_date->month(),
280         year        => $past_date->year(),
281         title       => "",
282         description => "",
283     );
284
285     my $past_exception = dt_from_string("2020-03-09");
286     $calendar1->insert_exception_holiday(
287         day         => $past_exception->day(),
288         month       => $past_exception->month(),
289         year        => $past_exception->year(),
290         title       => "",
291         description => "",
292     );
293
294     my $branch2 = $builder->build( { source => 'Branch' } )->{branchcode};
295
296     C4::Calendar->new( branchcode => $branch1 )->copy_to_branch( $branch2 );
297
298     my $calendar2 = C4::Calendar->new( branchcode => $branch2 );
299     my $exceptions = $calendar2->get_exception_holidays;
300
301     is( $calendar2->isHoliday( $sunday->day, $sunday->month, $sunday->year ), 1, "Weekday holiday copied to branch 2" );
302     is( $calendar2->isHoliday( $day_month->day, $day_month->month, $day_month->year ), 1, "Day/month holiday copied to branch 2" );
303     is( $calendar2->isHoliday( $future_date->day, $future_date->month, $future_date->year ), 1, "Single holiday copied to branch 2" );
304     is( ( grep { $_->{date} eq "9999-12-30"} values %$exceptions ), 1, "Exception holiday copied to branch 2" );
305     is( $calendar2->isHoliday( $past_date->day, $past_date->month, $past_date->year ), 0, "Don't copy past single holidays" );
306     is( ( grep { $_->{date} eq "2020-03-09"} values %$exceptions ), 0, "Don't copy past exception holidays " );
307
308     C4::Calendar->new( branchcode => $branch1 )->copy_to_branch( $branch2 );
309
310     #Select all rows with same values from database
311     my $dbh = C4::Context->dbh;
312     my $get_repeatable_holidays = "SELECT a.* FROM repeatable_holidays a
313         JOIN (SELECT branchcode, weekday, day, month, COUNT(*)
314         FROM repeatable_holidays
315         GROUP BY branchcode, weekday, day, month HAVING count(*) > 1) b
316         ON a.branchcode = b.branchcode
317         AND ( a.weekday = b.weekday OR (a.day = b.day AND a.month = b.month))
318         ORDER BY a.branchcode;";
319     my $sth  = $dbh->prepare($get_repeatable_holidays);
320     $sth->execute;
321
322     my @repeatable_holidays;
323     while(my $row = $sth->fetchrow_hashref){
324         push @repeatable_holidays, $row
325     }
326
327     is( scalar(@repeatable_holidays), 0, "None of the repeatable holidays were doubled");
328
329     my $get_special_holidays = "SELECT a.* FROM special_holidays a
330     JOIN (SELECT branchcode, day, month, year, isexception, COUNT(*)
331     FROM special_holidays
332     GROUP BY branchcode, day, month, year, isexception HAVING count(*) > 1) b
333     ON a.branchcode = b.branchcode
334     AND a.day = b.day AND a.month = b.month AND a.year = b.year AND a.isexception = b.isexception
335     ORDER BY a.branchcode;";
336     $sth  = $dbh->prepare($get_special_holidays);
337     $sth->execute;
338
339     my @special_holidays;
340     while(my $row = $sth->fetchrow_hashref){
341         push @special_holidays, $row
342     }
343
344     is( scalar(@special_holidays), 0, "None of the special holidays were doubled");
345
346     $schema->storage->txn_rollback;
347
348 };
349
350 # Clear cache
351 Koha::Caches->get_instance->flush_all;