first pass RT4 merge, RT#13852
[freeside.git] / rt / t / api / link.t
index eac9ae2..a9e54a7 100644 (file)
@@ -1,13 +1,12 @@
-
 use strict;
 use warnings;
 
-use RT::Test tests => 77;
+use RT::Test nodata => 1, tests => 84;
 use RT::Test::Web;
+use Test::Warn;
 
 use RT::Link;
-my $link = RT::Link->new($RT::SystemUser);
-
+my $link = RT::Link->new(RT->SystemUser);
 
 ok (ref $link);
 isa_ok( $link, 'RT::Link');
@@ -18,29 +17,37 @@ isa_ok( $link, 'DBIx::SearchBuilder::Record');
 my $queue = RT::Test->load_or_create_queue(Name => 'General');
 ok($queue->Id, "loaded the General queue");
 
-my $parent = RT::Ticket->new($RT::SystemUser);
+my $parent = RT::Ticket->new(RT->SystemUser);
 my ($pid, undef, $msg) = $parent->Create(
     Queue   => $queue->id,
     Subject => 'parent',
 );
 ok $pid, 'created a ticket #'. $pid or diag "error: $msg";
 
-my $child = RT::Ticket->new($RT::SystemUser);
-my ($cid, undef, $msg) = $child->Create(
+my $child = RT::Ticket->new(RT->SystemUser);
+((my $cid), undef, $msg) = $child->Create(
     Queue   => $queue->id,
     Subject => 'child',
 );
 ok $cid, 'created a ticket #'. $cid or diag "error: $msg";
 
 {
+    my ($status, $msg);
     clean_links();
-    my ($status, $msg) = $parent->AddLink;
+
+    warning_like {
+        ($status, $msg) = $parent->AddLink;
+    } qr/Base or Target must be specified/, "warned about linking a ticket to itself";
     ok(!$status, "didn't create a link: $msg");
 
-    ($status, $msg) = $parent->AddLink( Base => $parent->id );
+    warning_like {
+        ($status, $msg) = $parent->AddLink( Base => $parent->id );
+    } qr/Can't link a ticket to itself/, "warned about linking a ticket to itself";
     ok(!$status, "didn't create a link: $msg");
 
-    ($status, $msg) = $parent->AddLink( Base => $parent->id, Type => 'HasMember' );
+    warning_like {
+        ($status, $msg) = $parent->AddLink( Base => $parent->id, Type => 'HasMember' );
+    } qr/Can't link a ticket to itself/, "warned about linking a ticket to itself";
     ok(!$status, "didn't create a link: $msg");
 }
 
@@ -197,8 +204,39 @@ ok $cid, 'created a ticket #'. $cid or diag "error: $msg";
     ;
 }
 
+{
+    clean_links();
+    $child->SetStatus('deleted');
+
+    my ($status, $msg) = $parent->AddLink(
+        Type => 'MemberOf', Base => $child->id,
+    );
+    ok(!$status, "can't link to deleted ticket: $msg");
+
+    $child->SetStatus('new');
+    ($status, $msg) = $parent->AddLink(
+        Type => 'MemberOf', Base => $child->id,
+    );
+    ok($status, "created a link: $msg");
+
+    $child->SetStatus('deleted');
+    my $children = $parent->Members;
+    $children->RedoSearch;
+
+    my $total = 0;
+    $total++ while $children->Next;
+    is( $total, 0, 'Next skips deleted tickets' );
+
+    is( @{ $children->ItemsArrayRef },
+        0, 'ItemsArrayRef skips deleted tickets' );
+
+    # back to active status
+    $child->SetStatus('new');
+}
+
 sub clean_links {
-    my $links = RT::Links->new( $RT::SystemUser );
+    my $links = RT::Links->new( RT->SystemUser );
+    $links->UnLimit;
     while ( my $link = $links->Next ) {
         my ($status, $msg) = $link->Delete;
         $RT::Logger->error("Couldn't delete a link: $msg")
@@ -206,4 +244,3 @@ sub clean_links {
     }
 }
 
-1;