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;
9 my $queue = RT::Queue->new($RT::SystemUser);
10 $queue->Create( Name => 'other' );
11 ok( $queue->id, 'created queue other');
13 my $two_words_queue = RT::Test->load_or_create_queue(
16 ok $two_words_queue && $two_words_queue->id, 'loaded or created a queue';
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())." )";
24 require RT::Search::Googleish;
25 my $parser = RT::Search::Googleish->new(
26 TicketsObj => $tickets,
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";
34 is $parser->QueryToSQL("foo bar"),
35 "$active AND ( Subject LIKE 'foo' AND Subject LIKE 'bar' )",
37 is $parser->QueryToSQL("'foo bar'"),
38 "$active AND ( Subject LIKE 'foo bar' )",
41 is $parser->QueryToSQL("'foo \\' bar'"),
42 "$active AND ( Subject LIKE 'foo \\' bar' )",
44 is $parser->QueryToSQL('"foo \' bar"'),
45 "$active AND ( Subject LIKE 'foo \\' bar' )",
47 is $parser->QueryToSQL('"\f\o\o"'),
48 "$active AND ( Subject LIKE '\\\\f\\\\o\\\\o' )",
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";
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";
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";
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";
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);
75 $ticket_found_1->Create(
76 Subject => 'base ticket 1'.$$,
79 Requestor => 'customsearch@localhost',
80 Content => 'this is base ticket 1',
82 ok( $ticket_found_1->id, 'created ticket for custom search');
85 $ticket_found_2->Create(
86 Subject => 'base ticket 2'.$$,
89 Requestor => 'customsearch@localhost',
90 Content => 'this is base ticket 2',
92 ok( $ticket_found_2->id, 'created ticket for custom search');
94 $ticket_not_found = RT::Ticket->new($RT::SystemUser);
95 $ticket_not_found->Create(
96 Subject => 'not found subject' . $$,
99 Requestor => 'notfound@localhost',
100 Content => 'this is not found content',
102 ok( $ticket_not_found->id, 'created ticket for custom search');
104 ok($m->login, 'logged in');
107 'base ticket', 'root',
108 'customsearch@localhost', 'requestor:customsearch',
109 'subject:base', 'subject:"base ticket"',
110 'queue:general', 'owner:root',
113 for my $q (@queries) {
114 $m->form_with_fields('q');
115 $m->field( q => $q );
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' );
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 );
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' );
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 );
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' );
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 !,
153 ok ($id, "Creating user - " . $msg );
155 my ( $grantid, $grantmsg ) =
156 $user->PrincipalObj->GrantRight( Right => 'OwnTicket' );
157 ok( $grantid, $grantmsg );
161 my $ticket_quote = RT::Ticket->new($RT::SystemUser);
162 $ticket_quote->Create(
163 Subject => qq!base${quote}ticket $$!,
165 Owner => $user->Name,
166 Requestor => qq!custom${quote}search\@localhost!,
167 Content => qq!this is base${quote}ticket with quote inside!,
169 ok( $ticket_quote->id, 'created ticket with quote for custom search' );
172 qq!fulltext:base${quote}ticket!,
173 "base${quote}ticket",
174 "owner:foo${quote}bar",
177 # email doesn't allow " character
180 "requestor:custom${quote}search\@localhost",
181 "custom${quote}search\@localhost",
185 for my $q (@queries) {
186 $m->form_with_fields('q');
187 $m->field( q => $q );
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" );
198 my $cf = RT::CustomField->new(RT->SystemUser);
199 ok( $cf->Create(Name => 'Foo', Type => 'Freeform', MaxValues => '1', Queue => 0) );
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' );
209 @queries = qw/cf.Foo:bar/;
210 for my $q (@queries) {
211 $m->form_with_fields('q');
212 $m->field( q => $q );
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' );