1 use Test::MockTime qw(set_fixed_time restore_time);
6 use RT::Test nodata => 1, tests => 30;
7 RT->Config->Set( 'Timezone' => 'EST5EDT' ); # -04:00
10 { Principal => 'Everyone', Right => [qw(
11 SeeQueue ShowTicket CreateTicket SeeCustomField ModifyCustomField
15 my $q = RT::Test->load_or_create_queue( Name => 'General' );
16 ok $q && $q->id, 'loaded or created a queue';
18 my $user_m = RT::Test->load_or_create_user( Name => 'moscow', Timezone => 'Europe/Moscow' );
19 ok $user_m && $user_m->id;
21 my $user_b = RT::Test->load_or_create_user( Name => 'boston', Timezone => 'America/New_York' );
22 ok $user_b && $user_b->id;
24 my $cf = RT::CustomField->new(RT->SystemUser);
27 Name => 'TestDateTime',
30 LookupType => RT::Ticket->CustomFieldLookupType,
34 ok( $cf->AddToObject($q), 'date cf apply to queue' );
35 my $cf_name = $cf->Name;
37 my $ticket = RT::Ticket->new(RT->SystemUser);
43 'CustomField-' . $cf->id => '2010-05-04 07:00:00',
45 'create ticket with cf set to 2010-05-04 07:00:00( 2010-05-04 11:00:00 with UTC )'
49 $ticket->CustomFieldValues->First->Content,
50 '2010-05-04 11:00:00',
51 'date in db is in timezone UTC'
56 my $tickets = RT::Tickets->new(RT->SystemUser);
57 $tickets->LimitCustomField(
58 CUSTOMFIELD => $cf->id,
60 VALUE => '2010-05-04 07:00:00', # this timezone is server
63 is( $tickets->Count, 1, 'found the ticket with exact date: 2010-05-04 07:00:00' );
68 # TODO according to the code, if OPERATOR is '=', it means on that day
69 # this will test this behavior
70 my $tickets = RT::Tickets->new(RT->SystemUser);
71 $tickets->LimitCustomField(
72 CUSTOMFIELD => $cf->id,
74 VALUE => '2010-05-04',
77 is( $tickets->Count, 1, 'found the ticket with rough date: 2010-05-04' );
82 # TODO according to the code, if OPERATOR is '=', it means on that day
83 # this will test this behavior
84 my $tickets = RT::Tickets->new(RT->SystemUser);
85 $tickets->LimitCustomField(
86 CUSTOMFIELD => $cf->id,
88 VALUE => '2010-05-05',
91 is( $tickets->Count, 0, 'did not find the ticket with wrong datetime: 2010-05-05' );
95 my $tickets = RT::Tickets->new(RT->SystemUser);
96 $tickets->FromSQL( "'CF.{$cf_name}' = 'May 4 2010 7am'" );
97 is( $tickets->Count, 1, 'found the ticket with = May 4 2010 7am' );
99 $tickets->FromSQL( "'CF.{$cf_name}' = 'May 4 2010 8am'" );
100 is( $tickets->Count, 0, 'did not find the ticket with = May 4 2010 8am' );
102 $tickets->FromSQL( "'CF.{$cf_name}' > 'May 3 2010 7am'" );
103 is( $tickets->Count, 1, 'found the ticket with > May 3 2010 7am' );
105 $tickets->FromSQL( "'CF.{$cf_name}' < 'May 4 2010 8am'" );
106 is( $tickets->Count, 1, 'found the ticket with < May 4 2010 8am' );
111 my $tickets = RT::Tickets->new( RT->SystemUser );
113 while( my $ticket = $tickets->Next ) {
122 'CustomField-' . $cf->id => '2010-06-21 17:00:01',
124 'create ticket with cf set to 2010-06-21 17:00:01( 2010-06-21 21:00:01 with UTC )'
127 my $shanghai = RT::Test->load_or_create_user(
129 Timezone => 'Asia/Shanghai',
133 $shanghai->PrincipalObj->GrantRight(
134 Right => 'SuperUser',
135 Object => $RT::System,
139 my $current_user = RT::CurrentUser->new($shanghai);
140 my $tickets = RT::Tickets->new($current_user);
141 $tickets->LimitCustomField(
142 CUSTOMFIELD => $cf->id,
144 VALUE => '2010-06-22',
146 is( $tickets->Count, 1, 'found the ticket with rough datetime: 2010-06-22' );
149 $tickets->LimitCustomField(
150 CUSTOMFIELD => $cf->id,
152 VALUE => '2010-06-21',
154 is( $tickets->Count, 1, 'found the ticket with > 2010-06-21' );
157 $tickets->LimitCustomField(
158 CUSTOMFIELD => $cf->id,
160 VALUE => '2010-06-23',
162 is( $tickets->Count, 1, 'found the ticket with < 2010-06-23' );
165 $tickets->LimitCustomField(
166 CUSTOMFIELD => $cf->id,
168 VALUE => '2010-06-22 05:00:01',
170 is( $tickets->Count, 1, 'found the ticket with = 2010-06-22 01:00:01' );
173 # set timezone in all places to UTC
175 RT->SystemUser->UserObj->__Set(Field => 'Timezone', Value => 'UTC')
176 if RT->SystemUser->UserObj->Timezone;
177 RT->Config->Set( Timezone => 'UTC' );
180 # search by absolute date with '=', but date only
182 my $ticket = RT::Ticket->new(RT->SystemUser);
183 my ($tid) = $ticket->Create(
186 'CustomField-' . $cf->id => '2013-02-11 23:14:15',
188 is $ticket->FirstCustomFieldValue($cf_name), '2013-02-11 23:14:15';
190 my $tickets = RT::Tickets->new($user_m);
191 $tickets->FromSQL("'CustomField.{$cf_name}' = '2013-02-11' AND id = $tid");
192 is( $tickets->Count, 0);
194 $tickets = RT::Tickets->new($user_m);
195 $tickets->FromSQL("'CustomField.{$cf_name}' = '2013-02-12' AND id = $tid");
196 is( $tickets->Count, 1);
198 $tickets = RT::Tickets->new($user_b);
199 $tickets->FromSQL("'CustomField.{$cf_name}' = '2013-02-11' AND id = $tid");
200 is( $tickets->Count, 1);
202 $tickets = RT::Tickets->new($user_b);
203 $tickets->FromSQL("'CustomField.{$cf_name}' = '2013-02-12' AND id = $tid");
204 is( $tickets->Count, 0);
207 # search by relative date with '=', but date only
209 my $ticket = RT::Ticket->new(RT->SystemUser);
210 my ($tid) = $ticket->Create(
213 'CustomField-' . $cf->id => '2013-02-11 23:14:15',
215 is $ticket->FirstCustomFieldValue($cf_name), '2013-02-11 23:14:15';
217 set_fixed_time("2013-02-10T16:10:00Z");
218 my $tickets = RT::Tickets->new($user_m);
219 $tickets->FromSQL("'CustomField.{$cf_name}' = 'tomorrow' AND id = $tid");
220 is( $tickets->Count, 0);
222 set_fixed_time("2013-02-10T23:10:00Z");
223 $tickets = RT::Tickets->new($user_m);
224 $tickets->FromSQL("'CustomField.{$cf_name}' = 'tomorrow' AND id = $tid");
225 is( $tickets->Count, 1);
227 set_fixed_time("2013-02-10T23:10:00Z");
228 $tickets = RT::Tickets->new($user_b);
229 $tickets->FromSQL("'CustomField.{$cf_name}' = 'tomorrow' AND id = $tid");
230 is( $tickets->Count, 1);
232 set_fixed_time("2013-02-10T02:10:00Z");
233 $tickets = RT::Tickets->new($user_b);
234 $tickets->FromSQL("'CustomField.{$cf_name}' = 'tomorrow' AND id = $tid");
235 is( $tickets->Count, 0);