Bug 15839: Koha::Reviews - Add Koha::Review[s] classes
[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 => 15;
21 use DateTime;
22 use DateTime::TimeZone;
23
24 use t::lib::TestBuilder;
25 use C4::Context;
26 use Koha::Database;
27 use Koha::DateUtils;
28
29
30 BEGIN {
31     use_ok('Koha::Calendar');
32     use_ok('C4::Calendar');
33 }
34
35 my $schema = Koha::Database->new->schema;
36 $schema->storage->txn_begin;
37
38 my $dbh = C4::Context->dbh();
39
40 my $builder = t::lib::TestBuilder->new();
41 # Create two fresh branches for the tests
42 my $branch_1 = $builder->build({ source => 'Branch' })->{ branchcode };
43 my $branch_2 = $builder->build({ source => 'Branch' })->{ branchcode };
44
45 C4::Calendar->new( branchcode => $branch_1 )->insert_week_day_holiday(
46     weekday     => 0,
47     title       => '',
48     description => 'Sundays',
49 );
50
51 my $holiday2add = dt_from_string("2015-01-01");
52 C4::Calendar->new( branchcode => $branch_1 )->insert_day_month_holiday(
53     day         => $holiday2add->day(),
54     month       => $holiday2add->month(),
55     year        => $holiday2add->year(),
56     title       => '',
57     description => "New Year's Day",
58 );
59 $holiday2add = dt_from_string("2014-12-25");
60 C4::Calendar->new( branchcode => $branch_1 )->insert_day_month_holiday(
61     day         => $holiday2add->day(),
62     month       => $holiday2add->month(),
63     year        => $holiday2add->year(),
64     title       => '',
65     description => 'Christmas',
66 );
67
68 my $koha_calendar = Koha::Calendar->new( branchcode => $branch_1 );
69 my $c4_calendar = C4::Calendar->new( branchcode => $branch_1 );
70
71 isa_ok( $koha_calendar, 'Koha::Calendar', 'Koha::Calendar class returned' );
72 isa_ok( $c4_calendar,   'C4::Calendar',   'C4::Calendar class returned' );
73
74 my $sunday = DateTime->new(
75     year  => 2011,
76     month => 6,
77     day   => 26,
78 );
79 my $monday = DateTime->new(
80     year  => 2011,
81     month => 6,
82     day   => 27,
83 );
84 my $christmas = DateTime->new(
85     year  => 2032,
86     month => 12,
87     day   => 25,
88 );
89 my $newyear = DateTime->new(
90     year  => 2035,
91     month => 1,
92     day   => 1,
93 );
94
95 is( $koha_calendar->is_holiday($sunday),    1, 'Sunday is a closed day' );
96 is( $koha_calendar->is_holiday($monday),    0, 'Monday is not a closed day' );
97 is( $koha_calendar->is_holiday($christmas), 1, 'Christmas is a closed day' );
98 is( $koha_calendar->is_holiday($newyear),   1, 'New Years day is a closed day' );
99
100 $dbh->do("DELETE FROM repeatable_holidays");
101 $dbh->do("DELETE FROM special_holidays");
102
103 my $custom_holiday = DateTime->new(
104     year  => 2013,
105     month => 11,
106     day   => 12,
107 );
108
109 my $today = dt_from_string();
110 C4::Calendar->new( branchcode => $branch_2 )->insert_single_holiday(
111     day         => $today->day(),
112     month       => $today->month(),
113     year        => $today->year(),
114     title       => "$today",
115     description => "$today",
116 );
117
118 is( Koha::Calendar->new( branchcode => $branch_2 )->is_holiday( $today ), 1, "Today is a holiday for $branch_2" );
119 is( Koha::Calendar->new( branchcode => $branch_1 )->is_holiday( $today ), 0, "Today is not a holiday for $branch_1");
120
121 # Few tests for exception holidays
122 my ( $diff, $cal, $special );
123 $dbh->do("DELETE FROM special_holidays");
124 _add_exception( $today, $branch_1, 'Today' );
125 $cal = Koha::Calendar->new( branchcode => $branch_1 );
126 $special = $cal->exception_holidays;
127 is( $special->count, 1, 'One exception holiday added' );
128
129 my $tomorrow= dt_from_string();
130 $tomorrow->add_duration( DateTime::Duration->new(days => 1) );
131 _add_exception( $tomorrow, $branch_1, 'Tomorrow' );
132 $cal = Koha::Calendar->new( branchcode => $branch_1 );
133 $special = $cal->exception_holidays;
134 is( $special->count, 2, 'Set of exception holidays contains two dates' );
135
136 $diff = $today->delta_days( $special->min )->in_units('days');
137 is( $diff, 0, 'Lowest exception holiday is today' );
138 $diff = $tomorrow->delta_days( $special->max )->in_units('days');
139 is( $diff, 0, 'Highest exception holiday is tomorrow' );
140
141 C4::Calendar->new( branchcode => $branch_1 )->delete_holiday(
142     weekday => $tomorrow->day_of_week,
143     day     => $tomorrow->day,
144     month   => $tomorrow->month,
145     year    => $tomorrow->year,
146 );
147 $cal = Koha::Calendar->new( branchcode => $branch_1 );
148 $special = $cal->exception_holidays;
149 is( $special->count, 1, 'Set of exception holidays back to one' );
150
151 sub _add_exception {
152     my ( $dt, $branch, $descr ) = @_;
153     C4::Calendar->new( branchcode => $branch )->insert_exception_holiday(
154         day         => $dt->day,
155         month       => $dt->month,
156         year        => $dt->year,
157         title       => $descr,
158         description => $descr,
159     );
160 }
161
162 $schema->storage->txn_rollback;
163
164 1;