[Rt-commit] r5997 - in rtir/branches/2.1-EXPERIMENTAL: . etc lib/RT/Action lib/RT/Condition t

ruz at bestpractical.com ruz at bestpractical.com
Tue Sep 19 15:47:46 EDT 2006


Author: ruz
Date: Tue Sep 19 15:47:45 2006
New Revision: 5997

Added:
   rtir/branches/2.1-EXPERIMENTAL/lib/RT/Action/RTIR_SetConstituency.pm
   rtir/branches/2.1-EXPERIMENTAL/lib/RT/Condition/RTIR_RequireConstituencyChange.pm
   rtir/branches/2.1-EXPERIMENTAL/t/014-custom-field-constituency.t
Modified:
   rtir/branches/2.1-EXPERIMENTAL/   (props changed)
   rtir/branches/2.1-EXPERIMENTAL/etc/initialdata
   rtir/branches/2.1-EXPERIMENTAL/etc/upgrade/2.1.0/content
   rtir/branches/2.1-EXPERIMENTAL/t/rtir-test.pl

Log:
 r1585 at cubic-pc:  cubic | 2006-09-19 23:57:08 +0400
 Constituency
 * add condition 'Require Constituency Change'
 * add scrip action 'set constituency'
 * add tests
 ** use mailgate utils from RT test suite


Modified: rtir/branches/2.1-EXPERIMENTAL/etc/initialdata
==============================================================================
--- rtir/branches/2.1-EXPERIMENTAL/etc/initialdata	(original)
+++ rtir/branches/2.1-EXPERIMENTAL/etc/initialdata	Tue Sep 19 15:47:45 2006
@@ -402,6 +402,10 @@
        Description => 'Set IP custom field from message content' ,                                            # loc
        ExecModule  => 'RTIR_FindIP',
     },
+    {  Name        => 'RTIR Constituency',    # loc
+       Description => 'Set and cascade Constituency custom field' ,                                            # loc
+       ExecModule  => 'RTIR_SetConstituency',
+    },
 );
 
 @ScripConditions = (
@@ -436,6 +440,11 @@
        ApplicableTransTypes => 'Any',
        ExecModule => 'RTIR_RequireDueChange',
     },
+    {  Name        => 'RTIR Require Constituency Change',    # loc
+       Description => 'The constituency must be changed',                                            # loc
+       ApplicableTransTypes => 'Any',
+       ExecModule => 'RTIR_RequireConstituencyChange',
+    },
 );
 
 @Scrips = (
@@ -596,6 +605,12 @@
        ScripCondition    => 'On Create',
        ScripAction       => 'RTIR parse message for IPs',
        Template          => 'Blank' },
+
+    {  Description       => "SetConstituency",
+       Queue             => ['Incidents', 'Incident Reports', 'Investigations', 'Blocks'],
+       ScripCondition    => 'RTIR Require Constituency Change',
+       ScripAction       => 'RTIR Constituency',
+       Template          => 'Blank' },
 );
 
 # WARNING: If you change content of the templates, don't forget to

Modified: rtir/branches/2.1-EXPERIMENTAL/etc/upgrade/2.1.0/content
==============================================================================
--- rtir/branches/2.1-EXPERIMENTAL/etc/upgrade/2.1.0/content	(original)
+++ rtir/branches/2.1-EXPERIMENTAL/etc/upgrade/2.1.0/content	Tue Sep 19 15:47:45 2006
@@ -1,3 +1,4 @@
+# XXX: Add constituency scrips/conditions/actions
 
 sub get_cf {
     my $name = shift;

Added: rtir/branches/2.1-EXPERIMENTAL/lib/RT/Action/RTIR_SetConstituency.pm
==============================================================================
--- (empty file)
+++ rtir/branches/2.1-EXPERIMENTAL/lib/RT/Action/RTIR_SetConstituency.pm	Tue Sep 19 15:47:45 2006
@@ -0,0 +1,104 @@
+package RT::Action::RTIR_SetConstituency;
+
+use strict;
+use warnings;
+
+use base 'RT::Action::RTIR';
+
+=head2 Prepare
+
+Always run this.
+
+=cut
+
+
+sub Prepare { return 1 }
+
+=head2 Commit
+
+Set the Constituency custom field.
+
+=cut
+
+sub Commit {
+    my $self = shift;
+    my $ticket = $self->TicketObj;
+
+    my $transaction = $self->TransactionObj;
+    if ( $transaction->Type eq 'Create' ) {
+        # on create fetch value from X-RT-Mail-Extension field
+        my $attachments = $transaction->Attachments;
+        $attachments->OrderByCols(
+            { FIELD => 'Created', ORDER => 'ASC' },
+            { FIELD => 'id', ORDER => 'ASC' },
+        );
+        $attachments->Columns( qw(id Parent TransactionId ContentType ContentEncoding Headers Subject Created) );
+        my $attachment = $attachments->First;
+        return 1 unless $attachment;
+
+        my $value = $attachment->GetHeader('X-RT-Mail-Extension');
+        return 1 unless $self->IsValidConstituency( $value );
+
+        my ($status, $msg) = $ticket->AddCustomFieldValue(
+            Field => '_RTIR_Constituency',
+            Value => $value,
+        );
+        return ($status, $msg) unless $status;
+        return 1;
+    }
+
+    my $constituency = $ticket->FirstCustomFieldValue('_RTIR_Constituency');
+    my $actor = $self->CreatorCurrentUser;
+
+    # change owner of child Incident Reports, Investigations, Blocks
+    my $query =  "( Queue = 'Incidents'"
+        ." OR Queue = 'Incident Reports'"
+        ." OR Queue = 'Investigations'"
+        ." OR Queue = 'Blocks'"
+        .")"
+        ." AND ( MemberOf = ". $ticket->Id ." OR HasMember = ". $ticket->Id ." )";
+
+    if ( $constituency ) {
+        $query .= " AND ( CF.{_RTIR_Constituency} != '$constituency' OR CF.{_RTIR_Constituency} IS NULL )";
+    } else {
+        $query .= " AND ( CF.{_RTIR_Constituency} IS NOT NULL )";
+    }
+    my $tickets = new RT::Tickets( $actor );
+    $tickets->FromSQL( $query );
+
+    while ( my $t = $tickets->Next ) {
+        my ($res, $msg) = $t->AddCustomFieldValue(
+            Field => '_RTIR_Constituency',
+            Value => $constituency,
+        );
+        $RT::Logger->info( "Couldn't set CF: $msg" ) unless $res;
+    }
+    return 1;
+}
+
+{ my %constituency;
+
+sub IsValidConstituency {
+    my $self = shift;
+    my $value = shift or return 0;
+    unless ( keys %constituency ) {
+        my $cf = RT::CustomField->new( $RT::SystemUser );
+        $cf->Load('_RTIR_Constituency');
+        unless ( $cf->id ) {
+            $RT::Logger->crit("Couldn't load constituency field");
+            return 0;
+        }
+        %constituency = map { lc $_->Name => 1 } @{ $cf->Values->ItemsArrayRef };
+    }
+    return exists $constituency{ lc $value };
+}
+
+}
+
+eval "require RT::Action::RTIR_SetConstituency_Vendor";
+die $@ if ($@ && $@ !~ qr{^Can't locate RT/Action/RTIR_SetConstituency_Vendor.pm});
+eval "require RT::Action::RTIR_SetConstituency_Local";
+die $@ if ($@ && $@ !~ qr{^Can't locate RT/Action/RTIR_SetConstituency_Local.pm});
+
+1;
+

Added: rtir/branches/2.1-EXPERIMENTAL/lib/RT/Condition/RTIR_RequireConstituencyChange.pm
==============================================================================
--- (empty file)
+++ rtir/branches/2.1-EXPERIMENTAL/lib/RT/Condition/RTIR_RequireConstituencyChange.pm	Tue Sep 19 15:47:45 2006
@@ -0,0 +1,32 @@
+package RT::Condition::RTIR_RequireConstituencyChange;
+
+
+use strict;
+use base 'RT::Condition::RTIR';
+
+
+=head2 IsApplicable
+
+If a child had a Due Date change or changes parents.
+
+=cut
+
+sub IsApplicable {
+    my $self = shift;
+
+    my $type = $self->TransactionObj->Type;
+    return 1 if $type eq 'Create';
+    return 1 if $type eq 'AddLink';
+#    return 1 if $type eq "Set" && $self->TransactionObj->Field eq "Due";
+
+    return 0;
+}
+
+eval "require RT::Condition::RTIR_RequireConstituencyChange_Vendor";
+die $@ if ($@ && $@ !~ qr{^Can't locate RT/Condition/RTIR_RequireConstituencyChange_Vendor.pm});
+eval "require RT::Condition::RTIR_RequireConstituencyChange_Local";
+die $@ if ($@ && $@ !~ qr{^Can't locate RT/Condition/RTIR_RequireConstituencyChange_Local.pm});
+
+1;
+
+

Added: rtir/branches/2.1-EXPERIMENTAL/t/014-custom-field-constituency.t
==============================================================================
--- (empty file)
+++ rtir/branches/2.1-EXPERIMENTAL/t/014-custom-field-constituency.t	Tue Sep 19 15:47:45 2006
@@ -0,0 +1,126 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use Test::More tests => 97;
+
+require "t/rtir-test.pl";
+
+use_ok('RT');
+RT::LoadConfig();
+RT::Init();
+
+use_ok('RT::IR');
+
+my $cf;
+diag "load and check basic properties of the CF" if $ENV{'TEST_VERBOSE'};
+{
+    my $cfs = RT::CustomFields->new( $RT::SystemUser );
+    $cfs->Limit( FIELD => 'Name', VALUE => '_RTIR_Constituency' );
+    is( $cfs->Count, 1, "found one CF with name '_RTIR_Constituency'" );
+
+    $cf = $cfs->First;
+    is( $cf->Type, 'Select', 'type check' );
+    is( $cf->LookupType, 'RT::Queue-RT::Ticket', 'lookup type check' );
+    is( $cf->MaxValues, 1, "single value" );
+    ok( !$cf->Disabled, "not disabled" );
+}
+
+diag "check that CF applies to all RTIR's queues" if $ENV{'TEST_VERBOSE'};
+{
+    foreach ( 'Incidents', 'Incident Reports', 'Investigations', 'Blocks' ) {
+        my $queue = RT::Queue->new( $RT::SystemUser );
+        $queue->Load( $_ );
+        ok( $queue->id, 'loaded queue '. $_ );
+        my $cfs = $queue->TicketCustomFields;
+        $cfs->Limit( FIELD => 'id', VALUE => $cf->id, ENTRYAGGREGATOR => 'AND' );
+        is( $cfs->Count, 1, 'field applies to queue' );
+    }
+}
+
+my $agent = default_agent();
+my $rtir_user = rtir_user();
+
+diag "create a ticket via web and set field" if $ENV{'TEST_VERBOSE'};
+{
+    my $i = 0;
+    my $incident_id; # block couldn't be created without incident id
+    foreach my $queue( 'Incidents', 'Incident Reports', 'Investigations', 'Blocks' ) {
+        diag "create a ticket in the '$queue' queue" if $ENV{'TEST_VERBOSE'};
+
+        my $val = 'GOVNET';
+        my $id = create_rtir_ticket(
+            $agent, $queue,
+            {
+                Subject => "test ip",
+                ($queue eq 'Blocks'? (Incident => $incident_id): ()),
+            },
+            { Constituency => $val },
+        );
+        $incident_id = $id if $queue eq 'Incidents';
+
+        display_ticket($agent, $id);
+        $agent->content_like( qr/\Q$val/, "value on the page" );
+
+        my $ticket = RT::Ticket->new( $RT::SystemUser );
+        $ticket->Load( $id );
+        ok( $ticket->id, 'loaded ticket' );
+        is( $ticket->FirstCustomFieldValue('_RTIR_Constituency'), $val, 'correct value' );
+    }
+}
+
+diag "create a ticket via gate" if $ENV{'TEST_VERBOSE'};
+{
+    my $i = 0;
+    my $incident_id; # block couldn't be created without incident id
+    foreach my $queue( 'Incidents', 'Incident Reports', 'Investigations', 'Blocks' ) {
+        diag "create a ticket in the '$queue' queue" if $ENV{'TEST_VERBOSE'};
+
+        my $text = <<EOF;
+From: @{[ $rtir_user->EmailAddress ]}
+To: rt\@@{[RT->Config->Get('rtname')]}
+Subject: This is a test of constituency functionality
+
+Foob!
+EOF
+        my $val = 'GOVNET';
+        local $ENV{'EXTENSION'} = $val;
+        my ($status, $id) = create_ticket_via_gate($text, queue => $queue);
+        is ($status >> 8, 0, "The mail gateway exited ok");
+        ok ($id, "created ticket $id");
+        $incident_id = $id if $queue eq 'Incidents';
+
+        display_ticket($agent, $id);
+        $agent->content_like( qr/\Q$val/, "value on the page" );
+
+        my $ticket = RT::Ticket->new( $RT::SystemUser );
+        $ticket->Load( $id );
+        ok( $ticket->id, 'loaded ticket' );
+        is( $ticket->QueueObj->Name, $queue, 'correct queue' );
+        is( $ticket->FirstCustomFieldValue('_RTIR_Constituency'), $val, 'correct value' );
+    }
+}
+
+diag "create a ticket via web and set field" if $ENV{'TEST_VERBOSE'};
+{
+    my $i = 0;
+
+    my $val = 'EDUNET';
+    my $ir_id = create_ir(
+        $agent, { Subject => "test" }, { Constituency => $val }
+    );
+    ok( $ir_id, "created IR #$ir_id" );
+    display_ticket($agent, $ir_id);
+    $agent->content_like( qr/\Q$val/, "value on the page" );
+
+    my $inc_id = create_incident_for_ir(
+        $agent, $ir_id, { Subject => "test" },
+    );
+
+    my $ticket = RT::Ticket->new( $RT::SystemUser );
+    $ticket->Load( $inc_id );
+    ok( $ticket->id, 'loaded ticket' );
+    is( $ticket->QueueObj->Name, 'Incidents', 'correct value' );
+    is( $ticket->FirstCustomFieldValue('_RTIR_Constituency'), $val, 'correct value' );
+}
+

Modified: rtir/branches/2.1-EXPERIMENTAL/t/rtir-test.pl
==============================================================================
--- rtir/branches/2.1-EXPERIMENTAL/t/rtir-test.pl	(original)
+++ rtir/branches/2.1-EXPERIMENTAL/t/rtir-test.pl	Tue Sep 19 15:47:45 2006
@@ -19,6 +19,8 @@
 ok(RT::LoadConfig);
 ok(RT::Init, "Basic initialization and DB connectivity");
 
+require $RT::BasePath. '/lib/t/utils.pl';
+
 my $RTIR_TEST_USER = "rtir_test_user";
 my $RTIR_TEST_PASS = "rtir_test_pass";
 


More information about the Rt-commit mailing list