Bug 31069: Remove ExplodedTerms dependency on templates

This patch removes the use of templates and CGI in ExplodedTerms by using the
Koha::I18N library.

The functionality on the package is too simple, and messing with the
template paths complexity on the tests was too much, given we have a
nice way to have translatable strings at the package level.

This patch does that, and cleans up the test file as well, that required
complex template and CGI mocking to run properly.

To test:
1. Run:
   $ kshell
  k$ prove t/db_dependent/SuggestionEngine_ExplodedTerms.t
=> FAIL: It fails in master
2. Apply this patch
3. Repeat 1
=> SUCCESS: Tests pass
4. Follow the original test plan
=> SUCCESS: Things work
5. Enjoy the rest of the day and forget this ever happened

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
(cherry picked from commit ecd91d6089)

Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
This commit is contained in:
Tomás Cohen Arazi 2022-07-18 12:33:47 -03:00 committed by Lucas Gass
parent e90ddc67cf
commit e9fde507b6
3 changed files with 48 additions and 138 deletions

View file

@ -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;

View file

@ -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 -%]

View file

@ -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 <http://www.gnu.org/licenses>.
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" );