bug 4168 add keyword to marc mapping to menu
[koha.git] / t / Dates.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 276;
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 # Keep the number of test elements per [array] equal or the predicted number of tests 
20 # needs to be different for different (fake) sysprefs.
21 my %thash = (
22           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'],
23         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'],
24            us  => ["01-01-2001",'09-21-1989','01-00-1952', '09-21-1989 13:46:02'],
25           sql  => ['20010101    010101',
26                            '19890921    143907',
27                            '19520100    000000',
28                            '19890921    134602'     ],
29 );
30
31 my ($date, $format, $today, $today0, $val, $re, $syspref);
32 my @formats = sort keys %thash;
33 my $fake_syspref_default = 'us';
34 my $fake_syspref = (@ARGV) ? shift : $ENV{KOHA_TEST_DATE_FORMAT};
35 if ($fake_syspref) {
36     diag "You asked for date format '$fake_syspref'.";
37     unless (scalar grep {/^$fake_syspref$/} @formats) {
38         diag "Warning: Unkown date format '$fake_syspref', reverting to default '$fake_syspref_default'.";
39         $fake_syspref = $fake_syspref_default;
40     }
41 }
42 $fake_syspref or $fake_syspref = $fake_syspref_default;
43 $C4::Dates::prefformat = $fake_syspref;     # So Dates doesn't have to ask the DB anything.
44
45 diag <<EndOfDiag;
46
47 In order to run without DB access, this test will substitute '$fake_syspref'
48 as your default date format.  Export environmental variable KOHA_TEST_DATE_FORMAT
49 to override this default, or pass the value as an argument to this test script.
50
51 NOTE: we test for the system handling dd=00 and 00 for TIME values, therefore
52 you SHOULD see some warnings like:
53 Illegal date specified (year = 1952, month = 1, day = 00) at t/Dates.t ...
54
55 Testing Legacy Functions: format_date and format_date_in_iso
56
57 EndOfDiag
58
59 ok($syspref = C4::Dates->new->format(),         "Your system preference is: $syspref");
60 print "\n";
61 foreach (@{$thash{'iso'}}) {
62         ok($val = format_date($_),                  "format_date('$_'): $val"            );
63 }
64 foreach (@{$thash{$syspref}}) {
65         ok($val = format_date_in_iso($_),           "format_date_in_iso('$_'): $val"     );
66 }
67 ok($today0 = C4::Dates->today(),                "(default) CLASS ->today : $today0" );
68 diag "\nTesting " . scalar(@formats) . " formats.\nTesting no input (defaults):\n";
69 print "\n";
70 foreach (@formats) {
71         my $pre = sprintf '(%-6s)', $_;
72         ok($date = C4::Dates->new(),                "$pre Date Creation   : new()");
73         ok($_ eq ($format = $date->format($_)),     "$pre format($_)      : " . ($format|| 'FAILED') );
74         ok($format = $date->visual(),                           "$pre visual()        : " . ($format|| 'FAILED') );
75         ok($today  = $date->output(),               "$pre output()        : " . ($today || 'FAILED') );
76         ok($today  = $date->today(),                "$pre object->today   : " . ($today || 'FAILED') );
77         print "\n";
78 }
79
80 diag "\nTesting with valid inputs:\n";
81 foreach $format (@formats) {
82         my $pre = sprintf '(%-6s)', $format;
83   foreach my $testval (@{$thash{ $format }}) {
84         ok($date = C4::Dates->new($testval,$format),         "$pre Date Creation   : new('$testval','$format')");
85         ok($re   = $date->regexp,                            "$pre has regexp()" );
86         ok($testval =~ /^$re$/,                              "$pre has regexp() match $testval");
87         ok($val  = $date->output(),                 describe("$pre output()", $val) );
88     SKIP: {
89         skip("special case with explicit regexp('syspref') because $format isn't $syspref", 1) unless ($format eq $syspref);
90         my $re_syspref = C4::Dates->regexp('syspref');
91         ok($testval =~ /^$re_syspref$/,                  "$pre has regexp('syspref') match $testval");
92     }
93         foreach (grep {!/$format/} @formats) {
94                 ok($today = $date->output($_),          describe(sprintf("$pre output(%8s)","'$_'"), $today) );
95         }
96         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) );
99         # ok($format eq ($format = $date->format()),  "$pre format()        : $format" );
100         print "\n";
101   }
102 }
103
104 diag "\nTesting object independence from class\n";
105 my $in1 = '12/25/1952'; # us
106 my $in2 = '13/01/2001'; # metric
107 my $d1 = C4::Dates->new($in1, 'us');
108 my $d2 = C4::Dates->new($in2, 'metric');
109 my $out1 = $d1->output('iso');
110 my $out2 = $d2->output('iso');
111 ok($out1 ne $out2,                             "subsequent constructors get different dataspace ($out1 != $out2)");
112 diag "done.\n";