X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=rt%2Ft%2Fapi%2Fdate.t;h=6fcaa494b323fc47b8da3616feeac1d509a2b119;hb=c24d6e2242ae0e026684b8f95decf156aba6e75e;hp=bc1446f506dce6fc90f7f29827fe427ebadc5129;hpb=b4b0c7e72d7eaee2fbfc7022022c9698323203dd;p=freeside.git diff --git a/rt/t/api/date.t b/rt/t/api/date.t index bc1446f50..6fcaa494b 100644 --- a/rt/t/api/date.t +++ b/rt/t/api/date.t @@ -1,47 +1,31 @@ #!/usr/bin/perl use Test::MockTime qw(set_fixed_time restore_time); - -use Test::More; -my $tests; - -my $localized_datetime_tests; -BEGIN { - $tests = 167; - $localized_datetime_tests = - eval { require DateTime; 1; } && eval { require DateTime::Locale; 1; } && - DateTime->can('format_cldr') && DateTime::Locale::root->can('date_format_full'); - - if ($localized_datetime_tests) { - - # Include RT::Date::LocalizedDateTime tests - $tests += 7; - } -} +use DateTime; use warnings; use strict; -use RT::Test tests => $tests; +use RT::Test tests => 173; use RT::User; use Test::Warn; use_ok('RT::Date'); { - my $date = RT::Date->new($RT::SystemUser); + my $date = RT::Date->new(RT->SystemUser); isa_ok($date, 'RT::Date', "constructor returned RT::Date oject"); - $date = $date->new($RT::SystemUser); + $date = $date->new(RT->SystemUser); isa_ok($date, 'RT::Date', "constructor returned RT::Date oject"); } { # set timezone in all places to UTC - $RT::SystemUser->UserObj->__Set(Field => 'Timezone', Value => 'UTC') - if $RT::SystemUser->UserObj->Timezone; + RT->SystemUser->UserObj->__Set(Field => 'Timezone', Value => 'UTC') + if RT->SystemUser->UserObj->Timezone; RT->Config->Set( Timezone => 'UTC' ); } my $current_user; { - my $user = RT::User->new($RT::SystemUser); + my $user = RT::User->new(RT->SystemUser); my($uid, $msg) = $user->Create( Name => "date_api". rand(200), Lang => 'en', @@ -53,7 +37,6 @@ my $current_user; { my $date = RT::Date->new( $current_user ); - is($date->Timezone, 'UTC', "dropped all timzones to UTC"); is($date->Timezone('user'), 'UTC', "dropped all timzones to UTC"); is($date->Timezone('server'), 'UTC', "dropped all timzones to UTC"); is($date->Timezone('unknown'), 'UTC', "with wrong context returns UTC"); @@ -65,7 +48,6 @@ my $current_user; is($date->Timezone('user'), 'Europe/Moscow', "in user context returns user's timezone"); - is($date->Timezone, 'UTC', "the deafult value is always UTC"); is($date->Timezone('server'), 'UTC', "wasn't changed"); RT->Config->Set( Timezone => 'Africa/Ouagadougou' ); @@ -75,7 +57,6 @@ my $current_user; is($date->Timezone('user'), 'Europe/Moscow', "in user context still returns user's timezone"); - is($date->Timezone, 'UTC', "the deafult value is always UTC"); $current_user->UserObj->__Set( Field => 'Timezone', Value => ''); is_empty($current_user->UserObj->Timezone, @@ -83,7 +64,6 @@ my $current_user; is($date->Timezone('user'), 'Africa/Ouagadougou', "in user context returns timezone of the server if user's one is not defined"); - is($date->Timezone, 'UTC', "the deafult value is always UTC"); RT->Config->Set( Timezone => 'GMT' ); is($date->Timezone('server'), @@ -91,7 +71,6 @@ my $current_user; "timezone is GMT which one is alias for UTC"); RT->Config->Set( Timezone => '' ); - is($date->Timezone, 'UTC', "dropped all timzones to UTC"); is($date->Timezone('user'), 'UTC', "user's and server's timzones are not defined, so UTC"); @@ -103,12 +82,14 @@ my $current_user; } { - my $date = RT::Date->new($RT::SystemUser); + my $date = RT::Date->new(RT->SystemUser); is($date->Unix, 0, "new date returns 0 in Unix format"); is($date->Get, '1970-01-01 00:00:00', "default is ISO format"); - is($date->Get(Format =>'SomeBadFormat'), - '1970-01-01 00:00:00', - "don't know format, return ISO format"); + warning_like { + is($date->Get(Format =>'SomeBadFormat'), + '1970-01-01 00:00:00', + "don't know format, return ISO format"); + } qr/Invalid date formatter/; is($date->Get(Format =>'W3CDTF'), '1970-01-01T00:00:00Z', "W3CDTF format with defaults"); @@ -117,7 +98,7 @@ my $current_user; "RFC2822 format with defaults"); is($date->Get(Format =>'LocalizedDateTime'), 'Thu, Jan 1, 1970 12:00:00 AM', - "LocalizedDateTime format with defaults") if ( $localized_datetime_tests ); + "LocalizedDateTime format with defaults"); is($date->ISO(Time => 0), '1970-01-01', @@ -130,7 +111,7 @@ my $current_user; "RFC2822 format without time part"); is($date->LocalizedDateTime(Time => 0), 'Thu, Jan 1, 1970', - "LocalizedDateTime format without time part") if ( $localized_datetime_tests ); + "LocalizedDateTime format without time part"); is($date->ISO(Date => 0), '00:00:00', @@ -143,7 +124,7 @@ my $current_user; "RFC2822 format without date part"); is($date->LocalizedDateTime(Date => 0), '12:00:00 AM', - "LocalizedDateTime format without date part") if ( $localized_datetime_tests ); + "LocalizedDateTime format without date part"); is($date->ISO(Date => 0, Seconds => 0), '00:00', @@ -164,16 +145,16 @@ my $current_user; is($date->LocalizedDateTime(AbbrDay => 0), 'Thursday, Jan 1, 1970 12:00:00 AM', - "LocalizedDateTime format without abbreviation of day") if ( $localized_datetime_tests ); + "LocalizedDateTime format without abbreviation of day"); is($date->LocalizedDateTime(AbbrMonth => 0), 'Thu, January 1, 1970 12:00:00 AM', - "LocalizedDateTime format without abbreviation of month") if ( $localized_datetime_tests ); + "LocalizedDateTime format without abbreviation of month"); is($date->LocalizedDateTime(DateFormat => 'date_format_short'), '1/1/70 12:00:00 AM', - "LocalizedDateTime format with non default DateFormat") if ( $localized_datetime_tests ); + "LocalizedDateTime format with non default DateFormat"); is($date->LocalizedDateTime(TimeFormat => 'time_format_short'), 'Thu, Jan 1, 1970 12:00 AM', - "LocalizedDateTime format with non default TimeFormat") if ( $localized_datetime_tests ); + "LocalizedDateTime format with non default TimeFormat"); is($date->Date, '1970-01-01', @@ -256,14 +237,14 @@ my $current_user; warning_like { # bad format - my $date = RT::Date->new($RT::SystemUser); + my $date = RT::Date->new(RT->SystemUser); $date->Set( Format => 'bad' ); is($date->Unix, 0, "bad format"); -} qr'Unknown Date format: bad'; +} qr{Unknown Date format: bad}; { # setting value via Unix method - my $date = RT::Date->new($RT::SystemUser); + my $date = RT::Date->new(RT->SystemUser); $date->Unix(1); is($date->ISO, '1970-01-01 00:00:01', "correct value"); @@ -280,7 +261,7 @@ warning_like my $year = (localtime(time))[5] + 1900; { # set+ISO format - my $date = RT::Date->new($RT::SystemUser); + my $date = RT::Date->new(RT->SystemUser); warning_like { $date->Set(Format => 'ISO', Value => 'weird date'); } qr/Couldn't parse date 'weird date' as a ISO format/; @@ -324,7 +305,7 @@ my $year = (localtime(time))[5] + 1900; } { # set+datemanip format(Time::ParseDate) - my $date = RT::Date->new($RT::SystemUser); + my $date = RT::Date->new(RT->SystemUser); $date->Set(Format => 'unknown', Value => 'weird date'); is($date->Unix, 0, "date was wrong"); @@ -343,7 +324,7 @@ my $year = (localtime(time))[5] + 1900; } { # set+unknown format(Time::ParseDate) - my $date = RT::Date->new($RT::SystemUser); + my $date = RT::Date->new(RT->SystemUser); $date->Set(Format => 'unknown', Value => 'weird date'); is($date->Unix, 0, "date was wrong"); @@ -380,7 +361,7 @@ my $year = (localtime(time))[5] + 1900; } { # SetToMidnight - my $date = RT::Date->new($RT::SystemUser); + my $date = RT::Date->new(RT->SystemUser); RT->Config->Set( Timezone => 'Europe/Moscow' ); $date->Set(Format => 'ISO', Value => '2005-11-28 15:10:00'); @@ -414,7 +395,7 @@ my $year = (localtime(time))[5] + 1900; } { # SetToNow - my $date = RT::Date->new($RT::SystemUser); + my $date = RT::Date->new(RT->SystemUser); my $time = time; $date->SetToNow; ok($date->Unix >= $time, 'close enough'); @@ -422,7 +403,7 @@ my $year = (localtime(time))[5] + 1900; } { - my $date = RT::Date->new($RT::SystemUser); + my $date = RT::Date->new(RT->SystemUser); $date->Unix(0); $date->AddSeconds; @@ -484,7 +465,7 @@ my $year = (localtime(time))[5] + 1900; } { # DurationAsString - my $date = RT::Date->new($RT::SystemUser); + my $date = RT::Date->new(RT->SystemUser); is($date->DurationAsString(1), '1 sec', '1 sec'); is($date->DurationAsString(59), '59 sec', '59 sec'); @@ -505,7 +486,7 @@ my $year = (localtime(time))[5] + 1900; } { # DiffAsString - my $date = RT::Date->new($RT::SystemUser); + my $date = RT::Date->new(RT->SystemUser); is($date->DiffAsString(1), '', 'no diff, wrong input'); is($date->DiffAsString(-1), '', 'no diff, wrong input'); is($date->DiffAsString('qwe'), '', 'no diff, wrong input'); @@ -516,14 +497,14 @@ my $year = (localtime(time))[5] + 1900; is($date->DiffAsString(3), '1 sec ago', 'diff: 1 sec ago'); is($date->DiffAsString(1), '1 sec', 'diff: 1 sec'); - my $ndate = RT::Date->new($RT::SystemUser); + my $ndate = RT::Date->new(RT->SystemUser); is($date->DiffAsString($ndate), '', 'no diff, wrong input'); $ndate->Unix(3); is($date->DiffAsString($ndate), '1 sec ago', 'diff: 1 sec ago'); } { # Diff - my $date = RT::Date->new($RT::SystemUser); + my $date = RT::Date->new(RT->SystemUser); $date->SetToNow; my $diff = $date->Diff; ok($diff <= 0, 'close enought'); @@ -531,14 +512,14 @@ my $year = (localtime(time))[5] + 1900; } { # AgeAsString - my $date = RT::Date->new($RT::SystemUser); + my $date = RT::Date->new(RT->SystemUser); $date->SetToNow; my $diff = $date->AgeAsString; like($diff, qr/^(0 sec|[1-5] sec ago)$/, 'close enought'); } { # GetWeekday - my $date = RT::Date->new($RT::SystemUser); + my $date = RT::Date->new(RT->SystemUser); is($date->GetWeekday(7), '', '7 and greater are invalid'); is($date->GetWeekday(6), 'Sat', '6 is Saturday'); is($date->GetWeekday(0), 'Sun', '0 is Sunday'); @@ -548,7 +529,7 @@ my $year = (localtime(time))[5] + 1900; } { # GetMonth - my $date = RT::Date->new($RT::SystemUser); + my $date = RT::Date->new(RT->SystemUser); is($date->GetMonth(12), '', '12 and greater are invalid'); is($date->GetMonth(11), 'Dec', '11 is December'); is($date->GetMonth(0), 'Jan', '0 is January'); @@ -557,8 +538,19 @@ my $year = (localtime(time))[5] + 1900; is($date->GetMonth(-13), '', '-13 and lesser are invalid'); } +{ + # set unknown format: edge cases + my $date = RT::Date->new(RT->SystemUser); + $date->Set( Value => 0, Format => 'unknown' ); + is( $date->Unix(), 0, "unix is 0 with Value => 0, Format => 'unknown'" ); + + $date->Set( Value => '', Format => 'unknown' ); + is( $date->Unix(), 0, "unix is 0 with Value => '', Format => 'unknown'" ); + + $date->Set( Value => ' ', Format => 'unknown' ); + is( $date->Unix(), 0, "unix is 0 with Value => ' ', Format => 'unknown'" ); +} + #TODO: AsString #TODO: RFC2822, W3CDTF with Timezones -exit(0); -