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