starting to work...
[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 => 'tools-config-queues' } );
20     $m->follow_link_ok( { text => $queue->Name } );
21     $m->follow_link_ok( { id => 'page-scrips-create'});
22     $m->form_name('ModifyScrip');
23     $m->field('Scrip-new-Description' => 'test');
24     $m->select('Scrip-new-ScripCondition' => 'On Transaction');
25     $m->select('Scrip-new-ScripAction' => 'User Defined');
26     $m->select('Scrip-new-Template' => 'Global template: Blank');
27     $m->select('Scrip-new-Stage' => 'TransactionBatch');
28     $m->field('Scrip-new-CustomPrepareCode' => 'return 1;');
29     $m->field('Scrip-new-CustomCommitCode' => 'return 1;');
30     $m->submit;
31     $m->content_contains("Scrip Created");
32
33
34     my $form = $m->form_name('ModifyScrip');
35     $sid = $form->value('id');
36     is $m->value("Scrip-$sid-Description"), 'test', 'correct description';
37     is value_name($form, "Scrip-$sid-ScripCondition"), 'On Transaction', 'correct condition';
38     is value_name($form, "Scrip-$sid-ScripAction"), 'User Defined', 'correct action';
39     is value_name($form, "Scrip-$sid-Template"), 'Global template: Blank', 'correct template';
40     is value_name($form, "Scrip-$sid-Stage"), 'TransactionBatch', 'correct stage';
41
42     my $tmp_fn = File::Spec->catfile( RT::Test->temp_directory, 'transactions' );
43     open my $tmp_fh, '+>', $tmp_fn or die $!;
44
45     my $code = <<END;
46 open( my \$fh, '>', '$tmp_fn' ) or die "Couldn't open '$tmp_fn':\$!";
47
48 my \$batch = \$self->TicketObj->TransactionBatch;
49 unless ( \$batch && \@\$batch ) {
50     print \$fh "no batch\n";
51     return 1;
52 }
53 foreach my \$txn ( \@\$batch ) {
54     print \$fh \$txn->Type ."\n";
55 }
56 return 1;
57 END
58
59     $m->field( "Scrip-$sid-CustomCommitCode" => $code );
60     $m->submit;
61
62     $m->goto_create_ticket( $queue );
63     $m->form_name('TicketCreate');
64     $m->submit;
65
66     is_deeply parse_handle($tmp_fh), ['Create'], 'Create';
67
68     $m->follow_link_ok( { text => 'Resolve' } );
69     $m->form_name('TicketUpdate');
70     $m->field( "UpdateContent" => 'resolve it' );
71     $m->click('SubmitTicket');
72
73     is_deeply parse_handle($tmp_fh), ['Comment', 'Status'], 'Comment + Resolve';
74 }
75
76 sub value_name {
77     my $form = shift;
78     my $field = shift;
79
80     my $input = $form->find_input( $field );
81
82     my @names = $input->value_names;
83     my @values = $input->possible_values;
84     for ( my $i = 0; $i < @values; $i++ ) {
85         return $names[ $i ] if $values[ $i ] eq $input->value;
86     }
87     return undef;
88 }
89
90 sub parse_handle {
91     my $fh = shift;
92     seek $fh, 0, 0;
93     my @lines = <$fh>;
94     foreach ( @lines ) { s/^\s+//gms; s/\s+$//gms }
95     truncate $fh, 0;
96     return \@lines;
97 }
98