[Rt-commit] rt branch, 4.2/validate-value-for-select-cf-2, created. rt-4.2.16-14-gf43da86a39

Jim Brandt jbrandt at bestpractical.com
Mon Feb 15 13:19:20 EST 2021


The branch, 4.2/validate-value-for-select-cf-2 has been created
        at  f43da86a39ff63ea04479e71069e1123f19bbb77 (commit)

- Log -----------------------------------------------------------------
commit f5d0c7d951be0559ed02be627e96d1fac2805421
Author: Jim Brandt <jbrandt at bestpractical.com>
Date:   Mon Feb 15 10:54:38 2021 -0500

    Add test setting select CF to a value not in values list

diff --git a/t/customfields/single_values.t b/t/customfields/single_values.t
index 42ba479b44..3ea02a15d8 100644
--- a/t/customfields/single_values.t
+++ b/t/customfields/single_values.t
@@ -2,7 +2,7 @@ use warnings;
 use strict;
 
 use RT;
-use RT::Test nodata => 1, tests => 8;
+use RT::Test tests => undef;
 
 
 
@@ -28,9 +28,24 @@ my $t = RT::Ticket->new(RT->SystemUser);
 
 ok($id,$msg);
 is($t->CustomFieldValues($cf->id)->Count, 0, "No values yet");
-$t->AddCustomFieldValue(Field => $cf->id, Value => 'First');
+
+my $ok;
+my $value = 'First';
+($ok, $msg) = $t->AddCustomFieldValue(Field => $cf->id, Value => $value);
+ok( $ok, $msg );
 is($t->CustomFieldValues($cf->id)->Count, 1, "One now");
+is($t->FirstCustomFieldValue($cf->id), $value, "Value is $value");
+
+$value = 'Second';
+($ok, $msg) = $t->AddCustomFieldValue(Field => $cf->id, Value => $value);
+ok( $ok, $msg );
+is($t->CustomFieldValues($cf->id)->Count, 1, "Still one value");
+is($t->FirstCustomFieldValue($cf->id), $value, "Value is $value");
 
-$t->AddCustomFieldValue(Field => $cf->id, Value => 'Second');
+($ok, $msg) = $t->AddCustomFieldValue(Field => $cf->id, Value => 'Bogus');
+ok( !$ok, 'Returned false with value not in values list' );
+like( $msg, qr/Invalid value/, 'Message reports invalid value');
 is($t->CustomFieldValues($cf->id)->Count, 1, "Still one");
+is($t->FirstCustomFieldValue($cf->id), $value, "Value is still $value");
 
+done_testing();

commit 40f8cd4d5f1af26ad18c174439d0f225b98ced51
Author: Steven Burr <steve at bestpractical.com>
Date:   Fri Jan 15 11:04:24 2021 -0500

    Ensure values for Select CFs are in the defined list
    
    The values to which Custom Fields of type 'Select' are set should
    be constrained to the list of defined values. While the Web interface
    provided for this, the REST2 interface did not.
    
    The ValidateValue method is implemented on the CustomField object
    to ensure that values are valid for Select custom fields regardless
    of how they are set.
    
    Previously ValidateValue was already called in _AddCustomFieldValue,
    executing the auto-created DBIx::SearchBuilder method, which
    always passed by default. Since the call was already there, no
    additional call to ValidateValue needs to be added to activate
    this new validation.

diff --git a/lib/RT/CustomField.pm b/lib/RT/CustomField.pm
index f36213f9e2..ea97c33be8 100644
--- a/lib/RT/CustomField.pm
+++ b/lib/RT/CustomField.pm
@@ -672,6 +672,38 @@ sub DeleteValue {
     return ($retval, $self->loc("Custom field value deleted"));
 }
 
+=head2 ValidateValue Value
+
+Make sure that the supplied value is valid
+
+=cut
+
+sub ValidateValue {
+    my $self = shift;
+    my $value = shift;
+
+    # NB: ensuring that the value (including possibly an empty one)
+    #     matches any validation pattern defined is already checked in
+    #     AddValueForObject and DeleteValueForObject
+
+    # For Select type custom fields, make sure the supplied value is
+    # in the list of defined values.
+    # Skip for external CFs since they could be more dynamic,
+    # depending on the external source.
+
+    if ( $self->Type eq "Select" && !$self->IsExternalValues() ) {
+        if ( $value ) {
+            my $cfvs = $self->Values;
+            while (my $cfv = $cfvs->Next) {
+                my $name = $cfv->Name;
+                return 1 if $name eq $value;
+            }
+            return 0;
+        }
+    }
+
+    return 1;
+}
 
 =head2 ValidateQueue Queue
 

commit 6beb64574ebcce0bd483c4c439f330e60925e17e
Author: Jim Brandt <jbrandt at bestpractical.com>
Date:   Mon Feb 15 12:10:46 2021 -0500

    Set values for select CFs used in tests

diff --git a/t/api/action-createtickets.t b/t/api/action-createtickets.t
index ceed4231cc..2114426bad 100644
--- a/t/api/action-createtickets.t
+++ b/t/api/action-createtickets.t
@@ -2,7 +2,7 @@
 use strict;
 use warnings;
 use RT;
-use RT::Test tests => 54;
+use RT::Test tests => undef;
 
 
 {
@@ -25,6 +25,8 @@ my ($id, $msg)=  $global_cf->Create( Name => 'GlobalCF',
                                  Type=> 'SelectSingle');
 ok($id, 'Global custom field correctly created');
 
+($id,$msg) = $global_cf->AddValue(Name => 'A Value');
+ok($id,$msg);
 
 my $approvalsq = RT::Queue->new(RT->SystemUser);
 $approvalsq->Create(Name => 'Approvals');
@@ -40,7 +42,8 @@ my $queue_cf = RT::CustomField->new($RT::SystemUser);
 );
 ok($id, 'Queue-specific custom field correctly created');
 
-
+($id,$msg) = $queue_cf->AddValue(Name => 'Another Value');
+ok($id,$msg);
 
 my $approvals = 
 '===Create-Ticket: approval
@@ -266,3 +269,4 @@ foreach my $id ( sort keys %expected ) {
 
 }
 
+done_testing();

commit f43da86a39ff63ea04479e71069e1123f19bbb77
Author: Jim Brandt <jbrandt at bestpractical.com>
Date:   Mon Feb 15 12:11:33 2021 -0500

    Refine case-insensitive test for Select CF to use HasEntry
    
    The Validation update in 40f8cd4d5f no longer allows values
    to be added to a Select CF if the case is inconsistent.
    Refine the existing test to show the new validation and
    also show that HasEntry retains the previous
    case-insensitive check for backward compatibility.

diff --git a/t/customfields/repeated_values.t b/t/customfields/repeated_values.t
index 584512c7d2..8eccb9c432 100644
--- a/t/customfields/repeated_values.t
+++ b/t/customfields/repeated_values.t
@@ -82,13 +82,17 @@ my ( $ret, $msg );
     is( $ret, $ocfv->id, "got the same previous object" );
     is( $ticket->FirstCustomFieldValue($select_single), 'foo', 'value is still foo' );
 
-    diag "select values are case insensitive";
+    diag "HasEntry for select values is case insensitive";
 
     ( $ret, $msg ) =
       $ticket->AddCustomFieldValue( Field => $select_single, Value => 'FOO' );
-    is( $ret, $ocfv->id, "got the same previous object" );
+    is( $ret, 0, "Adding a new value is not case-insensitive" );
     is( $ticket->FirstCustomFieldValue($select_single), 'foo', 'value is still foo' );
 
+    my $select_values = $select_single->ValuesForObject($ticket);
+    my $entry = $select_values->HasEntry('FOO');
+    is( $entry->id, $ocfv->id, 'HasEntry is case insensitive' );
+
     ($ret, $msg) = $ticket->AddCustomFieldValue( Field => $select_single, Value => 'bar' );
     ok($ret, $msg);
     isnt( $ret, $ocfv->id, "got a new value" );

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


More information about the rt-commit mailing list