RT 4.2.11, ticket#13852
[freeside.git] / rt / t / customfields / datetime_search.t
1 use Test::MockTime qw(set_fixed_time restore_time);
2
3 use warnings;
4 use strict;
5
6 use RT::Test nodata => 1, tests => undef;
7 RT->Config->Set( 'Timezone' => 'EST5EDT' ); # -04:00
8
9 RT::Test->set_rights(
10     { Principal => 'Everyone', Right => [qw(
11         SeeQueue ShowTicket CreateTicket SeeCustomField ModifyCustomField
12     )] },
13 );
14
15 my $q = RT::Test->load_or_create_queue( Name => 'General' );
16 ok $q && $q->id, 'loaded or created a queue';
17
18 my $user_m = RT::Test->load_or_create_user( Name => 'moscow', Timezone => 'Europe/Moscow' );
19 ok $user_m && $user_m->id;
20 $user_m = RT::CurrentUser->new( $user_m );
21
22 my $user_b = RT::Test->load_or_create_user( Name => 'boston', Timezone => 'America/New_York' );
23 ok $user_b && $user_b->id;
24 $user_b = RT::CurrentUser->new( $user_b );
25
26 my $cf = RT::CustomField->new(RT->SystemUser);
27 ok(
28     $cf->Create(
29         Name       => 'TestDateTime',
30         Type       => 'DateTime',
31         MaxValues  => 1,
32         LookupType => RT::Ticket->CustomFieldLookupType,
33     ),
34     'create cf datetime'
35 );
36 ok( $cf->AddToObject($q), 'date cf apply to queue' );
37 my $cf_name = $cf->Name;
38
39 my $ticket = RT::Ticket->new(RT->SystemUser);
40
41 ok(
42     $ticket->Create(
43         Queue                    => $q->id,
44         Subject                  => 'Test',
45         'CustomField-' . $cf->id => '2010-05-04 07:00:00',
46     ),
47     'create ticket with cf set to 2010-05-04 07:00:00( 2010-05-04 11:00:00 with UTC )'
48 );
49
50 is(
51     $ticket->CustomFieldValues->First->Content,
52     '2010-05-04 11:00:00',
53     'date in db is in timezone UTC'
54 );
55
56 {
57
58     my $tickets = RT::Tickets->new(RT->SystemUser);
59     $tickets->LimitCustomField(
60         CUSTOMFIELD => $cf->id,
61         OPERATOR    => '=',
62         VALUE       => '2010-05-04 07:00:00',    # this timezone is server
63     );
64
65     is( $tickets->Count, 1, 'found the ticket with exact date: 2010-05-04 07:00:00' );
66 }
67
68 {
69
70     # TODO according to the code, if OPERATOR is '=', it means on that day
71     # this will test this behavior
72     my $tickets = RT::Tickets->new(RT->SystemUser);
73     $tickets->LimitCustomField(
74         CUSTOMFIELD => $cf->id,
75         OPERATOR    => '=',
76         VALUE       => '2010-05-04',
77     );
78
79     is( $tickets->Count, 1, 'found the ticket with rough date: 2010-05-04' );
80 }
81
82 {
83
84     # TODO according to the code, if OPERATOR is '=', it means on that day
85     # this will test this behavior
86     my $tickets = RT::Tickets->new(RT->SystemUser);
87     $tickets->LimitCustomField(
88         CUSTOMFIELD => $cf->id,
89         OPERATOR    => '=',
90         VALUE       => '2010-05-05',
91     );
92
93     is( $tickets->Count, 0, 'did not find the ticket with wrong datetime: 2010-05-05' );
94 }
95
96 {
97     my $tickets = RT::Tickets->new(RT->SystemUser);
98     $tickets->FromSQL( "'CF.{$cf_name}' = 'May 4 2010 7am'" );
99     is( $tickets->Count, 1, 'found the ticket with = May 4 2010 7am' );
100
101     $tickets->FromSQL( "'CF.{$cf_name}' = 'May 4 2010 8am'" );
102     is( $tickets->Count, 0, 'did not find the ticket with = May 4 2010 8am' );
103
104     $tickets->FromSQL( "'CF.{$cf_name}' > 'May 3 2010 7am'" );
105     is( $tickets->Count, 1, 'found the ticket with > May 3 2010 7am' );
106
107     $tickets->FromSQL( "'CF.{$cf_name}' < 'May 4 2010 8am'" );
108     is( $tickets->Count, 1, 'found the ticket with < May 4 2010 8am' );
109
110 }
111
112
113 my $tickets = RT::Tickets->new( RT->SystemUser );
114 $tickets->UnLimit;
115 while( my $ticket  = $tickets->Next ) {
116     $ticket->Delete();
117 }
118
119 {
120     ok(
121         $ticket->Create(
122             Queue                    => $q->id,
123             Subject                  => 'Test',
124             'CustomField-' . $cf->id => '2010-06-21 17:00:01',
125         ),
126 'create ticket with cf set to 2010-06-21 17:00:01( 2010-06-21 21:00:01 with UTC )'
127     );
128
129     my $shanghai = RT::Test->load_or_create_user(
130         Name     => 'shanghai',
131         Timezone => 'Asia/Shanghai',
132     );
133
134     ok(
135         $shanghai->PrincipalObj->GrantRight(
136             Right  => 'SuperUser',
137             Object => $RT::System,
138         )
139     );
140
141     my $current_user = RT::CurrentUser->new($shanghai);
142     my $tickets      = RT::Tickets->new($current_user);
143     $tickets->LimitCustomField(
144         CUSTOMFIELD => $cf->id,
145         OPERATOR    => '=',
146         VALUE       => '2010-06-22',
147     );
148     is( $tickets->Count, 1, 'found the ticket with rough datetime: 2010-06-22' );
149
150     $tickets->UnLimit;
151     $tickets->LimitCustomField(
152         CUSTOMFIELD => $cf->id,
153         OPERATOR    => '>',
154         VALUE       => '2010-06-21',
155     );
156     is( $tickets->Count, 1, 'found the ticket with > 2010-06-21' );
157
158     $tickets->UnLimit;
159     $tickets->LimitCustomField(
160         CUSTOMFIELD => $cf->id,
161         OPERATOR    => '<',
162         VALUE       => '2010-06-23',
163     );
164     is( $tickets->Count, 1, 'found the ticket with < 2010-06-23' );
165
166     $tickets->UnLimit;
167     $tickets->LimitCustomField(
168         CUSTOMFIELD => $cf->id,
169         OPERATOR    => '=',
170         VALUE       => '2010-06-22 05:00:01',
171     );
172     is( $tickets->Count, 1, 'found the ticket with = 2010-06-22 01:00:01' );
173 }
174
175 # set timezone in all places to UTC
176 {
177     RT->SystemUser->UserObj->__Set(Field => 'Timezone', Value => 'UTC')
178                                 if RT->SystemUser->UserObj->Timezone;
179     RT->Config->Set( Timezone => 'UTC' );
180 }
181
182 # search by absolute date with '=', but date only
183 {
184     my $ticket = RT::Ticket->new(RT->SystemUser);
185     my ($tid) = $ticket->Create(
186         Queue                    => $q->id,
187         Subject                  => 'Test',
188         'CustomField-' . $cf->id => '2013-02-11 23:14:15',
189     );
190     is $ticket->FirstCustomFieldValue($cf_name), '2013-02-11 23:14:15';
191
192     my $tickets = RT::Tickets->new($user_m);
193     $tickets->FromSQL("'CustomField.{$cf_name}' = '2013-02-11' AND id = $tid");
194     is( $tickets->Count, 0);
195
196     $tickets = RT::Tickets->new($user_m);
197     $tickets->FromSQL("'CustomField.{$cf_name}' = '2013-02-12' AND id = $tid");
198     is( $tickets->Count, 1);
199
200     $tickets = RT::Tickets->new($user_b);
201     $tickets->FromSQL("'CustomField.{$cf_name}' = '2013-02-11' AND id = $tid");
202     is( $tickets->Count, 1);
203
204     $tickets = RT::Tickets->new($user_b);
205     $tickets->FromSQL("'CustomField.{$cf_name}' = '2013-02-12' AND id = $tid");
206     is( $tickets->Count, 0);
207 }
208
209 {
210     my $tickets = RT::Tickets->new(RT->SystemUser);
211     $tickets->LimitCustomField(
212         CUSTOMFIELD => $cf->id,
213         OPERATOR    => 'IS',
214         VALUE       => 'NULL',
215     );
216
217     is( $tickets->Count, 0, 'did not find the ticket with date IS NULL' );
218 }
219
220 {
221     my $tickets = RT::Tickets->new(RT->SystemUser);
222     $tickets->LimitCustomField(
223         CUSTOMFIELD => $cf->id,
224         OPERATOR    => 'IS NOT',
225         VALUE       => 'NULL',
226     );
227
228     is( $tickets->Count, 2, 'did find the ticket with date IS NOT NULL' );
229 }
230
231
232 # search by relative date with '=', but date only
233 {
234     my $ticket = RT::Ticket->new(RT->SystemUser);
235     my ($tid) = $ticket->Create(
236         Queue                    => $q->id,
237         Subject                  => 'Test',
238         'CustomField-' . $cf->id => '2013-02-11 23:14:15',
239     );
240     is $ticket->FirstCustomFieldValue($cf_name), '2013-02-11 23:14:15';
241
242     set_fixed_time("2013-02-10T16:10:00Z");
243     my $tickets = RT::Tickets->new($user_m);
244     $tickets->FromSQL("'CustomField.{$cf_name}' = 'tomorrow' AND id = $tid");
245     is( $tickets->Count, 0);
246
247     set_fixed_time("2013-02-10T23:10:00Z");
248     $tickets = RT::Tickets->new($user_m);
249     $tickets->FromSQL("'CustomField.{$cf_name}' = 'tomorrow' AND id = $tid");
250     is( $tickets->Count, 1);
251
252     set_fixed_time("2013-02-10T23:10:00Z");
253     $tickets = RT::Tickets->new($user_b);
254     $tickets->FromSQL("'CustomField.{$cf_name}' = 'tomorrow' AND id = $tid");
255     is( $tickets->Count, 1);
256
257     set_fixed_time("2013-02-10T02:10:00Z");
258     $tickets = RT::Tickets->new($user_b);
259     $tickets->FromSQL("'CustomField.{$cf_name}' = 'tomorrow' AND id = $tid");
260     is( $tickets->Count, 0);
261 }
262
263 done_testing;