[Rt-commit] r4774 - in rt/branches/3.7-EXPERIMENTAL: . lib/t/regression

ruz at bestpractical.com ruz at bestpractical.com
Mon Mar 20 10:03:13 EST 2006


Author: ruz
Date: Mon Mar 20 10:03:12 2006
New Revision: 4774

Modified:
   rt/branches/3.7-EXPERIMENTAL/   (props changed)
   rt/branches/3.7-EXPERIMENTAL/lib/RT/Ticket_Overlay.pm
   rt/branches/3.7-EXPERIMENTAL/lib/t/regression/14linking.t

Log:
 r2113 at cubic-pc:  cubic | 2006-03-20 10:34:20 +0300
 * implement Silent{Base,Target} boolean args in {Add,Delete}Link methods in RT::Ticket
 * tests


Modified: rt/branches/3.7-EXPERIMENTAL/lib/RT/Ticket_Overlay.pm
==============================================================================
--- rt/branches/3.7-EXPERIMENTAL/lib/RT/Ticket_Overlay.pm	(original)
+++ rt/branches/3.7-EXPERIMENTAL/lib/RT/Ticket_Overlay.pm	Mon Mar 20 10:03:12 2006
@@ -171,9 +171,7 @@
 # A helper table for links mapping to make it easier
 # to build and parse links between tickets
 
-use vars '%LINKTYPEMAP';
-
-%LINKTYPEMAP = (
+our %LINKTYPEMAP = (
     MemberOf => { Type => 'MemberOf',
                   Mode => 'Target', },
     Parents => { Type => 'MemberOf',
@@ -203,9 +201,7 @@
 # A helper table for links mapping to make it easier
 # to build and parse links between tickets
 
-use vars '%LINKDIRMAP';
-
-%LINKDIRMAP = (
+our %LINKDIRMAP = (
     MemberOf => { Base => 'MemberOf',
                   Target => 'HasMember', },
     RefersTo => { Base => 'RefersTo',
@@ -2468,9 +2464,14 @@
 
 =head2 DeleteLink
 
-Delete a link. takes a paramhash of Base, Target and Type.
-Either Base or Target must be null. The null value will 
-be replaced with this ticket\'s id
+Delete a link. takes a paramhash of Base, Target, Type, Silent,
+SilentBase and SilentTarget. Either Base or Target must be null.
+The null value will be replaced with this ticket\'s id.
+
+If Silent is true then no transaction would be recorded, in other
+case you can control creation of transactions on both base and
+target with SilentBase and SilentTarget respectively. By default
+both transactions are created.
 
 =cut 
 
@@ -2480,6 +2481,9 @@
         Base   => undef,
         Target => undef,
         Type   => undef,
+        Silent => undef,
+        SilentBase   => undef,
+        SilentTarget => undef,
         @_
     );
 
@@ -2491,50 +2495,48 @@
     }
 
     my ($val, $Msg) = $self->SUPER::_DeleteLink(%args);
+    return ( 0, $Msg ) unless $val;
 
-    if ( !$val ) {
-        $RT::Logger->debug("Couldn't find that link\n");
-        return ( 0, $Msg );
-    }
+    return ( $val, $Msg ) if $args{'Silent'};
 
     my ($direction, $remote_link);
 
     if ( $args{'Base'} ) {
-	$remote_link = $args{'Base'};
-    	$direction = 'Target';
+        $remote_link = $args{'Base'};
+        $direction = 'Target';
     }
     elsif ( $args{'Target'} ) {
-	$remote_link = $args{'Target'};
-        $direction='Base';
-    }
+        $remote_link = $args{'Target'};
+        $direction = 'Base';
+    } 
 
-    if ( $args{'Silent'} ) {
-        return ( $val, $Msg );
-    }
-    else {
-	my $remote_uri = RT::URI->new( $self->CurrentUser );
-    	$remote_uri->FromURI( $remote_link );
+    my $remote_uri = RT::URI->new( $self->CurrentUser );
+    $remote_uri->FromURI( $remote_link );
 
+    unless ( $args{ 'Silent'. $direction } ) {
         my ( $Trans, $Msg, $TransObj ) = $self->_NewTransaction(
             Type      => 'DeleteLink',
-            Field => $LINKDIRMAP{$args{'Type'}}->{$direction},
-	    OldValue =>  $remote_uri->URI || $remote_link,
+            Field     => $LINKDIRMAP{$args{'Type'}}->{$direction},
+            OldValue  =>  $remote_uri->URI || $remote_link,
             TimeTaken => 0
         );
+        $RT::Logger->error("Couldn't create transaction: $Msg") unless $Trans;
+    }
 
-        if ( $remote_uri->IsLocal ) {
-
-            my $OtherObj = $remote_uri->Object;
-            my ( $val, $Msg ) = $OtherObj->_NewTransaction(Type  => 'DeleteLink',
-                                                           Field => $direction eq 'Target' ? $LINKDIRMAP{$args{'Type'}}->{Base}
-                                                                                           : $LINKDIRMAP{$args{'Type'}}->{Target},
-                                                           OldValue => $self->URI,
-                                                           ActivateScrips => ! $RT::LinkTransactionsRun1Scrip,
-                                                           TimeTaken => 0 );
-        }
-
-        return ( $Trans, $Msg );
+    if ( !$args{ 'Silent'. ( $direction eq 'Target'? 'Base': 'Target' ) } && $remote_uri->IsLocal ) {
+        my $OtherObj = $remote_uri->Object;
+        my ( $val, $Msg ) = $OtherObj->_NewTransaction(
+            Type           => 'DeleteLink',
+            Field          => $direction eq 'Target' ? $LINKDIRMAP{$args{'Type'}}->{Base}
+                                            : $LINKDIRMAP{$args{'Type'}}->{Target},
+            OldValue       => $self->URI,
+            ActivateScrips => !RT->Config->Get('LinkTransactionsRun1Scrip'),
+            TimeTaken      => 0,
+        );
+        $RT::Logger->error("Couldn't create transaction: $Msg") unless $val;
     }
+
+    return ( $val, $Msg );
 }
 
 # }}}
@@ -2545,14 +2547,21 @@
 
 Takes a paramhash of Type and one of Base or Target. Adds that link to this ticket.
 
+If Silent is true then no transaction would be recorded, in other
+case you can control creation of transactions on both base and
+target with SilentBase and SilentTarget respectively. By default
+both transactions are created.
+
 =cut
 
 sub AddLink {
     my $self = shift;
-    my %args = ( Target => '',
-                 Base   => '',
-                 Type   => '',
-                 Silent => undef,
+    my %args = ( Target       => '',
+                 Base         => '',
+                 Type         => '',
+                 Silent       => undef,
+                 SilentBase   => undef,
+                 SilentTarget => undef,
                  @_ );
 
 
@@ -2572,10 +2581,12 @@
 
 sub _AddLink {
     my $self = shift;
-    my %args = ( Target => '',
-                 Base   => '',
-                 Type   => '',
-                 Silent => undef,
+    my %args = ( Target       => '',
+                 Base         => '',
+                 Type         => '',
+                 Silent       => undef,
+                 SilentBase   => undef,
+                 SilentTarget => undef,
                  @_ );
 
     # {{{ If the other URI is an RT::Ticket, we want to make sure the user
@@ -2584,7 +2595,6 @@
 
     if ( $args{'Target'} ) {
         $other_ticket_uri->FromURI( $args{'Target'} );
-
     }
     elsif ( $args{'Base'} ) {
         $other_ticket_uri->FromURI( $args{'Base'} );
@@ -2613,10 +2623,9 @@
     # }}}
 
     my ($val, $Msg) = $self->SUPER::_AddLink(%args);
+    return ($val, $Msg) unless $val;
 
-    if (!$val) {
-	return ($val, $Msg);
-    }
+    return ($val, $Msg) if $args{'Silent'};
 
     my ($direction, $remote_link);
     if ( $args{'Target'} ) {
@@ -2627,34 +2636,34 @@
         $direction    = 'Target';
     }
 
-    # Don't write the transaction if we're doing this on create
-    if ( $args{'Silent'} ) {
-        return ( $val, $Msg );
+    my $remote_uri = RT::URI->new( $self->CurrentUser );
+    $remote_uri->FromURI( $remote_link );
+
+    unless ( $args{ 'Silent'. $direction } ) {
+        my ( $Trans, $Msg, $TransObj ) = $self->_NewTransaction(
+            Type      => 'AddLink',
+            Field     => $LINKDIRMAP{$args{'Type'}}->{$direction},
+            NewValue  =>  $remote_uri->URI || $remote_link,
+            TimeTaken => 0
+        );
+        $RT::Logger->error("Couldn't create transaction: $Msg") unless $Trans;
     }
-    else {
-	my $remote_uri = RT::URI->new( $self->CurrentUser );
-    	$remote_uri->FromURI( $remote_link );
 
-        #Write the transaction
-        my ( $Trans, $Msg, $TransObj ) = 
-	    $self->_NewTransaction(Type  => 'AddLink',
-				   Field => $LINKDIRMAP{$args{'Type'}}->{$direction},
-				   NewValue =>  $remote_uri->URI || $remote_link,
-				   TimeTaken => 0 );
-
-        if ( $remote_uri->IsLocal ) {
-
-            my $OtherObj = $remote_uri->Object;
-            my ( $val, $Msg ) = $OtherObj->_NewTransaction(Type  => 'AddLink',
-                                                           Field => $direction eq 'Target' ? $LINKDIRMAP{$args{'Type'}}->{Base} 
-                                                                                           : $LINKDIRMAP{$args{'Type'}}->{Target},
-                                                           NewValue => $self->URI,
-                                                           ActivateScrips => ! $RT::LinkTransactionsRun1Scrip,
-                                                           TimeTaken => 0 );
-        }
-        return ( $val, $Msg );
+    if ( !$args{ 'Silent'. ( $direction eq 'Target'? 'Base': 'Target' ) } && $remote_uri->IsLocal ) {
+        my $OtherObj = $remote_uri->Object;
+        my ( $val, $Msg ) = $OtherObj->_NewTransaction(
+            Type           => 'AddLink',
+            Field          => $direction eq 'Target' ? $LINKDIRMAP{$args{'Type'}}->{Base}
+                                            : $LINKDIRMAP{$args{'Type'}}->{Target},
+            NewValue       => $self->URI,
+            ActivateScrips => !RT->Config->Get('LinkTransactionsRun1Scrip'),
+            TimeTaken      => 0,
+        );
+        $RT::Logger->error("Couldn't create transaction: $Msg") unless $val;
     }
 
+    return ( $val, $Msg );
+
 }
 
 # }}}

Modified: rt/branches/3.7-EXPERIMENTAL/lib/t/regression/14linking.t
==============================================================================
--- rt/branches/3.7-EXPERIMENTAL/lib/t/regression/14linking.t	(original)
+++ rt/branches/3.7-EXPERIMENTAL/lib/t/regression/14linking.t	Mon Mar 20 10:03:12 2006
@@ -1,4 +1,7 @@
-use Test::More tests => '39';
+use Test::More tests => '59';
+use strict;
+use warnings;
+
 use_ok('RT');
 use_ok('RT::Ticket');
 use_ok('RT::ScripConditions');
@@ -81,6 +84,12 @@
 
 ok ($id,$msg);
 
+# grant ShowTicket right to allow count transactions
+($id,$msg) = $u1->PrincipalObj->GrantRight ( Object => $q1, Right => 'ShowTicket');
+ok ($id,$msg);
+($id,$msg) = $u1->PrincipalObj->GrantRight ( Object => $q2, Right => 'ShowTicket');
+ok ($id,$msg);
+
 ($id,$msg) = $u1->PrincipalObj->GrantRight ( Object => $q1, Right => 'CreateTicket');
 ok ($id,$msg);
 ($id,$msg) = $u1->PrincipalObj->GrantRight ( Object => $q1, Right => 'ModifyTicket');
@@ -107,10 +116,10 @@
 ok ($id,$msg);
 ($id,$msg) = $u1->PrincipalObj->GrantRight ( Object => $q2, Right => 'ModifyTicket');
 ok ($id,$msg);
-($id,$msg) =$ticket->AddLink(Type => 'RefersTo', Target => $ticket2->id);
+($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);
+($id,$msg) = $ticket->AddLink(Type => 'RefersTo', Target => -1);
 ok(!$id,$msg);
 is(link_count($filename), 1, "scrips ok");
 
@@ -120,7 +129,7 @@
 ok( $transactions->First->Field eq 'ReferredToBy');
 ok( $transactions->First->NewValue eq $ticket->URI );
 
-($id,$msg) =$ticket->DeleteLink(Type => 'RefersTo', Target => $ticket2->id);
+($id,$msg) = $ticket->DeleteLink(Type => 'RefersTo', Target => $ticket2->id);
 ok($id,$msg);
 is(link_count($filename), 0, "scrips ok");
 $transactions = $ticket2->Transactions;
@@ -130,6 +139,7 @@
 ok( $transactions->First->OldValue eq $ticket->URI );
 
 RT->Config->Set( LinkTransactionsRun1Scrip => 0 );
+
 ($id,$msg) =$ticket->AddLink(Type => 'RefersTo', Target => $ticket2->id);
 ok($id,$msg);
 is(link_count($filename), 2, "scrips ok");
@@ -137,6 +147,58 @@
 ok($id,$msg);
 is(link_count($filename), 0, "scrips ok");
 
+# tests for silent behaviour
+($id,$msg) = $ticket->AddLink(Type => 'RefersTo', Target => $ticket2->id, Silent => 1);
+ok($id,$msg);
+is(link_count($filename), 0, "scrips ok");
+{
+    my $transactions = $ticket->Transactions;
+    $transactions->Limit( FIELD => 'Type', VALUE => 'AddLink' );
+    is( $transactions->Count, 5, "Still five txns on the base" );
+
+    $transactions = $ticket2->Transactions;
+    $transactions->Limit( FIELD => 'Type', VALUE => 'AddLink' );
+    is( $transactions->Count, 2, "Still two txns on the target" );
+
+}
+($id,$msg) =$ticket->DeleteLink(Type => 'RefersTo', Target => $ticket2->id, Silent => 1);
+ok($id,$msg);
+is(link_count($filename), 0, "scrips ok");
+
+($id,$msg) = $ticket->AddLink(Type => 'RefersTo', Target => $ticket2->id, SilentBase => 1);
+ok($id,$msg);
+is(link_count($filename), 1, "scrips ok");
+{
+    my $transactions = $ticket->Transactions;
+    $transactions->Limit( FIELD => 'Type', VALUE => 'AddLink' );
+    is( $transactions->Count, 5, "still five txn on the base" );
+
+    $transactions = $ticket2->Transactions;
+    $transactions->Limit( FIELD => 'Type', VALUE => 'AddLink' );
+    is( $transactions->Count, 3, "+1 txn on the target" );
+
+}
+($id,$msg) =$ticket->DeleteLink(Type => 'RefersTo', Target => $ticket2->id, SilentBase => 1);
+ok($id,$msg);
+is(link_count($filename), 0, "scrips ok");
+
+($id,$msg) = $ticket->AddLink(Type => 'RefersTo', Target => $ticket2->id, SilentTarget => 1);
+ok($id,$msg);
+is(link_count($filename), 1, "scrips ok");
+{
+    my $transactions = $ticket->Transactions;
+    $transactions->Limit( FIELD => 'Type', VALUE => 'AddLink' );
+    is( $transactions->Count, 6, "+1 txn on the base" );
+
+    $transactions = $ticket2->Transactions;
+    $transactions->Limit( FIELD => 'Type', VALUE => 'AddLink' );
+    is( $transactions->Count, 3, "three txns on the target" );
+}
+($id,$msg) =$ticket->DeleteLink(Type => 'RefersTo', Target => $ticket2->id, SilentTarget => 1);
+ok($id,$msg);
+is(link_count($filename), 0, "scrips ok");
+
+
 # restore
 RT->Config->Set( LinkTransactionsRun1Scrip => $link_scrips_orig );
 


More information about the Rt-commit mailing list