default to a session cookie instead of setting an explicit timeout, weird timezone...
[freeside.git] / rt / t / ticket / scrips_batch.t
1
2 use strict;
3 use warnings;
4
5 use RT::Test tests => 19;
6 use_ok('RT');
7 use_ok('RT::Ticket');
8
9 my $queue = RT::Test->load_or_create_queue( Name => 'Regression' );
10 ok $queue && $queue->id, 'loaded or created queue';
11
12 RT->Config->Set( UseTransactionBatch => 1 );
13
14 my ($baseurl, $m) = RT::Test->started_ok;
15 ok $m->login, 'logged in as root';
16
17 my $sid;
18 {
19     $m->follow_link_ok( { id => 'admin-queues' } );
20     $m->follow_link_ok( { text => $queue->Name } );
21     $m->follow_link_ok( { id => 'page-scrips-create'});
22
23     $m->form_name('CreateScrip');
24     $m->field('Description' => 'test');
25     $m->select('ScripCondition' => 'On Transaction');
26     $m->select('ScripAction' => 'User Defined');
27     $m->select('Template' => 'Blank');
28     $m->select('Stage' => 'Batch');
29     $m->field('CustomPrepareCode' => 'return 1;');
30     $m->field('CustomCommitCode' => 'return 1;');
31     $m->click('Create');
32     $m->content_contains("Scrip Created");
33
34     my $form = $m->form_name('ModifyScrip');
35     $sid = $form->value('id');
36     is $m->value("Description"), 'test', 'correct description';
37     is value_name($form, "ScripCondition"), 'On Transaction', 'correct condition';
38     is value_name($form, "ScripAction"), 'User Defined', 'correct action';
39     is value_name($form, "Template"), 'Blank', 'correct template';
40
41     {
42         my $rec = RT::ObjectScrip->new( RT->SystemUser );
43         $rec->LoadByCols( Scrip => $sid, ObjectId => $queue->id );
44         is $rec->Stage, 'TransactionBatch', "correct stage";
45     }
46
47     my $tmp_fn = File::Spec->catfile( RT::Test->temp_directory, 'transactions' );
48     open my $tmp_fh, '+>', $tmp_fn or die $!;
49
50     my $code = <<END;
51 open( my \$fh, '>', '$tmp_fn' ) or die "Couldn't open '$tmp_fn':\$!";
52
53 my \$batch = \$self->TicketObj->TransactionBatch;
54 unless ( \$batch && \@\$batch ) {
55     print \$fh "no batch\n";
56     return 1;
57 }
58 foreach my \$txn ( \@\$batch ) {
59     print \$fh \$txn->Type ."\n";
60 }
61 return 1;
62 END
63
64     $m->field( "CustomCommitCode" => $code );
65     $m->click('Update');
66
67     $m->goto_create_ticket( $queue );
68     $m->form_name('TicketCreate');
69     $m->submit;
70
71     is_deeply parse_handle($tmp_fh), ['Create'], 'Create';
72
73     $m->follow_link_ok( { text => 'Resolve' } );
74     $m->form_name('TicketUpdate');
75     $m->field( "UpdateContent" => 'resolve it' );
76     $m->click('SubmitTicket');
77
78     is_deeply parse_handle($tmp_fh), ['Comment', 'Status'], 'Comment + Resolve';
79 }
80
81 sub value_name {
82     my $form = shift;
83     my $field = shift;
84
85     my $input = $form->find_input( $field );
86
87     my @names = $input->value_names;
88     my @values = $input->possible_values;
89     for ( my $i = 0; $i < @values; $i++ ) {
90         return $names[ $i ] if $values[ $i ] eq $input->value;
91     }
92     return undef;
93 }
94
95 sub parse_handle {
96     my $fh = shift;
97     seek $fh, 0, 0;
98     my @lines = <$fh>;
99     foreach ( @lines ) { s/^\s+//gms; s/\s+$//gms }
100     truncate $fh, 0;
101     return \@lines;
102 }
103