summaryrefslogtreecommitdiff
path: root/rt/lib/t/regression/23cfsort.t
blob: ba404f2ba33aedd46db10b7e392eb6c9c0736e74 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/perl

use Test::More tests => 21;
use RT;
RT::LoadConfig();
RT::Init();

use strict;
use warnings;

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

my($ret,$msg);


# Test Sorting by custom fields.
# TODO: it's hard to read this file, conver to new style,
# for example look at 23cfsort-freeform-single.t

# ---- Create a queue to test with.
my $queue = "CFSortQueue-$$";
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 custom fields.  We're not currently using all of
# them to test with, but the more the merrier.
my $cfO = RT::CustomField->new($RT::SystemUser);
my $cfA = RT::CustomField->new($RT::SystemUser);
my $cfB = RT::CustomField->new($RT::SystemUser);
my $cfC = RT::CustomField->new($RT::SystemUser);

($ret, $msg) = $cfO->Create( Name => 'Order',
                             Queue => 0,
                             SortOrder => 1,
                             Description => q{Something to compare results for, since we can't guarantee ticket ID},
                             Type=> 'FreeformSingle');
ok($ret, "Custom Field Order created");

($ret, $msg) = $cfA->Create( Name => 'Alpha',
                             Queue => $queue_obj->id,
                             SortOrder => 1,
                             Description => 'A Testing custom field',
                             Type=> 'FreeformSingle');
ok($ret, "Custom Field Alpha created");

($ret, $msg) = $cfB->Create( Name => 'Beta',
                             Queue => $queue_obj->id,
                             Description => 'A Testing custom field',
                             Type=> 'FreeformSingle');
ok($ret, "Custom Field Beta created");

($ret, $msg) = $cfC->Create( Name => 'Charlie',
                             Queue => $queue_obj->id,
                             Description => 'A Testing custom field',
                             Type=> 'FreeformSingle');
ok($ret, "Custom Field Charlie created");

# ----- Create some tickets to test with.  Assign them some values to
# make it easy to sort with.
my $t1 = RT::Ticket->new($RT::SystemUser);
$t1->Create( Queue => $queue_obj->Id,
             Subject => 'One',
           );
$t1->AddCustomFieldValue(Field => $cfO->Id,  Value => '1');
$t1->AddCustomFieldValue(Field => $cfA->Id,  Value => '2');
$t1->AddCustomFieldValue(Field => $cfB->Id,  Value => '1');
$t1->AddCustomFieldValue(Field => $cfC->Id,  Value => 'BBB');

my $t2 = RT::Ticket->new($RT::SystemUser);
$t2->Create( Queue => $queue_obj->Id,
             Subject => 'Two',
           );
$t2->AddCustomFieldValue(Field => $cfO->Id,  Value => '2');
$t2->AddCustomFieldValue(Field => $cfA->Id,  Value => '1');
$t2->AddCustomFieldValue(Field => $cfB->Id,  Value => '2');
$t2->AddCustomFieldValue(Field => $cfC->Id,  Value => 'AAA');

# helper
sub check_order {
  my ($tx, @order) = @_;
  my @results;
  while (my $t = $tx->Next) {
    push @results, $t->CustomFieldValues($cfO->Id)->First->Content;
  }
  my $results = join (" ",@results);
  my $order = join(" ",@order);
  @_ = ($results, $order , "Ordered correctly: $order");
  goto \&is;
}

# The real tests start here
my $tx = new RT::Tickets( $RT::SystemUser );


# Make sure we can sort in both directions on a queue specific field.
$tx->FromSQL(qq[queue="$queue"] );
$tx->OrderBy( FIELD => "CF.${queue}.{Charlie}", ORDER => 'DES' );
is($tx->Count,2 ,"We found 2 tickets when lookign for cf charlie");
check_order( $tx, 1, 2);

$tx = new RT::Tickets( $RT::SystemUser );
$tx->FromSQL(qq[queue="$queue"] );
$tx->OrderBy( FIELD => "CF.${queue}.{Charlie}", ORDER => 'ASC' );
is($tx->Count,2, "We found two tickets when sorting by cf charlie without limiting to it" );
check_order( $tx, 2, 1);

# When ordering by _global_ CustomFields, if more than one queue has a
# CF named Charlie, things will go bad.  So, these results are uniqued
# in Tickets_Overlay.
$tx = new RT::Tickets( $RT::SystemUser );
$tx->FromSQL(qq[queue="$queue"] );
$tx->OrderBy( FIELD => "CF.{Charlie}", ORDER => 'DESC' );
diag $tx->BuildSelectQuery;
is($tx->Count,2);
check_order( $tx, 1, 2);

$tx = new RT::Tickets( $RT::SystemUser );
$tx->FromSQL(qq[queue="$queue"] );
$tx->OrderBy( FIELD => "CF.{Charlie}", ORDER => 'ASC' );
diag $tx->BuildSelectQuery;
is($tx->Count,2);
check_order( $tx, 2, 1);

# Add a new ticket, to test sorting on multiple columns.
my $t3 = RT::Ticket->new($RT::SystemUser);
$t3->Create( Queue => $queue_obj->Id,
             Subject => 'Three',
           );
$t3->AddCustomFieldValue(Field => $cfO->Id,  Value => '3');
$t3->AddCustomFieldValue(Field => $cfA->Id,  Value => '3');
$t3->AddCustomFieldValue(Field => $cfB->Id,  Value => '2');
$t3->AddCustomFieldValue(Field => $cfC->Id,  Value => 'AAA');

$tx = new RT::Tickets( $RT::SystemUser );
$tx->FromSQL(qq[queue="$queue"] );
$tx->OrderByCols(
    { FIELD => "CF.${queue}.{Charlie}", ORDER => 'ASC' },
    { FIELD => "CF.${queue}.{Alpha}",   ORDER => 'DES' },
);
is($tx->Count,3);
check_order( $tx, 3, 2, 1);

$tx = new RT::Tickets( $RT::SystemUser );
$tx->FromSQL(qq[queue="$queue"] );
$tx->OrderByCols(
    { FIELD => "CF.${queue}.{Charlie}", ORDER => 'DES' },
    { FIELD => "CF.${queue}.{Alpha}",   ORDER => 'ASC' },
);
is($tx->Count,3);
check_order( $tx, 1, 2, 3);

# Reverse the order of the secondary column, which changes the order
# of the first two tickets.
$tx = new RT::Tickets( $RT::SystemUser );
$tx->FromSQL(qq[queue="$queue"] );
$tx->OrderByCols(
    { FIELD => "CF.${queue}.{Charlie}", ORDER => 'ASC' },
    { FIELD => "CF.${queue}.{Alpha}",   ORDER => 'ASC' },
);
is($tx->Count,3);
check_order( $tx, 2, 3, 1);

$tx = new RT::Tickets( $RT::SystemUser );
$tx->FromSQL(qq[queue="$queue"] );
$tx->OrderByCols(
    { FIELD => "CF.${queue}.{Charlie}", ORDER => 'DES' },
    { FIELD => "CF.${queue}.{Alpha}",   ORDER => 'DES' },
);
is($tx->Count,3);
check_order( $tx, 1, 3, 2);