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'};
16 plan skip_all => "Can't determine sphinx version"
17 unless `$sphinx{searchd} --version` =~ /Sphinx (\d+)\.(\d+)(?:\.(\d+))?/;
19 $sphinx{version} = sprintf "%d.%03d%03d", $1, $2, ($3 || 0);
25 my $q = RT::Test->load_or_create_queue( Name => 'General' );
26 ok $q && $q->id, 'loaded or created queue';
30 # Since we're not running a webserver in this test, use the
31 # known-safe port we determined at test setup
32 my $port = $RT::Test::port;
33 my ($exit_code, $output) = RT::Test->run_and_capture(
35 command => $RT::SbinPath .'/rt-setup-fulltext-index',
36 dba => $ENV{'RT_DBA_USER'},
37 'dba-password' => $ENV{'RT_DBA_PASSWORD'},
38 url => "sphinx://127.0.0.1:$port/rt",
39 'index-type' => 'sphinx',
41 ok(!$exit_code, "setted up index");
42 diag "output: $output" if $ENV{'TEST_VERBOSE'};
44 my $tmp = $sphinx{'directory'} = File::Spec->catdir( RT::Test->temp_directory, 'sphinx' );
47 my $sphinx_conf = $output;
48 $sphinx_conf =~ s/.*?source rt \{/source rt {/ms;
49 $sphinx_conf =~ s{\Q$RT::VarPath\E/sphinx/}{$tmp/}g;
51 # Remove lines for different versions of sphinx than we're running
52 $sphinx_conf =~ s{^(\s+ \# \s+ for \s+ sphinx \s+
54 (\d+) \. (\d+) (?:\. (\d+))?
57 my $v = sprintf "%d.%03d%03d", $3, $4, ($5 || 0);
58 my $prefix = eval "$sphinx{version} $2 $v" ? "" : "#";
59 $1 . join("\n",map{"$prefix$_"} split "\n", $6) . "\n";
62 $sphinx{'config'} = File::Spec->catfile( $tmp, 'sphinx.conf' );
64 open my $fh, ">", $sphinx{'config'};
65 print $fh $sphinx_conf;
72 my ($exit_code, $output) = RT::Test->run_and_capture(
73 command => $sphinx{'searchd'},
74 config => $sphinx{'config'},
76 ok(!$exit_code, "setted up index") or diag "output: $output";
77 $sphinx{'started'} = 1 if !$exit_code;
82 local $SIG{'CHLD'} = 'DEFAULT';
83 local $SIG{'PIPE'} = 'DEFAULT';
84 open my $fh, '-|', $sphinx{'indexer'}, '--all',
85 '--config' => $sphinx{'config'},
86 $sphinx{'started'}? ('--rotate') : (),
90 my $exit_code = $?>>8;
91 ok(!$exit_code, "indexed") or diag "output: $output";
93 # We may need to wait a second for searchd to pick up the changes
99 while ( my ($query, $checks) = splice @test, 0, 2 ) {
100 run_test( $query, %$checks );
106 my ($query, %checks) = @_;
107 my $query_prefix = join ' OR ', map 'id = '. $_->id, @tickets;
109 my $tix = RT::Tickets->new(RT->SystemUser);
110 $tix->FromSQL( "( $query_prefix ) AND ( $query )" );
115 $count++ foreach grep $_, values %checks;
116 is($tix->Count, $count, "found correct number of ticket(s) by '$query'") or $error = 1;
118 my $good_tickets = ($tix->Count == $count);
119 while ( my $ticket = $tix->Next ) {
120 next if $checks{ $ticket->Subject };
121 diag $ticket->Subject ." ticket has been found when it's not expected";
124 ok( $good_tickets, "all tickets are good with '$query'" ) or $error = 1;
126 diag "Wrong SQL query for '$query':". $tix->BuildSelectQuery if $error;
129 @tickets = RT::Test->create_tickets(
131 { Subject => 'book', Content => 'book' },
132 { Subject => 'bar', Content => 'bar' },
136 RT->Config->Set( FullTextSearch => Enable => 1, Indexed => 1, Table => 'AttachmentsIndex', MaxMatches => 1000, Sphinx => 1 );
139 "Content LIKE 'book'" => { book => 1, bar => 0 },
140 "Content LIKE 'bar'" => { book => 0, bar => 1 },
144 my $Test = RT::Test->builder;
145 return if $Test->{Original_Pid} != $$;
146 return unless $sphinx{'started'};
148 my $pid = int RT::Test->file_content([$sphinx{'directory'}, 'searchd.pid']);
149 kill TERM => $pid if $pid;