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