View | Details | Raw Unified | Return to bug 17502
Collapse All | Expand All

(-)a/Koha/DateUtils.pm (-6 / +5 lines)
Lines 19-25 package Koha::DateUtils; Link Here
19
use Modern::Perl;
19
use Modern::Perl;
20
use DateTime;
20
use DateTime;
21
use C4::Context;
21
use C4::Context;
22
use Carp;
22
use Koha::Exceptions;
23
23
24
use base 'Exporter';
24
use base 'Exporter';
25
25
Lines 209-225 sub output_pref { Link Here
209
        $dt = $params;
209
        $dt = $params;
210
    }
210
    }
211
211
212
    carp "output_pref should not be called with both dt and str parameters"
212
    Koha::Exceptions::WrongParameter->throw( 'output_pref should not be called with both dt and str parameter' ) if $dt and $str;
213
        and return
214
            if $dt and $str;
215
213
216
    if ( $str ) {
214
    if ( $str ) {
217
        local $@;
215
        local $@;
218
        $dt = eval { dt_from_string( $str ) };
216
        $dt = eval { dt_from_string( $str ) };
219
        carp "Invalid date '$str' passed to output_pref\n" if $@;
217
        Koha::Exceptions::WrongParameter->throw("Invalid date '$str' passed to output_pref" ) if $@;
220
    }
218
    }
221
219
222
    return unless defined $dt && ref($dt) eq 'DateTime';
220
    return if !defined $dt; # NULL date
221
    Koha::Exceptions::WrongParameter->throw( 'dt is not a datetime' )  if ref($dt) ne 'DateTime';
223
222
224
    # FIXME: see bug 13242 => no TZ for dates 'infinite'
223
    # FIXME: see bug 13242 => no TZ for dates 'infinite'
225
    if ( $dt->ymd !~ /^9999/ ) {
224
    if ( $dt->ymd !~ /^9999/ ) {
(-)a/Koha/Exceptions.pm (+4 lines)
Lines 24-29 use Exception::Class ( Link Here
24
        isa => 'Koha::Exceptions::Exception',
24
        isa => 'Koha::Exceptions::Exception',
25
        description => 'A required parameter is missing'
25
        description => 'A required parameter is missing'
26
    },
26
    },
27
    'Koha::Exceptions::WrongParameter' => {
28
        isa => 'Koha::Exceptions::Exception',
29
        description => 'One or more parameters are wrong',
30
    },
27
    'Koha::Exceptions::CannotAddLibraryLimit' => {
31
    'Koha::Exceptions::CannotAddLibraryLimit' => {
28
        isa => 'Koha::Exceptions::Exception',
32
        isa => 'Koha::Exceptions::Exception',
29
        description => 'General problem adding a library limit'
33
        description => 'General problem adding a library limit'
(-)a/t/DateUtils.t (-11 / +33 lines)
Lines 9-14 use Test::More tests => 63; Link Here
9
use Test::MockModule;
9
use Test::MockModule;
10
use Test::Warn;
10
use Test::Warn;
11
use Time::HiRes qw/ gettimeofday /;
11
use Time::HiRes qw/ gettimeofday /;
12
use Try::Tiny;
13
12
use t::lib::Mocks;
14
use t::lib::Mocks;
13
15
14
BEGIN { use_ok('Koha::DateUtils'); }
16
BEGIN { use_ok('Koha::DateUtils'); }
Lines 223-241 $dt = dt_from_string('2015-01-31 01:02:03'); Link Here
223
is( output_pref( {dt => $dt} ), '31/01/2015 01:02', 'dt_from_string should fallback to sql format' );
225
is( output_pref( {dt => $dt} ), '31/01/2015 01:02', 'dt_from_string should fallback to sql format' );
224
226
225
# output_pref with no parameters, single parameter (no hash)
227
# output_pref with no parameters, single parameter (no hash)
226
is( output_pref(), undef, 'output_pref without parameters' );
228
is( output_pref(), undef, 'Call output_pref without parameters' );
227
is( output_pref( 'no_dt' ), undef, 'Passed single invalid dt to output_pref' );
229
try {
230
    output_pref( 'no_dt' );
231
    ok( 0, 'Passed single invalid dt to output_pref' );
232
} catch {
233
    is( ref($_), 'Koha::Exceptions::WrongParameter',
234
        'Passed single invalid dt to output_pref' );
235
};
228
236
229
# pass invalid dt via hash
237
# pass invalid dt via hash
230
is( output_pref({ dt => 'no_dt' }), undef, 'Passed invalid dt in hash to output_pref' );
238
try {
239
    output_pref({ dt => 'no_dt' });
240
    ok( 0, 'Passed invalid dt in hash to output_pref' );
241
} catch {
242
    is( ref($_), 'Koha::Exceptions::WrongParameter',
243
        'Passed invalid dt in hash to output_pref' );
244
};
231
245
232
# output_pref with str parameter
246
# output_pref with str parameter
233
is( output_pref( { 'str' => $testdate_iso, dateformat => 'iso', dateonly => 1 } ), $testdate_iso, 'output_pref should handle correctly the iso parameter' );
247
is( output_pref( { 'str' => $testdate_iso, dateformat => 'iso', dateonly => 1 } ), $testdate_iso, 'output_pref should handle correctly the iso parameter' );
234
my $output_for_invalid_date;
248
my $output_for_invalid_date;
235
warning_like { $output_for_invalid_date = output_pref( { str => 'invalid_date' } ) }
249
try {
236
             { carped => qr[^Invalid date 'invalid_date' passed to output_pref] },
250
    $output_for_invalid_date = output_pref({ str => 'invalid_date' });
237
             'output_pref should carp if an invalid date is passed for the str parameter';
251
    ok( 0, 'Invalid date string passed to output_pref' );
238
is( $output_for_invalid_date, undef, 'output_pref should return undef if an invalid date is passed' );
252
} catch {
239
warning_is { output_pref( { 'str' => $testdate_iso, dt => $dt, dateformat => 'iso', dateonly => 1 } ) }
253
    is( ref($_), 'Koha::Exceptions::WrongParameter',
240
           { carped => 'output_pref should not be called with both dt and str parameters' },
254
        'Invalid date string passed to output_pref' );
241
           'output_pref should carp if str and dt parameters are passed together';
255
};
256
is( $output_for_invalid_date, undef, 'No return value from output_pref' );
257
try {
258
    output_pref({ 'str' => $testdate_iso, dt => $dt, dateformat => 'iso', dateonly => 1 });
259
    ok( 0, 'output_pref should carp if str and dt parameters are passed together' );
260
} catch {
261
    is( ref($_), 'Koha::Exceptions::WrongParameter', 'output_pref should throw an exception if str and dt parameters are passed together' );
262
};
263
264
# End of tests
242
- 

Return to bug 17502