[Rt-commit] r11674 - in rt/branches/3.6-RELEASE/lib: t/regression
ruz at bestpractical.com
ruz at bestpractical.com
Thu Apr 10 16:33:51 EDT 2008
Author: ruz
Date: Thu Apr 10 16:33:50 2008
New Revision: 11674
Added:
rt/branches/3.6-RELEASE/lib/t/regression/20-sort-by-queue.t
rt/branches/3.6-RELEASE/lib/t/regression/20-sort-by-user.t
Modified:
rt/branches/3.6-RELEASE/lib/RT/Tickets_Overlay.pm
Log:
* fix sorting by Queue, Owner, Creator and LastUpdatedBy
Modified: rt/branches/3.6-RELEASE/lib/RT/Tickets_Overlay.pm
==============================================================================
--- rt/branches/3.6-RELEASE/lib/RT/Tickets_Overlay.pm (original)
+++ rt/branches/3.6-RELEASE/lib/RT/Tickets_Overlay.pm Thu Apr 10 16:33:50 2008
@@ -1512,10 +1512,43 @@
my $order = 0;
foreach my $row (@args) {
- if ( $row->{ALIAS} || $row->{FIELD} !~ /\./ ) {
+ if ( $row->{ALIAS} ) {
push @res, $row;
next;
}
+ if ( $row->{FIELD} !~ /\./ ) {
+ my $meta = $self->FIELDS->{ $row->{FIELD} };
+ unless ( $meta ) {
+ push @res, $row;
+ next;
+ }
+
+ if ( $meta->[0] eq 'ENUM' && ($meta->[1]||'') eq 'Queue' ) {
+ my $alias = $self->Join(
+ TYPE => 'LEFT',
+ ALIAS1 => 'main',
+ FIELD1 => $row->{'FIELD'},
+ TABLE2 => 'Queues',
+ FIELD2 => 'id',
+ );
+ push @res, { %$row, ALIAS => $alias, FIELD => "Name" };
+ } elsif ( ( $meta->[0] eq 'ENUM' && ($meta->[1]||'') eq 'User' )
+ || ( $meta->[0] eq 'WATCHERFIELD' && ($meta->[1]||'') eq 'Owner' )
+ ) {
+ my $alias = $self->Join(
+ TYPE => 'LEFT',
+ ALIAS1 => 'main',
+ FIELD1 => $row->{'FIELD'},
+ TABLE2 => 'Users',
+ FIELD2 => 'id',
+ );
+ push @res, { %$row, ALIAS => $alias, FIELD => "Name" };
+ } else {
+ push @res, $row;
+ }
+ next;
+ }
+
my ( $field, $subkey ) = split /\./, $row->{FIELD}, 2;
my $meta = $self->FIELDS->{$field};
if ( $meta->[0] eq 'WATCHERFIELD' ) {
Added: rt/branches/3.6-RELEASE/lib/t/regression/20-sort-by-queue.t
==============================================================================
--- (empty file)
+++ rt/branches/3.6-RELEASE/lib/t/regression/20-sort-by-queue.t Thu Apr 10 16:33:50 2008
@@ -0,0 +1,103 @@
+#!/usr/bin/perl
+
+use Test::More tests => 57;
+use RT;
+RT::LoadConfig();
+RT::Init();
+
+use strict;
+use warnings;
+
+use RT::Tickets;
+use RT::Queue;
+use RT::CustomField;
+
+#########################################################
+# Test sorting by Queue, we sort by its name
+#########################################################
+
+
+diag "Create queues to test with.";
+my @qids;
+my @queues;
+# create them in reverse order to avoid false positives
+foreach my $name ( qw(sort-by-queue-Z sort-by-queue-A) ) {
+ my $queue = RT::Queue->new( $RT::SystemUser );
+ my ($ret, $msg) = $queue->Create(
+ Name => $name ."-$$",
+ Description => 'queue to test sorting by queue'
+ );
+ ok($ret, "test queue creation. $msg");
+ push @queues, $queue;
+ push @qids, $queue->id;
+}
+
+my ($total, @data, @tickets, @test) = (0, ());
+
+sub add_tix_from_data {
+ my @res = ();
+ @data = sort { rand(100) <=> rand(100) } @data;
+ while (@data) {
+ my $t = RT::Ticket->new($RT::SystemUser);
+ my %args = %{ shift(@data) };
+ my ( $id, undef, $msg ) = $t->Create( %args );
+ ok( $id, "ticket created" ) or diag("error: $msg");
+ push @res, $t;
+ $total++;
+ }
+ return @res;
+}
+
+sub run_tests {
+ my $query_prefix = join ' OR ', map 'id = '. $_->id, @tickets;
+ foreach my $test ( @test ) {
+ my $query = join " AND ", map "( $_ )", grep defined && length,
+ $query_prefix, $test->{'Query'};
+
+ foreach my $order (qw(ASC DESC)) {
+ my $error = 0;
+ my $tix = RT::Tickets->new( $RT::SystemUser );
+ $tix->FromSQL( $query );
+ $tix->OrderBy( FIELD => $test->{'Order'}, ORDER => $order );
+
+ ok($tix->Count, "found ticket(s)")
+ or $error = 1;
+
+ my ($order_ok, $last) = (1, $order eq 'ASC'? '-': 'zzzzzz');
+ while ( my $t = $tix->Next ) {
+ my $tmp;
+ if ( $order eq 'ASC' ) {
+ $tmp = ((split( /,/, $last))[0] cmp (split( /,/, $t->Subject))[0]);
+ } else {
+ $tmp = -((split( /,/, $last))[-1] cmp (split( /,/, $t->Subject))[-1]);
+ }
+ if ( $tmp > 0 ) {
+ $order_ok = 0; last;
+ }
+ $last = $t->Subject;
+ }
+
+ ok( $order_ok, "$order order of tickets is good" )
+ or $error = 1;
+
+ if ( $error ) {
+ diag "Wrong SQL query:". $tix->BuildSelectQuery;
+ $tix->GotoFirstItem;
+ while ( my $t = $tix->Next ) {
+ diag sprintf "%02d - %s", $t->id, $t->Subject;
+ }
+ }
+ }
+ }
+}
+
+ at data = (
+ { Queue => $qids[0], Subject => 'z' },
+ { Queue => $qids[1], Subject => 'a' },
+);
+ at tickets = add_tix_from_data();
+ at test = (
+ { Order => "Queue" },
+);
+run_tests();
+
Added: rt/branches/3.6-RELEASE/lib/t/regression/20-sort-by-user.t
==============================================================================
--- (empty file)
+++ rt/branches/3.6-RELEASE/lib/t/regression/20-sort-by-user.t Thu Apr 10 16:33:50 2008
@@ -0,0 +1,155 @@
+#!/usr/bin/perl
+
+use Test::More tests => 32;
+use RT;
+RT::LoadConfig();
+RT::Init();
+
+use strict;
+use warnings;
+
+use RT::Tickets;
+use RT::Queue;
+use RT::CustomField;
+
+#########################################################
+# Test sorting by Owner, Creator and LastUpdatedBy
+# we sort by user name
+#########################################################
+
+diag "Create a queue to test with.";
+my $queue_name = "OwnerSortQueue$$";
+my $queue;
+{
+ $queue = RT::Queue->new( $RT::SystemUser );
+ my ($ret, $msg) = $queue->Create(
+ Name => $queue,
+ Description => 'queue for custom field sort testing'
+ );
+ ok($ret, "$queue test queue creation. $msg");
+}
+
+my @uids;
+my @users;
+# create them in reverse order to avoid false positives
+foreach my $u (qw(Z A)) {
+ my $name = $u ."-user-to-test-ordering-$$";
+ my $user = RT::User->new( $RT::SystemUser );
+ my ($uid) = $user->Create(
+ Name => $name,
+ Privileged => 1,
+ );
+ ok $uid, "created user #$uid";
+
+ my ($status, $msg) = $user->PrincipalObj->GrantRight( Right => 'OwnTicket', Object => $queue );
+ ok $status, "granted right";
+ ($status, $msg) = $user->PrincipalObj->GrantRight( Right => 'CreateTicket', Object => $queue );
+ ok $status, "granted right";
+
+ push @users, $user;
+ push @uids, $user->id;
+}
+
+my ($total, @data, @tickets, @test) = (0, ());
+
+sub add_tix_from_data {
+ my @res = ();
+ @data = sort { rand(100) <=> rand(100) } @data;
+ while (@data) {
+ my $t = RT::Ticket->new($RT::SystemUser);
+ my %args = %{ shift(@data) };
+
+ my ( $id, undef, $msg ) = $t->Create( %args, Queue => $queue->id );
+ if ( $args{'Owner'} ) {
+ is $t->Owner, $args{'Owner'}, "owner is correct";
+ }
+ if ( $args{'Creator'} ) {
+ is $t->Creator, $args{'Creator'}, "creator is correct";
+ }
+ # hackish, but simpler
+ if ( $args{'LastUpdatedBy'} ) {
+ $t->__Set( Field => 'LastUpdatedBy', Value => $args{'LastUpdatedBy'} );
+ }
+ ok( $id, "ticket created" ) or diag("error: $msg");
+ push @res, $t;
+ $total++;
+ }
+ return @res;
+}
+
+sub run_tests {
+ my $query_prefix = join ' OR ', map 'id = '. $_->id, @tickets;
+ foreach my $test ( @test ) {
+ my $query = join " AND ", map "( $_ )", grep defined && length,
+ $query_prefix, $test->{'Query'};
+
+ foreach my $order (qw(ASC DESC)) {
+ my $error = 0;
+ my $tix = RT::Tickets->new( $RT::SystemUser );
+ $tix->FromSQL( $query );
+ $tix->OrderBy( FIELD => $test->{'Order'}, ORDER => $order );
+
+ ok($tix->Count, "found ticket(s)")
+ or $error = 1;
+
+ my ($order_ok, $last) = (1, $order eq 'ASC'? '-': 'zzzzzz');
+ while ( my $t = $tix->Next ) {
+ my $tmp;
+ if ( $order eq 'ASC' ) {
+ $tmp = ((split( /,/, $last))[0] cmp (split( /,/, $t->Subject))[0]);
+ } else {
+ $tmp = -((split( /,/, $last))[-1] cmp (split( /,/, $t->Subject))[-1]);
+ }
+ if ( $tmp > 0 ) {
+ $order_ok = 0; last;
+ }
+ $last = $t->Subject;
+ }
+
+ ok( $order_ok, "$order order of tickets is good" )
+ or $error = 1;
+
+ if ( $error ) {
+ diag "Wrong SQL query:". $tix->BuildSelectQuery;
+ $tix->GotoFirstItem;
+ while ( my $t = $tix->Next ) {
+ diag sprintf "%02d - %s", $t->id, $t->Subject;
+ }
+ }
+ }
+ }
+}
+
+ at data = (
+ { Subject => 'Nobody' },
+ { Subject => 'Z', Owner => $uids[0] },
+ { Subject => 'A', Owner => $uids[1] },
+);
+ at tickets = add_tix_from_data();
+ at test = (
+ { Order => "Owner" },
+);
+run_tests();
+
+ at data = (
+ { Subject => 'RT' },
+ { Subject => 'Z', Creator => $uids[0] },
+ { Subject => 'A', Creator => $uids[1] },
+);
+ at tickets = add_tix_from_data();
+ at test = (
+ { Order => "Creator" },
+);
+run_tests();
+
+ at data = (
+ { Subject => 'RT' },
+ { Subject => 'Z', LastUpdatedBy => $uids[0] },
+ { Subject => 'A', LastUpdatedBy => $uids[1] },
+);
+ at tickets = add_tix_from_data();
+ at test = (
+ { Order => "LastUpdatedBy" },
+);
+run_tests();
+
More information about the Rt-commit
mailing list