@@ -, +, @@ $ kshell k$ prove t/db_dependent/SuggestionEngine_ExplodedTerms.t --- Koha/SuggestionEngine/Plugin/ExplodedTerms.pm | 36 +++-- .../en/modules/text/explodedterms.tt | 9 -- .../SuggestionEngine_ExplodedTerms.t | 141 ++++-------------- 3 files changed, 48 insertions(+), 138 deletions(-) delete mode 100644 koha-tmpl/opac-tmpl/bootstrap/en/modules/text/explodedterms.tt --- a/Koha/SuggestionEngine/Plugin/ExplodedTerms.pm +++ a/Koha/SuggestionEngine/Plugin/ExplodedTerms.pm @@ -32,10 +32,11 @@ subjects to subject searches. =cut use Modern::Perl; -use C4::Templates qw(gettemplate); # This is necessary for translatability use base qw(Koha::SuggestionEngine::Base); +use Koha::I18N qw(__); + =head2 NAME my $name = $plugin->NAME; @@ -64,8 +65,7 @@ terms to the search. =cut sub get_suggestions { - my $self = shift; - my $param = shift; + my ( $self, $param ) = @_; my $search = $param->{'search'}; @@ -73,29 +73,27 @@ sub get_suggestions { $search =~ s/(su|su-br|su-na|su-rl)[:=](\w*)/OP!$2/g; return if ( $search =~ m/\w+[:=]\w+/ ); - my @indexes = ( - 'su-na', - 'su-br', - 'su-rl' - ); - my $cgi = CGI->new; - my $template = C4::Templates::gettemplate('text/explodedterms.tt', 'opac', $cgi); + my $indexes_to_label = { + 'su-na' => __('Search also for narrower subjects'), + 'su-br' => __('Search also for broader subjects'), + 'su-rl' => __('Search also for related subjects'), + }; + my @results; - foreach my $index (@indexes) { + foreach my $index ( keys %{$indexes_to_label} ) { my $thissearch = $search; $thissearch = "$index:$thissearch" unless ( $thissearch =~ s/OP!/$index:/g ); - $template->{VARS}->{index} = $index; - my $label = $template->output; - push @results, - { + push @results, { 'search' => $thissearch, relevance => 100, - # FIXME: it'd be nice to have some empirical measure of - # "relevance" in this case, but we don't. - label => $label + + # FIXME: it'd be nice to have some empirical measure of + # "relevance" in this case, but we don't. + label => $indexes_to_label->{$index} }; - } return \@results; + } + return \@results; } 1; --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/text/explodedterms.tt +++ a/koha-tmpl/opac-tmpl/bootstrap/en/modules/text/explodedterms.tt @@ -1,9 +0,0 @@ -[% PROCESS 'i18n.inc' %] -[%- SWITCH index -%] -[%- CASE 'su-na' -%] -[% t('Search also for narrower subjects') | html %] -[%- CASE 'su-br' -%] -[% t('Search also for broader subjects') | html %] -[%- CASE 'su-rl' -%] -[% t('Search also for related subjects') | html %] -[%- END -%] --- a/t/db_dependent/SuggestionEngine_ExplodedTerms.t +++ a/t/db_dependent/SuggestionEngine_ExplodedTerms.t @@ -1,124 +1,45 @@ #!/usr/bin/perl -use strict; -use warnings; +# This file is part of Koha +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . -use Cwd qw/abs_path/; -use File::Basename; -use File::Spec; -use Test::More; -use Test::MockModule; -use Test::Warn; +use Modern::Perl; -my $contextModule = Test::MockModule->new('C4::Context'); -$contextModule->mock('preference', sub { - return ''; -}); -$contextModule->mock('config', sub { - my ($self,$key) = @_; - if ($key eq 'opachtdocs') { - return get_where() . '/koha-tmpl/opac-tmpl'; - } elsif ($key eq 'intrahtdocs') { - return get_where() . '/koha-tmpl/intranet-tmpl'; - } else { - return ''; - } -}); +use Test::More tests => 5; -use_ok('Koha::SuggestionEngine'); +use Koha::SuggestionEngine; -sub get_where { - my $location = File::Spec->rel2abs(dirname(__FILE__)); - if ($location =~ /db_dependent/) { - $location .= '/../..'; - } - else { - $location .= '/..'; - } - return abs_path($location); -} +my $suggestor = Koha::SuggestionEngine->new( { plugins => ['ExplodedTerms'] } ); +is( ref($suggestor), 'Koha::SuggestionEngine', 'Created suggestion engine' ); -my $langModule; -if (! defined $ENV{KOHA_CONF}) { - warning_like { $langModule = Test::MockModule->new('C4::Languages'); } - qr /unable to locate Koha configuration file koha-conf.xml/, - 'Expected warning for unset $KOHA_CONF'; -} -else { - $langModule = Test::MockModule->new('C4::Languages'); -} -$langModule->mock('regex_lang_subtags', sub { - return { - 'extension' => undef, - 'script' => undef, - 'privateuse' => undef, - 'variant' => undef, - 'language' => 'en', - 'region' => undef, - 'rfc4646_subtag' => 'en' - }; -}); -$langModule->mock('getTranslatedLanguages', sub { - return [ - { - 'sublanguages_loop' => [ - { - 'script' => undef, - 'extension' => undef, - 'language' => 'en', - 'region' => undef, - 'region_description' => undef, - 'sublanguage_current' => 1, - 'privateuse' => undef, - 'variant' => undef, - 'variant_description' => undef, - 'script_description' => undef, - 'rfc4646_subtag' => 'en', - 'native_description' => 'English', - 'enabled' => 1 - }, - ], - 'plural' => 1, - 'language' => 'en', - 'current' => 1, - 'native_description' => 'English', - 'rfc4646_subtag' => 'en', - 'group_enabled' => 1 - } - ]; -}); -my $tmplModule; -if (! defined $ENV{KOHA_CONF}) { - warning_like { $tmplModule = Test::MockModule->new('C4::Templates'); } - qr /unable to locate Koha configuration file koha-conf.xml/, - 'Expected warning for unset $KOHA_CONF'; -} -else { - $tmplModule = Test::MockModule->new('C4::Templates'); -} -$tmplModule->mock('_get_template_file', sub { - my ($tmplbase, $interface, $query) = @_; - my $opactmpl = get_where() . '/koha-tmpl/opac-tmpl'; - return ($opactmpl, 'bootstrap', 'en', "$opactmpl/bootstrap/en/modules/$tmplbase"); -}); +my $result = $suggestor->get_suggestions( { search => 'Cookery' } ); -my $suggestor = Koha::SuggestionEngine->new( { plugins => [ 'ExplodedTerms' ] } ); -is(ref($suggestor), 'Koha::SuggestionEngine', 'Created suggestion engine'); +ok( ( grep { $_->{'search'} eq 'su-na:Cookery' } @$result ) && ( grep { $_->{'search'} eq 'su-br:Cookery' } @$result ) && ( grep { $_->{'search'} eq 'su-rl:Cookery' } @$result ), + "Suggested correct alternatives for keyword search 'Cookery'" +); -my $result = $suggestor->get_suggestions({search => 'Cookery'}); +$result = $suggestor->get_suggestions( { search => 'su:Cookery' } ); -ok((grep { $_->{'search'} eq 'su-na:Cookery' } @$result) && (grep { $_->{'search'} eq 'su-br:Cookery' } @$result) && (grep { $_->{'search'} eq 'su-rl:Cookery' } @$result), "Suggested correct alternatives for keyword search 'Cookery'"); +ok( ( grep { $_->{'search'} eq 'su-na:Cookery' } @$result ) && ( grep { $_->{'search'} eq 'su-br:Cookery' } @$result ) && ( grep { $_->{'search'} eq 'su-rl:Cookery' } @$result ), + "Suggested correct alternatives for subject search 'Cookery'" +); -$result = $suggestor->get_suggestions({search => 'su:Cookery'}); +$result = $suggestor->get_suggestions( { search => 'nt:Cookery' } ); -ok((grep { $_->{'search'} eq 'su-na:Cookery' } @$result) && (grep { $_->{'search'} eq 'su-br:Cookery' } @$result) && (grep { $_->{'search'} eq 'su-rl:Cookery' } @$result), "Suggested correct alternatives for subject search 'Cookery'"); +is( scalar @$result, 0, "No suggestions for fielded search" ); -$result = $suggestor->get_suggestions({search => 'nt:Cookery'}); +$result = $suggestor->get_suggestions( { search => 'ccl=su:Cookery' } ); -is(scalar @$result, 0, "No suggestions for fielded search"); - -$result = $suggestor->get_suggestions({search => 'ccl=su:Cookery'}); - -is(scalar @$result, 0, "No suggestions for CCL search"); - -done_testing(); +is( scalar @$result, 0, "No suggestions for CCL search" ); --