summaryrefslogtreecommitdiff
path: root/rt/lib/t/regression/24pawsort.t
blob: 665c325a6b034c084118fa82fd8e61007fd4a786 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/perl

use Test::More qw/no_plan/;
use RT;
RT::LoadConfig();
RT::Init();

use strict;
use warnings;

use RT::Tickets;
use RT::Queue;
use RT::CustomField;

my($ret,$msg);

# Test Paw Sort



# ---- Create a queue to test with.
my $queue = "PAWSortQueue-$$";
my $queue_obj = RT::Queue->new($RT::SystemUser);
($ret, $msg) = $queue_obj->Create(Name => $queue,
                                  Description => 'queue for custom field sort testing');
ok($ret, "$queue test queue creation. $msg");


# ---- Create some users

my $me = RT::User->new($RT::SystemUser);
($ret, $msg) = $me->Create(Name => "Me$$", EmailAddress => $$.'create-me-1@example.com');
($ret, $msg) = $me->PrincipalObj->GrantRight(Object =>$queue_obj, Right => 'OwnTicket');
($ret, $msg) = $me->PrincipalObj->GrantRight(Object =>$queue_obj, Right => 'SeeQueue');
($ret, $msg) = $me->PrincipalObj->GrantRight(Object =>$queue_obj, Right => 'ShowTicket');
my $you = RT::User->new($RT::SystemUser);
($ret, $msg) = $you->Create(Name => "You$$", EmailAddress => $$.'create-you-1@example.com');
($ret, $msg) = $you->PrincipalObj->GrantRight(Object =>$queue_obj, Right => 'OwnTicket');
($ret, $msg) = $you->PrincipalObj->GrantRight(Object =>$queue_obj, Right => 'SeeQueue');
($ret, $msg) = $you->PrincipalObj->GrantRight(Object =>$queue_obj, Right => 'ShowTicket');

my $nobody = RT::User->new($RT::SystemUser);
$nobody->Load('nobody');


# ----- Create some tickets to test with.  Assign them some values to
# make it easy to sort with.

my @tickets = (
               [qw[1 10], $me],
               [qw[2 20], $me],
               [qw[3 20], $you],
               [qw[4 30], $you],
               [qw[5  5], $nobody],
               [qw[6 55], $nobody],
              );
for (@tickets) {
  my $t = RT::Ticket->new($RT::SystemUser);
  $t->Create( Queue => $queue_obj->Id,
              Subject => $_->[0],
              Owner => $_->[2]->Id,
              Priority => $_->[1],
            );
}

sub check_order {
  my ($tx, @order) = @_;
  my @results;
  while (my $t = $tx->Next) {
    push @results, $t->Subject;
  }
  my $results = join (" ",@results);
  my $order = join(" ",@order);
  is( $results, $order );
}


# The real tests start here

my $cme = new RT::CurrentUser( $me );
my $metx = new RT::Tickets( $cme );
# Make sure we can sort in both directions on a queue specific field.
$metx->FromSQL(qq[queue="$queue"] );
$metx->OrderBy( FIELD => "Custom.Ownership", ORDER => 'ASC' );
is($metx->Count,6);
check_order( $metx, qw[2 1 6 5 4 3]);

$metx->OrderBy( FIELD => "Custom.Ownership", ORDER => 'DESC' );
is($metx->Count,6);
check_order( $metx, reverse qw[2 1 6 5 4 3]);



my $cyou = new RT::CurrentUser( $you );
my $youtx = new RT::Tickets( $cyou );
# Make sure we can sort in both directions on a queue specific field.
$youtx->FromSQL(qq[queue="$queue"] );
$youtx->OrderBy( FIELD => "Custom.Ownership", ORDER => 'ASC' );
is($youtx->Count,6);
check_order( $youtx, qw[4 3 6 5 2 1]);

__END__