first pass RT4 merge, RT#13852
[freeside.git] / rt / t / fts / indexed_mysql.t
1 #!/usr/bin/perl -w
2
3 use strict;
4 use warnings;
5
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;
9
10 my %sphinx;
11 $sphinx{'searchd'} = RT::Test->find_executable('searchd');
12 $sphinx{'indexer'} = RT::Test->find_executable('indexer');
13
14 plan skip_all => "No searchd and indexer under PATH"
15     unless $sphinx{'searchd'} && $sphinx{'indexer'};
16
17 plan tests => 15;
18
19 RT->Config->Set( FullTextSearch => Enable => 1, Indexed => 1, Table => 'AttachmentsIndex', MaxMatches => 1000 );
20
21 setup_indexing();
22
23 my $q = RT::Test->load_or_create_queue( Name => 'General' );
24 ok $q && $q->id, 'loaded or created queue';
25 my $queue = $q->Name;
26
27 sub setup_indexing {
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(
32         'no-ask'       => 1,
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",
37     );
38     ok(!$exit_code, "setted up index");
39     diag "output: $output" if $ENV{'TEST_VERBOSE'};
40
41     my $tmp = $sphinx{'directory'} = File::Spec->catdir( RT::Test->temp_directory, 'sphinx' );
42     mkdir $tmp;
43
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;
47
48     $sphinx{'config'} = File::Spec->catfile( $tmp, 'sphinx.conf' );
49     {
50         open my $fh, ">", $sphinx{'config'};
51         print $fh $sphinx_conf;
52         close $fh;
53     }
54
55     sync_index();
56
57     {
58         my ($exit_code, $output) = RT::Test->run_and_capture(
59             command => $sphinx{'searchd'},
60             config => $sphinx{'config'},
61         );
62         ok(!$exit_code, "setted up index") or diag "output: $output";
63         $sphinx{'started'} = 1 if !$exit_code;
64     }
65 }
66
67 sub sync_index {
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') : (),
73     ;
74     my $output = <$fh>;
75     close $fh;
76     my $exit_code = $?>>8;
77     ok(!$exit_code, "indexed") or diag "output: $output";
78
79     # We may need to wait a second for searchd to pick up the changes
80     sleep 1;
81 }
82
83 sub run_tests {
84     my @test = @_;
85     while ( my ($query, $checks) = splice @test, 0, 2 ) {
86         run_test( $query, %$checks );
87     }
88 }
89
90 my @tickets;
91 sub run_test {
92     my ($query, %checks) = @_;
93     my $query_prefix = join ' OR ', map 'id = '. $_->id, @tickets;
94
95     my $tix = RT::Tickets->new(RT->SystemUser);
96     $tix->FromSQL( "( $query_prefix ) AND ( $query )" );
97
98     my $error = 0;
99
100     my $count = 0;
101     $count++ foreach grep $_, values %checks;
102     is($tix->Count, $count, "found correct number of ticket(s) by '$query'") or $error = 1;
103
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";
108         $good_tickets = 0;
109     }
110     ok( $good_tickets, "all tickets are good with '$query'" ) or $error = 1;
111
112     diag "Wrong SQL query for '$query':". $tix->BuildSelectQuery if $error;
113 }
114
115 @tickets = RT::Test->create_tickets(
116     { Queue => $q->id },
117     { Subject => 'book', Content => 'book' },
118     { Subject => 'bar', Content => 'bar' },
119 );
120 sync_index();
121
122 run_tests(
123     "Content LIKE 'book'" => { book => 1, bar => 0 },
124     "Content LIKE 'bar'" => { book => 0, bar => 1 },
125 );
126
127 END {
128     my $Test = RT::Test->builder;
129     return if $Test->{Original_Pid} != $$;
130     return unless $sphinx{'started'};
131
132     my $pid = int RT::Test->file_content([$sphinx{'directory'}, 'searchd.pid']);
133     kill TERM => $pid if $pid;
134 }