Bug 20434: Update UNIMARC framework - auth (TM)
[koha.git] / t / db_dependent / SuggestionEngine_ExplodedTerms.t
blobbac1b41c2ea10ee72cc5a7ea12483510d42b1fda
1 #!/usr/bin/perl
3 use strict;
4 use warnings;
6 use Cwd qw/abs_path/;
7 use File::Basename;
8 use File::Spec;
9 use Test::More;
10 use Test::MockModule;
11 use Test::Warn;
13 my $contextModule = new Test::MockModule('C4::Context');
14 $contextModule->mock('preference', sub {
15 return '';
16 });
17 $contextModule->mock('config', sub {
18 my ($self,$key) = @_;
19 if ($key eq 'opachtdocs') {
20 return get_where() . '/koha-tmpl/opac-tmpl';
21 } elsif ($key eq 'intrahtdocs') {
22 return get_where() . '/koha-tmpl/intranet-tmpl';
23 } else {
24 return '';
26 });
28 use_ok('Koha::SuggestionEngine');
30 sub get_where {
31 my $location = File::Spec->rel2abs(dirname(__FILE__));
32 if ($location =~ /db_dependent/) {
33 $location .= '/../..';
35 else {
36 $location .= '/..';
38 return abs_path($location);
41 my $langModule;
42 if (! defined $ENV{KOHA_CONF}) {
43 warning_like { $langModule = new Test::MockModule('C4::Languages'); }
44 qr /unable to locate Koha configuration file koha-conf.xml/,
45 'Expected warning for unset $KOHA_CONF';
47 else {
48 $langModule = new Test::MockModule('C4::Languages');
50 $langModule->mock('regex_lang_subtags', sub {
51 return {
52 'extension' => undef,
53 'script' => undef,
54 'privateuse' => undef,
55 'variant' => undef,
56 'language' => 'en',
57 'region' => undef,
58 'rfc4646_subtag' => 'en'
60 });
61 $langModule->mock('getTranslatedLanguages', sub {
62 return [
64 'sublanguages_loop' => [
66 'script' => undef,
67 'extension' => undef,
68 'language' => 'en',
69 'region' => undef,
70 'region_description' => undef,
71 'sublanguage_current' => 1,
72 'privateuse' => undef,
73 'variant' => undef,
74 'variant_description' => undef,
75 'script_description' => undef,
76 'rfc4646_subtag' => 'en',
77 'native_description' => 'English',
78 'enabled' => 1
81 'plural' => 1,
82 'language' => 'en',
83 'current' => 1,
84 'native_description' => 'English',
85 'rfc4646_subtag' => 'en',
86 'group_enabled' => 1
89 });
90 my $tmplModule;
91 if (! defined $ENV{KOHA_CONF}) {
92 warning_like { $tmplModule = new Test::MockModule('C4::Templates'); }
93 qr /unable to locate Koha configuration file koha-conf.xml/,
94 'Expected warning for unset $KOHA_CONF';
96 else {
97 $tmplModule = new Test::MockModule('C4::Templates');
99 $tmplModule->mock('_get_template_file', sub {
100 my ($tmplbase, $interface, $query) = @_;
101 my $opactmpl = get_where() . '/koha-tmpl/opac-tmpl';
102 return ($opactmpl, 'bootstrap', 'en', "$opactmpl/bootstrap/en/modules/$tmplbase");
105 my $suggestor = Koha::SuggestionEngine->new( { plugins => [ 'ExplodedTerms' ] } );
106 is(ref($suggestor), 'Koha::SuggestionEngine', 'Created suggestion engine');
108 my $result = $suggestor->get_suggestions({search => 'Cookery'});
110 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'");
112 $result = $suggestor->get_suggestions({search => 'su:Cookery'});
114 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'");
116 $result = $suggestor->get_suggestions({search => 'nt:Cookery'});
118 is(scalar @$result, 0, "No suggestions for fielded search");
120 $result = $suggestor->get_suggestions({search => 'ccl=su:Cookery'});
122 is(scalar @$result, 0, "No suggestions for CCL search");
124 done_testing();