bug 2295 [1/4]: moving C4::Dates tests into database dependent tests
[koha.git] / t / lib / KohaTest / Dates / Usage.pm
1 package KohaTest::Dates::Usage;
2 use base qw( KohaTest::Dates );
3
4 use strict;
5 use warnings;
6
7 use Test::More;
8
9 use C4::Dates qw(format_date format_date_in_iso);
10
11
12 my %thash = (
13     iso    => [ '2001-01-01',         '1989-09-21',         '1952-01-00' ],
14     metric => [ "01-01-2001",         '21-09-1989',         '00-01-1952' ],
15     us     => [ "01-01-2001",         '09-21-1989',         '01-00-1952' ],
16     sql    => [ '20010101    010101', '19890921    143907', '19520100    000000' ],
17 );
18
19
20 my @formats = sort keys %thash;
21
22 sub check_formats : Test( 10 ) {
23     my $self = shift;
24
25     my $syspref = C4::Dates->new->format();
26     ok( $syspref, "Your system preference is: $syspref" );
27
28     foreach ( @{ $thash{'iso'} } ) {
29         ok( format_date($_), "able to format_date() on $_" );
30     }
31
32     foreach ( @{ $thash{$syspref} } ) {
33         ok( format_date_in_iso($_), "able to format_date_in_iso() on $_" );
34     }
35     ok( C4::Dates->today(), "(default) CLASS ->today : " . C4::Dates->today() );
36 }
37
38 sub defaults : Test( 24 ) {
39     my $self = shift;
40
41     foreach (@formats) {
42         my $pre = sprintf '(%-6s)', $_;
43         my $date = C4::Dates->new();
44         ok( $date, "$pre Date Creation   : new()" );
45         isa_ok( $date, 'C4::Dates' );
46         ok( $_ eq $date->format($_),   "$pre format($_)      : " );
47         ok( $date->visual(), "$pre visual()" );
48         ok( $date->output(), "$pre output()" );
49         ok( $date->today(),  "$pre object->today" );
50
51     }
52 }
53
54 sub valid_inputs : Test( 108 ) {
55     my $self = shift;
56
57     foreach my $format (@formats) {
58         my $pre = sprintf '(%-6s)', $format;
59         foreach my $testval ( @{ $thash{$format} } ) {
60             my ( $val, $today );
61             my $date = C4::Dates->new( $testval, $format );
62             ok( $date, "$pre Date Creation   : new('$testval','$format')" );
63             isa_ok( $date, 'C4::Dates' );
64             ok( $date->regexp, "$pre has regexp()" );
65             ok( $val = $date->output(), describe( "$pre output()", $val ) );
66             foreach ( grep { !/$format/ } @formats ) {
67                 ok( $today = $date->output($_), describe( sprintf( "$pre output(%8s)", "'$_'" ), $today ) );
68             }
69             ok( $today = $date->today(), describe( "$pre object->today", $today ) );
70             ok( $val = $date->output(), describe( "$pre output()", $val ) );
71         }
72     }
73 }
74
75 sub independence_from_class : Test( 1 ) {
76     my $self = shift;
77
78     my $in1  = '12/25/1952';                       # us
79     my $in2  = '13/01/2001';                       # metric
80     my $d1   = C4::Dates->new( $in1, 'us' );
81     my $d2   = C4::Dates->new( $in2, 'metric' );
82     my $out1 = $d1->output('iso');
83     my $out2 = $d2->output('iso');
84     ok( $out1 ne $out2, "subsequent constructors get different dataspace ($out1 != $out2)" );
85
86 }
87
88
89
90 sub describe {
91     my $front = sprintf( "%-25s", shift );
92     my $tail = shift || 'FAILED';
93     return "$front : $tail";
94 }
95
96 1;