3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Test::More tests => 2;
27 use JSON::Validator::OpenAPI::Mojolicious;
29 # Dummy app for testing the plugin
30 use Mojolicious::Lite;
33 # Mock pluginsdir before loading Plugins module
34 my $path = dirname(__FILE__) . '/../../../../lib';
35 t::lib::Mocks::mock_config( 'pluginsdir', $path );
41 my $schema = Koha::Database->new->schema;
43 subtest 'Bad plugins tests' => sub {
47 $schema->storage->txn_begin;
50 t::lib::Mocks::mock_config( 'enable_plugins', 1 );
52 # remove any existing plugins that might interfere
53 Koha::Plugins::Methods->search->delete;
54 my $plugins = Koha::Plugins->new;
55 $plugins->InstallPlugins;
57 my @plugins = $plugins->GetPlugins( { all => 1 } );
58 foreach my $plugin (@plugins) {
62 # initialize Koha::REST::V1 after mocking
65 { $t = Test::Mojo->new('Koha::REST::V1'); }
66 'The resulting spec is invalid. Skipping Bad API Route Plugin',
67 'Bad plugins raise warning';
69 my $routes = get_defined_routes($t);
70 # Support placeholders () and <> (latter style used starting with Mojolicious::Plugin::OpenAPI@1.28)
71 # TODO: remove () if minimum version is bumped to at least 1.28.
72 ok( !exists $routes->{'/contrib/badass/patrons/(:patron_id)/bother_wrong'} && !exists $routes->{'/contrib/badass/patrons/<:patron_id>/bother_wrong'}, 'Route doesn\'t exist' );
73 ok( exists $routes->{'/contrib/testplugin/patrons/(:patron_id)/bother'} || exists $routes->{'/contrib/testplugin/patrons/<:patron_id>/bother'}, 'Route exists' );
75 $schema->storage->txn_rollback;
78 subtest 'Disabled plugins tests' => sub {
82 $schema->storage->txn_begin;
85 t::lib::Mocks::mock_config( 'enable_plugins', 1 );
89 my $plugins = Koha::Plugins->new;
90 $plugins->InstallPlugins;
92 my @plugins = $plugins->GetPlugins( { all => 1 } );
93 foreach my $plugin (@plugins) {
95 $good_plugin = $plugin
96 if $plugin->{metadata}->{description} eq 'Test plugin';
99 # initialize Koha::REST::V1 after mocking
100 my $t = Test::Mojo->new('Koha::REST::V1');
102 my $routes = get_defined_routes($t);
103 # Support placeholders () and <> (latter style used starting with Mojolicious::Plugin::OpenAPI@1.28)
104 # TODO: remove () if minimum version is bumped to at least 1.28.
105 ok( !exists $routes->{'/contrib/testplugin/patrons/(:patron_id)/bother'} && !exists $routes->{'/contrib/testplugin/patrons/<:patron_id>/bother'},
106 'Plugin disabled, route not defined' );
108 $good_plugin->enable;
110 $t = Test::Mojo->new('Koha::REST::V1');
111 $routes = get_defined_routes($t);
113 # Support placeholders () and <> (latter style used starting with Mojolicious::Plugin::OpenAPI@1.28)
114 # TODO: remove () if minimum version is bumped to at least 1.28.
115 ok( exists $routes->{'/contrib/testplugin/patrons/(:patron_id)/bother'} || exists $routes->{'/contrib/testplugin/patrons/<:patron_id>/bother'},
116 'Plugin enabled, route defined' );
118 $schema->storage->txn_rollback;
121 sub get_defined_routes {
124 traverse_routes( $_, 0, $routes ) for @{ $t->app->routes->children };
129 sub traverse_routes {
130 my ( $route, $depth, $routes ) = @_;
133 my $path = $route->pattern->unparsed || '/';
136 my $via = $route->via;
137 my $verb = !$via ? '*' : uc join ',', @$via;
138 $routes->{$path}->{$verb} = 1;
141 traverse_routes( $_, $depth, $routes ) for @{ $route->children };