From e7cbe943a8a891b714a81b0fae32adb0933cfe26 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bernd=20Ble=C3=9Fmann?= Date: Tue, 4 May 2021 21:47:23 +0200 Subject: [PATCH] =?utf8?q?Zeiterfassung:=20Konvertierung:=20Pr=C3=BCfung?= =?utf8?q?=20Datums-Parameter?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- SL/BackgroundJob/ConvertTimeRecordings.pm | 5 +++++ t/background_job/convert_time_recordings.t | 21 +++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/SL/BackgroundJob/ConvertTimeRecordings.pm b/SL/BackgroundJob/ConvertTimeRecordings.pm index ccea31a74..8db5cc108 100644 --- a/SL/BackgroundJob/ConvertTimeRecordings.pm +++ b/SL/BackgroundJob/ConvertTimeRecordings.pm @@ -63,6 +63,11 @@ sub run { my $to_date; $from_date = DateTime->from_kivitendo($self->data->{from_date}) if $self->data->{from_date}; $to_date = DateTime->from_kivitendo($self->data->{to_date}) if $self->data->{to_date}; + + # DateTime->from_kivitendo returns undef if the string cannot be parsed. Therefore test the result if it shopuld have been parsed. + die 'Cannot convert date from string "' . $self->data->{from_date} . '"' if $self->data->{from_date} && !$from_date; + die 'Cannot convert date to string "' . $self->data->{to_date} . '"' if $self->data->{to_date} && !$to_date; + $from_date ||= DateTime->new( day => 1, month => DateTime->today_local->month, year => DateTime->today_local->year)->subtract(months => 1); $to_date ||= DateTime->last_day_of_month(month => DateTime->today_local->month, year => DateTime->today_local->year)->subtract(months => 1); diff --git a/t/background_job/convert_time_recordings.t b/t/background_job/convert_time_recordings.t index db3294763..9f994dc96 100644 --- a/t/background_job/convert_time_recordings.t +++ b/t/background_job/convert_time_recordings.t @@ -1,4 +1,4 @@ -use Test::More tests => 27; +use Test::More tests => 28; use strict; @@ -71,7 +71,7 @@ push @time_recordings, new_time_recording( my %data = ( link_order => 1, project_id => $project->id, - from_date => '01.04.2021', + from_date => '01.01.2021', to_date => '30.04.2021', ); my $db_obj = SL::DB::BackgroundJob->new(); @@ -312,6 +312,23 @@ is($sales_order->items->[0]->ship*1, 1, 'linked by order_id: ship in related ord clear_up(); +######################################## +# are wrong params detected? +######################################## +%data = ( + from_date => 'x01.04.2021', +); +$db_obj = SL::DB::BackgroundJob->new(); +$db_obj->set_data(%data); +$job = SL::BackgroundJob::ConvertTimeRecordings->new; + +my $err_msg = ''; +eval { $ret = $job->run($db_obj); 1; } or do {$err_msg = $@}; +ok($err_msg =~ '^Cannot convert date from string', 'wrong date string detected'); + +clear_up(); + + ######################################## $::locale = $old_locale; -- 2.20.1