2 use strict; use warnings;
4 use Test::More qw/no_plan/;
10 my $q = RT::Queue->new($RT::SystemUser);
11 my $queue = 'SearchTests-'.rand(200);
12 $q->Create(Name => $queue);
14 my @requestors = ( ('bravo@example.com') x 6, ('alpha@example.com') x 6,
15 ('delta@example.com') x 6, ('charlie@example.com') x 6,
17 my @subjects = ("first test", "second test", "third test", "fourth test", "fifth test") x 6;
19 my $t = RT::Ticket->new($RT::SystemUser);
20 my ( $id, undef $msg ) = $t->Create(
22 Subject => shift @subjects,
23 Requestor => [ shift @requestors ]
29 my $tix = RT::Tickets->new($RT::SystemUser);
30 $tix->FromSQL("Queue = '$queue'");
31 is($tix->Count, 30, "found thirty tickets");
35 my $tix = RT::Tickets->new($RT::SystemUser);
36 $tix->FromSQL("Queue = '$queue' AND requestor = 'alpha\@example.com'");
37 $tix->OrderByCols({ FIELD => "Subject" });
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");
44 sub check_emails_order
46 my ($tix,$count,$order) = (@_);
48 while (my $t = $tix->Next) { push @mails, $t->RequestorAddresses; }
49 is(@mails, $count, "found $count tickets for ". $tix->Query);
51 if( $order =~ /asc/i ) {
52 @required_order = sort { $a? ($b? ($a cmp $b) : -1) : 1} @mails;
54 @required_order = sort { $a? ($b? ($b cmp $a) : -1) : 1} @mails;
56 foreach( reverse splice @mails ) {
57 if( $_ ) { unshift @mails, $_ }
58 else { push @mails, $_ }
60 is_deeply( \@mails, \@required_order, "Addresses are sorted");
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');
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');
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');
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(
96 Subject => "first test",
97 Requestor => 'badaboom@example.com',
99 ok( $id, "ticket created" ) or diag( "error: $msg" );
101 my $g = RT::Group->new($RT::SystemUser);
104 ($gid, $msg) = $g->CreateUserDefinedGroup(Name => '20-sort-by-requestor.t-'.rand(200));
105 ok($gid, "created group") or diag("error: $msg");
107 ($id, $msg) = $t->Requestors->AddMember( $gid );
108 ok($id, "added group to requestors group") or diag("error: $msg");
111 my $tix = RT::Tickets->new($RT::SystemUser);
112 $tix->FromSQL("Queue = '$queue' AND Subject = 'first test'");
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');
118 $tix->OrderByCols({ FIELD => "Requestor.EmailAddress", ORDER => 'DESC' });
119 check_emails_order($tix, 7, 'DESC');
122 my $tix = RT::Tickets->new($RT::SystemUser);
123 $tix->FromSQL("Queue = '$queue'");
124 $tix->OrderByCols({ FIELD => "Requestor.EmailAddress" });
125 $tix->RowsPerPage(30);
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)");
133 my $tix = RT::Tickets->new($RT::SystemUser);
134 $tix->FromSQL("Queue = '$queue'");
135 $tix->OrderByCols({ FIELD => "Requestor.EmailAddress" });
136 $tix->RowsPerPage(30);
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)");