default to a session cookie instead of setting an explicit timeout, weird timezone...
[freeside.git] / rt / t / web / cf_datetime.t
1
2 use strict;
3 use warnings;
4
5 use RT::Test tests => undef;
6
7 RT->Config->Set( 'Timezone' => 'EST5EDT' ); # -04:00
8 my ($baseurl, $m) = RT::Test->started_ok;
9 ok $m->login, 'logged in as root';
10 my $root = RT::User->new( RT->SystemUser );
11 ok( $root->Load('root'), 'load root user' );
12
13 my $cf_name = 'test cf datetime';
14
15 my $why;
16
17 if ( ( $ENV{RT_TEST_WEB_HANDLER} || '' ) =~ /^apache(\+mod_perl)?$/
18     && RT::Test::Apache->apache_mpm_type =~ /^(?:worker|event)$/ )
19 {
20     $why =
21 'localizing $ENV{TZ} does *not* work with mod_perl+mpm_event or mod_perl+mpm_worker';
22 }
23
24 my $cfid;
25 diag "Create a CF";
26 {
27     $m->follow_link( id => 'admin-custom-fields-create');
28     $m->submit_form(
29         form_name => "ModifyCustomField",
30         fields => {
31             Name          => $cf_name,
32             TypeComposite => 'DateTime-1',
33             LookupType    => 'RT::Queue-RT::Ticket',
34         },
35     );
36     $m->content_contains('Object created', 'created CF sucessfully' );
37     $cfid = $m->form_name('ModifyCustomField')->value('id');
38     ok $cfid, "found id of the CF in the form, it's #$cfid";
39 }
40
41 diag "apply the CF to General queue";
42 my $queue = RT::Test->load_or_create_queue( Name => 'General' );
43 ok $queue && $queue->id, 'loaded or created queue';
44
45 {
46     $m->follow_link( text => 'Queues' );
47     $m->title_is(q/Admin queues/, 'admin-queues screen');
48     $m->follow_link( text => 'General' );
49     $m->title_is(q/Configuration for queue General/, 'admin-queue: general');
50     $m->follow_link( id => 'page-custom-fields-tickets' );
51     $m->title_is(q/Custom Fields for queue General/, 'admin-queue: general cfid');
52
53     $m->form_name('EditCustomFields');
54     $m->tick( "AddCustomField" => $cfid );
55     $m->click('UpdateCFs');
56
57     $m->content_contains('Object created', 'TCF added to the queue' );
58 }
59
60 diag 'check valid inputs with various timezones in ticket create page';
61 {
62     my ( $ticket, $id );
63
64     $m->submit_form(
65         form_name => "CreateTicketInQueue",
66         fields => { Queue => 'General' },
67     );
68     $m->content_contains('Select datetime', 'has cf field');
69
70     $m->submit_form(
71         form_name => "TicketCreate",
72         fields    => {
73             Subject                                       => 'test 2010-05-04 13:00:01',
74             Content                                       => 'test',
75             "Object-RT::Ticket--CustomField-$cfid-Values" => '2010-05-04 13:00:01',
76         },
77     );
78     ok( ($id) = $m->content =~ /Ticket (\d+) created/,
79         "created ticket $id" );
80
81     $ticket = RT::Ticket->new( RT->SystemUser );
82     $ticket->Load($id);
83     TODO: {
84         local $TODO = $why;
85         is(
86             $ticket->CustomFieldValues($cfid)->First->Content,
87             '2010-05-04 17:00:01',
88             'date in db is in UTC'
89         );
90     }
91
92     $m->content_contains('test cf datetime:', 'has cf datetime field on the page');
93     $m->content_contains('Tue May 04 13:00:01 2010', 'has cf datetime value on the page');
94
95     $root->SetTimezone( 'Asia/Shanghai' );
96     # interesting that $m->reload doesn't work
97     $m->get_ok( $m->uri );
98
99     TODO: {
100         local $TODO = $why;
101         $m->content_contains( 'Wed May 05 01:00:01 2010',
102             'cf datetime value respects user timezone' );
103     }
104
105     $m->submit_form(
106         form_name => "CreateTicketInQueue",
107         fields => { Queue => 'General' },
108     );
109     $m->submit_form(
110         form_name => "TicketCreate",
111         fields    => {
112             Subject                                       => 'test 2010-05-06 07:00:01',
113             Content                                       => 'test',
114             "Object-RT::Ticket--CustomField-$cfid-Values" => '2010-05-06 07:00:01',
115         },
116     );
117     ok( ($id) = $m->content =~ /Ticket (\d+) created/,
118         "created ticket $id" );
119     $ticket = RT::Ticket->new( RT->SystemUser );
120     $ticket->Load($id);
121     TODO: {
122         local $TODO = $why;
123         is(
124             $ticket->CustomFieldValues($cfid)->First->Content,
125             '2010-05-05 23:00:01',
126             'date in db is in UTC'
127         );
128     }
129
130     $m->content_contains('test cf datetime:', 'has cf datetime field on the page');
131     $m->content_contains( 'Thu May 06 07:00:01 2010',
132         'cf datetime input respects user timezone' );
133     $root->SetTimezone( 'EST5EDT' ); # back to -04:00
134     $m->get_ok( $m->uri );
135
136     TODO: {
137         local $TODO = $why;
138         $m->content_contains( 'Wed May 05 19:00:01 2010',
139             'cf datetime value respects user timezone' );
140     }
141 }
142
143
144 diag 'check search build page';
145 {
146     $m->get_ok( $baseurl . '/Search/Build.html?Query=Queue=1' );
147
148     $m->form_name('BuildQuery');
149     my ($cf_op) =
150       $m->find_all_inputs( type => 'option', name_regex => qr/test cf datetime/ );
151     is_deeply(
152         [ $cf_op->possible_values ],
153         [ '<', '=', '>' ],
154         'right oprators'
155     );
156
157     my ($cf_field) =
158       $m->find_all_inputs( type => 'text', name_regex => qr/test cf datetime/ );
159
160     is_results_number( { $cf_op->name => '=', $cf_field->name => '2010-05-04', }, 1 );
161     $m->content_contains( '2010-05-04',     'got the right ticket' );
162     $m->content_lacks( '2010-05-06', 'did not get the wrong ticket' );
163
164     my $shanghai = RT::Test->load_or_create_user(
165         Name     => 'shanghai',
166         Password => 'password',
167         Timezone => 'Asia/Shanghai',
168     );
169     ok( $shanghai->PrincipalObj->GrantRight(
170         Right  => 'SuperUser',
171         Object => $RT::System,
172     ));
173     $m->login( 'shanghai', 'password', logout => 1 );
174
175     is_results_number( { $cf_op->name => '<', $cf_field->name => '2010-05-07', }, 2 );
176     is_results_number( { $cf_op->name => '>', $cf_field->name => '2010-05-04', }, 2 );
177
178     TODO: {
179         local $TODO = $why;
180         is_results_number( { $cf_op->name => '=', $cf_field->name => '2010-05-05', }, 1 );
181         is_results_number( { $cf_op->name => '=', $cf_field->name => '2010-05-05 01:00:01', }, 1 );
182     }
183
184     is_results_number(
185         { $cf_op->name => '=', $cf_field->name => '2010-05-05 02:00:01', }, 0 );
186
187     is_results_number( { $cf_op->name => '=', $cf_field->name => '2010-05-06', }, 1 );
188     is_results_number( { $cf_op->name => '=', $cf_field->name => '2010-05-06 07:00:01', }, 1 );
189     is_results_number( { $cf_op->name => '=', $cf_field->name => '2010-05-06 08:00:01', }, 0 );
190 }
191
192 diag 'check invalid inputs';
193 {
194     $m->submit_form(
195         form_name => "CreateTicketInQueue",
196         fields => { Queue => 'General' },
197     );
198     my $form = $m->form_name("TicketCreate");
199
200     $m->submit_form(
201         form_name => "TicketCreate",
202         fields    => {
203             Subject                                       => 'test',
204             Content                                       => 'test',
205             "Object-RT::Ticket--CustomField-$cfid-Values" => 'foodate',
206         },
207     );
208     $m->content_like(qr/Ticket \d+ created/, "a ticket is created succesfully");
209
210     $m->content_contains('test cf datetime:', 'has cf datetime field on the page');
211     $m->content_lacks('foodate', 'invalid dates not set');
212
213     my @warnings = $m->get_warnings;
214     chomp @warnings;
215     is_deeply( [@warnings], [(q{Couldn't parse date 'foodate' by Time::ParseDate})x2] );
216 }
217
218 diag 'retain values when adding attachments';
219 {
220     my ( $ticket, $id );
221
222     my $txn_cf = RT::CustomField->new( RT->SystemUser );
223     my ( $ret, $msg ) = $txn_cf->Create(
224         Name          => 'test txn cf datetime',
225         TypeComposite => 'DateTime-1',
226         LookupType    => 'RT::Queue-RT::Ticket-RT::Transaction',
227     );
228     ok( $ret, "created 'txn datetime': $msg" );
229     $txn_cf->AddToObject(RT::Queue->new(RT->SystemUser));
230     my $txn_cfid = $txn_cf->id;
231
232     $m->submit_form(
233         form_name => "CreateTicketInQueue",
234         fields    => { Queue => 'General' },
235     );
236     $m->content_contains('test cf datetime', 'has cf' );
237     $m->content_contains('test txn cf datetime', 'has txn cf' );
238
239     $m->submit_form_ok(
240         {
241             form_name => "TicketCreate",
242             fields    => {
243                 Subject => 'test 2015-06-04',
244                 Content => 'test',
245                 "Object-RT::Ticket--CustomField-$cfid-Values" => '2015-06-04 08:30:00',
246                 "Object-RT::Transaction--CustomField-$txn_cfid-Values" => '2015-08-15 12:30:30',
247             },
248             button => 'AddMoreAttach',
249         },
250         'Create test ticket'
251     );
252     $m->form_name("TicketCreate");
253     is( $m->value( "Object-RT::Ticket--CustomField-$cfid-Values" ),
254         "2015-06-04 08:30:00", "ticket cf date value still on form" );
255     $m->content_contains( "Jun 04 08:30:00 2015", 'date in parens' );
256     is( $m->value( "Object-RT::Transaction--CustomField-$txn_cfid-Values" ),
257         "2015-08-15 12:30:30", "txn cf date date value still on form" );
258     $m->content_contains( "Aug 15 12:30:30 2015", 'date in parens' );
259
260     $m->submit_form();
261     ok( ($id) = $m->content =~ /Ticket (\d+) created/, "Created ticket $id" );
262
263     $m->follow_link_ok( {text => 'Reply'} );
264     $m->title_like( qr/Update/ );
265     $m->content_contains('test txn cf date', 'has txn cf');
266     $m->submit_form_ok(
267         {
268             form_name => "TicketUpdate",
269             fields    => {
270                 Content => 'test',
271                 "Object-RT::Transaction--CustomField-$txn_cfid-Values" => '2015-09-16 09:30:40',
272             },
273             button => 'AddMoreAttach',
274         },
275         'Update test ticket'
276     );
277     $m->form_name("TicketUpdate");
278     is( $m->value( "Object-RT::Transaction--CustomField-$txn_cfid-Values" ),
279         "2015-09-16 09:30:40", "Date value still on form" );
280     $m->content_contains( "Sep 16 09:30:40 2015", 'date in parens' );
281
282     $m->follow_link_ok( {text => 'Jumbo'} );
283     $m->title_like( qr/Jumbo/ );
284
285     $m->submit_form_ok(
286         {
287             form_name => "TicketModifyAll",
288             fields    => {
289                 "Object-RT::Transaction--CustomField-$txn_cfid-Values" =>
290                   '2015-12-16 03:00:00',
291             },
292             button => 'AddMoreAttach',
293         },
294         'jumbo form'
295     );
296     $m->save_content('/tmp/x.html');
297
298     $m->form_name("TicketModifyAll");
299     is( $m->value( "Object-RT::Transaction--CustomField-$txn_cfid-Values" ),
300         "2015-12-16 03:00:00", "txn date value still on form" );
301     $m->content_contains( "Dec 16 03:00:00 2015", 'date in parens' );
302 }
303
304 sub is_results_number {
305     local $Test::Builder::Level = $Test::Builder::Level + 1;
306     my $fields = shift;
307     my $number = shift;
308     my $operator = shift;
309     my $value = shift;
310     {
311         local $TODO;
312         $m->get_ok( $baseurl . '/Search/Build.html?Query=Queue=1' );
313     }
314     $m->form_name('BuildQuery');
315     $m->submit_form(
316         fields => $fields,
317         button => 'DoSearch',
318     );
319     $m->content_contains( "Found $number ticket", "Found $number ticket" );
320 }
321
322 # to make $m->DESTROY happy
323 undef $m;
324
325 done_testing;