From: Bernd Bleßmann Date: Tue, 4 May 2021 19:47:23 +0000 (+0200) Subject: Zeiterfassung: Konvertierung: Prüfung Datums-Parameter X-Git-Tag: kivitendo-mebil_0.1-0~9^2~270 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=e7cbe943a8a891b714a81b0fae32adb0933cfe26;p=kivitendo-erp.git Zeiterfassung: Konvertierung: Prüfung Datums-Parameter --- 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;