use YAML;
use C4::Debug;
-use C4::Context;
+require C4::Context;
-use Test::More tests => 57;
+use Test::More tests => 78;
use Test::MockModule;
use MARC::Record;
use File::Spec;
use File::Basename;
use File::Find;
use Test::Warn;
+use File::Temp qw/ tempdir /;
+use File::Path;
+use DBI;
-system(dirname(__FILE__) . "/zebra_config.pl");
-
-my $datadir = dirname(__FILE__) . "/data";
-
-use_ok('C4::Search');
-
-foreach my $string ("Leçon","modèles") {
- my @results=C4::Search::_remove_stopwords($string,"kw");
- $debug && warn "$string ",Dump(@results);
- ok($results[0] eq $string,"$string is not modified");
-}
-
-foreach my $string ("A book about the stars") {
- my @results=C4::Search::_remove_stopwords($string,"kw");
- $debug && warn "$string ",Dump(@results);
- ok($results[0] ne $string,"$results[0] from $string");
-}
-
-my $indexes = C4::Search::getIndexes();
-is(scalar(grep(/^ti$/, @$indexes)), 1, "Title index supported");
+my $datadir = tempdir();
+system(dirname(__FILE__) . "/zebra_config.pl $datadir");
+my $sourcedir = dirname(__FILE__) . "/data";
my $QueryStemming = 0;
my $QueryAutoTruncate = 0;
my $QueryWeightFields = 0;
my $QueryFuzzy = 0;
my $QueryRemoveStopwords = 0;
+my $UseQueryParser = 0;
my $contextmodule = new Test::MockModule('C4::Context');
$contextmodule->mock('_new_dbh', sub {
my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
return $dbh });
$contextmodule->mock('preference', sub {
my ($self, $pref) = @_;
- if ($pref eq 'NoZebra') {
- return 0;
- } elsif ($pref eq 'marcflavour') {
+ if ($pref eq 'marcflavour') {
return 'MARC21';
} elsif ($pref eq 'QueryStemming') {
return $QueryStemming;
return $QueryFuzzy;
} elsif ($pref eq 'QueryRemoveStopwords') {
return $QueryRemoveStopwords;
+ } elsif ($pref eq 'UseQueryParser') {
+ return $UseQueryParser;
} elsif ($pref eq 'maxRecordsForFacets') {
return 20;
} elsif ($pref eq 'FacetLabelTruncationLength') {
return '490av';
} else {
warn "The syspref $pref was requested but I don't know what to say; this indicates that the test requires updating"
- unless $pref =~ m/(XSLT|item|branch|holding|image|insecure)/i;
+ unless $pref =~ m/(XSLT|item|branch|holding|image)/i;
return 0;
}
});
);
return \%hash;
});
+$contextmodule->mock('queryparser', sub {
+ my $QParser = Koha::QueryParser::Driver::PQF->new();
+ $QParser->load_config("$datadir/etc/searchengine/queryparser.yaml");
+ return $QParser;
+});
+my $context = new C4::Context("$datadir/etc/koha-conf.xml");
+$context->set_context();
+
+use_ok('C4::Search');
+
+foreach my $string ("Leçon","modèles") {
+ my @results=C4::Search::_remove_stopwords($string,"kw");
+ $debug && warn "$string ",Dump(@results);
+ ok($results[0] eq $string,"$string is not modified");
+}
+
+foreach my $string ("A book about the stars") {
+ my @results=C4::Search::_remove_stopwords($string,"kw");
+ $debug && warn "$string ",Dump(@results);
+ ok($results[0] ne $string,"$results[0] from $string");
+}
+
+my $indexes = C4::Search::getIndexes();
+is(scalar(grep(/^ti$/, @$indexes)), 1, "Title index supported");
+
my $bibliomodule = new Test::MockModule('C4::Biblio');
$bibliomodule->mock('_get_inverted_marc_field_map', sub {
my %hash = (
unlink("$datadir/zebra.log");
system("zebraidx -c $datadir/etc/koha/zebradb/zebra-biblios.cfg -v none,fatal,warn -g iso2709 -d biblios init");
-system("zebraidx -c $datadir/etc/koha/zebradb/zebra-biblios.cfg -v none,fatal,warn -g iso2709 -d biblios update $datadir/zebraexport/biblio");
+system("zebraidx -c $datadir/etc/koha/zebradb/zebra-biblios.cfg -v none,fatal,warn -g iso2709 -d biblios update $sourcedir/zebraexport/biblio");
system("zebraidx -c $datadir/etc/koha/zebradb/zebra-biblios.cfg -v none,fatal,warn -g iso2709 -d biblios commit");
my $child = fork();
($biblionumber,undef,$title) = FindDuplicate($record);
is($biblionumber, 203, 'Found duplicate with author/title');
-my $context = new C4::Context("$datadir/etc/koha-conf.xml");
-$context->set_context();
-
# Testing SimpleSearch
my ( $error, $marcresults, $total_hits ) = SimpleSearch("book", 0, 9);
$results_hashref->{'biblioserver'}->{"RECORDS"});
is($newresults[0]->{'alternateholdings_count'}, 1, 'Alternate holdings filled in correctly');
-kill 9, $child;
+END {
+ if ($child) {
+ kill 9, $child;
-# Clean up the Zebra files since the child process was just shot
+ # Clean up the Zebra files since the child process was just shot
+ rmtree $datadir;
+ }
+}
+
+# Testing exploding indexes
+my $term;
+my $searchmodule = new Test::MockModule('C4::Search');
+$searchmodule->mock('SimpleSearch', sub {
+ my $query = shift;
+
+ is($query, "he:$term", "Searching for expected term '$term' for exploding") or return '', [], 0;
+
+ my $record = MARC::Record->new;
+ if ($query =~ m/Arizona/) {
+ $record->add_fields(
+ [ '001', '1234' ],
+ [ '151', ' ', ' ', a => 'Arizona' ],
+ [ '551', ' ', ' ', a => 'United States', w => 'g' ],
+ [ '551', ' ', ' ', a => 'Maricopa County', w => 'h' ],
+ [ '551', ' ', ' ', a => 'Navajo County', w => 'h' ],
+ [ '551', ' ', ' ', a => 'Pima County', w => 'h' ],
+ [ '551', ' ', ' ', a => 'New Mexico' ],
+ );
+ }
+ return '', [ $record->as_usmarc() ], 1;
+});
+
+$UseQueryParser = 1;
+$term = 'Arizona';
+( $error, $query, $simple_query, $query_cgi,
+$query_desc, $limit, $limit_cgi, $limit_desc,
+$stopwords_removed, $query_type ) = buildQuery([], [ $term ], [ 'su-br' ], [ ], [], 0, 'en');
+matchesExplodedTerms("Advanced search for broader subjects", $query, 'Arizona', 'United States');
+
+( $error, $query, $simple_query, $query_cgi,
+$query_desc, $limit, $limit_cgi, $limit_desc,
+$stopwords_removed, $query_type ) = buildQuery([], [ $term ], [ 'su-na' ], [ ], [], 0, 'en');
+matchesExplodedTerms("Advanced search for narrower subjects", $query, 'Arizona', 'Maricopa County', 'Navajo County', 'Pima County');
+
+( $error, $query, $simple_query, $query_cgi,
+$query_desc, $limit, $limit_cgi, $limit_desc,
+$stopwords_removed, $query_type ) = buildQuery([], [ $term ], [ 'su-rl' ], [ ], [], 0, 'en');
+matchesExplodedTerms("Advanced search for related subjects", $query, 'Arizona', 'United States', 'Maricopa County', 'Navajo County', 'Pima County');
-find(sub { unlink($_) if ( -f $_ && m/\.(mf|pid|LCK)$/ ); }, "$datadir");
-unlink("$datadir/var/run/zebradb/authoritysocket");
-unlink("$datadir/var/run/zebradb/bibliosocket");
+( $error, $query, $simple_query, $query_cgi,
+$query_desc, $limit, $limit_cgi, $limit_desc,
+$stopwords_removed, $query_type ) = buildQuery([], [ "$term", 'history' ], [ 'su-rl', 'kw' ], [ ], [], 0, 'en');
+matchesExplodedTerms("Advanced search for related subjects and keyword 'history' searches related subjects", $query, 'Arizona', 'United States', 'Maricopa County', 'Navajo County', 'Pima County');
+like($query, qr/history/, "Advanced search for related subjects and keyword 'history' searches for 'history'");
+
+( $error, $query, $simple_query, $query_cgi,
+$query_desc, $limit, $limit_cgi, $limit_desc,
+$stopwords_removed, $query_type ) = buildQuery([], [ 'history', "$term" ], [ 'kw', 'su-rl' ], [ ], [], 0, 'en');
+matchesExplodedTerms("Order of terms doesn't matter for advanced search", $query, 'Arizona', 'United States', 'Maricopa County', 'Navajo County', 'Pima County');
+like($query, qr/history/, "Order of terms doesn't matter for advanced search");
+
+( $error, $query, $simple_query, $query_cgi,
+$query_desc, $limit, $limit_cgi, $limit_desc,
+$stopwords_removed, $query_type ) = buildQuery([], [ "su-br($term)" ], [ ], [ ], [], 0, 'en');
+matchesExplodedTerms("Simple search for broader subjects", $query, 'Arizona', 'United States');
+
+( $error, $query, $simple_query, $query_cgi,
+$query_desc, $limit, $limit_cgi, $limit_desc,
+$stopwords_removed, $query_type ) = buildQuery([], [ "su-na($term)" ], [ ], [ ], [], 0, 'en');
+matchesExplodedTerms("Simple search for narrower subjects", $query, 'Arizona', 'Maricopa County', 'Navajo County', 'Pima County');
+
+( $error, $query, $simple_query, $query_cgi,
+$query_desc, $limit, $limit_cgi, $limit_desc,
+$stopwords_removed, $query_type ) = buildQuery([], [ "su-rl($term)" ], [ ], [ ], [], 0, 'en');
+matchesExplodedTerms("Simple search for related subjects", $query, 'Arizona', 'United States', 'Maricopa County', 'Navajo County', 'Pima County');
+
+( $error, $query, $simple_query, $query_cgi,
+$query_desc, $limit, $limit_cgi, $limit_desc,
+$stopwords_removed, $query_type ) = buildQuery([], [ "history && su-rl($term)" ], [ ], [ ], [], 0, 'en');
+matchesExplodedTerms("Simple search for related subjects and keyword 'history' searches related subjects", $query, 'Arizona', 'United States', 'Maricopa County', 'Navajo County', 'Pima County');
+like($query, qr/history/, "Simple search for related subjects and keyword 'history' searches for 'history'");
+
+sub matchesExplodedTerms {
+ my ($message, $query, @terms) = @_;
+ my $match = '(' . join ('|', map { " \@attr 1=Subject \@attr 4=1 \"$_\"" } @terms) . "){" . scalar(@terms) . "}";
+ like($query, qr/$match/, $message);
+}
1;