From e012957186426b9577bfc8498c5971990b258024 Mon Sep 17 00:00:00 2001 From: Colin Campbell Date: Fri, 24 Jun 2011 14:28:22 +0100 Subject: [PATCH] Bug 5549 : Koha::Calendar::addDate should not loop on -1 Control variable of loop should be absolute value to avoid looping when backtracking --- Koha/Calendar.pm | 3 ++- t/Kalendar.t | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Koha/Calendar.pm b/Koha/Calendar.pm index bdd4b8f06a..a3ad60c994 100644 --- a/Koha/Calendar.pm +++ b/Koha/Calendar.pm @@ -87,6 +87,7 @@ sub addDate { if ( ref $add_duration ne 'DateTime::Duration' ) { $add_duration = DateTime::Duration->new( days => $add_duration ); } + $unit ||= q{}; # default days ? my $days_mode = $self->{days_mode}; Readonly::Scalar my $return_by_hour => 10; my $day_dur = DateTime::Duration->new( days => 1 ); @@ -114,7 +115,7 @@ sub addDate { } } else { - my $days = $add_duration->in_units('days'); + my $days = abs $add_duration->in_units('days'); while ($days) { $base_date->add_duration($day_dur); if ( $self->is_holiday($base_date) ) { diff --git a/t/Kalendar.t b/t/Kalendar.t index ef80ca929a..f955231264 100755 --- a/t/Kalendar.t +++ b/t/Kalendar.t @@ -5,7 +5,7 @@ use DateTime; use DateTime::TimeZone; use C4::Context; -use Test::More tests => 8; # last test to print +use Test::More tests => 9; BEGIN { use_ok('Koha::Calendar'); } @@ -57,3 +57,6 @@ is( $cal->is_holiday($notspecial), 0, 'open day test' ); my $dt = $cal->addDate( $saturday, 1, 'days' ); is( $dt->day_of_week, 1, 'addDate skips closed Sunday' ); + +$dt = $cal->addDate( $bloomsday, -1 ); +cmp_ok( $dt->ymd(), 'cmp', '2011-06-15', 'Negative call to addDate' ); -- 2.20.1