Bug 16330: Move patches to OpenAPI
[koha.git] / t / db_dependent / SuggestionEngine_ExplodedTerms.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
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;
12
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 '';
25     }
26 });
27
28 use_ok('Koha::SuggestionEngine');
29
30 sub get_where {
31     my $location = File::Spec->rel2abs(dirname(__FILE__));
32     if ($location =~ /db_dependent/) {
33         $location .= '/../..';
34     }
35     else {
36         $location .= '/..';
37     }
38     return abs_path($location);
39 }
40
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';
46 }
47 else {
48     $langModule = new Test::MockModule('C4::Languages');
49 }
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'
59     };
60 });
61 $langModule->mock('getTranslatedLanguages', sub {
62    return [
63        {
64            'sublanguages_loop' => [
65            {
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
79            },
80            ],
81            'plural' => 1,
82            'language' => 'en',
83            'current' => 1,
84            'native_description' => 'English',
85            'rfc4646_subtag' => 'en',
86            'group_enabled' => 1
87        }
88    ];
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';
95 }
96 else {
97     $tmplModule = new Test::MockModule('C4::Templates');
98 }
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");
103 });
104
105 my $suggestor = Koha::SuggestionEngine->new( { plugins => [ 'ExplodedTerms' ] } );
106 is(ref($suggestor), 'Koha::SuggestionEngine', 'Created suggestion engine');
107
108 my $result = $suggestor->get_suggestions({search => 'Cookery'});
109
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'");
111
112 $result = $suggestor->get_suggestions({search => 'su:Cookery'});
113
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'");
115
116 $result = $suggestor->get_suggestions({search => 'nt:Cookery'});
117
118 is(scalar @$result, 0, "No suggestions for fielded search");
119
120 $result = $suggestor->get_suggestions({search => 'ccl=su:Cookery'});
121
122 is(scalar @$result, 0, "No suggestions for CCL search");
123
124 done_testing();