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