first pass RT4 merge, RT#13852
[freeside.git] / rt / t / web / googleish_search.t
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4
5 use RT::Test tests => 96, config => 'Set( %FullTextSearch, Enable => 1, Indexed => 0 );';
6 my ($baseurl, $m) = RT::Test->started_ok;
7 my $url = $m->rt_base_url;
8
9 my $queue = RT::Queue->new($RT::SystemUser);
10 $queue->Create( Name => 'other' );
11 ok( $queue->id, 'created queue other');
12
13 my $two_words_queue = RT::Test->load_or_create_queue(
14     Name => 'Two Words',
15 );
16 ok $two_words_queue && $two_words_queue->id, 'loaded or created a queue';
17
18
19 {
20     my $tickets = RT::Tickets->new( RT->SystemUser );
21     my $active = "( ".join( " OR ", map "Status = '$_'", RT::Queue->ActiveStatusArray())." )";
22     my $inactive = "( ".join( " OR ", map "Status = '$_'", RT::Queue->InactiveStatusArray())." )";
23
24     require RT::Search::Googleish;
25     my $parser = RT::Search::Googleish->new(
26         TicketsObj => $tickets,
27         Argument   => '',
28     );
29     is $parser->QueryToSQL("foo"), "$active AND ( Subject LIKE 'foo' )", "correct parsing";
30     is $parser->QueryToSQL("1"), "( Id = 1 )", "correct parsing";
31     is $parser->QueryToSQL("#1"), "( Id = 1 )", "correct parsing";
32     is $parser->QueryToSQL("'1'"), "$active AND ( Subject LIKE '1' )", "correct parsing";
33
34     is $parser->QueryToSQL("foo bar"),
35         "$active AND ( Subject LIKE 'foo' AND Subject LIKE 'bar' )",
36         "correct parsing";
37     is $parser->QueryToSQL("'foo bar'"),
38         "$active AND ( Subject LIKE 'foo bar' )",
39         "correct parsing";
40
41     is $parser->QueryToSQL("'foo \\' bar'"),
42         "$active AND ( Subject LIKE 'foo \\' bar' )",
43         "correct parsing";
44     is $parser->QueryToSQL('"foo \' bar"'),
45         "$active AND ( Subject LIKE 'foo \\' bar' )",
46         "correct parsing";
47     is $parser->QueryToSQL('"\f\o\o"'),
48         "$active AND ( Subject LIKE '\\\\f\\\\o\\\\o' )",
49         "correct parsing";
50
51     is $parser->QueryToSQL("General"), "( Queue = 'General' ) AND $active", "correct parsing";
52     is $parser->QueryToSQL("'Two Words'"), "$active AND ( Subject LIKE 'Two Words' )", "correct parsing";
53     is $parser->QueryToSQL("queue:'Two Words'"), "( Queue = 'Two Words' ) AND $active", "correct parsing";
54     is $parser->QueryToSQL("subject:'Two Words'"), "$active AND ( Subject LIKE 'Two Words' )", "correct parsing";
55
56     is $parser->QueryToSQL("me"), "( Owner.id = '__CurrentUser__' ) AND $active", "correct parsing";
57     is $parser->QueryToSQL("'me'"), "$active AND ( Subject LIKE 'me' )", "correct parsing";
58     is $parser->QueryToSQL("owner:me"), "( Owner.id = '__CurrentUser__' ) AND $active", "correct parsing";
59     is $parser->QueryToSQL("owner:'me'"), "( Owner = 'me' ) AND $active", "correct parsing";
60
61     is $parser->QueryToSQL("resolved me"), "( Owner.id = '__CurrentUser__' ) AND ( Status = 'resolved' )", "correct parsing";
62     is $parser->QueryToSQL("resolved active me"), "( Owner.id = '__CurrentUser__' ) AND ( Status = 'resolved' OR Status = 'new' OR Status = 'open' OR Status = 'stalled' )", "correct parsing";
63     is $parser->QueryToSQL("status:active"), $active, "Explicit active search";
64     is $parser->QueryToSQL("status:'active'"), "( Status = 'active' )", "Quoting active makes it the actual word";
65     is $parser->QueryToSQL("inactive me"), "( Owner.id = '__CurrentUser__' ) AND $inactive", "correct parsing";
66
67     is $parser->QueryToSQL("cf.Foo:bar"), "( 'CF.{Foo}' LIKE 'bar' ) AND $active", "correct parsing of CFs";
68     is $parser->QueryToSQL(q{cf."don't foo?":'bar n\\' baz'}), qq/( 'CF.{don\\'t foo?}' LIKE 'bar n\\' baz' ) AND $active/, "correct parsing of CFs with quotes";
69 }
70
71 my $ticket_found_1 = RT::Ticket->new($RT::SystemUser);
72 my $ticket_found_2 = RT::Ticket->new($RT::SystemUser);
73 my $ticket_not_found = RT::Ticket->new($RT::SystemUser);
74
75 $ticket_found_1->Create(
76     Subject   => 'base ticket 1'.$$,
77     Queue     => 'general',
78     Owner     => 'root',
79     Requestor => 'customsearch@localhost',
80     Content   => 'this is base ticket 1',
81 );
82 ok( $ticket_found_1->id, 'created ticket for custom search');
83
84
85 $ticket_found_2->Create(
86     Subject   => 'base ticket 2'.$$,
87     Queue     => 'general',
88     Owner     => 'root',
89     Requestor => 'customsearch@localhost',
90     Content   => 'this is base ticket 2',
91 );
92 ok( $ticket_found_2->id, 'created ticket for custom search');
93
94 $ticket_not_found = RT::Ticket->new($RT::SystemUser);
95 $ticket_not_found->Create(
96     Subject   => 'not found subject' . $$,
97     Queue     => 'other',
98     Owner     => 'nobody',
99     Requestor => 'notfound@localhost',
100     Content   => 'this is not found content',
101 );
102 ok( $ticket_not_found->id, 'created ticket for custom search');
103
104 ok($m->login, 'logged in');
105
106 my @queries = (
107     'base ticket',            'root',
108     'customsearch@localhost', 'requestor:customsearch',
109     'subject:base',           'subject:"base ticket"',
110     'queue:general',          'owner:root',
111 );
112
113 for my $q (@queries) {
114     $m->form_with_fields('q');
115     $m->field( q => $q );
116     $m->submit;
117     $m->content_contains( 'base ticket 1', 'base ticket 1 is found' );
118     $m->content_contains( 'base ticket 2', 'base ticket 2 is found' );
119     $m->content_lacks( 'not found subject', 'not found ticket is not found' );
120 }
121
122 $ticket_not_found->SetStatus('open');
123 is( $ticket_not_found->Status, 'open', 'status of not found ticket is open' );
124 @queries = qw/new status:new/;
125 for my $q (@queries) {
126     $m->form_with_fields('q');
127     $m->field( q => $q );
128     $m->submit;
129     $m->content_contains( 'base ticket 1', 'base ticket 1 is found' );
130     $m->content_contains( 'base ticket 2', 'base ticket 2 is found' );
131     $m->content_lacks( 'not found subject', 'not found ticket is not found' );
132 }
133
134 @queries = ( 'fulltext:"base ticket 1"', "'base ticket 1'" );
135 for my $q (@queries) {
136     $m->form_with_fields('q');
137     $m->field( q => $q );
138     $m->submit;
139     $m->content_contains( 'base ticket 1', 'base ticket 1 is found' );
140     $m->content_lacks( 'base ticket 2',     'base ticket 2 is not found' );
141     $m->content_lacks( 'not found subject', 'not found ticket is not found' );
142 }
143
144 # now let's test with ' or "
145 for my $quote ( q{'}, q{"} ) {
146     my $user = RT::User->new($RT::SystemUser);
147     is( ref($user), 'RT::User' );
148     my ( $id, $msg ) = $user->Create(
149         Name         => qq!foo${quote}bar!,
150         EmailAddress => qq!foo${quote}bar$$\@example.com !,
151         Privileged   => 1,
152     );
153     ok ($id, "Creating user - " . $msg );
154
155     my ( $grantid, $grantmsg ) =
156       $user->PrincipalObj->GrantRight( Right => 'OwnTicket' );
157     ok( $grantid, $grantmsg );
158
159
160
161     my $ticket_quote = RT::Ticket->new($RT::SystemUser);
162     $ticket_quote->Create(
163         Subject   => qq!base${quote}ticket $$!,
164         Queue     => 'general',
165         Owner     => $user->Name,
166         Requestor => qq!custom${quote}search\@localhost!,
167         Content   => qq!this is base${quote}ticket with quote inside!,
168     );
169     ok( $ticket_quote->id, 'created ticket with quote for custom search' );
170
171     @queries = (
172         qq!fulltext:base${quote}ticket!,
173         "base${quote}ticket",
174         "owner:foo${quote}bar",
175         "foo${quote}bar",
176
177         # email doesn't allow " character
178         $quote eq q{'}
179         ? (
180             "requestor:custom${quote}search\@localhost",
181             "custom${quote}search\@localhost",
182           )
183         : (),
184     );
185     for my $q (@queries) {
186         $m->form_with_fields('q');
187         $m->field( q => $q );
188         $m->submit;
189         my $escape_quote = $quote;
190         RT::Interface::Web::EscapeUTF8(\$escape_quote);
191         $m->content_contains( "base${escape_quote}ticket",
192             "base${quote}ticket is found" );
193     }
194 }
195
196 # Create a CF
197 {
198     my $cf = RT::CustomField->new(RT->SystemUser);
199     ok( $cf->Create(Name => 'Foo', Type => 'Freeform', MaxValues => '1', Queue => 0) );
200     ok $cf->Id;
201
202     $ticket_found_1->AddCustomFieldValue( Field => 'Foo', Value => 'bar' );
203     $ticket_found_2->AddCustomFieldValue( Field => 'Foo', Value => 'bar' );
204     $ticket_not_found->AddCustomFieldValue( Field => 'Foo', Value => 'baz' );
205     is( $ticket_found_1->FirstCustomFieldValue('Foo'), 'bar', 'cf value is ok' );
206     is( $ticket_found_2->FirstCustomFieldValue('Foo'), 'bar', 'cf value is ok' );
207     is( $ticket_not_found->FirstCustomFieldValue('Foo'), 'baz', 'cf value is ok' );
208
209     @queries = qw/cf.Foo:bar/;
210     for my $q (@queries) {
211         $m->form_with_fields('q');
212         $m->field( q => $q );
213         $m->submit;
214         $m->content_contains( 'base ticket 1', 'base ticket 1 is found' );
215         $m->content_contains( 'base ticket 2', 'base ticket 2 is found' );
216         $m->content_lacks( 'not found subject', 'not found ticket is not found' );
217     }
218 }
219