Skip to content

Commit 381b00b

Browse files
Merge branch '5.0/email-lookup-fulltext-2' into 5.0-trunk
2 parents 3a1ada1 + f2e4629 commit 381b00b

2 files changed

Lines changed: 131 additions & 1 deletion

File tree

html/RTIR/Tools/Elements/LookupRelatedTickets

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,27 @@ if ( $LookupType && RT::IR->CustomFields( Field => $LookupType ) ) {
7272
unless (RT->Config->Get('FullTextSearch')->{Enable}) {
7373
RT->Logger->error("Asked to run a full text search from Lookup.html without full text search enabled, see %FullTextSearch in RT_Config.pm");
7474
}
75-
$query = "Content LIKE '$q'";
75+
# Email addresses, hostnames, etc. contain characters that several
76+
# FTS engines treat as reserved operators ('@' and '-' are
77+
# proximity/NOT in MySQL/MariaDB BOOLEAN MODE; both are reserved in
78+
# Oracle Text). Wrap the term using each engine's literal/phrase
79+
# syntax so it is searched verbatim. Pg's plainto_tsquery()
80+
# tokenizes and ignores punctuation, so no wrapping is required
81+
# there.
82+
my $db_type = RT->Config->Get('DatabaseType');
83+
if ( $db_type eq 'mysql' ) {
84+
my $phrase = $q;
85+
$phrase =~ s/(["\\])/\\$1/g;
86+
$query = qq{Content LIKE '"$phrase"'};
87+
}
88+
elsif ( $db_type eq 'Oracle' ) {
89+
my $phrase = $q;
90+
$phrase =~ s/([\\}])/\\$1/g;
91+
$query = qq{Content LIKE '{$phrase}'};
92+
}
93+
else {
94+
$query = "Content LIKE '$q'";
95+
}
7696
}
7797
my $age = RT->Config->Get('RTIR_OldestRelatedTickets') || 60;
7898
$query = "$query AND LastUpdated > '$age days ago'";

t/tools/lookup_fts.t

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
use strict;
2+
use warnings;
3+
4+
use RT::IR::Test tests => undef;
5+
6+
my $dbtype = RT->Config->Get('DatabaseType');
7+
if ( $dbtype eq 'SQLite' ) {
8+
diag "Indexed FTS is not supported on SQLite, skipping FTS-dependent assertions";
9+
done_testing;
10+
exit 0;
11+
}
12+
13+
# Configure FTS for the active database type, mirroring RT's per-DB FTS
14+
# test setup (t/fts/indexed_mysql.t, indexed_pg.t, indexed_oracle.t).
15+
my %fts_config = ( Enable => 1, Indexed => 1 );
16+
if ( $dbtype eq 'Pg' ) {
17+
$fts_config{Column} = 'ContentIndex';
18+
$fts_config{Table} = 'AttachmentsIndex';
19+
}
20+
elsif ( $dbtype eq 'Oracle' ) {
21+
$fts_config{IndexName} = 'rt_fts_index';
22+
}
23+
else {
24+
# mysql/mariadb
25+
$fts_config{Table} = 'AttachmentsIndex';
26+
}
27+
RT->Config->Set( FullTextSearch => %fts_config );
28+
29+
use RT::Test::FTS;
30+
RT::Test::FTS->setup_indexing();
31+
32+
RT::Test->started_ok;
33+
my $agent = default_agent();
34+
35+
my $email = 'lookup-test@example.com';
36+
37+
my $ir = $agent->create_ir(
38+
{ Subject => 'Email lookup FTS test', Content => "Investigation involving $email" },
39+
);
40+
41+
RT::Test::FTS->sync_index();
42+
43+
diag "Test Lookup page with an email address (containing '\@') and FTS enabled";
44+
{
45+
$agent->get_ok(
46+
"/RTIR/Tools/Lookup.html?ticket=$ir&type=email&q=$email",
47+
"Loaded Lookup page for $email",
48+
);
49+
$agent->content_contains( $email, 'Lookup page rendered for the email' );
50+
$agent->content_like(
51+
qr{Incident Reports: \Q$email\E.*?Email lookup FTS test}s,
52+
'Found the test ticket in search results on lookup',
53+
);
54+
55+
$agent->content_lacks(
56+
'(no Incident Reports)',
57+
'IR appears in the Incident Reports lookup results',
58+
);
59+
}
60+
61+
my $host = 'bad-host.example.com';
62+
63+
my $host_ir = $agent->create_ir(
64+
{ Subject => 'Host lookup FTS test', Content => "Investigation involving $host" },
65+
);
66+
67+
RT::Test::FTS->sync_index();
68+
69+
diag "Test Lookup page with a hostname (containing '-') and FTS enabled";
70+
{
71+
$agent->get_ok(
72+
"/RTIR/Tools/Lookup.html?ticket=$host_ir&type=host&q=$host",
73+
"Loaded Lookup page for $host",
74+
);
75+
$agent->content_contains( $host, 'Lookup page rendered for the host' );
76+
$agent->content_like(
77+
qr{Incident Reports: \Q$host\E.*?Host lookup FTS test}s,
78+
'Found the test ticket in search results on lookup',
79+
);
80+
81+
$agent->content_lacks(
82+
'(no Incident Reports)',
83+
'IR appears in the Incident Reports lookup results',
84+
);
85+
}
86+
87+
diag "Test Lookup page with values that do not appear in any ticket";
88+
{
89+
my $missing_email = 'nomatch-test@example.org';
90+
$agent->get_ok(
91+
"/RTIR/Tools/Lookup.html?ticket=$ir&type=email&q=$missing_email",
92+
"Loaded Lookup page for $missing_email",
93+
);
94+
$agent->content_contains(
95+
'(no Incident Reports)',
96+
'No IR is reported for an email that is not indexed',
97+
);
98+
99+
my $missing_host = 'nonexistent-host.example.org';
100+
$agent->get_ok(
101+
"/RTIR/Tools/Lookup.html?ticket=$host_ir&type=host&q=$missing_host",
102+
"Loaded Lookup page for $missing_host",
103+
);
104+
$agent->content_contains(
105+
'(no Incident Reports)',
106+
'No IR is reported for a host that is not indexed',
107+
);
108+
}
109+
110+
done_testing;

0 commit comments

Comments
 (0)