[Rt-commit] r2378 - in rt/branches/PLATANO-EXPERIMENTAL: . lib/RT lib/t/regression

jesse at bestpractical.com jesse at bestpractical.com
Mon Mar 14 02:33:15 EST 2005


Author: jesse
Date: Mon Mar 14 02:33:15 2005
New Revision: 2378

Added:
   rt/branches/PLATANO-EXPERIMENTAL/lib/t/regression/16-transaction_cf_tests.t
Modified:
   rt/branches/PLATANO-EXPERIMENTAL/   (props changed)
   rt/branches/PLATANO-EXPERIMENTAL/lib/RT/Transaction_Overlay.pm
Log:
 r8482 at hualien:  jesse | 2005-03-14 02:27:29 -0500
  r5747 at hualien:  jesse | 2005-02-22 16:33:09 -0500
  Better transaction UpdateCustomFields API
  
 


Modified: rt/branches/PLATANO-EXPERIMENTAL/lib/RT/Transaction_Overlay.pm
==============================================================================
--- rt/branches/PLATANO-EXPERIMENTAL/lib/RT/Transaction_Overlay.pm	(original)
+++ rt/branches/PLATANO-EXPERIMENTAL/lib/RT/Transaction_Overlay.pm	Mon Mar 14 02:33:15 2005
@@ -919,18 +919,44 @@
     return $self->loc($type);
 }
 
+=head2 UpdateCustomFields
+    
+    Takes a hash of 
+
+    CustomField-<<Id>> => Value
+        or 
+
+    Object-RT::Transaction-CustomField-<<Id>> => Value parameters to update
+    this transaction's custom fields
+
+=cut
+
 sub UpdateCustomFields {
     my $self = shift;
-    my (%args) = @_;
+    my %args = (@_);
 
-    my $args_ref = $args{ARGSRef} or return;
+    # This method used to have an API that took a hash of a single
+    # value "ARGSRef", which was a reference to a hash of arguments.
+    # This was insane. The next few lines of code preserve that API
+    # while giving us something saner.
+       
+
+    # TODO: 3.6: DEPRECATE OLD API
+
+    my $args; 
+
+    if ($args{'ARGSRef'}) { 
+        $args = $args{ARGSRef};
+    } else {
+        $args = \%args;
+    }
 
-    foreach my $arg ( keys %$args_ref ) {
+    foreach my $arg ( keys %$args ) {
         next
           unless ( $arg =~
-            /^(?:Object-RT::Transaction--)?CustomField-(\d+).*?(?<!-Magic)$/ );
+            /^(?:Object-RT::Transaction--)?CustomField-(\d+)/ );
         my $cfid   = $1;
-        my $values = $args_ref->{$arg};
+        my $values = $args->{$arg};
         foreach
           my $value ( UNIVERSAL::isa( $values, 'ARRAY' ) ? @$values : $values )
         {

Added: rt/branches/PLATANO-EXPERIMENTAL/lib/t/regression/16-transaction_cf_tests.t
==============================================================================
--- (empty file)
+++ rt/branches/PLATANO-EXPERIMENTAL/lib/t/regression/16-transaction_cf_tests.t	Mon Mar 14 02:33:15 2005
@@ -0,0 +1,61 @@
+#!/usr/bin/perl
+
+use warnings;
+use strict;
+use Data::Dumper;
+use Test::More qw/no_plan/;
+
+use_ok('RT');
+use_ok('RT::Transactions');
+RT::LoadConfig();
+RT::Init();
+
+my $q = RT::Queue->new($RT::SystemUser);
+my ($id,$msg) = $q->Create( Name => 'TxnCFTest'.$$);
+ok($id,$msg);
+
+my $cf = RT::CustomField->new($RT::SystemUser);
+($id,$msg) = $cf->Create(Name => 'Txnfreeform-'.$$, Type => 'Freeform', MaxValues => '0', LookupType => RT::Transaction->_LookupTypes );
+
+ok($id,$msg);
+
+($id,$msg) = $cf->AddToObject($q);
+
+ok($id,$msg);
+
+
+my $ticket = RT::Ticket->new($RT::SystemUser);
+
+my $transid;
+($id,$transid, $msg) = $ticket->Create(Queue => $q->id,
+                Subject => 'TxnCF test',
+            );
+ok($id,$msg);
+
+my $trans = RT::Transaction->new($RT::SystemUser);
+$trans->Load($transid);
+
+is($trans->ObjectId,$id);
+is ($trans->ObjectType, 'RT::Ticket');
+is ($trans->Type, 'Create');
+my $txncfs = $trans->CustomFields;
+is ($txncfs->Count, 1, "We have one custom field");
+my $txn_cf = $txncfs->First;
+is ($txn_cf->id, $cf->id, "It's the right custom field");
+my $values = $trans->CustomFieldValues($txn_cf->id);
+is ($values->Count, 0, "It has no values");
+
+# Old API
+my %cf_updates = ( 'CustomField-'.$cf->id => 'Testing');
+$trans->UpdateCustomFields( ARGSRef => \%cf_updates);
+
+ $values = $trans->CustomFieldValues($txn_cf->id);
+is ($values->Count, 1, "It has one value");
+
+# New API
+
+$trans->UpdateCustomFields( 'CustomField-'.$cf->id => 'Test two');
+ $values = $trans->CustomFieldValues($txn_cf->id);
+is ($values->Count, 2, "it has two values");
+
+# TODO ok(0, "Should updating custom field values remove old values?");


More information about the Rt-commit mailing list