From 40b907bc0bc6db7500dde805ee188c0cf0ac6ae5 Mon Sep 17 00:00:00 2001 From: Colin Campbell Date: Thu, 23 Jun 2011 00:06:29 +0100 Subject: [PATCH] Bug 5549 : Make Koha::Calendar testable Add a testing option Add a script with tests Some simplifications of the logic --- Koha/Calendar.pm | 47 +++++++++++++++++++++++++++++++++------------ t/Kalendar.t | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 12 deletions(-) create mode 100755 t/Kalendar.t diff --git a/Koha/Calendar.pm b/Koha/Calendar.pm index e37f33cbcd..b44bdd6dbb 100644 --- a/Koha/Calendar.pm +++ b/Koha/Calendar.pm @@ -13,14 +13,18 @@ use Readonly; sub new { my ( $classname, %options ) = @_; my $self = {}; + bless $self, $classname; for my $o_name ( keys %options ) { my $o = lc $o_name; $self->{$o} = $options{$o_name}; } + if (exists $options{TEST_MODE}) { + $self->_mockinit(); + return $self; + } if ( !defined $self->{branchcode} ) { croak 'No branchcode argument passed to Koha::Calendar->new'; } - bless $self, $classname; $self->_init(); return $self; } @@ -33,17 +37,15 @@ sub _init { 'SELECT * from repeatable_holidays WHERE branchcode = ? AND ISNULL(weekday) = ?' ); $repeat_sth->execute( $branch, 0 ); - $self->{weekly_closed_days} = []; + $self->{weekly_closed_days} = [0,0,0,0,0,0,0]; Readonly::Scalar my $sunday => 7; while ( my $tuple = $repeat_sth->fetchrow_hashref ) { - my $day = $tuple->{weekday} == 0 ? $sunday : $tuple->{weekday}; - push @{ $self->{weekly_closed_days} }, $day; + $self->{weekly_closed_days}->[$tuple->{weekday }] = 1; } $repeat_sth->execute( $branch, 1 ); $self->{day_month_closed_days} = []; while ( my $tuple = $repeat_sth->fetchrow_hashref ) { - push @{ $self->{day_month_closed_days} }, - { day => $tuple->{day}, month => $tuple->{month}, }; + $self->{day_month_closed_days}->{ $tuple->{day}}->{$tuple->{month} } = 1; } my $special = $dbh->prepare( 'SELECT day, month, year, title, description FROM special_holidays WHERE ( branchcode = ? ) AND (isexception = ?)' @@ -132,17 +134,17 @@ sub addDate { sub is_holiday { my ( $self, $dt ) = @_; my $dow = $dt->day_of_week; - my @matches = grep { $_ == $dow } @{ $self->{weekly_closed_days} }; - if (@matches) { + if ($dow == 7) { + $dow = 0; + } + if ($self->{weekly_closed_days}->[$dow] == 1) { return 1; } $dt->truncate( to => 'days' ); my $day = $dt->day; my $month = $dt->month; - for my $dm ( @{ $self->{day_month_closed_days} } ) { - if ( $month == $dm->{month} && $day == $dm->{day} ) { - return 1; - } + if ( $self->{day_month_closed_days}->{$month}->{$day} == 1 ) { + return 1; } if ( $self->{exception_holidays}->contains($dt) ) { return 1; @@ -176,6 +178,27 @@ sub days_between { } +sub _mockinit { + my $self = shift; + $self->{weekly_closed_days} = [1,0,0,0,0,0,0]; # Sunday only + $self->{day_month_closed_days} = { + 6 => { + 16 => 1, + } + }; + my $dates = []; + $self->{exception_holidays} = DateTime::Set->from_datetimes( dates => $dates ); + my $special = DateTime->new( + year => 2011, + month => 6, + day => 1, + time_zone => 'Europe/London', + ); + push @{$dates}, $special; + $self->{single_holidays} = DateTime::Set->from_datetimes( dates => $dates ); + return; +} + 1; __END__ diff --git a/t/Kalendar.t b/t/Kalendar.t new file mode 100755 index 0000000000..f5e27599bf --- /dev/null +++ b/t/Kalendar.t @@ -0,0 +1,50 @@ +use strict; +use warnings; +use 5.010; +use DateTime; +use DateTime::TimeZone; + +use C4::Context; +use Test::More tests => 7; # last test to print + +BEGIN { use_ok('Koha::Calendar'); } + +my $cal = Koha::Calendar->new( TEST_MODE => 1); + +isa_ok( $cal, 'Koha::Calendar', 'Calendar class returned'); + +my $sunday = DateTime->new( + year => 2011, + month => 6, + day => 26, + time_zone => 'Europe/London', +); +my $monday = DateTime->new( + year => 2011, + month => 6, + day => 27, + time_zone => 'Europe/London', +); +my $bloomsday = DateTime->new( + year => 2011, + month => 6, + day => 16, + time_zone => 'Europe/London', +); # should be a holiday +my $special = DateTime->new( + year => 2011, + month => 6, + day => 1, + time_zone => 'Europe/London', +); # should be a holiday +my $notspecial = DateTime->new( + year => 2011, + month => 6, + day => 2, + time_zone => 'Europe/London', +); # should NOT be a holiday +is ( $cal->is_holiday($sunday), 1, 'Sunday is a closed day');# wee free test; +is ( $cal->is_holiday($monday), 0, 'Monday is not a closed day');# alas +is ( $cal->is_holiday($bloomsday), 1, 'month/day closed day test'); +is ( $cal->is_holiday($special), 1, 'special closed day test'); +is ( $cal->is_holiday($notspecial), 0, 'open day test'); -- 2.39.5