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

(-)a/Koha/DateUtils.pm (-6 / +12 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::DateUtils;
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::DateUtils::output_pref::ConflictingParameters->throw
213
        and return
213
        if $dt and $str;
214
            if $dt and $str;
215
214
216
    if ( $str ) {
215
    if ( $str ) {
217
        local $@;
216
        local $@;
218
        $dt = eval { dt_from_string( $str ) };
217
        $dt = eval { dt_from_string( $str ) };
219
        carp "Invalid date '$str' passed to output_pref\n" if $@;
218
        Koha::Exceptions::DateUtils::output_pref::InvalidDateString->throw(
219
            "Invalid date '$str' passed to output_pref"
220
        ) if $@;
220
    }
221
    }
221
222
222
    return unless defined $dt && ref($dt) eq 'DateTime';
223
    if( !defined $dt || ref($dt) ne 'DateTime' ) {
224
        Koha::Exceptions::DateUtils::output_pref::NoDateTime->throw(
225
            "Object should have been a DateTime; check the call from ".
226
            join ',', (caller),
227
        );
228
    }
223
229
224
    # FIXME: see bug 13242 => no TZ for dates 'infinite'
230
    # FIXME: see bug 13242 => no TZ for dates 'infinite'
225
    if ( $dt->ymd !~ /^9999/ ) {
231
    if ( $dt->ymd !~ /^9999/ ) {
(-)a/Koha/Exceptions/DateUtils.pm (+21 lines)
Line 0 Link Here
1
package Koha::Exceptions::DateUtils;
2
3
use Modern::Perl;
4
5
use Exception::Class (
6
7
    'Koha::Exceptions::DateUtils' => {
8
        description => 'Something went wrong in DateUtils',
9
    },
10
    'Koha::Exceptions::DateUtils::output_pref::ConflictingParameters' => {
11
        description => 'output_pref should not be called with both dt and str parameters',
12
    },
13
    'Koha::Exceptions::DateUtils::output_pref::InvalidDateString' => {
14
        description => 'Invalid date string passed to output_pref',
15
    },
16
    'Koha::Exceptions::DateUtils::output_pref::NoDateTime' => {
17
        description => 'Object should have been a DateTime',
18
    },
19
);
20
21
1;
(-)a/t/DateUtils.t (-11 / +39 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
try {
227
is( output_pref( 'no_dt' ), undef, 'Passed single invalid dt to output_pref' );
229
    output_pref();
230
    ok( 0, 'output_pref without parameters' );
231
} catch {
232
    is( ref($_), 'Koha::Exceptions::DateUtils::output_pref::NoDateTime',
233
        'output_pref without parameters' );
234
};
235
try {
236
    output_pref( 'no_dt' );
237
    ok( 0, 'Passed single invalid dt to output_pref' );
238
} catch {
239
    is( ref($_), 'Koha::Exceptions::DateUtils::output_pref::NoDateTime',
240
        'Passed single invalid dt to output_pref' );
241
};
228
242
229
# pass invalid dt via hash
243
# pass invalid dt via hash
230
is( output_pref({ dt => 'no_dt' }), undef, 'Passed invalid dt in hash to output_pref' );
244
try {
245
    output_pref({ dt => 'no_dt' });
246
    ok( 0, 'Passed invalid dt in hash to output_pref' );
247
} catch {
248
    is( ref($_), 'Koha::Exceptions::DateUtils::output_pref::NoDateTime',
249
        'Passed invalid dt in hash to output_pref' );
250
};
231
251
232
# output_pref with str parameter
252
# 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' );
253
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;
254
my $output_for_invalid_date;
235
warning_like { $output_for_invalid_date = output_pref( { str => 'invalid_date' } ) }
255
try {
236
             { carped => qr[^Invalid date 'invalid_date' passed to output_pref] },
256
    $output_for_invalid_date = output_pref({ str => 'invalid_date' });
237
             'output_pref should carp if an invalid date is passed for the str parameter';
257
    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' );
258
} catch {
239
warning_is { output_pref( { 'str' => $testdate_iso, dt => $dt, dateformat => 'iso', dateonly => 1 } ) }
259
    is( ref($_), 'Koha::Exceptions::DateUtils::output_pref::InvalidDateString',
240
           { carped => 'output_pref should not be called with both dt and str parameters' },
260
        'Invalid date string passed to output_pref' );
241
           'output_pref should carp if str and dt parameters are passed together';
261
};
262
is( $output_for_invalid_date, undef, 'No return value from output_pref' );
263
try {
264
    output_pref({ 'str' => $testdate_iso, dt => $dt, dateformat => 'iso', dateonly => 1 });
265
    ok( 0, 'output_pref should carp if str and dt parameters are passed together' );
266
} catch {
267
    is( ref($_), 'Koha::Exceptions::DateUtils::output_pref::ConflictingParameters', 'output_pref should throw an exception if str and dt parameters are passed together' );
268
};
269
270
# End of tests
242
- 

Return to bug 17502