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