Bug 25793: OAI 'Set' and 'Metadata' dropdowns broken by OPAC jQuery upgrade
[koha.git] / t / db_dependent / Holidays.t
blob023c4a89ff67fbcb9a4a8321aaabd0701d93b9ec
1 #!/usr/bin/perl
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>.
18 use Modern::Perl;
20 use Test::More tests => 17;
22 use DateTime;
23 use DateTime::TimeZone;
25 use t::lib::TestBuilder;
26 use C4::Context;
27 use Koha::Database;
28 use Koha::DateUtils;
31 BEGIN {
32 use_ok('Koha::Calendar');
33 use_ok('C4::Calendar');
36 my $schema = Koha::Database->new->schema;
37 my $dbh = C4::Context->dbh;
38 my $builder = t::lib::TestBuilder->new;
40 subtest 'exception_holidays() tests' => sub {
42 plan tests => 1;
44 $schema->storage->txn_begin;
46 $dbh->do("DELETE FROM special_holidays");
47 # Clear cache
48 Koha::Caches->get_instance->flush_all;
50 # Artificially set timezone
51 my $timezone = 'America/Santiago';
52 $ENV{TZ} = $timezone;
53 use POSIX qw(tzset);
54 tzset;
56 my $branch = $builder->build( { source => 'Branch' } )->{branchcode};
57 my $calendar = Koha::Calendar->new( branchcode => $branch );
59 C4::Calendar->new( branchcode => $branch )->insert_exception_holiday(
60 day => 6,
61 month => 9,
62 year => 2015,
63 title => 'Invalid date',
64 description => 'Invalid date description',
67 my $exception_holiday = $calendar->exception_holidays->iterator->next;
68 my $now_dt = DateTime->now;
70 my $diff;
71 eval { $diff = $calendar->days_between( $now_dt, $exception_holiday ) };
72 unlike(
73 $@,
74 qr/Invalid local time for date in time zone: America\/Santiago/,
75 'Avoid invalid datetime due to DST'
78 $schema->storage->txn_rollback;
81 $schema->storage->txn_begin;
83 # Create two fresh branches for the tests
84 my $branch_1 = $builder->build({ source => 'Branch' })->{ branchcode };
85 my $branch_2 = $builder->build({ source => 'Branch' })->{ branchcode };
87 C4::Calendar->new( branchcode => $branch_1 )->insert_week_day_holiday(
88 weekday => 0,
89 title => '',
90 description => 'Sundays',
93 my $holiday2add = dt_from_string("2015-01-01");
94 C4::Calendar->new( branchcode => $branch_1 )->insert_day_month_holiday(
95 day => $holiday2add->day(),
96 month => $holiday2add->month(),
97 year => $holiday2add->year(),
98 title => '',
99 description => "New Year's Day",
101 $holiday2add = dt_from_string("2014-12-25");
102 C4::Calendar->new( branchcode => $branch_1 )->insert_day_month_holiday(
103 day => $holiday2add->day(),
104 month => $holiday2add->month(),
105 year => $holiday2add->year(),
106 title => '',
107 description => 'Christmas',
110 my $koha_calendar = Koha::Calendar->new( branchcode => $branch_1 );
111 my $c4_calendar = C4::Calendar->new( branchcode => $branch_1 );
113 isa_ok( $koha_calendar, 'Koha::Calendar', 'Koha::Calendar class returned' );
114 isa_ok( $c4_calendar, 'C4::Calendar', 'C4::Calendar class returned' );
116 my $sunday = DateTime->new(
117 year => 2011,
118 month => 6,
119 day => 26,
121 my $monday = DateTime->new(
122 year => 2011,
123 month => 6,
124 day => 27,
126 my $christmas = DateTime->new(
127 year => 2032,
128 month => 12,
129 day => 25,
131 my $newyear = DateTime->new(
132 year => 2035,
133 month => 1,
134 day => 1,
137 is( $koha_calendar->is_holiday($sunday), 1, 'Sunday is a closed day' );
138 is( $koha_calendar->is_holiday($monday), 0, 'Monday is not a closed day' );
139 is( $koha_calendar->is_holiday($christmas), 1, 'Christmas is a closed day' );
140 is( $koha_calendar->is_holiday($newyear), 1, 'New Years day is a closed day' );
142 $dbh->do("DELETE FROM repeatable_holidays");
143 $dbh->do("DELETE FROM special_holidays");
145 my $custom_holiday = DateTime->new(
146 year => 2013,
147 month => 11,
148 day => 12,
151 my $today = dt_from_string();
152 C4::Calendar->new( branchcode => $branch_2 )->insert_single_holiday(
153 day => $today->day(),
154 month => $today->month(),
155 year => $today->year(),
156 title => "$today",
157 description => "$today",
160 is( Koha::Calendar->new( branchcode => $branch_2 )->is_holiday( $today ), 1, "Today is a holiday for $branch_2" );
161 is( Koha::Calendar->new( branchcode => $branch_1 )->is_holiday( $today ), 0, "Today is not a holiday for $branch_1");
163 # Few tests for exception holidays
164 my ( $diff, $cal, $special );
165 $dbh->do("DELETE FROM special_holidays");
166 _add_exception( $today, $branch_1, 'Today' );
167 $cal = Koha::Calendar->new( branchcode => $branch_1 );
168 $special = $cal->exception_holidays;
169 is( $special->count, 1, 'One exception holiday added' );
171 my $tomorrow= dt_from_string();
172 $tomorrow->add_duration( DateTime::Duration->new(days => 1) );
173 _add_exception( $tomorrow, $branch_1, 'Tomorrow' );
174 $cal = Koha::Calendar->new( branchcode => $branch_1 );
175 $special = $cal->exception_holidays;
176 is( $special->count, 2, 'Set of exception holidays contains two dates' );
178 $diff = $today->delta_days( $special->min )->in_units('days');
179 is( $diff, 0, 'Lowest exception holiday is today' );
180 $diff = $tomorrow->delta_days( $special->max )->in_units('days');
181 is( $diff, 0, 'Highest exception holiday is tomorrow' );
183 C4::Calendar->new( branchcode => $branch_1 )->delete_holiday(
184 weekday => $tomorrow->day_of_week,
185 day => $tomorrow->day,
186 month => $tomorrow->month,
187 year => $tomorrow->year,
189 $cal = Koha::Calendar->new( branchcode => $branch_1 );
190 $special = $cal->exception_holidays;
191 is( $special->count, 1, 'Set of exception holidays back to one' );
193 sub _add_exception {
194 my ( $dt, $branch, $descr ) = @_;
195 C4::Calendar->new( branchcode => $branch )->insert_exception_holiday(
196 day => $dt->day,
197 month => $dt->month,
198 year => $dt->year,
199 title => $descr,
200 description => $descr,
204 $schema->storage->txn_rollback;
206 subtest 'copy_to_branch' => sub {
208 plan tests => 8;
210 $schema->storage->txn_begin;
212 my $branch1 = $builder->build( { source => 'Branch' } )->{ branchcode };
213 my $calendar1 = C4::Calendar->new( branchcode => $branch1 );
214 my $sunday = dt_from_string("2020-03-15");
215 $calendar1->insert_week_day_holiday(
216 weekday => 0,
217 title => '',
218 description => 'Sundays',
221 my $day_month = dt_from_string("2020-03-17");
222 $calendar1->insert_day_month_holiday(
223 day => $day_month->day(),
224 month => $day_month->month(),
225 year => $day_month->year(),
226 title => '',
227 description => "",
230 my $future_date = dt_from_string("9999-12-31");
231 $calendar1->insert_single_holiday(
232 day => $future_date->day(),
233 month => $future_date->month(),
234 year => $future_date->year(),
235 title => "",
236 description => "",
239 my $future_exception = dt_from_string("9999-12-30");
240 $calendar1->insert_exception_holiday(
241 day => $future_exception->day(),
242 month => $future_exception->month(),
243 year => $future_exception->year(),
244 title => "",
245 description => "",
248 my $past_date = dt_from_string("2019-11-20");
249 $calendar1->insert_single_holiday(
250 day => $past_date->day(),
251 month => $past_date->month(),
252 year => $past_date->year(),
253 title => "",
254 description => "",
257 my $past_exception = dt_from_string("2020-03-09");
258 $calendar1->insert_exception_holiday(
259 day => $past_exception->day(),
260 month => $past_exception->month(),
261 year => $past_exception->year(),
262 title => "",
263 description => "",
266 my $branch2 = $builder->build( { source => 'Branch' } )->{branchcode};
268 C4::Calendar->new( branchcode => $branch1 )->copy_to_branch( $branch2 );
270 my $calendar2 = C4::Calendar->new( branchcode => $branch2 );
271 my $exceptions = $calendar2->get_exception_holidays;
273 is( $calendar2->isHoliday( $sunday->day, $sunday->month, $sunday->year ), 1, "Weekday holiday copied to branch 2" );
274 is( $calendar2->isHoliday( $day_month->day, $day_month->month, $day_month->year ), 1, "Day/month holiday copied to branch 2" );
275 is( $calendar2->isHoliday( $future_date->day, $future_date->month, $future_date->year ), 1, "Single holiday copied to branch 2" );
276 is( ( grep { $_->{date} eq "9999-12-30"} values %$exceptions ), 1, "Exception holiday copied to branch 2" );
277 is( $calendar2->isHoliday( $past_date->day, $past_date->month, $past_date->year ), 0, "Don't copy past single holidays" );
278 is( ( grep { $_->{date} eq "2020-03-09"} values %$exceptions ), 0, "Don't copy past exception holidays " );
280 C4::Calendar->new( branchcode => $branch1 )->copy_to_branch( $branch2 );
282 #Select all rows with same values from database
283 my $dbh = C4::Context->dbh;
284 my $get_repeatable_holidays = "SELECT a.* FROM repeatable_holidays a
285 JOIN (SELECT branchcode, weekday, day, month, COUNT(*)
286 FROM repeatable_holidays
287 GROUP BY branchcode, weekday, day, month HAVING count(*) > 1) b
288 ON a.branchcode = b.branchcode
289 AND ( a.weekday = b.weekday OR (a.day = b.day AND a.month = b.month))
290 ORDER BY a.branchcode;";
291 my $sth = $dbh->prepare($get_repeatable_holidays);
292 $sth->execute;
294 my @repeatable_holidays;
295 while(my $row = $sth->fetchrow_hashref){
296 push @repeatable_holidays, $row
299 is( scalar(@repeatable_holidays), 0, "None of the repeatable holidays were doubled");
301 my $get_special_holidays = "SELECT a.* FROM special_holidays a
302 JOIN (SELECT branchcode, day, month, year, isexception, COUNT(*)
303 FROM special_holidays
304 GROUP BY branchcode, day, month, year, isexception HAVING count(*) > 1) b
305 ON a.branchcode = b.branchcode
306 AND a.day = b.day AND a.month = b.month AND a.year = b.year AND a.isexception = b.isexception
307 ORDER BY a.branchcode;";
308 $sth = $dbh->prepare($get_special_holidays);
309 $sth->execute;
311 my @special_holidays;
312 while(my $row = $sth->fetchrow_hashref){
313 push @special_holidays, $row
316 is( scalar(@special_holidays), 0, "None of the special holidays were doubled");
318 $schema->storage->txn_rollback;
322 # Clear cache
323 Koha::Caches->get_instance->flush_all;