[Rt-commit] rt branch 5.0/unique-conditions-and-actions created. rt-5.0.3-294-g7fc3193541

BPS Git Server git at git.bestpractical.com
Wed Apr 26 17:13:15 UTC 2023


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "rt".

The branch, 5.0/unique-conditions-and-actions has been created
        at  7fc319354129e573e2fff7ba3621b30acb4b9002 (commit)

- Log -----------------------------------------------------------------
commit 7fc319354129e573e2fff7ba3621b30acb4b9002
Author: Ronaldo Richieri <ronaldo at bestpractical.com>
Date:   Wed Mar 15 10:50:12 2023 -0300

    Add tests for ScripAction and ScripCondition
    
    Tests if empty name actions or conditions can not be created.
    Tests if actions or conditions can be created with existing name.

diff --git a/t/api/scrip.t b/t/api/scrip.t
index d6019a082e..ecc4981a13 100644
--- a/t/api/scrip.t
+++ b/t/api/scrip.t
@@ -264,3 +264,49 @@ note 'basic check for disabling scrips';
     is($scrip->FriendlyStage('TransactionBatch'), 'Batch (disabled by config)',
         'Correct stage wording for TransactionBatch with UseTransactionBatch disabled');
 }
+
+note 'check scrip actions name constrains';
+{
+    # Test that we can't create unnamed actions
+    my $action1 = RT::ScripAction->new( RT->SystemUser );
+    my ($id1, $msg1) = $action1->Create(
+        Name        => '',
+    );
+    is ($msg1,'empty name');
+
+    # Create action Foo
+    my $action2 = RT::ScripAction->new( RT->SystemUser );
+    $action2->Create(
+        Name        => 'Foo Action',
+    );
+
+    my $action3 = RT::ScripAction->new( RT->SystemUser );
+    my ($id3, $msg3) = $action3->Create(
+        Name        => 'Foo Action',
+    );
+    # Make sure we can't create a action with the same name
+    is ($msg3,'Name in use');
+}
+
+note 'check scrip conditions name constrains';
+{
+    # Test that we can't create unnamed conditions
+    my $condition1 = RT::ScripCondition->new( RT->SystemUser );
+    my ($id1, $msg1) = $condition1->Create(
+        Name        => '',
+    );
+    is ($msg1,'empty name');
+
+    # Create condition Foo
+    my $condition2 = RT::ScripCondition->new( RT->SystemUser );
+    $condition2->Create(
+        Name        => 'Foo Condition',
+    );
+
+    my $condition3 = RT::ScripCondition->new( RT->SystemUser );
+    my ($id3, $msg3) = $condition3->Create(
+        Name        => 'Foo Condition',
+    );
+    # Make sure we can't create a condition with the same name
+    is ($msg3,'Name in use');
+}

commit ce7deb3137a7705a799397dacb303f9242c567c9
Author: Ronaldo Richieri <ronaldo at bestpractical.com>
Date:   Fri Mar 10 16:54:55 2023 -0300

    Require unique name for Conditions and Actions
    
    Checks if the same name is already in use when creating or updating
    conditions and actions, avoiding confusion on managing them.
    Also requires non empty name for actions and conditions.

diff --git a/lib/RT/ScripAction.pm b/lib/RT/ScripAction.pm
index 37b0ed7250..9ec432cbe9 100644
--- a/lib/RT/ScripAction.pm
+++ b/lib/RT/ScripAction.pm
@@ -97,8 +97,57 @@ Takes a hash. Creates a new Action entry.
 
 sub Create  {
     my $self = shift;
-    #TODO check these args and do smart things.
-    return($self->SUPER::Create(@_));
+    my %args = (
+        @_    # get the real argumentlist
+    );
+
+    my ( $val, $msg ) = $self->ValidateName( $args{'Name'} );
+    return ( 0, $msg ) unless $val;
+
+    return($self->SUPER::Create( %args ));
+}
+
+=head2 SetName
+
+Check to make sure name is not already in use
+
+=cut
+
+sub SetName {
+    my $self  = shift;
+    my $Value = shift;
+
+    my ( $val, $message ) = $self->ValidateName($Value);
+    if ($val) {
+        return $self->_Set( Field => 'Name', Value => $Value );
+    }
+    else {
+        return ( 0, $message );
+    }
+}
+
+=head2 ValidateName STRING
+
+Returns either (0, "failure reason") or 1 depending on whether the given
+name is valid.
+
+=cut
+
+sub ValidateName {
+    my $self = shift;
+    my $name = shift;
+
+    return ( 0, $self->loc('empty name') ) unless defined $name && length $name;
+
+    my $TempAction = RT::ScripAction->new( RT->SystemUser );
+    $TempAction->Load($name);
+
+    if ( $TempAction->id && ( !$self->id || $TempAction->id != $self->id ) ) {
+        return ( 0, $self->loc('Name in use') );
+    }
+    else {
+        return 1;
+    }
 }
 
 sub Delete {
diff --git a/lib/RT/ScripCondition.pm b/lib/RT/ScripCondition.pm
index a186770f08..a84a33ef33 100644
--- a/lib/RT/ScripCondition.pm
+++ b/lib/RT/ScripCondition.pm
@@ -102,9 +102,56 @@ sub _Accessible  {
 
 sub Create  {
     my $self = shift;
-    return($self->SUPER::Create(@_));
+    my %args = ( @_ );
+
+    my ( $val, $msg ) = $self->ValidateName( $args{'Name'} );
+    return ( 0, $msg ) unless $val;
+
+    return($self->SUPER::Create( %args ));
+}
+
+=head2 SetName
+
+Check to make sure name is not already in use
+
+=cut
+
+sub SetName {
+    my $self  = shift;
+    my $Value = shift;
+
+    my ( $val, $message ) = $self->ValidateName($Value);
+    if ($val) {
+        return $self->_Set( Field => 'Name', Value => $Value );
+    }
+    else {
+        return ( 0, $message );
+    }
 }
 
+=head2 ValidateName STRING
+
+Returns either (0, "failure reason") or 1 depending on whether the given
+name is valid.
+
+=cut
+
+sub ValidateName {
+    my $self = shift;
+    my $name = shift;
+
+    return ( 0, $self->loc('empty name') ) unless defined $name && length $name;
+
+    my $TempCondition = RT::ScripCondition->new( RT->SystemUser );
+    $TempCondition->Load($name);
+
+    if ( $TempCondition->id && ( !$self->id || $TempCondition->id != $self->id ) ) {
+        return ( 0, $self->loc('Name in use') );
+    }
+    else {
+        return 1;
+    }
+}
 
 =head2 Delete
 

-----------------------------------------------------------------------


hooks/post-receive
-- 
rt


More information about the rt-commit mailing list