RT 4.2.11, ticket#13852
[freeside.git] / rt / t / customfields / date_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
8 RT::Test->set_rights(
9     { Principal => 'Everyone', Right => [qw(
10         SeeQueue ShowTicket CreateTicket SeeCustomField ModifyCustomField
11     )] },
12 );
13
14 my $q = RT::Test->load_or_create_queue( Name => 'General' );
15 ok $q && $q->id, 'loaded or created a queue';
16
17 my $user_m = RT::Test->load_or_create_user( Name => 'moscow', Timezone => 'Europe/Moscow' );
18 ok $user_m && $user_m->id;
19 $user_m = RT::CurrentUser->new( $user_m );
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 $user_b = RT::CurrentUser->new( $user_b );
24
25 my $cf = RT::CustomField->new(RT->SystemUser);
26 ok(
27     $cf->Create(
28         Name       => 'TestDate',
29         Type       => 'Date',
30         MaxValues  => 1,
31         LookupType => RT::Ticket->CustomFieldLookupType,
32     ),
33     'create cf date'
34 );
35 ok( $cf->AddToObject($q), 'date cf apply to queue' );
36 my $cf_name = $cf->Name;
37
38 my $ticket = RT::Ticket->new(RT->SystemUser);
39
40 ok(
41     $ticket->Create(
42         Queue                    => $q->id,
43         Subject                  => 'Test',
44         'CustomField-' . $cf->id => '2010-05-04',
45     ),
46     'create ticket with cf set to 2010-05-04'
47 );
48
49 is( $ticket->CustomFieldValues->First->Content, '2010-05-04', 'date in db is' );
50
51 {
52
53     my $tickets = RT::Tickets->new(RT->SystemUser);
54     $tickets->LimitCustomField(
55         CUSTOMFIELD => $cf->id,
56         OPERATOR    => '=',
57         VALUE       => '2010-05-04',
58     );
59     is( $tickets->Count, 1, 'found the ticket with exact date: 2010-05-04' );
60
61 }
62
63 {
64     my $tickets = RT::Tickets->new(RT->SystemUser);
65     $tickets->LimitCustomField(
66         CUSTOMFIELD => $cf->id,
67         OPERATOR    => '>',
68         VALUE       => '2010-05-03',
69     );
70
71     is( $tickets->Count, 1, 'found ticket with > 2010-05-03' );
72 }
73
74 {
75     my $tickets = RT::Tickets->new(RT->SystemUser);
76     $tickets->LimitCustomField(
77         CUSTOMFIELD => $cf->id,
78         OPERATOR    => '<',
79         VALUE       => '2010-05-05',
80     );
81
82     is( $tickets->Count, 1, 'found ticket with < 2010-05-05' );
83 }
84
85 {
86
87     my $tickets = RT::Tickets->new(RT->SystemUser);
88     $tickets->LimitCustomField(
89         CUSTOMFIELD => $cf->id,
90         OPERATOR    => '=',
91         VALUE       => '2010-05-05',
92     );
93
94     is( $tickets->Count, 0, 'did not find the ticket with = 2010-05-05' );
95 }
96
97 {
98     my $tickets = RT::Tickets->new(RT->SystemUser);
99     $tickets->FromSQL( "'CF.{$cf_name}' = 'May 4 2010'" );
100     is( $tickets->Count, 1, 'found the ticket with = May 4 2010' );
101
102     $tickets->FromSQL( "'CF.{$cf_name}' < 'May 4 2010'" );
103     is( $tickets->Count, 0, 'did not find the ticket with < May 4 2010' );
104
105     $tickets->FromSQL( "'CF.{$cf_name}' < 'May 5 2010'" );
106     is( $tickets->Count, 1, 'found the ticket with < May 5 2010' );
107
108     $tickets->FromSQL( "'CF.{$cf_name}' > 'May 3 2010'" );
109     is( $tickets->Count, 1, 'found the ticket with > May 3 2010' );
110 }
111
112
113 {
114
115     my $tickets = RT::Tickets->new(RT->SystemUser);
116     $tickets->LimitCustomField(
117         CUSTOMFIELD => $cf->id,
118         OPERATOR    => '<',
119         VALUE       => '2010-05-03',
120     );
121
122     is( $tickets->Count, 0, 'did not find the ticket with < 2010-05-03' );
123 }
124
125 {
126
127     my $tickets = RT::Tickets->new(RT->SystemUser);
128     $tickets->LimitCustomField(
129         CUSTOMFIELD => $cf->id,
130         OPERATOR    => '>',
131         VALUE       => '2010-05-05',
132     );
133
134     is( $tickets->Count, 0, 'did not find the ticket with > 2010-05-05' );
135 }
136
137 {
138     my $tickets = RT::Tickets->new(RT->SystemUser);
139     $tickets->LimitCustomField(
140         CUSTOMFIELD => $cf->id,
141         OPERATOR    => 'IS',
142         VALUE       => 'NULL',
143     );
144
145     is( $tickets->Count, 0, 'did not find the ticket with date IS NULL' );
146 }
147
148 {
149     my $tickets = RT::Tickets->new(RT->SystemUser);
150     $tickets->LimitCustomField(
151         CUSTOMFIELD => $cf->id,
152         OPERATOR    => 'IS NOT',
153         VALUE       => 'NULL',
154     );
155
156     is( $tickets->Count, 1, 'did find the ticket with date IS NOT NULL' );
157 }
158
159 # relative search by users in different TZs
160 {
161     my $ticket = RT::Ticket->new(RT->SystemUser);
162     my ($tid) = $ticket->Create(
163         Queue                    => $q->id,
164         Subject                  => 'Test',
165         'CustomField-' . $cf->id => '2013-02-12',
166     );
167
168     set_fixed_time("2013-02-10T23:10:00Z");
169     my $tickets = RT::Tickets->new($user_m);
170     $tickets->FromSQL("'CustomField.{$cf_name}' = 'tomorrow' AND id = $tid");
171     is( $tickets->Count, 1, 'found the ticket' );
172
173     set_fixed_time("2013-02-10T15:10:00Z");
174     $tickets = RT::Tickets->new($user_m);
175     $tickets->FromSQL("'CustomField.{$cf_name}' = 'tomorrow' AND id = $tid");
176     is( $tickets->Count, 0, 'found no tickets' );
177
178     set_fixed_time("2013-02-10T23:10:00Z");
179     $tickets = RT::Tickets->new($user_b);
180     $tickets->FromSQL("'CustomField.{$cf_name}' = 'tomorrow' AND id = $tid");
181     is( $tickets->Count, 0, 'found no tickets' );
182
183     set_fixed_time("2013-02-11T23:10:00Z");
184     $tickets = RT::Tickets->new($user_b);
185     $tickets->FromSQL("'CustomField.{$cf_name}' = 'tomorrow' AND id = $tid");
186     is( $tickets->Count, 1, 'found the tickets' );
187 }
188
189 done_testing;