Bug Fix 3320 : condition on enabling field input was wrong
[koha.git] / t / Dates.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 126;
7 BEGIN {
8         use FindBin;
9         use lib $FindBin::Bin;
10         use_ok('C4::Dates', qw(format_date format_date_in_iso));
11 }
12
13 sub describe ($$) {
14         my $front = sprintf("%-25s", shift);
15         my $tail = shift || 'FAILED';
16         return  "$front : $tail";
17 }
18
19 my %thash = (
20           iso  => ['2001-01-01','1989-09-21','1952-01-00', '1989-09-21 13:46:02'],
21         metric => ["01-01-2001",'21-09-1989','00-01-1952'],
22            us  => ["01-01-2001",'09-21-1989','01-00-1952'],
23           sql  => ['20010101    010101',
24                            '19890921    143907',
25                            '19520100    000000'     ],
26 );
27
28 my ($date, $format, $today, $today0, $val, $re, $syspref);
29 my @formats = sort keys %thash;
30 my $fake_syspref_default = 'us';
31 my $fake_syspref = (@ARGV) ? shift : $ENV{KOHA_TEST_DATE_FORMAT};
32 if ($fake_syspref) {
33     diag "You asked for date format '$fake_syspref'.";
34     unless (scalar grep {/^$fake_syspref$/} @formats) {
35         diag "Warning: Unkown date format '$fake_syspref', reverting to default '$fake_syspref_default'.";
36         $fake_syspref = $fake_syspref_default;
37     }
38 }
39 $fake_syspref or $fake_syspref = $fake_syspref_default;
40 $C4::Dates::prefformat = $fake_syspref;     # So Dates doesn't have to ask the DB anything.
41
42 diag <<EndOfDiag;
43
44 In order to run without DB access, this test will substitute '$fake_syspref'
45 as your default date format.  Export environmental variable KOHA_TEST_DATE_FORMAT
46 to override this default, or pass the value as an argument to this test script.
47
48 NOTE: we test for the system handling dd=00 and 00 for TIME values,
49 therefore you *should* see some warnings 'Illegal date specified' related to those.
50
51 Testing Legacy Functions: format_date and format_date_in_iso
52
53 EndOfDiag
54
55 ok($syspref = C4::Dates->new->format(),         "Your system preference is: $syspref");
56 print "\n";
57 foreach (@{$thash{'iso'}}) {
58         ok($val = format_date($_),                  "format_date('$_'): $val"            );
59 }
60 foreach (@{$thash{$syspref}}) {
61         ok($val = format_date_in_iso($_),           "format_date_in_iso('$_'): $val"     );
62 }
63 ok($today0 = C4::Dates->today(),                "(default) CLASS ->today : $today0" );
64 diag "\nTesting " . scalar(@formats) . " formats.\nTesting no input (defaults):\n";
65 print "\n";
66 foreach (@formats) {
67         my $pre = sprintf '(%-6s)', $_;
68         ok($date = C4::Dates->new(),                "$pre Date Creation   : new()");
69         ok($_ eq ($format = $date->format($_)),     "$pre format($_)      : " . ($format|| 'FAILED') );
70         ok($format = $date->visual(),                           "$pre visual()        : " . ($format|| 'FAILED') );
71         ok($today  = $date->output(),               "$pre output()        : " . ($today || 'FAILED') );
72         ok($today  = $date->today(),                "$pre object->today   : " . ($today || 'FAILED') );
73         print "\n";
74 }
75
76 diag "\nTesting with valid inputs:\n";
77 foreach $format (@formats) {
78         my $pre = sprintf '(%-6s)', $format;
79   foreach my $testval (@{$thash{ $format }}) {
80         ok($date = C4::Dates->new($testval,$format),         "$pre Date Creation   : new('$testval','$format')");
81         ok($re   = $date->regexp,                            "$pre has regexp()" );
82         ok($val  = $date->output(),                 describe("$pre output()", $val) );
83         foreach (grep {!/$format/} @formats) {
84                 ok($today = $date->output($_),          describe(sprintf("$pre output(%8s)","'$_'"), $today) );
85         }
86         ok($today  = $date->today(),                describe("$pre object->today", $today) );
87         # ok($today == ($today = C4::Dates->today()), "$pre CLASS ->today   : $today" );
88         ok($val  = $date->output(),                 describe("$pre output()", $val) );
89         # ok($format eq ($format = $date->format()),  "$pre format()        : $format" );
90         print "\n";
91   }
92 }
93
94 diag "\nTesting object independence from class\n";
95 my $in1 = '12/25/1952'; # us
96 my $in2 = '13/01/2001'; # metric
97 my $d1 = C4::Dates->new($in1, 'us');
98 my $d2 = C4::Dates->new($in2, 'metric');
99 my $out1 = $d1->output('iso');
100 my $out2 = $d2->output('iso');
101 ok($out1 ne $out2,                             "subsequent constructors get different dataspace ($out1 != $out2)");
102 diag "done.\n";