import rt 3.8.7
[freeside.git] / rt / t / ticket / requestor-order.t
1 #!/usr/bin/perl -w
2 use strict; use warnings;
3
4 use RT::Test tests => 58;
5 use_ok('RT');
6
7 use RT::Ticket;
8
9 my $q = RT::Queue->new($RT::SystemUser);
10 my $queue = 'SearchTests-'.rand(200);
11 $q->Create(Name => $queue);
12
13 my @requestors = ( ('bravo@example.com') x 6, ('alpha@example.com') x 6,
14                    ('delta@example.com') x 6, ('charlie@example.com') x 6,
15                    (undef) x 6);
16 my @subjects = ("first test", "second test", "third test", "fourth test", "fifth test") x 6;
17 while (@requestors) {
18     my $t = RT::Ticket->new($RT::SystemUser);
19     my ( $id, undef $msg ) = $t->Create(
20         Queue      => $q->id,
21         Subject    => shift @subjects,
22         Requestor => [ shift @requestors ]
23     );
24     ok( $id, $msg );
25 }
26
27 {
28     my $tix = RT::Tickets->new($RT::SystemUser);
29     $tix->FromSQL("Queue = '$queue'");
30     is($tix->Count, 30, "found thirty tickets");
31 }
32
33 {
34     my $tix = RT::Tickets->new($RT::SystemUser);
35     $tix->FromSQL("Queue = '$queue' AND requestor = 'alpha\@example.com'");
36     $tix->OrderByCols({ FIELD => "Subject" });
37     my @subjects;
38     while (my $t = $tix->Next) { push @subjects, $t->Subject; }
39     is(@subjects, 6, "found six tickets");
40     is_deeply( \@subjects, [ sort @subjects ], "Subjects are sorted");
41 }
42
43 sub check_emails_order
44 {
45     my ($tix,$count,$order) = (@_);
46     my @mails;
47     while (my $t = $tix->Next) { push @mails, $t->RequestorAddresses; }
48     is(@mails, $count, "found $count tickets for ". $tix->Query);
49     my @required_order;
50     if( $order =~ /asc/i ) {
51         @required_order = sort { $a? ($b? ($a cmp $b) : -1) : 1} @mails;
52     } else {
53         @required_order = sort { $a? ($b? ($b cmp $a) : -1) : 1} @mails;
54     }
55     foreach( reverse splice @mails ) {
56         if( $_ ) { unshift @mails, $_ }
57         else { push @mails, $_ }
58     }
59     is_deeply( \@mails, \@required_order, "Addresses are sorted");
60 }
61
62 {
63     my $tix = RT::Tickets->new($RT::SystemUser);
64     $tix->FromSQL("Queue = '$queue' AND subject = 'first test' AND Requestor.EmailAddress LIKE 'example.com'");
65     $tix->OrderByCols({ FIELD => "Requestor.EmailAddress" });
66     check_emails_order($tix, 5, 'ASC');
67     $tix->OrderByCols({ FIELD => "Requestor.EmailAddress", ORDER => 'DESC' });
68     check_emails_order($tix, 5, 'DESC');
69 }
70
71 {
72     my $tix = RT::Tickets->new($RT::SystemUser);
73     $tix->FromSQL("Queue = '$queue' AND Subject = 'first test'");
74     $tix->OrderByCols({ FIELD => "Requestor.EmailAddress" });
75     check_emails_order($tix, 6, 'ASC');
76     $tix->OrderByCols({ FIELD => "Requestor.EmailAddress", ORDER => 'DESC' });
77     check_emails_order($tix, 6, 'DESC');
78 }
79
80
81 {
82     my $tix = RT::Tickets->new($RT::SystemUser);
83     $tix->FromSQL("Queue = '$queue' AND Subject = 'first test'");
84     $tix->OrderByCols({ FIELD => "Requestor.EmailAddress" });
85     check_emails_order($tix, 6, 'ASC');
86     $tix->OrderByCols({ FIELD => "Requestor.EmailAddress", ORDER => 'DESC' });
87     check_emails_order($tix, 6, 'DESC');
88 }
89
90 {
91     # create ticket with group as member of the requestors group
92     my $t = RT::Ticket->new($RT::SystemUser);
93     my ( $id, $msg ) = $t->Create(
94         Queue      => $q->id,
95         Subject    => "first test",
96         Requestor  => 'badaboom@example.com',
97     );
98     ok( $id, "ticket created" ) or diag( "error: $msg" );
99
100     my $g = RT::Group->new($RT::SystemUser);
101
102     my ($gid);
103     ($gid, $msg) = $g->CreateUserDefinedGroup(Name => '20-sort-by-requestor.t-'.rand(200));
104     ok($gid, "created group") or diag("error: $msg");
105
106     ($id, $msg) = $t->Requestors->AddMember( $gid );
107     ok($id, "added group to requestors group") or diag("error: $msg");
108 }
109
110     my $tix = RT::Tickets->new($RT::SystemUser);    
111     $tix->FromSQL("Queue = '$queue' AND Subject = 'first test'");
112
113     $tix->OrderByCols({ FIELD => "Requestor.EmailAddress" });
114     check_emails_order($tix, 7, 'ASC');
115
116     $tix->OrderByCols({ FIELD => "Requestor.EmailAddress", ORDER => 'DESC' });
117     check_emails_order($tix, 7, 'DESC');
118
119 {
120     my $tix = RT::Tickets->new($RT::SystemUser);
121     $tix->FromSQL("Queue = '$queue'");
122     $tix->OrderByCols({ FIELD => "Requestor.EmailAddress" });
123     $tix->RowsPerPage(30);
124     my @mails;
125     while (my $t = $tix->Next) { push @mails, $t->RequestorAddresses; }
126     is(@mails, 30, "found thirty tickets");
127     is_deeply( [grep {$_} @mails], [ sort grep {$_} @mails ], "Paging works (exclude nulls, which are db-dependant)");
128 }
129
130 {
131     my $tix = RT::Tickets->new($RT::SystemUser);
132     $tix->FromSQL("Queue = '$queue'");
133     $tix->OrderByCols({ FIELD => "Requestor.EmailAddress" });
134     $tix->RowsPerPage(30);
135     my @mails;
136     while (my $t = $tix->Next) { push @mails, $t->RequestorAddresses; }
137     is(@mails, 30, "found thirty tickets");
138     is_deeply( [grep {$_} @mails], [ sort grep {$_} @mails ], "Paging works (exclude nulls, which are db-dependant)");
139 }
140 RT::Test->mailsent_ok(25);
141
142 # vim:ft=perl: