[Rt-commit] rt branch, 4.2/xxxgroup-ticketsql, created. rt-4.1.8-414-g8db8889

Ruslan Zakirov ruz at bestpractical.com
Fri May 24 03:53:40 EDT 2013


The branch, 4.2/xxxgroup-ticketsql has been created
        at  8db8889d7e996390218a368bc39358e5e3eb4a8b (commit)

- Log -----------------------------------------------------------------
commit 01aadc7952480e1827126ce43b09a724ec4a74c9
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Thu May 23 18:04:18 2013 +0400

    improve *Group TicketSQL (WatcherGroup, CCGroup, ...)
    
    * add OwnerGroup
    * support name in addition to id, name means user defined group
    * make it clear that only '=' op is supported
    * simplify code by using existing API
    * add tests

diff --git a/lib/RT/Tickets.pm b/lib/RT/Tickets.pm
index df0a4c5..f85576e 100644
--- a/lib/RT/Tickets.pm
+++ b/lib/RT/Tickets.pm
@@ -150,6 +150,7 @@ our %FIELD_METADATA = (
     CustomField      => [ 'CUSTOMFIELD', ], #loc_left_pair
     CF               => [ 'CUSTOMFIELD', ], #loc_left_pair
     Updated          => [ 'TRANSDATE', ], #loc_left_pair
+    OwnerGroup       => [ 'MEMBERSHIPFIELD' => 'Owner', ], #loc_left_pair
     RequestorGroup   => [ 'MEMBERSHIPFIELD' => 'Requestor', ], #loc_left_pair
     CCGroup          => [ 'MEMBERSHIPFIELD' => 'Cc', ], #loc_left_pair
     AdminCCGroup     => [ 'MEMBERSHIPFIELD' => 'AdminCc', ], #loc_left_pair
@@ -952,90 +953,49 @@ LIMIT 25
 =cut
 
 sub _WatcherMembershipLimit {
-    my ( $self, $field, $op, $value, @rest ) = @_;
-    my %rest = @rest;
-
-    $self->_OpenParen;
-
-    my $groups       = $self->NewAlias('Groups');
-    my $groupmembers = $self->NewAlias('CachedGroupMembers');
-    my $users        = $self->NewAlias('Users');
-    my $memberships  = $self->NewAlias('CachedGroupMembers');
-
-    $self->Limit(
-        ALIAS    => $memberships,
-        FIELD    => 'GroupId',
-        VALUE    => $value,
-        OPERATOR => $op,
-        @rest,
-    );
+    my ( $self, $field, $op, $value, %rest ) = @_;
 
-    # Tie to groups for tickets we care about
-    $self->Limit(
-        ALIAS           => $groups,
-        FIELD           => 'Domain',
-        VALUE           => 'RT::Ticket-Role',
-        ENTRYAGGREGATOR => 'AND'
-    );
+    # we don't support anything but '='
+    die "Invalid $field Op: $op"
+        unless $op =~ /^=$/;
 
-    $self->Join(
-        ALIAS1 => $groups,
-        FIELD1 => 'Instance',
-        ALIAS2 => 'main',
-        FIELD2 => 'id'
-    );
-
-    # }}}
+    unless ( $value =~ /^\d+$/ ) {
+        my $group = RT::Group->new( $self->CurrentUser );
+        $group->LoadUserDefinedGroup( $value );
+        $value = $group->id || 0;
+    }
 
-    # If we care about which sort of watcher
     my $meta = $FIELD_METADATA{$field};
-    my $type = ( defined $meta->[1] ? $meta->[1] : undef );
+    my $type = $meta->[1] || '';
 
-    if ($type) {
-        $self->Limit(
-            ALIAS           => $groups,
-            FIELD           => 'Type',
-            VALUE           => $type,
-            ENTRYAGGREGATOR => 'AND'
-        );
+    my ($members_alias, $members_column);
+    if ( $type eq 'Owner' ) {
+        ($members_alias, $members_column) = ('main', 'Owner');
+    } else {
+        (undef, undef, $members_alias) = $self->_WatcherJoin( New => 1, Type => $type );
+        $members_column = 'id';
     }
 
-    $self->Join(
-        ALIAS1 => $groups,
-        FIELD1 => 'id',
-        ALIAS2 => $groupmembers,
-        FIELD2 => 'GroupId'
-    );
-
-    $self->Join(
-        ALIAS1 => $groupmembers,
-        FIELD1 => 'MemberId',
-        ALIAS2 => $users,
-        FIELD2 => 'id'
+    my $cgm_alias = $self->Join(
+        ALIAS1          => $members_alias,
+        FIELD1          => $members_column,
+        TABLE2          => 'CachedGroupMembers',
+        FIELD2          => 'MemberId',
     );
-
     $self->Limit(
-        ALIAS => $groupmembers,
+        LEFTJOIN => $cgm_alias,
+        ALIAS => $cgm_alias,
         FIELD => 'Disabled',
         VALUE => 0,
     );
 
-    $self->Join(
-        ALIAS1 => $memberships,
-        FIELD1 => 'MemberId',
-        ALIAS2 => $users,
-        FIELD2 => 'id'
-    );
-
     $self->Limit(
-        ALIAS => $memberships,
-        FIELD => 'Disabled',
-        VALUE => 0,
+        ALIAS    => $cgm_alias,
+        FIELD    => 'GroupId',
+        VALUE    => $value,
+        OPERATOR => $op,
+        %rest,
     );
-
-
-    $self->_CloseParen;
-
 }
 
 =head2 _CustomFieldDecipher
diff --git a/t/ticket/search_by_watcher_group.t b/t/ticket/search_by_watcher_group.t
new file mode 100644
index 0000000..103e6e4
--- /dev/null
+++ b/t/ticket/search_by_watcher_group.t
@@ -0,0 +1,73 @@
+use strict;
+use warnings;
+
+use RT::Test tests => undef;
+
+my $q = RT::Test->load_or_create_queue( Name => 'General' );
+ok $q && $q->id, 'loaded or created queue';
+my $queue = $q->Name;
+
+
+my $group;
+{
+    $group = RT::Group->new( RT->SystemUser );
+    my ($id, $msg) = $group->CreateUserDefinedGroup( Name => 'Test' );
+    ok $id, "$msg";
+}
+
+my $root = RT::Test->load_or_create_user( Name => 'root', MemberOf => $group->id );
+ok $root && $root->id;
+
+RT::Test->create_tickets(
+    { Queue => $q, },
+    { Subject => '-', },
+    { Subject => 'o', Owner => $root->id },
+    { Subject => 'r', Requestor => $root->id },
+    { Subject => 'c', Cc => $root->id },
+    { Subject => 'a', AdminCc => $root->id },
+);
+
+run_tests(
+    'OwnerGroup = "Test"' => { '-' => 0, o => 1, r => 0, c => 0, a => 0 },
+    'RequestorGroup = "Test"' => { '-' => 0, o => 0, r => 1, c => 0, a => 0 },
+    'CCGroup = "Test"' => { '-' => 0, o => 0, r => 0, c => 1, a => 0 },
+    'AdminCCGroup = "Test"' => { '-' => 0, o => 0, r => 0, c => 0, a => 1 },
+    'WatcherGroup = "Test"' => { '-' => 0, o => 1, r => 1, c => 1, a => 1 },
+);
+
+{
+    my $tickets = RT::Tickets->new( RT->SystemUser );
+    my ($status, $msg) = $tickets->FromSQL('OwnerGroup != "Test"');
+    ok !$status, "incorrect op: $msg";
+}
+
+done_testing();
+
+sub run_tests {
+    my @test = @_;
+    while ( my ($query, $checks) = splice @test, 0, 2 ) {
+        run_test( $query, %$checks );
+    }
+}
+
+sub run_test {
+    my ($query, %checks) = @_;
+
+    my $tix = RT::Tickets->new(RT->SystemUser);
+    $tix->FromSQL($query);
+    my $error = 0;
+
+    my $count = 0;
+    $count++ foreach grep $_, values %checks;
+    is($tix->Count, $count, "found correct number of ticket(s) by '$query'") or $error = 1;
+
+    my $good_tickets = ($tix->Count == $count);
+    while ( my $ticket = $tix->Next ) {
+        next if $checks{ $ticket->Subject };
+        diag $ticket->Subject ." ticket has been found when it's not expected";
+        $good_tickets = 0;
+    }
+    ok( $good_tickets, "all tickets are good with '$query'" ) or $error = 1;
+
+    diag "Wrong SQL query for '$query':". $tix->BuildSelectQuery if $error;
+}

commit 8db8889d7e996390218a368bc39358e5e3eb4a8b
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Fri May 24 11:31:49 2013 +0400

    show *Group TicketSQL in the query builder

diff --git a/share/html/Search/Elements/PickBasics b/share/html/Search/Elements/PickBasics
index 61ffc2e..a58ddb4 100644
--- a/share/html/Search/Elements/PickBasics
+++ b/share/html/Search/Elements/PickBasics
@@ -141,6 +141,19 @@ my @lines = (
         Value => { Type => 'text', Size => 20 }
     },
     {
+        Name => 'WatcherGroup',
+        Field => {
+            Type => 'component',
+            Path => 'SelectPersonType',
+            Arguments => { Default => 'Owner', Suffix => 'Group' },
+        },
+        Op => {
+            Type => 'select',
+            Options => [ '=' => loc('is') ],
+        },
+        Value => { Type => 'text', Size => 20 }
+    },
+    {
         Name => 'Date',
         Field => {
             Type => 'component',
diff --git a/share/html/Search/Elements/SelectPersonType b/share/html/Search/Elements/SelectPersonType
index 70df7ba..74fb619 100644
--- a/share/html/Search/Elements/SelectPersonType
+++ b/share/html/Search/Elements/SelectPersonType
@@ -51,7 +51,7 @@
 % }
 % for my $option (@types) {
 %  if ($Suffix) {
-<option value="<% $option %><% $Suffix %>"<%$option eq $Default && qq[ selected="selected"] |n %> ><%loc($option)%></option>
+<option value="<% $option %><% $Suffix %>"<%$option eq $Default && qq[ selected="selected"] |n %> ><% loc($option) %> <% loc('Group') %></option>
 %   next;
 %  }
 %  foreach my $subtype (@subtypes) {
@@ -66,7 +66,7 @@ if ($Scope =~ /queue/) {
    @types = qw(Cc AdminCc);
 }
 elsif ($Suffix eq 'Group') {
-   @types = qw(Requestor Cc AdminCc Watcher);
+   @types = qw(Owner Requestor Cc AdminCc Watcher);
 }
 else { 
    @types = qw(Requestor Cc AdminCc Watcher Owner QueueCc QueueAdminCc QueueWatcher);

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


More information about the Rt-commit mailing list