File Coverage

File:t/Dates.t
Coverage:86.0%

linestmtbrancondsubtimecode
1#!/usr/bin/perl
2
3
1
1
1
1.33283523613023e+15
6
30
use strict;
4
1
1
1
8
5
39
use warnings;
5
6
1
1
1
195
20012
22
use Test::More tests => 327;
7
8BEGIN {
9
1
1
1
51333
1166
47
    use FindBin;
10
1
1
1
149985
981
11
    use lib $FindBin::Bin;
11
1
132
    use_ok( 'C4::Dates', qw(format_date format_date_in_iso) );
12}
13
14sub describe ($$) {
15
182
570
    my $front = sprintf( "%-25s", shift );
16
182
420
    my $tail = shift || 'FAILED';
17
182
1415
    return "$front : $tail";
18}
19
20# Keep the number of test elements per [array] equal or the predicted number of tests
21# needs to be different for different (fake) sysprefs.
22
1
376954
my %thash = (
23    iso => [ '2001-1-1', '1989-9-21', '1952-1-0', '1989-9-21 13:46:02', '2001-01-01', '1989-09-21', '1952-01-00', '1989-09-21 13:46:02' ],
24    metric => [ "1-1-2001", '21-9-1989', '00-1-1952', '21-9-1989 13:46:02', "01-01-2001", '21-09-1989', '00-01-1952', '21-09-1989 13:46:02' ],
25    us => [ "01-01-2001", '09-21-1989', '01-00-1952', '09-21-1989 13:46:02' ],
26    sql => [ '20010101 010101', '19890921 143907', '19520100 000000', '19890921 134602' ],
27    rfc822 => [ 'Wed, 02 Oct 2002 15:00:00 +0200', 'Fri, 10 Sep 2010 08:00:00 +0500' ],
28);
29
30
1
57
my ( $date, $format, $today, $today0, $val, $re, $syspref );
31
1
74
my @formats = sort keys %thash;
32
1
46
my $fake_syspref_default = 'us';
33
1
44
my $fake_syspref = (@ARGV) ? shift : $ENV{KOHA_TEST_DATE_FORMAT};
34
1
40
if ($fake_syspref) {
35
0
0
    diag "You asked for date format '$fake_syspref'.";
36
0
0
0
0
    unless ( scalar grep { /^$fake_syspref$/ } @formats ) {
37
0
0
        diag "Warning: Unkown date format '$fake_syspref', reverting to default '$fake_syspref_default'.";
38
0
0
        $fake_syspref = $fake_syspref_default;
39    }
40}
41
1
36
$fake_syspref or $fake_syspref = $fake_syspref_default;
42
1
33
$C4::Dates::prefformat = $fake_syspref; # So Dates doesn't have to ask the DB anything.
43
44
1
40
diag <<EndOfDiag;
45
46In order to run without DB access, this test will substitute '$fake_syspref'
47as your default date format. Export environmental variable KOHA_TEST_DATE_FORMAT
48to override this default, or pass the value as an argument to this test script.
49
50NOTE: we test for the system handling dd=00 and 00 for TIME values, therefore
51you SHOULD see some warnings like:
52Illegal date specified (year = 1952, month = 1, day = 00) at t/Dates.t ...
53
54Testing Legacy Functions: format_date and format_date_in_iso
55
56EndOfDiag
57
58
1
411
ok( $syspref = C4::Dates->new->format(), "Your system preference is: $syspref" );
59
1
801
print "\n";
60
1
1
2
3
foreach ( @{ $thash{'iso'} } ) {
61
8
3311
    ok( $val = format_date($_), "format_date('$_'): $val" );
62}
63
1
1
421
3
foreach ( @{ $thash{$syspref} } ) {
64
4
1276
    ok( $val = format_date_in_iso($_), "format_date_in_iso('$_'): $val" );
65}
66
1
245
ok( $today0 = C4::Dates->today(), "(default) CLASS ->today : $today0" );
67
1
432
diag "\nTesting " . scalar(@formats) . " formats.\nTesting no input (defaults):\n";
68
1
184
print "\n";
69
1
3
foreach (@formats) {
70
5
29
    my $pre = sprintf '(%-6s)', $_;
71
5
50
    ok( $date = C4::Dates->new(), "$pre Date Creation : new()" );
72
5
2213
    ok( $_ eq ( $format = $date->format($_) ), "$pre format($_) : " . ( $format || 'FAILED' ) );
73
5
2021
    ok( $format = $date->visual(), "$pre visual() : " . ( $format || 'FAILED' ) );
74
5
2215
    ok( $today = $date->output(), "$pre output() : " . ( $today || 'FAILED' ) );
75
5
2341
    ok( $today = $date->today(), "$pre object->today : " . ( $today || 'FAILED' ) );
76
5
3032
    print "\n";
77}
78
79
1
11
diag "\nTesting with valid inputs:\n";
80
1
189
foreach $format (@formats) {
81
5
41
    my $pre = sprintf '(%-6s)', $format;
82
5
5
9
17
    foreach my $testval ( @{ $thash{$format} } ) {
83
26
266
        ok( $date = C4::Dates->new( $testval, $format ), "$pre Date Creation : new('$testval','$format')" );
84
26
12665
        ok( $re = $date->regexp, "$pre has regexp()" );
85
26
13511
        ok( $testval =~ /^$re$/, "$pre has regexp() match $testval" );
86
26
12210
        ok( $val = $date->output(), describe( "$pre output()", $val ) );
87
26
276
      SKIP: {
88
26
12813
            skip( "special case with explicit regexp('syspref') because $format isn't $syspref", 1 ) unless ( $format eq $syspref );
89
4
46
            my $re_syspref = C4::Dates->regexp('syspref');
90
4
119
            ok( $testval =~ /^$re_syspref$/, "$pre has regexp('syspref') match $testval" );
91        }
92
26
130
8721
311
        foreach ( grep { !/$format/ } @formats ) {
93
104
35473
            ok( $today = $date->output($_), describe( sprintf( "$pre output(%8s)", "'$_'" ), $today ) );
94        }
95
26
12028
        ok( $today = $date->today(), describe( "$pre object->today", $today ) );
96
97        # ok($today == ($today = C4::Dates->today()), "$pre CLASS ->today : $today" );
98
26
12390
        ok( $val = $date->output(), describe( "$pre output()", $val ) );
99
100        # ok($format eq ($format = $date->format()), "$pre format() : $format" );
101
26
12625
        print "\n";
102    }
103}
104
105
1
29
diag "\nTesting object independence from class\n";
106
1
155
my $in1 = '12/25/1952'; # us
107
1
3
my $in2 = '13/01/2001'; # metric
108
1
7
my $d1 = C4::Dates->new( $in1, 'us' );
109
1
7
my $d2 = C4::Dates->new( $in2, 'metric' );
110
1
33
my $out1 = $d1->output('iso');
111
1
8
my $out2 = $d2->output('iso');
112
1
11
ok( $out1 ne $out2, "subsequent constructors get different dataspace ($out1 != $out2)" );
113
1
559
diag "done.\n";