Bug 10694: (follow-up) fix various issues
[koha.git] / t / SuggestionEngine.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use File::Spec;
6
7 use Test::More;
8
9 BEGIN {
10         use_ok('Koha::SuggestionEngine');
11 }
12
13 my $plugindir = File::Spec->rel2abs('Koha/SuggestionEngine/Plugin');
14
15 opendir(my $dh, $plugindir);
16 my @installed_plugins = map { ( /\.pm$/ && -f "$plugindir/$_" && s/\.pm$// ) ? "Koha::SuggestionEngine::Plugin::$_" : () } readdir($dh);
17 my @available_plugins = Koha::SuggestionEngine::AvailablePlugins();
18
19 foreach my $plugin (@installed_plugins) {
20     ok(grep($plugin, @available_plugins), "Found plugin $plugin");
21 }
22
23 my $suggestor = Koha::SuggestionEngine->new( { plugins => [ 'ABCD::EFGH::IJKL' ] } );
24
25 is(ref($suggestor), 'Koha::SuggestionEngine', 'Created suggestion engine with invalid plugin');
26 is(scalar @{ $suggestor->get_suggestions({ 'search' => 'books' }) }, 0 , 'Request suggestions with empty suggestor');
27
28 $suggestor = Koha::SuggestionEngine->new( { plugins => [ 'Null' ] } );
29 is(ref($suggestor->plugins->[0]), 'Koha::SuggestionEngine::Plugin::Null', 'Created record suggestor with implicitly scoped Null filter');
30
31 $suggestor = Koha::SuggestionEngine->new( { plugins => [ 'Koha::SuggestionEngine::Plugin::Null' ] } );
32 is(ref($suggestor->plugins->[0]), 'Koha::SuggestionEngine::Plugin::Null', 'Created record suggestor with explicitly scoped Null filter');
33
34 my $suggestions = $suggestor->get_suggestions({ 'search' => 'books' });
35
36 is_deeply($suggestions->[0], { 'search' => 'book', label => 'Book!', relevance => 1 }, "Good suggestion");
37
38 $suggestions = $suggestor->get_suggestions({ 'search' => 'silliness' });
39
40 eval {
41     $suggestor = Koha::SuggestionEngine->new( { plugins => [ 'Koha::SuggestionEngine::Plugin::Null' ] } );
42     undef $suggestor;
43 };
44 ok(!$@, 'Destroyed suggestor successfully');
45
46 done_testing();