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