Bug 9000 [3] - Change conf key from tmpl to template
[koha.git] / t / Dates.t
blob148bb3e8f9d76c009aa752a1eab672575120c3c9
1 #!/usr/bin/perl
3 use strict;
4 use warnings;
6 use Test::More tests => 327;
8 BEGIN {
9 use FindBin;
10 use lib $FindBin::Bin;
11 use_ok( 'C4::Dates', qw(format_date format_date_in_iso) );
14 sub describe ($$) {
15 my $front = sprintf( "%-25s", shift );
16 my $tail = shift || 'FAILED';
17 return "$front : $tail";
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 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' ],
30 my ( $date, $format, $today, $today0, $val, $re, $syspref );
31 my @formats = sort keys %thash;
32 my $fake_syspref_default = 'us';
33 my $fake_syspref = (@ARGV) ? shift : $ENV{KOHA_TEST_DATE_FORMAT};
34 if ($fake_syspref) {
35 diag "You asked for date format '$fake_syspref'.";
36 unless ( scalar grep { /^$fake_syspref$/ } @formats ) {
37 diag "Warning: Unkown date format '$fake_syspref', reverting to default '$fake_syspref_default'.";
38 $fake_syspref = $fake_syspref_default;
41 $fake_syspref or $fake_syspref = $fake_syspref_default;
42 $C4::Dates::prefformat = $fake_syspref; # So Dates doesn't have to ask the DB anything.
44 diag <<EndOfDiag;
46 In order to run without DB access, this test will substitute '$fake_syspref'
47 as your default date format. Export environmental variable KOHA_TEST_DATE_FORMAT
48 to override this default, or pass the value as an argument to this test script.
50 NOTE: we test for the system handling dd=00 and 00 for TIME values, therefore
51 you SHOULD see some warnings like:
52 Illegal date specified (year = 1952, month = 1, day = 00) at t/Dates.t ...
54 Testing Legacy Functions: format_date and format_date_in_iso
56 EndOfDiag
58 ok( $syspref = C4::Dates->new->format(), "Your system preference is: $syspref" );
59 print "\n";
60 foreach ( @{ $thash{'iso'} } ) {
61 ok( $val = format_date($_), "format_date('$_'): $val" );
63 foreach ( @{ $thash{$syspref} } ) {
64 ok( $val = format_date_in_iso($_), "format_date_in_iso('$_'): $val" );
66 ok( $today0 = C4::Dates->today(), "(default) CLASS ->today : $today0" );
67 diag "\nTesting " . scalar(@formats) . " formats.\nTesting no input (defaults):\n";
68 print "\n";
69 foreach (@formats) {
70 my $pre = sprintf '(%-6s)', $_;
71 ok( $date = C4::Dates->new(), "$pre Date Creation : new()" );
72 ok( $_ eq ( $format = $date->format($_) ), "$pre format($_) : " . ( $format || 'FAILED' ) );
73 ok( $format = $date->visual(), "$pre visual() : " . ( $format || 'FAILED' ) );
74 ok( $today = $date->output(), "$pre output() : " . ( $today || 'FAILED' ) );
75 ok( $today = $date->today(), "$pre object->today : " . ( $today || 'FAILED' ) );
76 print "\n";
79 diag "\nTesting with valid inputs:\n";
80 foreach $format (@formats) {
81 my $pre = sprintf '(%-6s)', $format;
82 foreach my $testval ( @{ $thash{$format} } ) {
83 ok( $date = C4::Dates->new( $testval, $format ), "$pre Date Creation : new('$testval','$format')" );
84 ok( $re = $date->regexp, "$pre has regexp()" );
85 ok( $testval =~ /^$re$/, "$pre has regexp() match $testval" );
86 ok( $val = $date->output(), describe( "$pre output()", $val ) );
87 SKIP: {
88 skip( "special case with explicit regexp('syspref') because $format isn't $syspref", 1 ) unless ( $format eq $syspref );
89 my $re_syspref = C4::Dates->regexp('syspref');
90 ok( $testval =~ /^$re_syspref$/, "$pre has regexp('syspref') match $testval" );
92 foreach ( grep { !/$format/ } @formats ) {
93 ok( $today = $date->output($_), describe( sprintf( "$pre output(%8s)", "'$_'" ), $today ) );
95 ok( $today = $date->today(), describe( "$pre object->today", $today ) );
97 # ok($today == ($today = C4::Dates->today()), "$pre CLASS ->today : $today" );
98 ok( $val = $date->output(), describe( "$pre output()", $val ) );
100 # ok($format eq ($format = $date->format()), "$pre format() : $format" );
101 print "\n";
105 diag "\nTesting object independence from class\n";
106 my $in1 = '12/25/1952'; # us
107 my $in2 = '13/01/2001'; # metric
108 my $d1 = C4::Dates->new( $in1, 'us' );
109 my $d2 = C4::Dates->new( $in2, 'metric' );
110 my $out1 = $d1->output('iso');
111 my $out2 = $d2->output('iso');
112 ok( $out1 ne $out2, "subsequent constructors get different dataspace ($out1 != $out2)" );
113 diag "done.\n";