import rt 3.8.10
[freeside.git] / rt / lib / t / regression / 24pawsort.t
1 #!/usr/bin/perl
2
3 use Test::More qw/no_plan/;
4 use RT;
5 RT::LoadConfig();
6 RT::Init();
7
8 use strict;
9 use warnings;
10
11 use RT::Tickets;
12 use RT::Queue;
13 use RT::CustomField;
14
15 my($ret,$msg);
16
17 # Test Paw Sort
18
19
20
21 # ---- Create a queue to test with.
22 my $queue = "PAWSortQueue-$$";
23 my $queue_obj = RT::Queue->new($RT::SystemUser);
24 ($ret, $msg) = $queue_obj->Create(Name => $queue,
25                                   Description => 'queue for custom field sort testing');
26 ok($ret, "$queue test queue creation. $msg");
27
28
29 # ---- Create some users
30
31 my $me = RT::User->new($RT::SystemUser);
32 ($ret, $msg) = $me->Create(Name => "Me$$", EmailAddress => $$.'create-me-1@example.com');
33 ($ret, $msg) = $me->PrincipalObj->GrantRight(Object =>$queue_obj, Right => 'OwnTicket');
34 ($ret, $msg) = $me->PrincipalObj->GrantRight(Object =>$queue_obj, Right => 'SeeQueue');
35 ($ret, $msg) = $me->PrincipalObj->GrantRight(Object =>$queue_obj, Right => 'ShowTicket');
36 my $you = RT::User->new($RT::SystemUser);
37 ($ret, $msg) = $you->Create(Name => "You$$", EmailAddress => $$.'create-you-1@example.com');
38 ($ret, $msg) = $you->PrincipalObj->GrantRight(Object =>$queue_obj, Right => 'OwnTicket');
39 ($ret, $msg) = $you->PrincipalObj->GrantRight(Object =>$queue_obj, Right => 'SeeQueue');
40 ($ret, $msg) = $you->PrincipalObj->GrantRight(Object =>$queue_obj, Right => 'ShowTicket');
41
42 my $nobody = RT::User->new($RT::SystemUser);
43 $nobody->Load('nobody');
44
45
46 # ----- Create some tickets to test with.  Assign them some values to
47 # make it easy to sort with.
48
49 my @tickets = (
50                [qw[1 10], $me],
51                [qw[2 20], $me],
52                [qw[3 20], $you],
53                [qw[4 30], $you],
54                [qw[5  5], $nobody],
55                [qw[6 55], $nobody],
56               );
57 for (@tickets) {
58   my $t = RT::Ticket->new($RT::SystemUser);
59   $t->Create( Queue => $queue_obj->Id,
60               Subject => $_->[0],
61               Owner => $_->[2]->Id,
62               Priority => $_->[1],
63             );
64 }
65
66 sub check_order {
67   my ($tx, @order) = @_;
68   my @results;
69   while (my $t = $tx->Next) {
70     push @results, $t->Subject;
71   }
72   my $results = join (" ",@results);
73   my $order = join(" ",@order);
74   is( $results, $order );
75 }
76
77
78 # The real tests start here
79
80 my $cme = new RT::CurrentUser( $me );
81 my $metx = new RT::Tickets( $cme );
82 # Make sure we can sort in both directions on a queue specific field.
83 $metx->FromSQL(qq[queue="$queue"] );
84 $metx->OrderBy( FIELD => "Custom.Ownership", ORDER => 'ASC' );
85 is($metx->Count,6);
86 check_order( $metx, qw[2 1 6 5 4 3]);
87
88 $metx->OrderBy( FIELD => "Custom.Ownership", ORDER => 'DESC' );
89 is($metx->Count,6);
90 check_order( $metx, reverse qw[2 1 6 5 4 3]);
91
92
93
94 my $cyou = new RT::CurrentUser( $you );
95 my $youtx = new RT::Tickets( $cyou );
96 # Make sure we can sort in both directions on a queue specific field.
97 $youtx->FromSQL(qq[queue="$queue"] );
98 $youtx->OrderBy( FIELD => "Custom.Ownership", ORDER => 'ASC' );
99 is($youtx->Count,6);
100 check_order( $youtx, qw[4 3 6 5 2 1]);
101
102 __END__
103
104