[Bps-public-commit] rt-extension-notificationmatrix branch, master, updated. 034e405625f2ad28d500029086c8098379d33042

Chia-liang Kao clkao at bestpractical.com
Sun Jun 20 11:47:47 EDT 2010


The branch, master has been updated
       via  034e405625f2ad28d500029086c8098379d33042 (commit)
      from  9e08cdf55e47b592658f77ad7badcf3f4126113f (commit)

Summary of changes:
 html/Admin/Queues/NotificationMatrix.html     |    1 +
 lib/RT/Extension/NotificationMatrix.pm        |    8 ++
 lib/RT/Extension/NotificationMatrix/Notify.pm |   50 +++++++++-
 t/basic.t                                     |  133 +++++++++++++++++++++++++
 4 files changed, 189 insertions(+), 3 deletions(-)
 create mode 100644 t/basic.t

- Log -----------------------------------------------------------------
commit 034e405625f2ad28d500029086c8098379d33042
Author: Chia-liang Kao <clkao at clkao.org>
Date:   Sun Jun 20 23:48:43 2010 +0800

    basic test and rule for ticketcreated condition.

diff --git a/html/Admin/Queues/NotificationMatrix.html b/html/Admin/Queues/NotificationMatrix.html
index 5d0a770..4dbcc28 100644
--- a/html/Admin/Queues/NotificationMatrix.html
+++ b/html/Admin/Queues/NotificationMatrix.html
@@ -86,6 +86,7 @@ if ($ARGS{SelectUserGroup}) {
 
 if ($ARGS{SetMatrix}) {
     for my $action (@actions) {
+        # XXX: move this to some api and do permission check
         $matrix->{$action} = [ map { s/NM-\Q$action\E-// ? $_ : () } keys %ARGS ];
     }
     $QueueObj->SetAttribute(Name => 'NotificationMatrix', Description => 'Notification Matrix Internal Data', Content => $matrix);
diff --git a/lib/RT/Extension/NotificationMatrix.pm b/lib/RT/Extension/NotificationMatrix.pm
index 0b09da9..424b13a 100644
--- a/lib/RT/Extension/NotificationMatrix.pm
+++ b/lib/RT/Extension/NotificationMatrix.pm
@@ -10,4 +10,12 @@ RT::Ruleset->Add(
         'RT::Extension::NotificationMatrix::Notify',
     ]);
 
+sub get_queue_matrix {
+    my ($self, $queue) = @_;
+
+    my $attr = $queue->FirstAttribute('NotificationMatrix');
+
+    $attr ? $attr->Content : {};
+}
+
 1;
diff --git a/lib/RT/Extension/NotificationMatrix/Notify.pm b/lib/RT/Extension/NotificationMatrix/Notify.pm
index 6d1c893..2cec51d 100644
--- a/lib/RT/Extension/NotificationMatrix/Notify.pm
+++ b/lib/RT/Extension/NotificationMatrix/Notify.pm
@@ -1,15 +1,59 @@
-package RT::Extension::WF::AccountRequest::Approval;
+package  RT::Extension::NotificationMatrix::Notify;
 use strict;
 use warnings;
-
+use List::MoreUtils qw(uniq);
+use RT::Action::SendEmail;
 use base 'RT::Rule';
 
 sub Prepare {
+    my $self = shift;
+
+    my $q = $self->TicketObj->QueueObj;
+    my $matrix = RT::Extension::NotificationMatrix->get_queue_matrix($q);
+
+    if (my $t = $matrix->{TicketCreated}) {
+        my @recipients = uniq map {
+            my $g = RT::Group->new($self->CurrentUser);
+            $g->Load($_);
+            if ($g->Domain eq 'RT::Queue-Role') {
+                $g->LoadTicketRoleGroup( Ticket => $self->TicketObj->Id, Type => $g->Type);
+            }
+            $g->MemberEmailAddresses
+        } @$t;
+
+        if (@recipients) {
+            my $template = RT::Template->new($self->CurrentUser);
+            $template->Load('Transaction') or die;
+            # RT::Action weakens the following, so we need to keep additional references
+            my $ref = [RT::Scrip->new($self->CurrentUser),
+                       { _Message_ID => 0},
+                       $template];
+            my $email = RT::Action::SendEmail->new ( Argument => undef,
+                                                     CurrentUser => $self->CurrentUser,
+                                                     ScripObj => $ref->[0],
+                                                     ScripActionObj => $ref->[1],
+                                                     TemplateObj => $ref->[2],
+                                                     TicketObj => $self->TicketObj,
+                                                     TransactionObj => $self->TransactionObj,
+                                                 );
+            $email->{To} = \@recipients;
+            $email->Prepare;
+            $self->{__ref} = $ref;
+            $self->{__email} = $email;
+            return 1;
+        }
+
+    }
+
     return 0;
 }
 
 sub Commit {
-    
+    my $self = shift;
+    if ($self->{__email}) {
+        $self->{__email}->Commit;
+    }
+
 }
 
 1;
diff --git a/t/basic.t b/t/basic.t
new file mode 100644
index 0000000..f645e50
--- /dev/null
+++ b/t/basic.t
@@ -0,0 +1,133 @@
+#!/usr/bin/perl -w
+use strict;
+use warnings;
+use Test::More;
+
+BEGIN {
+    $ENV{LC_ALL} = $ENV{LANG} = 'en_US.UTF-8';
+    eval { require Email::Abstract; require Test::Email; 1 }
+        or plan skip_all => 'require Email::Abstract and Test::Email';
+}
+
+use RT;
+use RT::Test tests => 6;
+use RT::Test::Email;
+RT->Config->Set( LogToScreen => 'debug' );
+RT->Config->Set('Plugins',qw(RT::Extension::NotificationMatrix));
+
+use_ok('RT::Extension::NotificationMatrix');
+
+# we need to lie to RT and have it find our templates 
+# in the local directory
+{ no warnings 'redefine';
+  use Cwd 'abs_path';
+
+  my $orig_base = \&RT::Plugin::_BasePath;
+  *RT::Plugin::_BasePath = sub {
+      my $self = $_[0];
+      my $base = $self->{'name'};
+      $base =~ s'::'/'g;
+      my $lib = abs_path($INC{"$base.pm"});
+      $lib =~ s{\Qlib/$base.pm}{} ? $lib : goto $orig_base;
+  };
+}
+
+my %users;
+for my $user_name (qw(user_a user_b user_c)) {
+    my $user = $users{$user_name} = RT::User->new($RT::SystemUser);
+    $user->Create( Name => uc($user_name),
+                   Privileged => 1,
+                   EmailAddress => $user_name.'@company.com');
+}
+
+my $q = RT::Queue->new($RT::SystemUser);
+$q->Load('General');
+
+my %groups;
+
+{
+my $group_obj = RT::Group->new($RT::SystemUser);
+my ($ret, $msg) = $group_obj->CreateUserDefinedGroup
+    ( Name => 'GroupA',
+      Description => 'GroupA');
+($ret, $msg) = $group_obj->AddMember($users{$_}->PrincipalObj->Id())
+    for qw(user_a user_b);
+$groups{group_a} = $group_obj;
+
+$group_obj = RT::Group->new($RT::SystemUser);
+($ret, $msg) = $group_obj->CreateUserDefinedGroup
+    ( Name => 'GroupB',
+      Description => 'GroupB');
+($ret, $msg) = $group_obj->AddMember($users{$_}->PrincipalObj->Id())
+    for qw(user_b user_c);
+$groups{group_b} = $group_obj;
+
+$group_obj->LoadSystemInternalGroup('Privileged');
+$group_obj->PrincipalObj->GrantRight(Object => $q, Right => $_)
+    for (qw(OwnTicket ModifyTicket ShowTicket showticketcomments));
+
+$group_obj->LoadSystemInternalGroup('Everyone');
+$group_obj->PrincipalObj->GrantRight(Object => $q, Right => $_)
+    for (qw(CreateTicket));
+
+}
+
+# remove all existing notification
+my $scrips = RT::Scrips->new($RT::SystemUser);
+$scrips->LimitToGlobal;
+while (my $sc = $scrips->Next) {
+    $sc->Delete;
+}
+
+my ($tid, $ttrans, $tmsg);
+my $cu = RT::CurrentUser->new;
+$cu->Load( $users{user_a} );
+
+my $t = RT::Ticket->new($cu);
+
+mail_ok {
+    ($tid, $ttrans, $tmsg) =
+        $t->Create(Subject => "a test",
+                   Owner => "user_a", Requestor => 'user_b',
+                   Queue => $q->Id,
+                   AdminCc => 'user_c',
+               );
+    ok($tid);
+};
+
+my $Groups = RT::Groups->new($RT::SystemUser);
+$Groups->LimitToRolesForQueue($q->Id);
+
+my @groups = @{ $Groups->ItemsArrayRef };
+
+my $owners = RT::Group->new($RT::SystemUser);
+$owners->LoadQueueRoleGroup(Queue => $q->Id, Type => 'Owner');
+
+my $matrix = { TicketCreated => [ $owners->id, $groups{group_a}->id ] };
+
+$q->SetAttribute(Name => 'NotificationMatrix',
+                 Description => 'Notification Matrix Internal Data',
+                 Content => $matrix);
+
+
+
+mail_ok {
+    ($tid, $ttrans, $tmsg) =
+        $t->Create(Subject => "a test",
+                   Owner => "user_a", Requestor => 'user_b',
+                   Queue => $q->Id,
+                   AdminCc => 'user_c',
+               );
+    ok($tid);
+} { from => qr'USER_A via RT',
+    to => 'user_a at company.com, user_b at company.com',
+    subject => qr/a test/,
+    body => qr/Transaction: Ticket created by USER_A/,
+};
+
+#my ($baseurl, $m) = RT::Test->started_ok;
+
+#diag "$baseurl/?user=root&pass=password"; sleep 1 while 1;
+
+1;
+

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



More information about the Bps-public-commit mailing list