2 use strict; use warnings;
4 use RT::Test tests => 58;
9 my $q = RT::Queue->new(RT->SystemUser);
10 my $queue = 'SearchTests-'.rand(200);
11 $q->Create(Name => $queue);
13 my @requestors = ( ('bravo@example.com') x 6, ('alpha@example.com') x 6,
14 ('delta@example.com') x 6, ('charlie@example.com') x 6,
16 my @subjects = ("first test", "second test", "third test", "fourth test", "fifth test") x 6;
18 my $t = RT::Ticket->new(RT->SystemUser);
19 my ( $id, undef $msg ) = $t->Create(
21 Subject => shift @subjects,
22 Requestor => [ shift @requestors ]
28 my $tix = RT::Tickets->new(RT->SystemUser);
29 $tix->FromSQL("Queue = '$queue'");
30 is($tix->Count, 30, "found thirty tickets");
34 my $tix = RT::Tickets->new(RT->SystemUser);
35 $tix->FromSQL("Queue = '$queue' AND requestor = 'alpha\@example.com'");
36 $tix->OrderByCols({ FIELD => "Subject" });
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");
43 sub check_emails_order
45 my ($tix,$count,$order) = (@_);
47 while (my $t = $tix->Next) { push @mails, $t->RequestorAddresses; }
48 is(@mails, $count, "found $count tickets for ". $tix->Query);
50 if( $order =~ /asc/i ) {
51 @required_order = sort { $a? ($b? ($a cmp $b) : -1) : 1} @mails;
53 @required_order = sort { $a? ($b? ($b cmp $a) : -1) : 1} @mails;
55 foreach( reverse splice @mails ) {
56 if( $_ ) { unshift @mails, $_ }
57 else { push @mails, $_ }
59 is_deeply( \@mails, \@required_order, "Addresses are sorted");
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');
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');
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');
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(
95 Subject => "first test",
96 Requestor => 'badaboom@example.com',
98 ok( $id, "ticket created" ) or diag( "error: $msg" );
100 my $g = RT::Group->new(RT->SystemUser);
103 ($gid, $msg) = $g->CreateUserDefinedGroup(Name => '20-sort-by-requestor.t-'.rand(200));
104 ok($gid, "created group") or diag("error: $msg");
106 ($id, $msg) = $t->Requestors->AddMember( $gid );
107 ok($id, "added group to requestors group") or diag("error: $msg");
110 my $tix = RT::Tickets->new(RT->SystemUser);
111 $tix->FromSQL("Queue = '$queue' AND Subject = 'first test'");
113 $tix->OrderByCols({ FIELD => "Requestor.EmailAddress" });
114 check_emails_order($tix, 7, 'ASC');
116 $tix->OrderByCols({ FIELD => "Requestor.EmailAddress", ORDER => 'DESC' });
117 check_emails_order($tix, 7, 'DESC');
120 my $tix = RT::Tickets->new(RT->SystemUser);
121 $tix->FromSQL("Queue = '$queue'");
122 $tix->OrderByCols({ FIELD => "Requestor.EmailAddress" });
123 $tix->RowsPerPage(30);
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)");
131 my $tix = RT::Tickets->new(RT->SystemUser);
132 $tix->FromSQL("Queue = '$queue'");
133 $tix->OrderByCols({ FIELD => "Requestor.EmailAddress" });
134 $tix->RowsPerPage(30);
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)");
140 RT::Test->mailsent_ok(25);