PCI compliance section showing tokenization/encryption status and SAQ level
[freeside.git] / rt / t / fts / indexed_sphinx.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 skip_all => "Can't determine sphinx version"
17     unless `$sphinx{searchd} --version` =~ /Sphinx (\d+)\.(\d+)(?:\.(\d+))?/;
18
19 $sphinx{version} = sprintf "%d.%03d%03d", $1, $2, ($3 || 0);
20
21 plan tests => 15;
22
23 setup_indexing();
24
25 my $q = RT::Test->load_or_create_queue( Name => 'General' );
26 ok $q && $q->id, 'loaded or created queue';
27 my $queue = $q->Name;
28
29 sub setup_indexing {
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(
34         'no-ask'       => 1,
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',
40     );
41     ok(!$exit_code, "setted up index");
42     diag "output: $output" if $ENV{'TEST_VERBOSE'};
43
44     my $tmp = $sphinx{'directory'} = File::Spec->catdir( RT::Test->temp_directory, 'sphinx' );
45     mkdir $tmp;
46
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;
50
51     # Remove lines for different versions of sphinx than we're running
52     $sphinx_conf =~ s{^(\s+ \# \s+ for \s+ sphinx \s+
53                           (<=?|>=?|=) \s*
54                           (\d+) \. (\d+) (?:\. (\d+))?
55                           .* \n)
56                       ((?:^\s* \w .*\n)+)}{
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";
60     }emix;
61
62     $sphinx{'config'} = File::Spec->catfile( $tmp, 'sphinx.conf' );
63     {
64         open my $fh, ">", $sphinx{'config'};
65         print $fh $sphinx_conf;
66         close $fh;
67     }
68
69     sync_index();
70
71     {
72         my ($exit_code, $output) = RT::Test->run_and_capture(
73             command => $sphinx{'searchd'},
74             config => $sphinx{'config'},
75         );
76         ok(!$exit_code, "setted up index") or diag "output: $output";
77         $sphinx{'started'} = 1 if !$exit_code;
78     }
79 }
80
81 sub sync_index {
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') : (),
87     ;
88     my $output = <$fh>;
89     close $fh;
90     my $exit_code = $?>>8;
91     ok(!$exit_code, "indexed") or diag "output: $output";
92
93     # We may need to wait a second for searchd to pick up the changes
94     sleep 1;
95 }
96
97 sub run_tests {
98     my @test = @_;
99     while ( my ($query, $checks) = splice @test, 0, 2 ) {
100         run_test( $query, %$checks );
101     }
102 }
103
104 my @tickets;
105 sub run_test {
106     my ($query, %checks) = @_;
107     my $query_prefix = join ' OR ', map 'id = '. $_->id, @tickets;
108
109     my $tix = RT::Tickets->new(RT->SystemUser);
110     $tix->FromSQL( "( $query_prefix ) AND ( $query )" );
111
112     my $error = 0;
113
114     my $count = 0;
115     $count++ foreach grep $_, values %checks;
116     is($tix->Count, $count, "found correct number of ticket(s) by '$query'") or $error = 1;
117
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";
122         $good_tickets = 0;
123     }
124     ok( $good_tickets, "all tickets are good with '$query'" ) or $error = 1;
125
126     diag "Wrong SQL query for '$query':". $tix->BuildSelectQuery if $error;
127 }
128
129 @tickets = RT::Test->create_tickets(
130     { Queue => $q->id },
131     { Subject => 'book', Content => 'book' },
132     { Subject => 'bar', Content => 'bar' },
133 );
134 sync_index();
135
136 RT->Config->Set( FullTextSearch => Enable => 1, Indexed => 1, Table => 'AttachmentsIndex', MaxMatches => 1000, Sphinx => 1 );
137
138 run_tests(
139     "Content LIKE 'book'" => { book => 1, bar => 0 },
140     "Content LIKE 'bar'" => { book => 0, bar => 1 },
141 );
142
143 END {
144     my $Test = RT::Test->builder;
145     return if $Test->{Original_Pid} != $$;
146     return unless $sphinx{'started'};
147
148     my $pid = int RT::Test->file_content([$sphinx{'directory'}, 'searchd.pid']);
149     kill TERM => $pid if $pid;
150 }