Bug 19532: (follow-up) aria-hidden attr on OPAC, and more
[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 => 12;
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 qw( dt_from_string );
29
30 BEGIN {
31     use_ok('Koha::Calendar');
32     use_ok('C4::Calendar', qw( insert_exception_holiday insert_week_day_holiday insert_day_month_holiday insert_single_holiday copy_to_branch get_exception_holidays isHoliday ));
33 }
34
35 my $schema = Koha::Database->new->schema;
36 my $dbh = C4::Context->dbh;
37 my $builder = t::lib::TestBuilder->new;
38
39 subtest 'is_holiday timezone tests' => sub {
40
41     plan tests => 1;
42
43     $schema->storage->txn_begin;
44
45     $dbh->do("DELETE FROM special_holidays");
46     # Clear cache
47     Koha::Caches->get_instance->flush_all;
48
49     # Artificially set timezone
50     my $timezone = 'America/Santiago';
51     $ENV{TZ} = $timezone;
52     use POSIX qw(tzset);
53     tzset;
54
55     my $branch = $builder->build( { source => 'Branch' } )->{branchcode};
56     my $calendar = Koha::Calendar->new( branchcode => $branch );
57
58     C4::Calendar->new( branchcode => $branch )->insert_exception_holiday(
59         day         => 6,
60         month       => 9,
61         year        => 2015,
62         title       => 'Invalid date',
63         description => 'Invalid date description',
64     );
65
66     my $exception_holiday = DateTime->new( day => 6, month => 9, year => 2015 );
67     my $now_dt            = DateTime->now;
68
69     my $diff;
70     eval { $diff = $calendar->days_between( $now_dt, $exception_holiday ) };
71     unlike(
72         $@,
73         qr/Invalid local time for date in time zone: America\/Santiago/,
74         'Avoid invalid datetime due to DST'
75     );
76
77     $schema->storage->txn_rollback;
78 };
79
80 $schema->storage->txn_begin;
81
82 # Create two fresh branches for the tests
83 my $branch_1 = $builder->build({ source => 'Branch' })->{ branchcode };
84 my $branch_2 = $builder->build({ source => 'Branch' })->{ branchcode };
85
86 C4::Calendar->new( branchcode => $branch_1 )->insert_week_day_holiday(
87     weekday     => 0,
88     title       => '',
89     description => 'Sundays',
90 );
91
92 my $holiday2add = dt_from_string("2015-01-01");
93 C4::Calendar->new( branchcode => $branch_1 )->insert_day_month_holiday(
94     day         => $holiday2add->day(),
95     month       => $holiday2add->month(),
96     year        => $holiday2add->year(),
97     title       => '',
98     description => "New Year's Day",
99 );
100 $holiday2add = dt_from_string("2014-12-25");
101 C4::Calendar->new( branchcode => $branch_1 )->insert_day_month_holiday(
102     day         => $holiday2add->day(),
103     month       => $holiday2add->month(),
104     year        => $holiday2add->year(),
105     title       => '',
106     description => 'Christmas',
107 );
108
109 my $koha_calendar = Koha::Calendar->new( branchcode => $branch_1 );
110 my $c4_calendar = C4::Calendar->new( branchcode => $branch_1 );
111
112 isa_ok( $koha_calendar, 'Koha::Calendar', 'Koha::Calendar class returned' );
113 isa_ok( $c4_calendar,   'C4::Calendar',   'C4::Calendar class returned' );
114
115 my $sunday = DateTime->new(
116     year  => 2011,
117     month => 6,
118     day   => 26,
119 );
120 my $monday = DateTime->new(
121     year  => 2011,
122     month => 6,
123     day   => 27,
124 );
125 my $christmas = DateTime->new(
126     year  => 2032,
127     month => 12,
128     day   => 25,
129 );
130 my $newyear = DateTime->new(
131     year  => 2035,
132     month => 1,
133     day   => 1,
134 );
135
136 is( $koha_calendar->is_holiday($sunday),    1, 'Sunday is a closed day' );
137 is( $koha_calendar->is_holiday($monday),    0, 'Monday is not a closed day' );
138 is( $koha_calendar->is_holiday($christmas), 1, 'Christmas is a closed day' );
139 is( $koha_calendar->is_holiday($newyear),   1, 'New Years day is a closed day' );
140
141 $dbh->do("DELETE FROM repeatable_holidays");
142 $dbh->do("DELETE FROM special_holidays");
143
144 my $custom_holiday = DateTime->new(
145     year  => 2013,
146     month => 11,
147     day   => 12,
148 );
149
150 my $today = dt_from_string();
151 C4::Calendar->new( branchcode => $branch_2 )->insert_single_holiday(
152     day         => $today->day(),
153     month       => $today->month(),
154     year        => $today->year(),
155     title       => "$today",
156     description => "$today",
157 );
158
159 is( Koha::Calendar->new( branchcode => $branch_2 )->is_holiday( $today ), 1, "Today is a holiday for $branch_2" );
160 is( Koha::Calendar->new( branchcode => $branch_1 )->is_holiday( $today ), 0, "Today is not a holiday for $branch_1");
161
162 $schema->storage->txn_rollback;
163
164 subtest 'copy_to_branch' => sub {
165
166     plan tests => 8;
167
168     $schema->storage->txn_begin;
169
170     my $branch1 = $builder->build( { source => 'Branch' } )->{ branchcode };
171     my $calendar1 = C4::Calendar->new( branchcode => $branch1 );
172     my $sunday = dt_from_string("2020-03-15");
173     $calendar1->insert_week_day_holiday(
174         weekday     => 0,
175         title       => '',
176         description => 'Sundays',
177     );
178
179     my $day_month = dt_from_string("2020-03-17");
180     $calendar1->insert_day_month_holiday(
181         day         => $day_month->day(),
182         month       => $day_month->month(),
183         year        => $day_month->year(),
184         title       => '',
185         description => "",
186     );
187
188     my $future_date = dt_from_string("9999-12-31");
189     $calendar1->insert_single_holiday(
190         day         => $future_date->day(),
191         month       => $future_date->month(),
192         year        => $future_date->year(),
193         title       => "",
194         description => "",
195     );
196
197     my $future_exception = dt_from_string("9999-12-30");
198     $calendar1->insert_exception_holiday(
199         day         => $future_exception->day(),
200         month       => $future_exception->month(),
201         year        => $future_exception->year(),
202         title       => "",
203         description => "",
204     );
205
206     my $past_date = dt_from_string("2019-11-20");
207     $calendar1->insert_single_holiday(
208         day         => $past_date->day(),
209         month       => $past_date->month(),
210         year        => $past_date->year(),
211         title       => "",
212         description => "",
213     );
214
215     my $past_exception = dt_from_string("2020-03-09");
216     $calendar1->insert_exception_holiday(
217         day         => $past_exception->day(),
218         month       => $past_exception->month(),
219         year        => $past_exception->year(),
220         title       => "",
221         description => "",
222     );
223
224     my $branch2 = $builder->build( { source => 'Branch' } )->{branchcode};
225
226     C4::Calendar->new( branchcode => $branch1 )->copy_to_branch( $branch2 );
227
228     my $calendar2 = C4::Calendar->new( branchcode => $branch2 );
229     my $exceptions = $calendar2->get_exception_holidays;
230
231     is( $calendar2->isHoliday( $sunday->day, $sunday->month, $sunday->year ), 1, "Weekday holiday copied to branch 2" );
232     is( $calendar2->isHoliday( $day_month->day, $day_month->month, $day_month->year ), 1, "Day/month holiday copied to branch 2" );
233     is( $calendar2->isHoliday( $future_date->day, $future_date->month, $future_date->year ), 1, "Single holiday copied to branch 2" );
234     is( ( grep { $_->{date} eq "9999-12-30"} values %$exceptions ), 1, "Exception holiday copied to branch 2" );
235     is( $calendar2->isHoliday( $past_date->day, $past_date->month, $past_date->year ), 0, "Don't copy past single holidays" );
236     is( ( grep { $_->{date} eq "2020-03-09"} values %$exceptions ), 0, "Don't copy past exception holidays " );
237
238     C4::Calendar->new( branchcode => $branch1 )->copy_to_branch( $branch2 );
239
240     #Select all rows with same values from database
241     my $dbh = C4::Context->dbh;
242     my $get_repeatable_holidays = "SELECT a.* FROM repeatable_holidays a
243         JOIN (SELECT branchcode, weekday, day, month, COUNT(*)
244         FROM repeatable_holidays
245         GROUP BY branchcode, weekday, day, month HAVING count(*) > 1) b
246         ON a.branchcode = b.branchcode
247         AND ( a.weekday = b.weekday OR (a.day = b.day AND a.month = b.month))
248         ORDER BY a.branchcode;";
249     my $sth  = $dbh->prepare($get_repeatable_holidays);
250     $sth->execute;
251
252     my @repeatable_holidays;
253     while(my $row = $sth->fetchrow_hashref){
254         push @repeatable_holidays, $row
255     }
256
257     is( scalar(@repeatable_holidays), 0, "None of the repeatable holidays were doubled");
258
259     my $get_special_holidays = "SELECT a.* FROM special_holidays a
260     JOIN (SELECT branchcode, day, month, year, isexception, COUNT(*)
261     FROM special_holidays
262     GROUP BY branchcode, day, month, year, isexception HAVING count(*) > 1) b
263     ON a.branchcode = b.branchcode
264     AND a.day = b.day AND a.month = b.month AND a.year = b.year AND a.isexception = b.isexception
265     ORDER BY a.branchcode;";
266     $sth  = $dbh->prepare($get_special_holidays);
267     $sth->execute;
268
269     my @special_holidays;
270     while(my $row = $sth->fetchrow_hashref){
271         push @special_holidays, $row
272     }
273
274     is( scalar(@special_holidays), 0, "None of the special holidays were doubled");
275
276     $schema->storage->txn_rollback;
277
278 };
279
280 # Clear cache
281 Koha::Caches->get_instance->flush_all;