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