3 # This file is part of Koha.
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.
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.
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>.
20 use Test::More tests => 12;
23 use DateTime::TimeZone;
25 use t::lib::TestBuilder;
28 use Koha::DateUtils qw( dt_from_string );
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 ));
35 my $schema = Koha::Database->new->schema;
36 my $dbh = C4::Context->dbh;
37 my $builder = t::lib::TestBuilder->new;
39 subtest 'is_holiday timezone tests' => sub {
43 $schema->storage->txn_begin;
45 $dbh->do("DELETE FROM special_holidays");
47 Koha::Caches->get_instance->flush_all;
49 # Artificially set timezone
50 my $timezone = 'America/Santiago';
55 my $branch = $builder->build( { source => 'Branch' } )->{branchcode};
56 my $calendar = Koha::Calendar->new( branchcode => $branch );
58 C4::Calendar->new( branchcode => $branch )->insert_exception_holiday(
62 title => 'Invalid date',
63 description => 'Invalid date description',
66 my $exception_holiday = DateTime->new( day => 6, month => 9, year => 2015 );
67 my $now_dt = DateTime->now;
70 eval { $diff = $calendar->days_between( $now_dt, $exception_holiday ) };
73 qr/Invalid local time for date in time zone: America\/Santiago/,
74 'Avoid invalid datetime due to DST'
77 $schema->storage->txn_rollback;
80 $schema->storage->txn_begin;
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 };
86 C4::Calendar->new( branchcode => $branch_1 )->insert_week_day_holiday(
89 description => 'Sundays',
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(),
98 description => "New Year's Day",
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(),
106 description => 'Christmas',
109 my $koha_calendar = Koha::Calendar->new( branchcode => $branch_1 );
110 my $c4_calendar = C4::Calendar->new( branchcode => $branch_1 );
112 isa_ok( $koha_calendar, 'Koha::Calendar', 'Koha::Calendar class returned' );
113 isa_ok( $c4_calendar, 'C4::Calendar', 'C4::Calendar class returned' );
115 my $sunday = DateTime->new(
120 my $monday = DateTime->new(
125 my $christmas = DateTime->new(
130 my $newyear = DateTime->new(
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' );
141 $dbh->do("DELETE FROM repeatable_holidays");
142 $dbh->do("DELETE FROM special_holidays");
144 my $custom_holiday = DateTime->new(
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(),
156 description => "$today",
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");
162 $schema->storage->txn_rollback;
164 subtest 'copy_to_branch' => sub {
168 $schema->storage->txn_begin;
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(
176 description => 'Sundays',
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(),
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(),
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(),
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(),
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(),
224 my $branch2 = $builder->build( { source => 'Branch' } )->{branchcode};
226 C4::Calendar->new( branchcode => $branch1 )->copy_to_branch( $branch2 );
228 my $calendar2 = C4::Calendar->new( branchcode => $branch2 );
229 my $exceptions = $calendar2->get_exception_holidays;
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 " );
238 C4::Calendar->new( branchcode => $branch1 )->copy_to_branch( $branch2 );
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);
252 my @repeatable_holidays;
253 while(my $row = $sth->fetchrow_hashref){
254 push @repeatable_holidays, $row
257 is( scalar(@repeatable_holidays), 0, "None of the repeatable holidays were doubled");
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);
269 my @special_holidays;
270 while(my $row = $sth->fetchrow_hashref){
271 push @special_holidays, $row
274 is( scalar(@special_holidays), 0, "None of the special holidays were doubled");
276 $schema->storage->txn_rollback;
281 Koha::Caches->get_instance->flush_all;