Bug 25596: DBRev 20.06.00.054
[koha.git] / t / SuggestionEngine.t
blobc8c981fa60a9f46c8c333fdbe71623a96f06ed18
1 #!/usr/bin/perl
3 use strict;
4 use warnings;
5 use File::Spec;
7 use Test::More;
9 BEGIN {
10 use_ok('Koha::SuggestionEngine');
13 my $plugindir = File::Spec->rel2abs('Koha/SuggestionEngine/Plugin');
15 opendir(my $dh, $plugindir);
16 my @installed_plugins = map { my $p = $_; ( $p =~ /\.pm$/ && -f "$plugindir/$p" && $p =~ s/\.pm$// ) ? "Koha::SuggestionEngine::Plugin::$p" : () } readdir($dh);
17 my @available_plugins = Koha::SuggestionEngine::AvailablePlugins();
19 foreach my $plugin (@installed_plugins) {
20 ok(grep($plugin, @available_plugins), "Found plugin $plugin");
23 my $suggestor = Koha::SuggestionEngine->new( { plugins => [ 'ABCD::EFGH::IJKL' ] } );
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');
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');
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');
34 my $suggestions = $suggestor->get_suggestions({ 'search' => 'books' });
36 is_deeply($suggestions->[0], { 'search' => 'book', label => 'Book!', relevance => 1 }, "Good suggestion");
38 $suggestions = $suggestor->get_suggestions({ 'search' => 'silliness' });
40 eval {
41 $suggestor = Koha::SuggestionEngine->new( { plugins => [ 'Koha::SuggestionEngine::Plugin::Null' ] } );
42 undef $suggestor;
44 ok(!$@, 'Destroyed suggestor successfully');
46 done_testing();