RT 4.0.13
[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 => 21;
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
20 my $user_b = RT::Test->load_or_create_user( Name => 'boston', Timezone => 'America/New_York' );
21 ok $user_b && $user_b->id;
22
23 my $cf = RT::CustomField->new(RT->SystemUser);
24 ok(
25     $cf->Create(
26         Name       => 'TestDate',
27         Type       => 'Date',
28         MaxValues  => 1,
29         LookupType => RT::Ticket->CustomFieldLookupType,
30     ),
31     'create cf date'
32 );
33 ok( $cf->AddToObject($q), 'date cf apply to queue' );
34 my $cf_name = $cf->Name;
35
36 my $ticket = RT::Ticket->new(RT->SystemUser);
37
38 ok(
39     $ticket->Create(
40         Queue                    => $q->id,
41         Subject                  => 'Test',
42         'CustomField-' . $cf->id => '2010-05-04',
43     ),
44     'create ticket with cf set to 2010-05-04'
45 );
46
47 is( $ticket->CustomFieldValues->First->Content, '2010-05-04', 'date in db is' );
48
49 {
50
51     my $tickets = RT::Tickets->new(RT->SystemUser);
52     $tickets->LimitCustomField(
53         CUSTOMFIELD => $cf->id,
54         OPERATOR    => '=',
55         VALUE       => '2010-05-04',
56     );
57     is( $tickets->Count, 1, 'found the ticket with exact date: 2010-05-04' );
58
59 }
60
61 {
62     my $tickets = RT::Tickets->new(RT->SystemUser);
63     $tickets->LimitCustomField(
64         CUSTOMFIELD => $cf->id,
65         OPERATOR    => '>',
66         VALUE       => '2010-05-03',
67     );
68
69     is( $tickets->Count, 1, 'found ticket with > 2010-05-03' );
70 }
71
72 {
73     my $tickets = RT::Tickets->new(RT->SystemUser);
74     $tickets->LimitCustomField(
75         CUSTOMFIELD => $cf->id,
76         OPERATOR    => '<',
77         VALUE       => '2010-05-05',
78     );
79
80     is( $tickets->Count, 1, 'found ticket with < 2010-05-05' );
81 }
82
83 {
84
85     my $tickets = RT::Tickets->new(RT->SystemUser);
86     $tickets->LimitCustomField(
87         CUSTOMFIELD => $cf->id,
88         OPERATOR    => '=',
89         VALUE       => '2010-05-05',
90     );
91
92     is( $tickets->Count, 0, 'did not find the ticket with = 2010-05-05' );
93 }
94
95 {
96     my $tickets = RT::Tickets->new(RT->SystemUser);
97     $tickets->FromSQL( "'CF.{$cf_name}' = 'May 4 2010'" );
98     is( $tickets->Count, 1, 'found the ticket with = May 4 2010' );
99
100     $tickets->FromSQL( "'CF.{$cf_name}' < 'May 4 2010'" );
101     is( $tickets->Count, 0, 'did not find the ticket with < May 4 2010' );
102
103     $tickets->FromSQL( "'CF.{$cf_name}' < 'May 5 2010'" );
104     is( $tickets->Count, 1, 'found the ticket with < May 5 2010' );
105
106     $tickets->FromSQL( "'CF.{$cf_name}' > 'May 3 2010'" );
107     is( $tickets->Count, 1, 'found the ticket with > May 3 2010' );
108 }
109
110
111 {
112
113     my $tickets = RT::Tickets->new(RT->SystemUser);
114     $tickets->LimitCustomField(
115         CUSTOMFIELD => $cf->id,
116         OPERATOR    => '<',
117         VALUE       => '2010-05-03',
118     );
119
120     is( $tickets->Count, 0, 'did not find the ticket with < 2010-05-03' );
121 }
122
123 {
124
125     my $tickets = RT::Tickets->new(RT->SystemUser);
126     $tickets->LimitCustomField(
127         CUSTOMFIELD => $cf->id,
128         OPERATOR    => '>',
129         VALUE       => '2010-05-05',
130     );
131
132     is( $tickets->Count, 0, 'did not find the ticket with > 2010-05-05' );
133 }
134
135 # relative search by users in different TZs
136 {
137     my $ticket = RT::Ticket->new(RT->SystemUser);
138     my ($tid) = $ticket->Create(
139         Queue                    => $q->id,
140         Subject                  => 'Test',
141         'CustomField-' . $cf->id => '2013-02-12',
142     );
143
144     set_fixed_time("2013-02-10T23:10:00Z");
145     my $tickets = RT::Tickets->new($user_m);
146     $tickets->FromSQL("'CustomField.{$cf_name}' = 'tomorrow' AND id = $tid");
147     is( $tickets->Count, 1, 'found the ticket' );
148
149     set_fixed_time("2013-02-10T15:10:00Z");
150     $tickets = RT::Tickets->new($user_m);
151     $tickets->FromSQL("'CustomField.{$cf_name}' = 'tomorrow' AND id = $tid");
152     is( $tickets->Count, 0, 'found no tickets' );
153
154     set_fixed_time("2013-02-10T23:10:00Z");
155     $tickets = RT::Tickets->new($user_b);
156     $tickets->FromSQL("'CustomField.{$cf_name}' = 'tomorrow' AND id = $tid");
157     is( $tickets->Count, 0, 'found no tickets' );
158
159     set_fixed_time("2013-02-11T23:10:00Z");
160     $tickets = RT::Tickets->new($user_b);
161     $tickets->FromSQL("'CustomField.{$cf_name}' = 'tomorrow' AND id = $tid");
162     is( $tickets->Count, 1, 'found the tickets' );
163 }
164