use Modern::Perl;
-use Test::More tests => 10;
-use t::lib::TestBuilder;
+use Test::More tests => 16;
+
+use DateTime;
+use DateTime::TimeZone;
+use t::lib::TestBuilder;
use C4::Context;
-use C4::Branch;
+use Koha::Database;
use Koha::DateUtils;
-use DateTime;
-use DateTime::TimeZone;
BEGIN {
use_ok('Koha::Calendar');
use_ok('C4::Calendar');
}
-my $dbh = C4::Context->dbh();
-# Start transaction
-$dbh->{AutoCommit} = 0;
-$dbh->{RaiseError} = 1;
+my $schema = Koha::Database->new->schema;
+my $dbh = C4::Context->dbh;
+my $builder = t::lib::TestBuilder->new;
+
+subtest 'exception_holidays() tests' => sub {
+
+ plan tests => 1;
+
+ $schema->storage->txn_begin;
+
+ $dbh->do("DELETE FROM special_holidays");
+ # Clear cache
+ Koha::Caches->get_instance->flush_all;
+
+ # Artificially set timezone
+ my $timezone = 'America/Santiago';
+ $ENV{TZ} = $timezone;
+ use POSIX qw(tzset);
+ tzset;
+
+ my $branch = $builder->build( { source => 'Branch' } )->{branchcode};
+ my $calendar = Koha::Calendar->new( branchcode => $branch );
+
+ C4::Calendar->new( branchcode => $branch )->insert_exception_holiday(
+ day => 6,
+ month => 9,
+ year => 2015,
+ title => 'Invalid date',
+ description => 'Invalid date description',
+ );
+
+ my $exception_holiday = $calendar->exception_holidays->iterator->next;
+ my $now_dt = DateTime->now;
+
+ my $diff;
+ eval { $diff = $calendar->days_between( $now_dt, $exception_holiday ) };
+ unlike(
+ $@,
+ qr/Invalid local time for date in time zone: America\/Santiago/,
+ 'Avoid invalid datetime due to DST'
+ );
+
+ $schema->storage->txn_rollback;
+};
+
+$schema->storage->txn_begin;
-my $builder = t::lib::TestBuilder->new();
# Create two fresh branches for the tests
my $branch_1 = $builder->build({ source => 'Branch' })->{ branchcode };
my $branch_2 = $builder->build({ source => 'Branch' })->{ branchcode };
is( $koha_calendar->is_holiday($christmas), 1, 'Christmas is a closed day' );
is( $koha_calendar->is_holiday($newyear), 1, 'New Years day is a closed day' );
+$dbh->do("DELETE FROM repeatable_holidays");
+$dbh->do("DELETE FROM special_holidays");
+
my $custom_holiday = DateTime->new(
year => 2013,
month => 11,
is( Koha::Calendar->new( branchcode => $branch_2 )->is_holiday( $today ), 1, "Today is a holiday for $branch_2" );
is( Koha::Calendar->new( branchcode => $branch_1 )->is_holiday( $today ), 0, "Today is not a holiday for $branch_1");
-$dbh->rollback;
+# Few tests for exception holidays
+my ( $diff, $cal, $special );
+$dbh->do("DELETE FROM special_holidays");
+_add_exception( $today, $branch_1, 'Today' );
+$cal = Koha::Calendar->new( branchcode => $branch_1 );
+$special = $cal->exception_holidays;
+is( $special->count, 1, 'One exception holiday added' );
+
+my $tomorrow= dt_from_string();
+$tomorrow->add_duration( DateTime::Duration->new(days => 1) );
+_add_exception( $tomorrow, $branch_1, 'Tomorrow' );
+$cal = Koha::Calendar->new( branchcode => $branch_1 );
+$special = $cal->exception_holidays;
+is( $special->count, 2, 'Set of exception holidays contains two dates' );
+
+$diff = $today->delta_days( $special->min )->in_units('days');
+is( $diff, 0, 'Lowest exception holiday is today' );
+$diff = $tomorrow->delta_days( $special->max )->in_units('days');
+is( $diff, 0, 'Highest exception holiday is tomorrow' );
+
+C4::Calendar->new( branchcode => $branch_1 )->delete_holiday(
+ weekday => $tomorrow->day_of_week,
+ day => $tomorrow->day,
+ month => $tomorrow->month,
+ year => $tomorrow->year,
+);
+$cal = Koha::Calendar->new( branchcode => $branch_1 );
+$special = $cal->exception_holidays;
+is( $special->count, 1, 'Set of exception holidays back to one' );
+
+sub _add_exception {
+ my ( $dt, $branch, $descr ) = @_;
+ C4::Calendar->new( branchcode => $branch )->insert_exception_holiday(
+ day => $dt->day,
+ month => $dt->month,
+ year => $dt->year,
+ title => $descr,
+ description => $descr,
+ );
+}
+
+$schema->storage->txn_rollback;
-1;