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;
10 my $queue = RT::Queue->new($RT::SystemUser);
11 $queue->Create( Name => 'other' );
12 ok( $queue->id, 'created queue other');
14 my $two_words_queue = RT::Test->load_or_create_queue(
17 ok $two_words_queue && $two_words_queue->id, 'loaded or created a queue';
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())." )";
25 require RT::Search::Googleish;
26 my $parser = RT::Search::Googleish->new(
27 TicketsObj => $tickets,
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";
35 is $parser->QueryToSQL("foo bar"),
36 "$active AND ( Subject LIKE 'foo' AND Subject LIKE 'bar' )",
38 is $parser->QueryToSQL("'foo bar'"),
39 "$active AND ( Subject LIKE 'foo bar' )",
42 is $parser->QueryToSQL("'foo \\' bar'"),
43 "$active AND ( Subject LIKE 'foo \\' bar' )",
45 is $parser->QueryToSQL('"foo \' bar"'),
46 "$active AND ( Subject LIKE 'foo \\' bar' )",
48 is $parser->QueryToSQL('"\f\o\o"'),
49 "$active AND ( Subject LIKE '\\\\f\\\\o\\\\o' )",
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";
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";
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";
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";
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);
77 $ticket_found_1->Create(
78 Subject => 'base ticket 1'.$$,
81 Requestor => 'customsearch@localhost',
82 Content => 'this is base ticket 1',
84 ok( $ticket_found_1->id, 'created ticket for custom search');
87 $ticket_found_2->Create(
88 Subject => 'base ticket 2'.$$,
91 Requestor => 'customsearch@localhost',
92 Content => 'this is base ticket 2',
94 ok( $ticket_found_2->id, 'created ticket for custom search');
96 $ticket_not_found = RT::Ticket->new($RT::SystemUser);
97 $ticket_not_found->Create(
98 Subject => 'not found subject' . $$,
101 Requestor => 'notfound@localhost',
102 Content => 'this is not found content',
104 ok( $ticket_not_found->id, 'created ticket for custom search');
106 ok($m->login, 'logged in');
109 'base ticket', 'root',
110 'customsearch@localhost', 'requestor:customsearch',
111 'subject:base', 'subject:"base ticket"',
112 'queue:general', 'owner:root',
115 for my $q (@queries) {
116 $m->form_with_fields('q');
117 $m->field( q => $q );
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' );
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 );
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' );
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 );
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' );
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 !,
155 ok ($id, "Creating user - " . $msg );
157 my ( $grantid, $grantmsg ) =
158 $user->PrincipalObj->GrantRight( Right => 'OwnTicket' );
159 ok( $grantid, $grantmsg );
163 my $ticket_quote = RT::Ticket->new($RT::SystemUser);
164 $ticket_quote->Create(
165 Subject => qq!base${quote}ticket $$!,
167 Owner => $user->Name,
168 Requestor => qq!custom${quote}search\@localhost!,
169 Content => qq!this is base${quote}ticket with quote inside!,
171 ok( $ticket_quote->id, 'created ticket with quote for custom search' );
174 qq!fulltext:base${quote}ticket!,
175 "base${quote}ticket",
176 "owner:foo${quote}bar",
179 # email doesn't allow " character
182 "requestor:custom${quote}search\@localhost",
183 "custom${quote}search\@localhost",
187 for my $q (@queries) {
188 $m->form_with_fields('q');
189 $m->field( q => $q );
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" );
200 my $cf = RT::CustomField->new(RT->SystemUser);
201 ok( $cf->Create(Name => 'Foo', Type => 'Freeform', MaxValues => '1', Queue => 0) );
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' );
211 @queries = qw/cf.Foo:bar/;
212 for my $q (@queries) {
213 $m->form_with_fields('q');
214 $m->field( q => $q );
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' );