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

Jim Brandt jbrandt at bestpractical.com
Mon Feb 15 12:13:23 EST 2021


The branch, 4.2/validate-value-for-select-cf-2 has been created
        at  e403a8aaaf5ad8b40787f627c85c2069d4acfc45 (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 e403a8aaaf5ad8b40787f627c85c2069d4acfc45
Author: Jim Brandt <jbrandt at bestpractical.com>
Date:   Mon Feb 15 12:11:33 2021 -0500

    Evaluate Select CFs case-insensitively
    
    This aligns with existing behavior for Select CFs.

diff --git a/lib/RT/CustomField.pm b/lib/RT/CustomField.pm
index ea97c33be8..ca21562ddf 100644
--- a/lib/RT/CustomField.pm
+++ b/lib/RT/CustomField.pm
@@ -696,7 +696,10 @@ sub ValidateValue {
             my $cfvs = $self->Values;
             while (my $cfv = $cfvs->Next) {
                 my $name = $cfv->Name;
-                return 1 if $name eq $value;
+
+                # Select is case-insensitive
+                # See RT::ObjectCustomFieldValues::HasEntry
+                return 1 if lc $name eq lc $value;
             }
             return 0;
         }

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


More information about the rt-commit mailing list