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