From e6540b6e554a130fd3275a9d97f996eb7286f15d Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 10 Nov 2015 16:38:51 +0000 Subject: [PATCH] [PASSED QA] Bug 15166: Make output_pref takes a string in parameter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To simplify the management of dates, it would be great that output_pref takes a string in parameter. my $date = $input->param('my_date'); $date = eval { dt_from_string( $date ) } if $date; $date = output_pref({dt => $date}) if $date; Could be replace with: my $date = output_pref({ str => $input->param('my_date') }); Tested with t/DateUtils.t, passed OK. Signed-off-by: Marc VĂ©ron Signed-off-by: Katrin Fischer --- Koha/DateUtils.pm | 14 ++++++++++---- t/DateUtils.t | 10 +++++++++- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/Koha/DateUtils.pm b/Koha/DateUtils.pm index 7a98120..ca2ede5 100644 --- a/Koha/DateUtils.pm +++ b/Koha/DateUtils.pm @@ -16,11 +16,10 @@ package Koha::DateUtils; # Koha; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -use strict; -use warnings; -use 5.010; +use Modern::Perl; use DateTime; use C4::Context; +use Carp; use base 'Exporter'; use version; our $VERSION = qv('1.0.0'); @@ -189,9 +188,10 @@ should be returned without the time. sub output_pref { my $params = shift; - my ( $dt, $force_pref, $force_time, $dateonly, $as_due_date ); + my ( $dt, $str, $force_pref, $force_time, $dateonly, $as_due_date ); if ( ref $params eq 'HASH' ) { $dt = $params->{dt}; + $str = $params->{str}; $force_pref = $params->{dateformat}; # if testing we want to override Context $force_time = $params->{timeformat}; $dateonly = $params->{dateonly} || 0; # if you don't want the hours and minutes @@ -200,6 +200,12 @@ sub output_pref { $dt = $params; } + carp "output_pref should not be called with both dt and str parameters" + and return + if $dt and $str; + + $dt = eval { dt_from_string( $str ) } if $str; + return unless defined $dt; # FIXME: see bug 13242 => no TZ for dates 'infinite' diff --git a/t/DateUtils.t b/t/DateUtils.t index 4f7c773..0dd749b 100755 --- a/t/DateUtils.t +++ b/t/DateUtils.t @@ -3,8 +3,9 @@ use DateTime; use DateTime::TimeZone; use C4::Context; -use Test::More tests => 55; +use Test::More tests => 58; use Test::MockModule; +use Test::Warn; use Time::HiRes qw/ gettimeofday /; use t::lib::Mocks; @@ -215,3 +216,10 @@ is( output_pref( { dt => $dt, dateonly => 1 } ), '01/01/1900', 'dt_from_string s # fallback $dt = dt_from_string('2015-01-31 01:02:03'); is( output_pref( {dt => $dt} ), '31/01/2015 01:02', 'dt_from_string should fallback to sql format' ); + +# output_pref with str parameter +is( output_pref( { 'str' => $testdate_iso, dateformat => 'iso', dateonly => 1 } ), $testdate_iso, 'output_pref should handle correctly the iso parameter' ); +is( output_pref( { 'str' => 'invalid_date', dateformat => 'iso', dateonly => 1 } ), undef, 'output_pref should return undef if an invalid date is passed' ); +warning_is { output_pref( { 'str' => $testdate_iso, dt => $dt, dateformat => 'iso', dateonly => 1 } ) } + { carped => 'output_pref should not be called with both dt and str parameters' }, + 'output_pref should carp if str and dt parameters are passed together'; -- 1.9.1