starting to work...
[freeside.git] / rt / t / ticket / linking.t
index fc4761d..1bd83d6 100644 (file)
@@ -2,7 +2,8 @@
 use strict;
 use warnings;
 
-use RT::Test tests => '101';
+use RT::Test tests => 102;
+use Test::Warn;
 use_ok('RT');
 use_ok('RT::Ticket');
 use_ok('RT::ScripConditions');
@@ -12,35 +13,36 @@ use_ok('RT::Scrips');
 use_ok('RT::Scrip');
 
 
-use File::Temp qw/tempfile/;
-my ($fh, $filename) = tempfile( UNLINK => 1, SUFFIX => '.rt');
+my $filename = File::Spec->catfile( RT::Test->temp_directory, 'link_count' );
+open my $fh, '>', $filename or die $!;
+close $fh;
+
 my $link_scrips_orig = RT->Config->Get( 'LinkTransactionsRun1Scrip' );
 RT->Config->Set( 'LinkTransactionsRun1Scrip', 1 );
 
 my $link_acl_checks_orig = RT->Config->Get( 'StrictLinkACL' );
 RT->Config->Set( 'StrictLinkACL', 1);
 
-my $condition = RT::ScripCondition->new( $RT::SystemUser );
+my $condition = RT::ScripCondition->new( RT->SystemUser );
 $condition->Load('User Defined');
 ok($condition->id);
-my $action = RT::ScripAction->new( $RT::SystemUser );
+my $action = RT::ScripAction->new( RT->SystemUser );
 $action->Load('User Defined');
 ok($action->id);
-my $template = RT::Template->new( $RT::SystemUser );
+my $template = RT::Template->new( RT->SystemUser );
 $template->Load('Blank');
 ok($template->id);
 
-my $q1 = RT::Queue->new($RT::SystemUser);
+my $q1 = RT::Queue->new(RT->SystemUser);
 my ($id,$msg) = $q1->Create(Name => "LinkTest1.$$");
 ok ($id,$msg);
-my $q2 = RT::Queue->new($RT::SystemUser);
+my $q2 = RT::Queue->new(RT->SystemUser);
 ($id,$msg) = $q2->Create(Name => "LinkTest2.$$");
 ok ($id,$msg);
 
 my $commit_code = <<END;
 open( my \$file, '<', "$filename" ) or die "couldn't open $filename";
 my \$data = <\$file>;
-chomp \$data;
 \$data += 0;
 close \$file;
 \$RT::Logger->debug("Data is \$data");
@@ -62,14 +64,14 @@ close \$file;
 1;
 END
 
-my $Scrips = RT::Scrips->new( $RT::SystemUser );
+my $Scrips = RT::Scrips->new( RT->SystemUser );
 $Scrips->UnLimit;
 while ( my $Scrip = $Scrips->Next ) {
     $Scrip->Delete if $Scrip->Description and $Scrip->Description =~ /Add or Delete Link \d+/;
 }
 
 
-my $scrip = RT::Scrip->new($RT::SystemUser);
+my $scrip = RT::Scrip->new(RT->SystemUser);
 ($id,$msg) = $scrip->Create( Description => "Add or Delete Link $$",
                           ScripCondition => $condition->id,
                           ScripAction    => $action->id,
@@ -82,7 +84,7 @@ my $scrip = RT::Scrip->new($RT::SystemUser);
                            );
 ok($id, "Scrip created");
 
-my $u1 = RT::User->new($RT::SystemUser);
+my $u1 = RT::User->new(RT->SystemUser);
 ($id,$msg) = $u1->Create(Name => "LinkTestUser.$$");
 ok ($id,$msg);
 
@@ -96,31 +98,31 @@ ok ($id,$msg);
 
 my $creator = RT::CurrentUser->new($u1->id);
 
-diag('Create tickets without rights to link') if $ENV{'TEST_VERBOSE'};
+diag('Create tickets without rights to link');
 {
     # on q2 we have no rights, yet
-    my $parent = RT::Ticket->new( $RT::SystemUser );
+    my $parent = RT::Ticket->new( RT->SystemUser );
     my ($id,$tid,$msg) = $parent->Create( Subject => 'Link test 1', Queue => $q2->id );
     ok($id,$msg);
     my $child = RT::Ticket->new( $creator );
     ($id,$tid,$msg) = $child->Create( Subject => 'Link test 1', Queue => $q1->id, MemberOf => $parent->id );
     ok($id,$msg);
-    $child->CurrentUser( $RT::SystemUser );
+    $child->CurrentUser( RT->SystemUser );
     is($child->_Links('Base')->Count, 0, 'link was not created, no permissions');
     is($child->_Links('Target')->Count, 0, 'link was not create, no permissions');
 }
 
-diag('Create tickets with rights checks on one end of a link') if $ENV{'TEST_VERBOSE'};
+diag('Create tickets with rights checks on one end of a link');
 {
     # on q2 we have no rights, but use checking one only on thing
     RT->Config->Set( StrictLinkACL => 0 );
-    my $parent = RT::Ticket->new( $RT::SystemUser );
+    my $parent = RT::Ticket->new( RT->SystemUser );
     my ($id,$tid,$msg) = $parent->Create( Subject => 'Link test 1', Queue => $q2->id );
     ok($id,$msg);
     my $child = RT::Ticket->new( $creator );
     ($id,$tid,$msg) = $child->Create( Subject => 'Link test 1', Queue => $q1->id, MemberOf => $parent->id );
     ok($id,$msg);
-    $child->CurrentUser( $RT::SystemUser );
+    $child->CurrentUser( RT->SystemUser );
     is($child->_Links('Base')->Count, 1, 'link was created');
     is($child->_Links('Target')->Count, 0, 'link was created only one');
     # no scrip run on second ticket accroding to config option
@@ -131,10 +133,10 @@ diag('Create tickets with rights checks on one end of a link') if $ENV{'TEST_VER
 ($id,$msg) = $u1->PrincipalObj->GrantRight ( Object => $q1, Right => 'ModifyTicket');
 ok ($id,$msg);
 
-diag('try to add link without rights') if $ENV{'TEST_VERBOSE'};
+diag('try to add link without rights');
 {
     # on q2 we have no rights, yet
-    my $parent = RT::Ticket->new( $RT::SystemUser );
+    my $parent = RT::Ticket->new( RT->SystemUser );
     my ($id,$tid,$msg) = $parent->Create( Subject => 'Link test 1', Queue => $q2->id );
     ok($id,$msg);
     my $child = RT::Ticket->new( $creator );
@@ -143,16 +145,16 @@ diag('try to add link without rights') if $ENV{'TEST_VERBOSE'};
     ($id, $msg) = $child->AddLink(Type => 'MemberOf', Target => $parent->id);
     ok(!$id, $msg);
     is(link_count($filename), undef, "scrips ok");
-    $child->CurrentUser( $RT::SystemUser );
+    $child->CurrentUser( RT->SystemUser );
     is($child->_Links('Base')->Count, 0, 'link was not created, no permissions');
     is($child->_Links('Target')->Count, 0, 'link was not create, no permissions');
 }
 
-diag('add link with rights only on base') if $ENV{'TEST_VERBOSE'};
+diag('add link with rights only on base');
 {
     # on q2 we have no rights, but use checking one only on thing
     RT->Config->Set( StrictLinkACL => 0 );
-    my $parent = RT::Ticket->new( $RT::SystemUser );
+    my $parent = RT::Ticket->new( RT->SystemUser );
     my ($id,$tid,$msg) = $parent->Create( Subject => 'Link test 1', Queue => $q2->id );
     ok($id,$msg);
     my $child = RT::Ticket->new( $creator );
@@ -161,7 +163,7 @@ diag('add link with rights only on base') if $ENV{'TEST_VERBOSE'};
     ($id, $msg) = $child->AddLink(Type => 'MemberOf', Target => $parent->id);
     ok($id, $msg);
     is(link_count($filename), 1, "scrips ok");
-    $child->CurrentUser( $RT::SystemUser );
+    $child->CurrentUser( RT->SystemUser );
     is($child->_Links('Base')->Count, 1, 'link was created');
     is($child->_Links('Target')->Count, 0, 'link was created only one');
     $child->CurrentUser( $creator );
@@ -171,7 +173,7 @@ diag('add link with rights only on base') if $ENV{'TEST_VERBOSE'};
     ($id, $msg) = $child->AddLink(Type => 'MemberOf', Target => $parent->id);
     ok(!$id, $msg);
     is(link_count($filename), 1, "scrips ok");
-    $child->CurrentUser( $RT::SystemUser );
+    $child->CurrentUser( RT->SystemUser );
     $child->_Links('Base')->_DoCount;
     is($child->_Links('Base')->Count, 1, 'link was not deleted');
     $child->CurrentUser( $creator );
@@ -181,7 +183,7 @@ diag('add link with rights only on base') if $ENV{'TEST_VERBOSE'};
     ($id, $msg) = $child->DeleteLink(Type => 'MemberOf', Target => $parent->id);
     ok($id, $msg);
     is(link_count($filename), 0, "scrips ok");
-    $child->CurrentUser( $RT::SystemUser );
+    $child->CurrentUser( RT->SystemUser );
     $child->_Links('Base')->_DoCount;
     is($child->_Links('Base')->Count, 0, 'link was deleted');
     RT->Config->Set( StrictLinkACL => 1 );
@@ -193,14 +195,16 @@ ok($ticket->isa('RT::Ticket'));
 ($id,$tid, $msg) = $ticket->Create(Subject => 'Link test 1', Queue => $q1->id);
 ok ($id,$msg);
 
-diag('try link to itself') if $ENV{'TEST_VERBOSE'};
+diag('try link to itself');
 {
-    my ($id, $msg) = $ticket->AddLink(Type => 'RefersTo', Target => $ticket->id);
+    warning_like {
+        ($id, $msg) = $ticket->AddLink(Type => 'RefersTo', Target => $ticket->id);
+    } qr/Can't link a ticket to itself/;
     ok(!$id, $msg);
     is(link_count($filename), 0, "scrips ok");
 }
 
-my $ticket2 = RT::Ticket->new($RT::SystemUser);
+my $ticket2 = RT::Ticket->new(RT->SystemUser);
 ($id, $tid, $msg) = $ticket2->Create(Subject => 'Link test 2', Queue => $q2->id);
 ok ($id, $msg);
 ($id,$msg) =$ticket->AddLink(Type => 'RefersTo', Target => $ticket2->id);
@@ -214,8 +218,14 @@ ok ($id,$msg);
 ($id,$msg) = $ticket->AddLink(Type => 'RefersTo', Target => $ticket2->id);
 ok($id,$msg);
 is(link_count($filename), 1, "scrips ok");
-($id,$msg) = $ticket->AddLink(Type => 'RefersTo', Target => -1);
-ok(!$id,$msg);
+
+warnings_like {
+    ($id,$msg) = $ticket->AddLink(Type => 'RefersTo', Target => -1);
+} [
+    qr/Could not determine a URI scheme for -1/,
+    qr/Couldn't resolve '-1' into a URI/,
+];
+
 ($id,$msg) = $ticket->AddLink(Type => 'RefersTo', Target => $ticket2->id);
 ok($id,$msg);
 is(link_count($filename), 1, "scrips ok");
@@ -301,13 +311,13 @@ RT->Config->Set( LinkTransactionsRun1Scrip => $link_scrips_orig );
 RT->Config->Set( StrictLinkACL => $link_acl_checks_orig );
 
 {
-    my $Scrips = RT::Scrips->new( $RT::SystemUser );
+    my $Scrips = RT::Scrips->new( RT->SystemUser );
     $Scrips->Limit( FIELD => 'Description', OPERATOR => 'STARTSWITH', VALUE => 'Add or Delete Link ');
     while ( my $s = $Scrips->Next ) { $s->Delete };
 }
 
 
-my $link = RT::Link->new( $RT::SystemUser );
+my $link = RT::Link->new( RT->SystemUser );
 ($id,$msg) = $link->Create( Base => $ticket->URI, Target => $ticket2->URI, Type => 'MyLinkType' );
 ok($id, $msg);
 ok($link->LocalBase   == $ticket->id,  "LocalBase   set correctly");
@@ -375,7 +385,7 @@ ok($link->LocalBase   == 0, "LocalBase set correctly");
 
 sub link_count {
     my $file = shift;
-    open ( my $fh, '<', $file ) or die "couldn't open $file";
+    open( my $fh, '<', $file ) or die "couldn't open $file";
     my $data = <$fh>;
     close $fh;