6 use RT::Test tests => undef;
7 plan skip_all => 'Not mysql' unless RT->Config->Get('DatabaseType') eq 'mysql';
8 plan skip_all => "No SphinxSE in mysql" unless $RT::Handle->CheckSphinxSE;
11 $sphinx{'searchd'} = RT::Test->find_executable('searchd');
12 $sphinx{'indexer'} = RT::Test->find_executable('indexer');
14 plan skip_all => "No searchd and indexer under PATH"
15 unless $sphinx{'searchd'} && $sphinx{'indexer'};
19 RT->Config->Set( FullTextSearch => Enable => 1, Indexed => 1, Table => 'AttachmentsIndex', MaxMatches => 1000 );
23 my $q = RT::Test->load_or_create_queue( Name => 'General' );
24 ok $q && $q->id, 'loaded or created queue';
28 # Since we're not running a webserver in this test, use the
29 # known-safe port we determined at test setup
30 my $port = $RT::Test::port;
31 my ($exit_code, $output) = RT::Test->run_and_capture(
33 command => $RT::SbinPath .'/rt-setup-fulltext-index',
34 dba => $ENV{'RT_DBA_USER'},
35 'dba-password' => $ENV{'RT_DBA_PASSWORD'},
36 url => "sphinx://localhost:$port/rt",
38 ok(!$exit_code, "setted up index");
39 diag "output: $output" if $ENV{'TEST_VERBOSE'};
41 my $tmp = $sphinx{'directory'} = File::Spec->catdir( RT::Test->temp_directory, 'sphinx' );
44 my $sphinx_conf = $output;
45 $sphinx_conf =~ s/.*?source rt {/source rt {/ms;
46 $sphinx_conf =~ s{\Q$RT::VarPath\E/sphinx/}{$tmp/}g;
48 $sphinx{'config'} = File::Spec->catfile( $tmp, 'sphinx.conf' );
50 open my $fh, ">", $sphinx{'config'};
51 print $fh $sphinx_conf;
58 my ($exit_code, $output) = RT::Test->run_and_capture(
59 command => $sphinx{'searchd'},
60 config => $sphinx{'config'},
62 ok(!$exit_code, "setted up index") or diag "output: $output";
63 $sphinx{'started'} = 1 if !$exit_code;
68 local $SIG{'CHLD'} = 'DEFAULT';
69 local $SIG{'PIPE'} = 'DEFAULT';
70 open my $fh, '-|', $sphinx{'indexer'}, '--all',
71 '--config' => $sphinx{'config'},
72 $sphinx{'started'}? ('--rotate') : (),
76 my $exit_code = $?>>8;
77 ok(!$exit_code, "indexed") or diag "output: $output";
79 # We may need to wait a second for searchd to pick up the changes
85 while ( my ($query, $checks) = splice @test, 0, 2 ) {
86 run_test( $query, %$checks );
92 my ($query, %checks) = @_;
93 my $query_prefix = join ' OR ', map 'id = '. $_->id, @tickets;
95 my $tix = RT::Tickets->new(RT->SystemUser);
96 $tix->FromSQL( "( $query_prefix ) AND ( $query )" );
101 $count++ foreach grep $_, values %checks;
102 is($tix->Count, $count, "found correct number of ticket(s) by '$query'") or $error = 1;
104 my $good_tickets = ($tix->Count == $count);
105 while ( my $ticket = $tix->Next ) {
106 next if $checks{ $ticket->Subject };
107 diag $ticket->Subject ." ticket has been found when it's not expected";
110 ok( $good_tickets, "all tickets are good with '$query'" ) or $error = 1;
112 diag "Wrong SQL query for '$query':". $tix->BuildSelectQuery if $error;
115 @tickets = RT::Test->create_tickets(
117 { Subject => 'book', Content => 'book' },
118 { Subject => 'bar', Content => 'bar' },
123 "Content LIKE 'book'" => { book => 1, bar => 0 },
124 "Content LIKE 'bar'" => { book => 0, bar => 1 },
128 my $Test = RT::Test->builder;
129 return if $Test->{Original_Pid} != $$;
130 return unless $sphinx{'started'};
132 my $pid = int RT::Test->file_content([$sphinx{'directory'}, 'searchd.pid']);
133 kill TERM => $pid if $pid;