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

BPS Git Server git at git.bestpractical.com
Wed Mar 15 14:14:09 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  673884623a4cf7861b2d1521367af0a0ec7479f8 (commit)

- Log -----------------------------------------------------------------
commit 673884623a4cf7861b2d1521367af0a0ec7479f8
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/scripaction.t b/t/api/scripaction.t
new file mode 100644
index 0000000000..22af32391e
--- /dev/null
+++ b/t/api/scripaction.t
@@ -0,0 +1,30 @@
+
+use strict;
+use warnings;
+use RT;
+use RT::Test tests => undef;
+
+
+{
+    # Test that we can't create unnamed actions
+    my $action1 = RT::ScripAction->new( RT->SystemUser );
+    my ($id1, $msg1) = $action1->Create(
+        Name        => '',
+    );
+    ok ($msg1 eq '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
+    ok ($msg3 eq 'Name in use');
+}
+
+done_testing();
diff --git a/t/api/scripcondition.t b/t/api/scripcondition.t
new file mode 100644
index 0000000000..121dfdc3fe
--- /dev/null
+++ b/t/api/scripcondition.t
@@ -0,0 +1,30 @@
+
+use strict;
+use warnings;
+use RT;
+use RT::Test tests => undef;
+
+
+{
+    # Test that we can't create unnamed conditions
+    my $condition1 = RT::ScripCondition->new( RT->SystemUser );
+    my ($id1, $msg1) = $condition1->Create(
+        Name        => '',
+    );
+    ok ($msg1 eq '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
+    ok ($msg3 eq 'Name in use');
+}
+
+done_testing();

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