5 use RT::Test tests => 102;
9 use_ok('RT::ScripConditions');
10 use_ok('RT::ScripActions');
11 use_ok('RT::Template');
16 my $filename = File::Spec->catfile( RT::Test->temp_directory, 'link_count' );
17 open my $fh, '>', $filename or die $!;
20 my $link_scrips_orig = RT->Config->Get( 'LinkTransactionsRun1Scrip' );
21 RT->Config->Set( 'LinkTransactionsRun1Scrip', 1 );
23 my $link_acl_checks_orig = RT->Config->Get( 'StrictLinkACL' );
24 RT->Config->Set( 'StrictLinkACL', 1);
26 my $condition = RT::ScripCondition->new( RT->SystemUser );
27 $condition->Load('User Defined');
29 my $action = RT::ScripAction->new( RT->SystemUser );
30 $action->Load('User Defined');
32 my $template = RT::Template->new( RT->SystemUser );
33 $template->Load('Blank');
36 my $q1 = RT::Queue->new(RT->SystemUser);
37 my ($id,$msg) = $q1->Create(Name => "LinkTest1.$$");
39 my $q2 = RT::Queue->new(RT->SystemUser);
40 ($id,$msg) = $q2->Create(Name => "LinkTest2.$$");
43 my $commit_code = <<END;
44 open( my \$file, '<', "$filename" ) or die "couldn't open $filename";
48 \$RT::Logger->debug("Data is \$data");
50 open( \$file, '>', "$filename" ) or die "couldn't open $filename";
51 if (\$self->TransactionObj->Type eq 'AddLink') {
52 \$RT::Logger->debug("AddLink");
53 print \$file \$data+1, "\n";
55 elsif (\$self->TransactionObj->Type eq 'DeleteLink') {
56 \$RT::Logger->debug("DeleteLink");
57 print \$file \$data-1, "\n";
60 \$RT::Logger->error("THIS SHOULDN'T HAPPEN");
67 my $Scrips = RT::Scrips->new( RT->SystemUser );
69 while ( my $Scrip = $Scrips->Next ) {
70 $Scrip->Delete if $Scrip->Description and $Scrip->Description =~ /Add or Delete Link \d+/;
74 my $scrip = RT::Scrip->new(RT->SystemUser);
75 ($id,$msg) = $scrip->Create( Description => "Add or Delete Link $$",
76 ScripCondition => $condition->id,
77 ScripAction => $action->id,
78 Template => $template->id,
79 Stage => 'TransactionCreate',
81 CustomIsApplicableCode => '$self->TransactionObj->Type =~ /(Add|Delete)Link/;',
82 CustomPrepareCode => '1;',
83 CustomCommitCode => $commit_code,
85 ok($id, "Scrip created");
87 my $u1 = RT::User->new(RT->SystemUser);
88 ($id,$msg) = $u1->Create(Name => "LinkTestUser.$$");
91 # grant ShowTicket right to allow count transactions
92 ($id,$msg) = $u1->PrincipalObj->GrantRight ( Object => $q1, Right => 'ShowTicket');
94 ($id,$msg) = $u1->PrincipalObj->GrantRight ( Object => $q2, Right => 'ShowTicket');
96 ($id,$msg) = $u1->PrincipalObj->GrantRight ( Object => $q1, Right => 'CreateTicket');
99 my $creator = RT::CurrentUser->new($u1->id);
101 diag('Create tickets without rights to link');
103 # on q2 we have no rights, yet
104 my $parent = RT::Ticket->new( RT->SystemUser );
105 my ($id,$tid,$msg) = $parent->Create( Subject => 'Link test 1', Queue => $q2->id );
107 my $child = RT::Ticket->new( $creator );
108 ($id,$tid,$msg) = $child->Create( Subject => 'Link test 1', Queue => $q1->id, MemberOf => $parent->id );
110 $child->CurrentUser( RT->SystemUser );
111 is($child->_Links('Base')->Count, 0, 'link was not created, no permissions');
112 is($child->_Links('Target')->Count, 0, 'link was not create, no permissions');
115 diag('Create tickets with rights checks on one end of a link');
117 # on q2 we have no rights, but use checking one only on thing
118 RT->Config->Set( StrictLinkACL => 0 );
119 my $parent = RT::Ticket->new( RT->SystemUser );
120 my ($id,$tid,$msg) = $parent->Create( Subject => 'Link test 1', Queue => $q2->id );
122 my $child = RT::Ticket->new( $creator );
123 ($id,$tid,$msg) = $child->Create( Subject => 'Link test 1', Queue => $q1->id, MemberOf => $parent->id );
125 $child->CurrentUser( RT->SystemUser );
126 is($child->_Links('Base')->Count, 1, 'link was created');
127 is($child->_Links('Target')->Count, 0, 'link was created only one');
128 # no scrip run on second ticket accroding to config option
129 is(link_count($filename), undef, "scrips ok");
130 RT->Config->Set( StrictLinkACL => 1 );
133 ($id,$msg) = $u1->PrincipalObj->GrantRight ( Object => $q1, Right => 'ModifyTicket');
136 diag('try to add link without rights');
138 # on q2 we have no rights, yet
139 my $parent = RT::Ticket->new( RT->SystemUser );
140 my ($id,$tid,$msg) = $parent->Create( Subject => 'Link test 1', Queue => $q2->id );
142 my $child = RT::Ticket->new( $creator );
143 ($id,$tid,$msg) = $child->Create( Subject => 'Link test 1', Queue => $q1->id );
145 ($id, $msg) = $child->AddLink(Type => 'MemberOf', Target => $parent->id);
147 is(link_count($filename), undef, "scrips ok");
148 $child->CurrentUser( RT->SystemUser );
149 is($child->_Links('Base')->Count, 0, 'link was not created, no permissions');
150 is($child->_Links('Target')->Count, 0, 'link was not create, no permissions');
153 diag('add link with rights only on base');
155 # on q2 we have no rights, but use checking one only on thing
156 RT->Config->Set( StrictLinkACL => 0 );
157 my $parent = RT::Ticket->new( RT->SystemUser );
158 my ($id,$tid,$msg) = $parent->Create( Subject => 'Link test 1', Queue => $q2->id );
160 my $child = RT::Ticket->new( $creator );
161 ($id,$tid,$msg) = $child->Create( Subject => 'Link test 1', Queue => $q1->id );
163 ($id, $msg) = $child->AddLink(Type => 'MemberOf', Target => $parent->id);
165 is(link_count($filename), 1, "scrips ok");
166 $child->CurrentUser( RT->SystemUser );
167 is($child->_Links('Base')->Count, 1, 'link was created');
168 is($child->_Links('Target')->Count, 0, 'link was created only one');
169 $child->CurrentUser( $creator );
171 # turn off feature and try to delete link, we should fail
172 RT->Config->Set( StrictLinkACL => 1 );
173 ($id, $msg) = $child->AddLink(Type => 'MemberOf', Target => $parent->id);
175 is(link_count($filename), 1, "scrips ok");
176 $child->CurrentUser( RT->SystemUser );
177 $child->_Links('Base')->_DoCount;
178 is($child->_Links('Base')->Count, 1, 'link was not deleted');
179 $child->CurrentUser( $creator );
181 # try to delete link, we should success as feature is active
182 RT->Config->Set( StrictLinkACL => 0 );
183 ($id, $msg) = $child->DeleteLink(Type => 'MemberOf', Target => $parent->id);
185 is(link_count($filename), 0, "scrips ok");
186 $child->CurrentUser( RT->SystemUser );
187 $child->_Links('Base')->_DoCount;
188 is($child->_Links('Base')->Count, 0, 'link was deleted');
189 RT->Config->Set( StrictLinkACL => 1 );
193 my $ticket = RT::Ticket->new( $creator);
194 ok($ticket->isa('RT::Ticket'));
195 ($id,$tid, $msg) = $ticket->Create(Subject => 'Link test 1', Queue => $q1->id);
198 diag('try link to itself');
201 ($id, $msg) = $ticket->AddLink(Type => 'RefersTo', Target => $ticket->id);
202 } qr/Can't link a ticket to itself/;
204 is(link_count($filename), 0, "scrips ok");
207 my $ticket2 = RT::Ticket->new(RT->SystemUser);
208 ($id, $tid, $msg) = $ticket2->Create(Subject => 'Link test 2', Queue => $q2->id);
210 ($id,$msg) =$ticket->AddLink(Type => 'RefersTo', Target => $ticket2->id);
212 is(link_count($filename), 0, "scrips ok");
214 ($id,$msg) = $u1->PrincipalObj->GrantRight ( Object => $q2, Right => 'CreateTicket');
216 ($id,$msg) = $u1->PrincipalObj->GrantRight ( Object => $q2, Right => 'ModifyTicket');
218 ($id,$msg) = $ticket->AddLink(Type => 'RefersTo', Target => $ticket2->id);
220 is(link_count($filename), 1, "scrips ok");
223 ($id,$msg) = $ticket->AddLink(Type => 'RefersTo', Target => -1);
225 qr/Could not determine a URI scheme for -1/,
226 qr/Couldn't resolve '-1' into a URI/,
229 ($id,$msg) = $ticket->AddLink(Type => 'RefersTo', Target => $ticket2->id);
231 is(link_count($filename), 1, "scrips ok");
233 my $transactions = $ticket2->Transactions;
234 $transactions->Limit( FIELD => 'Type', VALUE => 'AddLink' );
235 is( $transactions->Count, 1, "Transaction found in other ticket" );
236 is( $transactions->First->Field , 'ReferredToBy');
237 is( $transactions->First->NewValue , $ticket->URI );
239 ($id,$msg) = $ticket->DeleteLink(Type => 'RefersTo', Target => $ticket2->id);
241 is(link_count($filename), 0, "scrips ok");
242 $transactions = $ticket2->Transactions;
243 $transactions->Limit( FIELD => 'Type', VALUE => 'DeleteLink' );
244 is( $transactions->Count, 1, "Transaction found in other ticket" );
245 is( $transactions->First->Field , 'ReferredToBy');
246 is( $transactions->First->OldValue , $ticket->URI );
248 RT->Config->Set( LinkTransactionsRun1Scrip => 0 );
250 ($id,$msg) =$ticket->AddLink(Type => 'RefersTo', Target => $ticket2->id);
252 is(link_count($filename), 2, "scrips ok");
253 ($id,$msg) =$ticket->DeleteLink(Type => 'RefersTo', Target => $ticket2->id);
255 is(link_count($filename), 0, "scrips ok");
257 # tests for silent behaviour
258 ($id,$msg) = $ticket->AddLink(Type => 'RefersTo', Target => $ticket2->id, Silent => 1);
260 is(link_count($filename), 0, "scrips ok");
262 my $transactions = $ticket->Transactions;
263 $transactions->Limit( FIELD => 'Type', VALUE => 'AddLink' );
264 is( $transactions->Count, 2, "Still two txns on the base" );
266 $transactions = $ticket2->Transactions;
267 $transactions->Limit( FIELD => 'Type', VALUE => 'AddLink' );
268 is( $transactions->Count, 2, "Still two txns on the target" );
271 ($id,$msg) =$ticket->DeleteLink(Type => 'RefersTo', Target => $ticket2->id, Silent => 1);
273 is(link_count($filename), 0, "scrips ok");
275 ($id,$msg) = $ticket->AddLink(Type => 'RefersTo', Target => $ticket2->id, SilentBase => 1);
277 is(link_count($filename), 1, "scrips ok");
279 my $transactions = $ticket->Transactions;
280 $transactions->Limit( FIELD => 'Type', VALUE => 'AddLink' );
281 is( $transactions->Count, 2, "still five txn on the base" );
283 $transactions = $ticket2->Transactions;
284 $transactions->Limit( FIELD => 'Type', VALUE => 'AddLink' );
285 is( $transactions->Count, 3, "+1 txn on the target" );
288 ($id,$msg) =$ticket->DeleteLink(Type => 'RefersTo', Target => $ticket2->id, SilentBase => 1);
290 is(link_count($filename), 0, "scrips ok");
292 ($id,$msg) = $ticket->AddLink(Type => 'RefersTo', Target => $ticket2->id, SilentTarget => 1);
294 is(link_count($filename), 1, "scrips ok");
296 my $transactions = $ticket->Transactions;
297 $transactions->Limit( FIELD => 'Type', VALUE => 'AddLink' );
298 is( $transactions->Count, 3, "+1 txn on the base" );
300 $transactions = $ticket2->Transactions;
301 $transactions->Limit( FIELD => 'Type', VALUE => 'AddLink' );
302 is( $transactions->Count, 3, "three txns on the target" );
304 ($id,$msg) =$ticket->DeleteLink(Type => 'RefersTo', Target => $ticket2->id, SilentTarget => 1);
306 is(link_count($filename), 0, "scrips ok");
310 RT->Config->Set( LinkTransactionsRun1Scrip => $link_scrips_orig );
311 RT->Config->Set( StrictLinkACL => $link_acl_checks_orig );
314 my $Scrips = RT::Scrips->new( RT->SystemUser );
315 $Scrips->Limit( FIELD => 'Description', OPERATOR => 'STARTSWITH', VALUE => 'Add or Delete Link ');
316 while ( my $s = $Scrips->Next ) { $s->Delete };
320 my $link = RT::Link->new( RT->SystemUser );
321 ($id,$msg) = $link->Create( Base => $ticket->URI, Target => $ticket2->URI, Type => 'MyLinkType' );
323 ok($link->LocalBase == $ticket->id, "LocalBase set correctly");
324 ok($link->LocalTarget == $ticket2->id, "LocalTarget set correctly");
328 *RT::NotTicket::Id = sub { return $$ };
329 *RT::NotTicket::id = \&RT::NotTicket::Id;
333 package RT::URI::not_ticket;
336 @ISA = qw/RT::URI::base/;
338 sub Object { return bless {}, 'RT::NotTicket'; }
341 my $orig_getresolver = \&RT::URI::_GetResolver;
343 no warnings 'redefine';
344 *RT::URI::_GetResolver = sub {
348 $scheme =~ s/(\.|-)/_/g;
350 my $module = "RT::URI::$scheme";
351 $resolver = $module->new($self->CurrentUser);
354 $self->{'resolver'} = $resolver;
356 $self->{'resolver'} = RT::URI::base->new($self->CurrentUser);
361 ($id,$msg) = $link->Create( Base => "not_ticket::$RT::Organization/notticket/$$", Target => $ticket2->URI, Type => 'MyLinkType' );
363 ok($link->LocalBase == 0, "LocalBase set correctly");
364 ok($link->LocalTarget == $ticket2->id, "LocalTarget set correctly");
366 ($id,$msg) = $link->Create( Target => "not_ticket::$RT::Organization/notticket/$$", Base => $ticket->URI, Type => 'MyLinkType' );
368 ok($link->LocalTarget == 0, "LocalTarget set correctly");
369 ok($link->LocalBase == $ticket->id, "LocalBase set correctly");
371 ($id,$msg) = $link->Create(
372 Target => "not_ticket::$RT::Organization/notticket/1$$",
373 Base => "not_ticket::$RT::Organization/notticket/$$",
374 Type => 'MyLinkType' );
377 ok($link->LocalTarget == 0, "LocalTarget set correctly");
378 ok($link->LocalBase == 0, "LocalBase set correctly");
380 # restore _GetResolver
382 no warnings 'redefine';
383 *RT::URI::_GetResolver = $orig_getresolver;
388 open( my $fh, '<', $file ) or die "couldn't open $file";
392 return undef unless $data;