summaryrefslogtreecommitdiff
path: root/rt/lib/t/regression/20-sort-by-requestor.t
blob: e6903b433c430e747bc8667c0d91390a230f6cb4 (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
#!/usr/bin/perl -w
use strict; use warnings;

use Test::More qw/no_plan/;
use_ok('RT');
RT::LoadConfig();
RT::Init();
use RT::Ticket;

my $q = RT::Queue->new($RT::SystemUser);
my $queue = 'SearchTests-'.rand(200);
$q->Create(Name => $queue);

my @requestors = ( ('bravo@example.com') x 6, ('alpha@example.com') x 6,
                   ('delta@example.com') x 6, ('charlie@example.com') x 6,
                   (undef) x 6);
my @subjects = ("first test", "second test", "third test", "fourth test", "fifth test") x 6;
while (@requestors) {
    my $t = RT::Ticket->new($RT::SystemUser);
    my ( $id, undef $msg ) = $t->Create(
        Queue      => $q->id,
        Subject    => shift @subjects,
        Requestor => [ shift @requestors ]
    );
    ok( $id, $msg );
}

{
    my $tix = RT::Tickets->new($RT::SystemUser);
    $tix->FromSQL("Queue = '$queue'");
    is($tix->Count, 30, "found thirty tickets");
}

{
    my $tix = RT::Tickets->new($RT::SystemUser);
    $tix->FromSQL("Queue = '$queue' AND requestor = 'alpha\@example.com'");
    $tix->OrderByCols({ FIELD => "Subject" });
    my @subjects;
    while (my $t = $tix->Next) { push @subjects, $t->Subject; }
    is(@subjects, 6, "found six tickets");
    is_deeply( \@subjects, [ sort @subjects ], "Subjects are sorted");
}

sub check_emails_order
{
    my ($tix,$count,$order) = (@_);
    my @mails;
    while (my $t = $tix->Next) { push @mails, $t->RequestorAddresses; }
    is(@mails, $count, "found $count tickets for ". $tix->Query);
    my @required_order;
    if( $order =~ /asc/i ) {
        @required_order = sort { $a? ($b? ($a cmp $b) : -1) : 1} @mails;
    } else {
        @required_order = sort { $a? ($b? ($b cmp $a) : -1) : 1} @mails;
    }
    foreach( reverse splice @mails ) {
        if( $_ ) { unshift @mails, $_ }
        else { push @mails, $_ }
    }
    is_deeply( \@mails, \@required_order, "Addresses are sorted");
}

{
    my $tix = RT::Tickets->new($RT::SystemUser);
    $tix->FromSQL("Queue = '$queue' AND subject = 'first test' AND Requestor.EmailAddress LIKE 'example.com'");
    $tix->OrderByCols({ FIELD => "Requestor.EmailAddress" });
    check_emails_order($tix, 5, 'ASC');
    $tix->OrderByCols({ FIELD => "Requestor.EmailAddress", ORDER => 'DESC' });
    check_emails_order($tix, 5, 'DESC');
}

{
    my $tix = RT::Tickets->new($RT::SystemUser);
    $tix->FromSQL("Queue = '$queue' AND Subject = 'first test'");
    $tix->OrderByCols({ FIELD => "Requestor.EmailAddress" });
    check_emails_order($tix, 6, 'ASC');
    $tix->OrderByCols({ FIELD => "Requestor.EmailAddress", ORDER => 'DESC' });
    check_emails_order($tix, 6, 'DESC');
}


{
    my $tix = RT::Tickets->new($RT::SystemUser);
    $tix->FromSQL("Queue = '$queue' AND Subject = 'first test'");
    $tix->OrderByCols({ FIELD => "Requestor.EmailAddress" });
    check_emails_order($tix, 6, 'ASC');
    $tix->OrderByCols({ FIELD => "Requestor.EmailAddress", ORDER => 'DESC' });
    check_emails_order($tix, 6, 'DESC');
}

{
    # create ticket with group as member of the requestors group
    my $t = RT::Ticket->new($RT::SystemUser);
    my ( $id, $msg ) = $t->Create(
        Queue      => $q->id,
        Subject    => "first test",
        Requestor  => 'badaboom@example.com',
    );
    ok( $id, "ticket created" ) or diag( "error: $msg" );

    my $g = RT::Group->new($RT::SystemUser);

    my ($gid);
    ($gid, $msg) = $g->CreateUserDefinedGroup(Name => '20-sort-by-requestor.t-'.rand(200));
    ok($gid, "created group") or diag("error: $msg");

    ($id, $msg) = $t->Requestors->AddMember( $gid );
    ok($id, "added group to requestors group") or diag("error: $msg");
}

    my $tix = RT::Tickets->new($RT::SystemUser);    
    $tix->FromSQL("Queue = '$queue' AND Subject = 'first test'");
TODO: {
    local $TODO = "if group has non users members we get wrong order";
    $tix->OrderByCols({ FIELD => "Requestor.EmailAddress" });
    check_emails_order($tix, 7, 'ASC');
}
    $tix->OrderByCols({ FIELD => "Requestor.EmailAddress", ORDER => 'DESC' });
    check_emails_order($tix, 7, 'DESC');

{
    my $tix = RT::Tickets->new($RT::SystemUser);
    $tix->FromSQL("Queue = '$queue'");
    $tix->OrderByCols({ FIELD => "Requestor.EmailAddress" });
    $tix->RowsPerPage(30);
    my @mails;
    while (my $t = $tix->Next) { push @mails, $t->RequestorAddresses; }
    is(@mails, 30, "found thirty tickets");
    is_deeply( [grep {$_} @mails], [ sort grep {$_} @mails ], "Paging works (exclude nulls, which are db-dependant)");
}

{
    my $tix = RT::Tickets->new($RT::SystemUser);
    $tix->FromSQL("Queue = '$queue'");
    $tix->OrderByCols({ FIELD => "Requestor.EmailAddress" });
    $tix->RowsPerPage(30);
    my @mails;
    while (my $t = $tix->Next) { push @mails, $t->RequestorAddresses; }
    is(@mails, 30, "found thirty tickets");
    is_deeply( [grep {$_} @mails], [ sort grep {$_} @mails ], "Paging works (exclude nulls, which are db-dependant)");
}

# vim:ft=perl: