1 use Test::More tests => '39';
4 use_ok('RT::ScripConditions');
5 use_ok('RT::ScripActions');
6 use_ok('RT::Template');
12 use File::Temp qw/tempfile/;
13 my ($fh, $filename) = tempfile( UNLINK => 1, SUFFIX => '.rt');
14 my $link_scrips_orig = $RT::LinkTransactionsRun1Scrip;
15 $RT::LinkTransactionsRun1Scrip = 1;
17 my $condition = RT::ScripCondition->new( $RT::SystemUser );
18 $condition->Load('User Defined');
20 my $action = RT::ScripAction->new( $RT::SystemUser );
21 $action->Load('User Defined');
23 my $template = RT::Template->new( $RT::SystemUser );
24 $template->Load('Blank');
27 my $q1 = RT::Queue->new($RT::SystemUser);
28 my ($id,$msg) = $q1->Create(Name => "LinkTest1.$$");
30 my $q2 = RT::Queue->new($RT::SystemUser);
31 ($id,$msg) = $q2->Create(Name => "LinkTest2.$$");
34 my $commit_code = <<END;
35 open(FILE, "<$filename");
39 open(FILE, ">$filename");
40 if (\$self->TransactionObj->Type eq 'AddLink') {
41 print FILE \$data+1, "\n";
44 print FILE \$data-1, "\n";
50 my $Scrips = RT::Scrips->new( $RT::SystemUser );
52 while ( my $Scrip = $Scrips->Next ) {
53 $Scrip->Delete if $Scrip->Description =~ /Add or Delete Link \d+/;
57 my $scrip = RT::Scrip->new($RT::SystemUser);
58 ($id,$msg) = $scrip->Create( Description => "Add or Delete Link $$",
59 ScripCondition => $condition->id,
60 ScripAction => $action->id,
61 Template => $template->id,
62 Stage => 'TransactionCreate',
64 CustomIsApplicableCode => '$self->TransactionObj->Type =~ /(Add|Delete)Link/;',
65 CustomPrepareCode => '1;',
66 CustomCommitCode => $commit_code,
68 ok($id, "Scrip created");
70 my $u1 = RT::User->new($RT::SystemUser);
71 ($id,$msg) =$u1->Create(Name => "LinkTestUser.$$");
75 ($id,$msg) = $u1->PrincipalObj->GrantRight ( Object => $q1, Right => 'CreateTicket');
77 ($id,$msg) = $u1->PrincipalObj->GrantRight ( Object => $q1, Right => 'ModifyTicket');
82 my $creator = RT::CurrentUser->new($u1->id);
84 my $ticket = RT::Ticket->new( $creator);
85 ok($ticket->isa('RT::Ticket'));
86 ($id,$tid, $msg) = $ticket->Create(Subject => 'Link test 1', Queue => $q1->id);
90 my $ticket2 = RT::Ticket->new($RT::SystemUser);
91 ($id, $tid, $msg) = $ticket2->Create(Subject => 'Link test 2', Queue => $q2->id);
94 ($id,$msg) =$ticket->AddLink(Type => 'RefersTo', Target => $ticket2->id);
96 ok(link_count($filename) == 0, "scrips ok");
97 ($id,$msg) = $u1->PrincipalObj->GrantRight ( Object => $q2, Right => 'CreateTicket');
99 ($id,$msg) = $u1->PrincipalObj->GrantRight ( Object => $q2, Right => 'ModifyTicket');
101 ($id,$msg) =$ticket->AddLink(Type => 'RefersTo', Target => $ticket2->id);
103 ok(link_count($filename) == 1, "scrips ok");
104 ($id,$msg) =$ticket->AddLink(Type => 'RefersTo', Target => -1);
106 ok(link_count($filename) == 1, "scrips ok");
108 my $transactions = $ticket2->Transactions;
109 $transactions->Limit( FIELD => 'Type', VALUE => 'AddLink' );
110 ok( $transactions->Count == 1, "Transaction found in other ticket" );
111 ok( $transactions->First->Field eq 'ReferredToBy');
112 ok( $transactions->First->NewValue eq $ticket->URI );
114 ($id,$msg) =$ticket->DeleteLink(Type => 'RefersTo', Target => $ticket2->id);
116 ok(link_count($filename) == 0, "scrips ok");
117 $transactions = $ticket2->Transactions;
118 $transactions->Limit( FIELD => 'Type', VALUE => 'DeleteLink' );
119 ok( $transactions->Count == 1, "Transaction found in other ticket" );
120 ok( $transactions->First->Field eq 'ReferredToBy');
121 ok( $transactions->First->OldValue eq $ticket->URI );
123 $RT::LinkTransactionsRun1Scrip = 0;
124 ($id,$msg) =$ticket->AddLink(Type => 'RefersTo', Target => $ticket2->id);
126 ok(link_count($filename) == 2, "scrips ok");
127 ($id,$msg) =$ticket->DeleteLink(Type => 'RefersTo', Target => $ticket2->id);
129 ok(link_count($filename) == 0, "scrips ok");
132 $RT::LinkTransactionsRun1Scrip = $link_scrips_orig;
137 open(FILE, "<$file");