[Rt-commit] r12791 - in rt/branches/3.8-TESTING: .

jesse at bestpractical.com jesse at bestpractical.com
Mon Jun 2 11:23:01 EDT 2008


Author: jesse
Date: Mon Jun  2 11:22:59 2008
New Revision: 12791

Added:
   rt/branches/3.8-TESTING/t/ticket/add-watchers.t
   rt/branches/3.8-TESTING/t/ticket/cfsort-freeform-multiple.t
   rt/branches/3.8-TESTING/t/ticket/cfsort-freeform-single.t
   rt/branches/3.8-TESTING/t/ticket/sort-by-queue.t
   rt/branches/3.8-TESTING/t/ticket/sort-by-user.t
Modified:
   rt/branches/3.8-TESTING/   (props changed)

Log:
 r32098 at 31b:  jesse | 2008-06-02 11:19:38 -0400
 * Test files from 3.6 that were merged by hand and not checked in


Added: rt/branches/3.8-TESTING/t/ticket/add-watchers.t
==============================================================================
--- (empty file)
+++ rt/branches/3.8-TESTING/t/ticket/add-watchers.t	Mon Jun  2 11:22:59 2008
@@ -0,0 +1,157 @@
+#!/usr/bin/perl -w
+# BEGIN BPS TAGGED BLOCK {{{
+# 
+# COPYRIGHT:
+#  
+# This software is Copyright (c) 1996-2005 Best Practical Solutions, LLC 
+#                                          <jesse.com>
+# 
+# (Except where explicitly superseded by other copyright notices)
+# 
+# 
+# LICENSE:
+# 
+# This work is made available to you under the terms of Version 2 of
+# the GNU General Public License. A copy of that license should have
+# been provided with this software, but in any event can be snarfed
+# from www.gnu.org.
+# 
+# This work is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+# 
+# 
+# CONTRIBUTION SUBMISSION POLICY:
+# 
+# (The following paragraph is not intended to limit the rights granted
+# to you to modify and distribute this software under the terms of
+# the GNU General Public License and is only of importance to you if
+# you choose to contribute your changes and enhancements to the
+# community by submitting them to Best Practical Solutions, LLC.)
+# 
+# By intentionally submitting any modifications, corrections or
+# derivatives to this work, or any other work intended for use with
+# Request Tracker, to Best Practical Solutions, LLC, you confirm that
+# you are the copyright holder for those contributions and you grant
+# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
+# royalty-free, perpetual, license to use, copy, create derivative
+# works based on those contributions, and sublicense and distribute
+# those contributions and any derivatives thereof.
+# 
+# END BPS TAGGED BLOCK }}}
+
+use Test::More tests => 28;
+use RT::Test;
+
+use strict;
+use warnings;
+no warnings 'once';
+
+use RT::Queue;
+use RT::User;
+use RT::Group;
+use RT::Ticket;
+use RT::CurrentUser;
+
+
+# clear all global right
+my $acl = RT::ACL->new($RT::SystemUser);
+$acl->Limit( FIELD => 'RightName', OPERATOR => '!=', VALUE => 'SuperUser' );
+$acl->LimitToObject( $RT::System );
+while( my $ace = $acl->Next ) {
+	$ace->Delete;
+}
+
+# create new queue to be sure we do not mess with rights
+my $queue = RT::Queue->new($RT::SystemUser);
+my ($queue_id) = $queue->Create( Name => 'watcher tests '.$$);
+ok( $queue_id, 'queue created for watcher tests' );
+
+# new privileged user to check rights
+my $user = RT::User->new( $RT::SystemUser );
+my ($user_id) = $user->Create( Name => 'watcher'.$$,
+			   EmailAddress => "watcher$$".'@localhost',
+			   Privileged => 1,
+			   Password => 'qwe123',
+			 );
+my $cu= RT::CurrentUser->new($user);
+
+# make sure user can see tickets in the queue
+my $principal = $user->PrincipalObj;
+ok( $principal, "principal loaded" );
+$principal->GrantRight( Right => 'ShowTicket', Object => $queue );
+$principal->GrantRight( Right => 'SeeQueue'  , Object => $queue );
+
+ok(  $user->HasRight( Right => 'SeeQueue',     Object => $queue ), "user can see queue" );
+ok(  $user->HasRight( Right => 'ShowTicket',   Object => $queue ), "user can show queue tickets" );
+ok( !$user->HasRight( Right => 'ModifyTicket', Object => $queue ), "user can't modify queue tickets" );
+ok( !$user->HasRight( Right => 'Watch',        Object => $queue ), "user can't watch queue tickets" );
+
+my $ticket = RT::Ticket->new( $RT::SystemUser );
+my ($rv, $msg) = $ticket->Create( Subject => 'watcher tests', Queue => $queue->Name );
+ok( $ticket->id, "ticket created" );
+
+my $ticket2 = RT::Ticket->new( $cu );
+$ticket2->Load( $ticket->id );
+ok( $ticket2->Subject, "ticket load by user" );
+
+# user can add self to ticket only after getting Watch right
+($rv, $msg) = $ticket2->AddWatcher( Type => 'Cc', PrincipalId => $user->PrincipalId );
+ok( !$rv, "user can't add self as Cc" );
+($rv, $msg) = $ticket2->AddWatcher( Type => 'Requestor', PrincipalId => $user->PrincipalId );
+ok( !$rv, "user can't add self as Requestor" );
+$principal->GrantRight( Right => 'Watch'  , Object => $queue );
+ok(  $user->HasRight( Right => 'Watch',        Object => $queue ), "user can watch queue tickets" );
+($rv, $msg) = $ticket2->AddWatcher( Type => 'Cc', PrincipalId => $user->PrincipalId );
+ok(  $rv, "user can add self as Cc by PrincipalId" );
+($rv, $msg) = $ticket2->AddWatcher( Type => 'Requestor', PrincipalId => $user->PrincipalId );
+ok(  $rv, "user can add self as Requestor by PrincipalId" );
+
+# remove user and try adding with Email address
+($rv, $msg) = $ticket->DeleteWatcher( Type => 'Cc',        PrincipalId => $user->PrincipalId );
+ok( $rv, "watcher removed by PrincipalId" );
+($rv, $msg) = $ticket->DeleteWatcher( Type => 'Requestor', Email => $user->EmailAddress );
+ok( $rv, "watcher removed by Email" );
+
+($rv, $msg) = $ticket2->AddWatcher( Type => 'Cc', Email => $user->EmailAddress );
+ok(  $rv, "user can add self as Cc by Email" );
+($rv, $msg) = $ticket2->AddWatcher( Type => 'Requestor', Email => $user->EmailAddress );
+ok(  $rv, "user can add self as Requestor by Email" );
+
+# Queue watcher tests
+$principal->RevokeRight( Right => 'Watch'  , Object => $queue );
+ok( !$user->HasRight( Right => 'Watch',        Object => $queue ), "user queue watch right revoked" );
+
+my $queue2 = RT::Queue->new( $cu );
+($rv, $msg) = $queue2->Load( $queue->id );
+ok( $rv, "user loaded queue" );
+
+# user can add self to queue only after getting Watch right
+($rv, $msg) = $queue2->AddWatcher( Type => 'Cc', PrincipalId => $user->PrincipalId );
+ok( !$rv, "user can't add self as Cc" );
+($rv, $msg) = $queue2->AddWatcher( Type => 'Requestor', PrincipalId => $user->PrincipalId );
+ok( !$rv, "user can't add self as Requestor" );
+$principal->GrantRight( Right => 'Watch'  , Object => $queue );
+ok(  $user->HasRight( Right => 'Watch',        Object => $queue ), "user can watch queue queues" );
+($rv, $msg) = $queue2->AddWatcher( Type => 'Cc', PrincipalId => $user->PrincipalId );
+ok(  $rv, "user can add self as Cc by PrincipalId" );
+($rv, $msg) = $queue2->AddWatcher( Type => 'Requestor', PrincipalId => $user->PrincipalId );
+ok(  $rv, "user can add self as Requestor by PrincipalId" );
+
+# remove user and try adding with Email address
+($rv, $msg) = $queue->DeleteWatcher( Type => 'Cc',        PrincipalId => $user->PrincipalId );
+ok( $rv, "watcher removed by PrincipalId" );
+($rv, $msg) = $queue->DeleteWatcher( Type => 'Requestor', Email => $user->EmailAddress );
+ok( $rv, "watcher removed by Email" );
+
+($rv, $msg) = $queue2->AddWatcher( Type => 'Cc', Email => $user->EmailAddress );
+ok(  $rv, "user can add self as Cc by Email" );
+($rv, $msg) = $queue2->AddWatcher( Type => 'Requestor', Email => $user->EmailAddress );
+ok(  $rv, "user can add self as Requestor by Email" );
+
+

Added: rt/branches/3.8-TESTING/t/ticket/cfsort-freeform-multiple.t
==============================================================================
--- (empty file)
+++ rt/branches/3.8-TESTING/t/ticket/cfsort-freeform-multiple.t	Mon Jun  2 11:22:59 2008
@@ -0,0 +1,135 @@
+#!/usr/bin/perl
+
+use Test::More tests => 24;
+use RT::Test;
+
+use strict;
+use warnings;
+
+use RT::Tickets;
+use RT::Queue;
+use RT::CustomField;
+
+# Test Sorting by custom fields.
+
+diag "Create a queue to test with.";
+my $queue_name = "CFSortQueue-$$";
+my $queue;
+{
+    $queue = RT::Queue->new( $RT::SystemUser );
+    my ($ret, $msg) = $queue->Create(
+        Name => $queue_name,
+        Description => 'queue for custom field sort testing'
+    );
+    ok($ret, "$queue_name - test queue creation. $msg");
+}
+
+diag "create a CF\n";
+my $cf_name = "Order$$";
+my $cf;
+{
+    $cf = RT::CustomField->new( $RT::SystemUser );
+    my ($ret, $msg) = $cf->Create(
+        Name  => $cf_name,
+        Queue => $queue->id,
+        Type  => 'FreeformMultiple',
+    );
+    ok($ret, "Custom Field Order created");
+}
+
+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 @values = ();
+        if ( exists $args{'CF'} && ref $args{'CF'} ) {
+            @values = @{ delete $args{'CF'} };
+        } elsif ( exists $args{'CF'} ) {
+            @values = (delete $args{'CF'});
+        }
+        $args{ 'CustomField-'. $cf->id } = \@values
+            if @values;
+        my $subject = join(",", sort @values) || '-';
+        my ( $id, undef $msg ) = $t->Create(
+            %args,
+            Queue => $queue->id,
+            Subject => $subject,
+        );
+        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 = (
+    { },
+    { CF => ['b', 'd'] },
+    { CF => ['a', 'c'] },
+);
+ at tickets = add_tix_from_data();
+ at test = (
+    { Order => "CF.{$cf_name}" },
+    { Order => "CF.$queue_name.{$cf_name}" },
+);
+run_tests();
+
+ at data = (
+    { CF => ['m', 'a'] },
+    { CF => ['m'] },
+    { CF => ['m', 'o'] },
+);
+ at tickets = add_tix_from_data();
+ at test = (
+    { Order => "CF.{$cf_name}", Query => "CF.{$cf_name} = 'm'" },
+    { Order => "CF.$queue_name.{$cf_name}", Query => "CF.{$cf_name} = 'm'" },
+);
+run_tests();
+

Added: rt/branches/3.8-TESTING/t/ticket/cfsort-freeform-single.t
==============================================================================
--- (empty file)
+++ rt/branches/3.8-TESTING/t/ticket/cfsort-freeform-single.t	Mon Jun  2 11:22:59 2008
@@ -0,0 +1,189 @@
+#!/usr/bin/perl
+
+use Test::More tests => 57;
+use RT::Test;
+
+use strict;
+use warnings;
+
+use RT::Tickets;
+use RT::Queue;
+use RT::CustomField;
+
+# Test Sorting by FreeformSingle custom field.
+
+diag "Create a queue to test with.";
+my $queue_name = "CFSortQueue$$";
+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");
+}
+
+# CFs for testing, later we create another one
+my %CF;
+my $cf_name;
+
+diag "create a CF\n";
+{
+    $cf_name = $CF{'CF'}{'name'} = "Order$$";
+    $CF{'CF'}{'obj'} = RT::CustomField->new( $RT::SystemUser );
+    my ($ret, $msg) = $CF{'CF'}{'obj'}->Create(
+        Name  => $CF{'CF'}{'name'},
+        Queue => $queue->id,
+        Type  => 'FreeformSingle',
+    );
+    ok($ret, "Custom Field $CF{'CF'}{'name'} created");
+}
+
+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 $subject = '-';
+        foreach my $e ( grep exists $CF{$_} && defined $CF{$_}, keys %args ) {
+            my @values = ();
+            if ( ref $args{ $e } ) {
+                @values = @{ delete $args{ $e } };
+            } else {
+                @values = (delete $args{ $e });
+            }
+            $args{ 'CustomField-'. $CF{ $e }{'obj'}->id } = \@values
+                if @values;
+            $subject = join(",", sort @values) || '-'
+                if $e eq 'CF';
+        }
+
+        my ( $id, undef $msg ) = $t->Create(
+            %args,
+            Queue => $queue->id,
+            Subject => $subject,
+        );
+        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 = (
+    { },
+    { CF => 'a' },
+    { CF => 'b' },
+);
+ at tickets = add_tix_from_data();
+ at test = (
+    { Order => "CF.{$cf_name}" },
+    { Order => "CF.$queue_name.{$cf_name}" },
+);
+run_tests();
+
+ at data = (
+    { },
+    { CF => 'aa' },
+    { CF => 'ab' },
+);
+ at tickets = add_tix_from_data();
+ at test = (
+    { Query => "CF.{$cf_name} LIKE 'a'", Order => "CF.{$cf_name}" },
+    { Query => "CF.{$cf_name} LIKE 'a'", Order => "CF.$queue_name.{$cf_name}" },
+);
+run_tests();
+
+ at data = (
+    { Subject => '-', },
+    { Subject => 'a', CF => 'a' },
+    { Subject => 'b', CF => 'b' },
+    { Subject => 'c', CF => 'c' },
+);
+ at tickets = add_tix_from_data();
+ at test = (
+    { Query => "CF.{$cf_name} != 'c'", Order => "CF.{$cf_name}" },
+    { Query => "CF.{$cf_name} != 'c'", Order => "CF.$queue_name.{$cf_name}" },
+);
+run_tests();
+
+
+
+diag "create another CF\n";
+{
+    $CF{'AnotherCF'}{'name'} = "OrderAnother$$";
+    $CF{'AnotherCF'}{'obj'} = RT::CustomField->new( $RT::SystemUser );
+    my ($ret, $msg) = $CF{'AnotherCF'}{'obj'}->Create(
+        Name  => $CF{'AnotherCF'}{'name'},
+        Queue => $queue->id,
+        Type  => 'FreeformSingle',
+    );
+    ok($ret, "Custom Field $CF{'AnotherCF'}{'name'} created");
+}
+
+# test that order is not affect by other fields (had such problem)
+ at data = (
+    { Subject => '-', },
+    { Subject => 'a', CF => 'a', AnotherCF => 'za' },
+    { Subject => 'b', CF => 'b', AnotherCF => 'ya' },
+    { Subject => 'c', CF => 'c', AnotherCF => 'xa' },
+);
+ at tickets = add_tix_from_data();
+ at test = (
+    { Order => "CF.{$cf_name}" },
+    { Order => "CF.$queue_name.{$cf_name}" },
+    { Query => "CF.{$cf_name} != 'c'", Order => "CF.{$cf_name}" },
+    { Query => "CF.{$cf_name} != 'c'", Order => "CF.$queue_name.{$cf_name}" },
+);
+run_tests();
+
+
+

Added: rt/branches/3.8-TESTING/t/ticket/sort-by-queue.t
==============================================================================
--- (empty file)
+++ rt/branches/3.8-TESTING/t/ticket/sort-by-queue.t	Mon Jun  2 11:22:59 2008
@@ -0,0 +1,101 @@
+#!/usr/bin/perl
+
+use Test::More tests => 8;
+use RT::Test;
+
+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.8-TESTING/t/ticket/sort-by-user.t
==============================================================================
--- (empty file)
+++ rt/branches/3.8-TESTING/t/ticket/sort-by-user.t	Mon Jun  2 11:22:59 2008
@@ -0,0 +1,153 @@
+#!/usr/bin/perl
+
+use Test::More tests => 32;
+use RT::Test;
+
+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