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