first pass RT4 merge, RT#13852
[freeside.git] / rt / t / customfields / datetime_search.t
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 use RT::Test nodata => 1, tests => 14;
7 RT->Config->Set( 'Timezone' => 'EST5EDT' ); # -04:00
8
9 my $q = RT::Queue->new(RT->SystemUser);
10 ok( $q->Create( Name => 'DateTimeCFTest' . $$ ), 'create queue' );
11
12 my $cf = RT::CustomField->new(RT->SystemUser);
13 ok(
14     $cf->Create(
15         Name       => 'datetime-' . $$,
16         Type       => 'DateTime',
17         MaxValues  => 1,
18         LookupType => RT::Ticket->CustomFieldLookupType,
19     ),
20     'create cf datetime'
21 );
22 ok( $cf->AddToObject($q), 'date cf apply to queue' );
23
24 my $ticket = RT::Ticket->new(RT->SystemUser);
25
26 ok(
27     $ticket->Create(
28         Queue                    => $q->id,
29         Subject                  => 'Test',
30         'CustomField-' . $cf->id => '2010-05-04 07:00:00',
31     ),
32     'create ticket with cf set to 2010-05-04 07:00:00( 2010-05-04 11:00:00 with UTC )'
33 );
34
35 is(
36     $ticket->CustomFieldValues->First->Content,
37     '2010-05-04 11:00:00',
38     'date in db is in timezone UTC'
39 );
40
41 {
42
43     my $tickets = RT::Tickets->new(RT->SystemUser);
44     $tickets->LimitCustomField(
45         CUSTOMFIELD => $cf->id,
46         OPERATOR    => '=',
47         VALUE       => '2010-05-04 07:00:00',    # this timezone is server
48     );
49
50     is( $tickets->Count, 1, 'found the ticket with exact date: 2010-05-04 07:00:00' );
51 }
52
53 {
54
55     # TODO according to the code, if OPERATOR is '=', it means on that day
56     # this will test this behavior
57     my $tickets = RT::Tickets->new(RT->SystemUser);
58     $tickets->LimitCustomField(
59         CUSTOMFIELD => $cf->id,
60         OPERATOR    => '=',
61         VALUE       => '2010-05-04',
62     );
63
64     is( $tickets->Count, 1, 'found the ticket with rough date: 2010-05-04' );
65 }
66
67 {
68
69     # TODO according to the code, if OPERATOR is '=', it means on that day
70     # this will test this behavior
71     my $tickets = RT::Tickets->new(RT->SystemUser);
72     $tickets->LimitCustomField(
73         CUSTOMFIELD => $cf->id,
74         OPERATOR    => '=',
75         VALUE       => '2010-05-05',
76     );
77
78     is( $tickets->Count, 0, 'did not find the ticket with wrong datetime: 2010-05-05' );
79 }
80
81 my $tickets = RT::Tickets->new( RT->SystemUser );
82 $tickets->UnLimit;
83 while( my $ticket  = $tickets->Next ) {
84     $ticket->Delete();
85 }
86
87 {
88     ok(
89         $ticket->Create(
90             Queue                    => $q->id,
91             Subject                  => 'Test',
92             'CustomField-' . $cf->id => '2010-06-21 17:00:01',
93         ),
94 'create ticket with cf set to 2010-06-21 17:00:01( 2010-06-21 21:00:01 with UTC )'
95     );
96
97     my $shanghai = RT::Test->load_or_create_user(
98         Name     => 'shanghai',
99         Timezone => 'Asia/Shanghai',
100     );
101
102     ok(
103         $shanghai->PrincipalObj->GrantRight(
104             Right  => 'SuperUser',
105             Object => $RT::System,
106         )
107     );
108
109     my $current_user = RT::CurrentUser->new($shanghai);
110     my $tickets      = RT::Tickets->new($current_user);
111     $tickets->LimitCustomField(
112         CUSTOMFIELD => $cf->id,
113         OPERATOR    => '=',
114         VALUE       => '2010-06-22',
115     );
116     is( $tickets->Count, 1, 'found the ticket with rough datetime: 2010-06-22' );
117
118     $tickets->UnLimit;
119     $tickets->LimitCustomField(
120         CUSTOMFIELD => $cf->id,
121         OPERATOR    => '>',
122         VALUE       => '2010-06-21',
123     );
124     is( $tickets->Count, 1, 'found the ticket with > 2010-06-21' );
125
126     $tickets->UnLimit;
127     $tickets->LimitCustomField(
128         CUSTOMFIELD => $cf->id,
129         OPERATOR    => '<',
130         VALUE       => '2010-06-23',
131     );
132     is( $tickets->Count, 1, 'found the ticket with < 2010-06-23' );
133
134     $tickets->UnLimit;
135     $tickets->LimitCustomField(
136         CUSTOMFIELD => $cf->id,
137         OPERATOR    => '=',
138         VALUE       => '2010-06-22 05:00:01',
139     );
140     is( $tickets->Count, 1, 'found the ticket with = 2010-06-22 01:00:01' );
141 }