RT 4.0.13
[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 => 30;
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
21 my $user_b = RT::Test->load_or_create_user( Name => 'boston', Timezone => 'America/New_York' );
22 ok $user_b && $user_b->id;
23
24 my $cf = RT::CustomField->new(RT->SystemUser);
25 ok(
26     $cf->Create(
27         Name       => 'TestDateTime',
28         Type       => 'DateTime',
29         MaxValues  => 1,
30         LookupType => RT::Ticket->CustomFieldLookupType,
31     ),
32     'create cf datetime'
33 );
34 ok( $cf->AddToObject($q), 'date cf apply to queue' );
35 my $cf_name = $cf->Name;
36
37 my $ticket = RT::Ticket->new(RT->SystemUser);
38
39 ok(
40     $ticket->Create(
41         Queue                    => $q->id,
42         Subject                  => 'Test',
43         'CustomField-' . $cf->id => '2010-05-04 07:00:00',
44     ),
45     'create ticket with cf set to 2010-05-04 07:00:00( 2010-05-04 11:00:00 with UTC )'
46 );
47
48 is(
49     $ticket->CustomFieldValues->First->Content,
50     '2010-05-04 11:00:00',
51     'date in db is in timezone UTC'
52 );
53
54 {
55
56     my $tickets = RT::Tickets->new(RT->SystemUser);
57     $tickets->LimitCustomField(
58         CUSTOMFIELD => $cf->id,
59         OPERATOR    => '=',
60         VALUE       => '2010-05-04 07:00:00',    # this timezone is server
61     );
62
63     is( $tickets->Count, 1, 'found the ticket with exact date: 2010-05-04 07:00:00' );
64 }
65
66 {
67
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,
73         OPERATOR    => '=',
74         VALUE       => '2010-05-04',
75     );
76
77     is( $tickets->Count, 1, 'found the ticket with rough date: 2010-05-04' );
78 }
79
80 {
81
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,
87         OPERATOR    => '=',
88         VALUE       => '2010-05-05',
89     );
90
91     is( $tickets->Count, 0, 'did not find the ticket with wrong datetime: 2010-05-05' );
92 }
93
94 {
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' );
98
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' );
101
102     $tickets->FromSQL( "'CF.{$cf_name}' > 'May 3 2010 7am'" );
103     is( $tickets->Count, 1, 'found the ticket with > May 3 2010 7am' );
104
105     $tickets->FromSQL( "'CF.{$cf_name}' < 'May 4 2010 8am'" );
106     is( $tickets->Count, 1, 'found the ticket with < May 4 2010 8am' );
107
108 }
109
110
111 my $tickets = RT::Tickets->new( RT->SystemUser );
112 $tickets->UnLimit;
113 while( my $ticket  = $tickets->Next ) {
114     $ticket->Delete();
115 }
116
117 {
118     ok(
119         $ticket->Create(
120             Queue                    => $q->id,
121             Subject                  => 'Test',
122             'CustomField-' . $cf->id => '2010-06-21 17:00:01',
123         ),
124 'create ticket with cf set to 2010-06-21 17:00:01( 2010-06-21 21:00:01 with UTC )'
125     );
126
127     my $shanghai = RT::Test->load_or_create_user(
128         Name     => 'shanghai',
129         Timezone => 'Asia/Shanghai',
130     );
131
132     ok(
133         $shanghai->PrincipalObj->GrantRight(
134             Right  => 'SuperUser',
135             Object => $RT::System,
136         )
137     );
138
139     my $current_user = RT::CurrentUser->new($shanghai);
140     my $tickets      = RT::Tickets->new($current_user);
141     $tickets->LimitCustomField(
142         CUSTOMFIELD => $cf->id,
143         OPERATOR    => '=',
144         VALUE       => '2010-06-22',
145     );
146     is( $tickets->Count, 1, 'found the ticket with rough datetime: 2010-06-22' );
147
148     $tickets->UnLimit;
149     $tickets->LimitCustomField(
150         CUSTOMFIELD => $cf->id,
151         OPERATOR    => '>',
152         VALUE       => '2010-06-21',
153     );
154     is( $tickets->Count, 1, 'found the ticket with > 2010-06-21' );
155
156     $tickets->UnLimit;
157     $tickets->LimitCustomField(
158         CUSTOMFIELD => $cf->id,
159         OPERATOR    => '<',
160         VALUE       => '2010-06-23',
161     );
162     is( $tickets->Count, 1, 'found the ticket with < 2010-06-23' );
163
164     $tickets->UnLimit;
165     $tickets->LimitCustomField(
166         CUSTOMFIELD => $cf->id,
167         OPERATOR    => '=',
168         VALUE       => '2010-06-22 05:00:01',
169     );
170     is( $tickets->Count, 1, 'found the ticket with = 2010-06-22 01:00:01' );
171 }
172
173 # set timezone in all places to UTC
174 {
175     RT->SystemUser->UserObj->__Set(Field => 'Timezone', Value => 'UTC')
176                                 if RT->SystemUser->UserObj->Timezone;
177     RT->Config->Set( Timezone => 'UTC' );
178 }
179
180 # search by absolute date with '=', but date only
181 {
182     my $ticket = RT::Ticket->new(RT->SystemUser);
183     my ($tid) = $ticket->Create(
184         Queue                    => $q->id,
185         Subject                  => 'Test',
186         'CustomField-' . $cf->id => '2013-02-11 23:14:15',
187     );
188     is $ticket->FirstCustomFieldValue($cf_name), '2013-02-11 23:14:15';
189
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);
193
194     $tickets = RT::Tickets->new($user_m);
195     $tickets->FromSQL("'CustomField.{$cf_name}' = '2013-02-12' AND id = $tid");
196     is( $tickets->Count, 1);
197
198     $tickets = RT::Tickets->new($user_b);
199     $tickets->FromSQL("'CustomField.{$cf_name}' = '2013-02-11' AND id = $tid");
200     is( $tickets->Count, 1);
201
202     $tickets = RT::Tickets->new($user_b);
203     $tickets->FromSQL("'CustomField.{$cf_name}' = '2013-02-12' AND id = $tid");
204     is( $tickets->Count, 0);
205 }
206
207 # search by relative date with '=', but date only
208 {
209     my $ticket = RT::Ticket->new(RT->SystemUser);
210     my ($tid) = $ticket->Create(
211         Queue                    => $q->id,
212         Subject                  => 'Test',
213         'CustomField-' . $cf->id => '2013-02-11 23:14:15',
214     );
215     is $ticket->FirstCustomFieldValue($cf_name), '2013-02-11 23:14:15';
216
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);
221
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);
226
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);
231
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);
236 }
237