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