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