5 use RT::Test tests => undef;
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' );
13 my $cf_name = 'test cf datetime';
17 if ( ( $ENV{RT_TEST_WEB_HANDLER} || '' ) =~ /^apache(\+mod_perl)?$/
18 && RT::Test::Apache->apache_mpm_type =~ /^(?:worker|event)$/ )
21 'localizing $ENV{TZ} does *not* work with mod_perl+mpm_event or mod_perl+mpm_worker';
27 $m->follow_link( id => 'admin-custom-fields-create');
29 form_name => "ModifyCustomField",
32 TypeComposite => 'DateTime-1',
33 LookupType => 'RT::Queue-RT::Ticket',
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";
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';
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');
53 $m->form_name('EditCustomFields');
54 $m->tick( "AddCustomField" => $cfid );
55 $m->click('UpdateCFs');
57 $m->content_contains('Object created', 'TCF added to the queue' );
60 diag 'check valid inputs with various timezones in ticket create page';
65 form_name => "CreateTicketInQueue",
66 fields => { Queue => 'General' },
68 $m->content_contains('Select datetime', 'has cf field');
71 form_name => "TicketCreate",
73 Subject => 'test 2010-05-04 13:00:01',
75 "Object-RT::Ticket--CustomField-$cfid-Values" => '2010-05-04 13:00:01',
78 ok( ($id) = $m->content =~ /Ticket (\d+) created/,
79 "created ticket $id" );
81 $ticket = RT::Ticket->new( RT->SystemUser );
86 $ticket->CustomFieldValues($cfid)->First->Content,
87 '2010-05-04 17:00:01',
88 'date in db is in UTC'
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');
95 $root->SetTimezone( 'Asia/Shanghai' );
96 # interesting that $m->reload doesn't work
97 $m->get_ok( $m->uri );
101 $m->content_contains( 'Wed May 05 01:00:01 2010',
102 'cf datetime value respects user timezone' );
106 form_name => "CreateTicketInQueue",
107 fields => { Queue => 'General' },
110 form_name => "TicketCreate",
112 Subject => 'test 2010-05-06 07:00:01',
114 "Object-RT::Ticket--CustomField-$cfid-Values" => '2010-05-06 07:00:01',
117 ok( ($id) = $m->content =~ /Ticket (\d+) created/,
118 "created ticket $id" );
119 $ticket = RT::Ticket->new( RT->SystemUser );
124 $ticket->CustomFieldValues($cfid)->First->Content,
125 '2010-05-05 23:00:01',
126 'date in db is in UTC'
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 );
138 $m->content_contains( 'Wed May 05 19:00:01 2010',
139 'cf datetime value respects user timezone' );
144 diag 'check search build page';
146 $m->get_ok( $baseurl . '/Search/Build.html?Query=Queue=1' );
148 $m->form_name('BuildQuery');
150 $m->find_all_inputs( type => 'option', name_regex => qr/test cf datetime/ );
152 [ $cf_op->possible_values ],
158 $m->find_all_inputs( type => 'text', name_regex => qr/test cf datetime/ );
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' );
164 my $shanghai = RT::Test->load_or_create_user(
166 Password => 'password',
167 Timezone => 'Asia/Shanghai',
169 ok( $shanghai->PrincipalObj->GrantRight(
170 Right => 'SuperUser',
171 Object => $RT::System,
173 $m->login( 'shanghai', 'password', logout => 1 );
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 );
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 );
185 { $cf_op->name => '=', $cf_field->name => '2010-05-05 02:00:01', }, 0 );
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 );
192 diag 'check invalid inputs';
195 form_name => "CreateTicketInQueue",
196 fields => { Queue => 'General' },
198 my $form = $m->form_name("TicketCreate");
201 form_name => "TicketCreate",
205 "Object-RT::Ticket--CustomField-$cfid-Values" => 'foodate',
208 $m->content_like(qr/Ticket \d+ created/, "a ticket is created succesfully");
210 $m->content_contains('test cf datetime:', 'has cf datetime field on the page');
211 $m->content_lacks('foodate', 'invalid dates not set');
213 my @warnings = $m->get_warnings;
215 is_deeply( [@warnings], [(q{Couldn't parse date 'foodate' by Time::ParseDate})x2] );
218 diag 'retain values when adding attachments';
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',
228 ok( $ret, "created 'txn datetime': $msg" );
229 $txn_cf->AddToObject(RT::Queue->new(RT->SystemUser));
230 my $txn_cfid = $txn_cf->id;
233 form_name => "CreateTicketInQueue",
234 fields => { Queue => 'General' },
236 $m->content_contains('test cf datetime', 'has cf' );
237 $m->content_contains('test txn cf datetime', 'has txn cf' );
241 form_name => "TicketCreate",
243 Subject => 'test 2015-06-04',
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',
248 button => 'AddMoreAttach',
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' );
261 ok( ($id) = $m->content =~ /Ticket (\d+) created/, "Created ticket $id" );
263 $m->follow_link_ok( {text => 'Reply'} );
264 $m->title_like( qr/Update/ );
265 $m->content_contains('test txn cf date', 'has txn cf');
268 form_name => "TicketUpdate",
271 "Object-RT::Transaction--CustomField-$txn_cfid-Values" => '2015-09-16 09:30:40',
273 button => 'AddMoreAttach',
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' );
282 $m->follow_link_ok( {text => 'Jumbo'} );
283 $m->title_like( qr/Jumbo/ );
287 form_name => "TicketModifyAll",
289 "Object-RT::Transaction--CustomField-$txn_cfid-Values" =>
290 '2015-12-16 03:00:00',
292 button => 'AddMoreAttach',
296 $m->save_content('/tmp/x.html');
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' );
304 sub is_results_number {
305 local $Test::Builder::Level = $Test::Builder::Level + 1;
308 my $operator = shift;
312 $m->get_ok( $baseurl . '/Search/Build.html?Query=Queue=1' );
314 $m->form_name('BuildQuery');
317 button => 'DoSearch',
319 $m->content_contains( "Found $number ticket", "Found $number ticket" );
322 # to make $m->DESTROY happy