Bug 22053: (QA follow-up) More tests
authorTomas Cohen Arazi <tomascohen@theke.io>
Mon, 29 Apr 2019 18:21:32 +0000 (15:21 -0300)
committerNick Clemens <nick@bywatersolutions.com>
Thu, 9 May 2019 18:54:45 +0000 (18:54 +0000)
This patch adds tests for:
- $plugin->enable and $plugin->disable are chainable (i.e. return $self)
- Disabled plugins don't inject routes

To test:
- Apply this patch
- Run:
  $ kshell
 k$ prove t/db_dependent/Koha/REST/Plugin/PluginRoutes.t \
          t/db_dependent/Plugins.t
=> SUCCESS: Tests pass

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

Koha/Plugins/Base.pm
t/db_dependent/Koha/REST/Plugin/PluginRoutes.t
t/db_dependent/Plugins.t

index 20d8b79..392ab4c 100644 (file)
@@ -300,6 +300,8 @@ sub disable {
     my ($self) = @_;
 
     $self->store_data( {'__ENABLED__' => 0}  );
+
+    return $self;
 }
 
 1;
index 06fecd3..cc50b56 100644 (file)
@@ -17,7 +17,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 1;
+use Test::More tests => 2;
 use Test::Mojo;
 use Test::Warn;
 
@@ -35,18 +35,28 @@ BEGIN {
     t::lib::Mocks::mock_config( 'pluginsdir', $path );
 }
 
+use Koha::Database;
+use Koha::Plugins;
+
+my $schema = Koha::Database->new->schema;
+
 subtest 'Bad plugins tests' => sub {
 
     plan tests => 3;
 
+    $schema->storage->txn_begin;
+
     # enable plugins
     t::lib::Mocks::mock_config( 'enable_plugins', 1 );
     t::lib::Mocks::mock_preference( 'UseKohaPlugins', 1 );
 
+    my @plugins = Koha::Plugins->new->GetPlugins( { all => 1 } );
+    foreach my $plugin (@plugins) {
+        $plugin->enable;
+    }
+
     # initialize Koha::REST::V1 after mocking
-    my $remote_address = '127.0.0.1';
     my $t;
-
     warning_is
         { $t = Test::Mojo->new('Koha::REST::V1'); }
         'The resulting spec is invalid. Skipping Bad API Route Plugin',
@@ -56,6 +66,44 @@ subtest 'Bad plugins tests' => sub {
     ok( !exists $routes->{'/contrib/badass/patrons/(:patron_id)/bother_wrong'}, 'Route doesn\'t exist' );
     ok( exists $routes->{'/contrib/testplugin/patrons/(:patron_id)/bother'}, 'Route exists' );
 
+    $schema->storage->txn_rollback;
+};
+
+subtest 'Disabled plugins tests' => sub {
+
+    plan tests => 2;
+
+    $schema->storage->txn_begin;
+
+    # enable plugins
+    t::lib::Mocks::mock_config( 'enable_plugins', 1 );
+    t::lib::Mocks::mock_preference( 'UseKohaPlugins', 1 );
+
+    my $good_plugin;
+
+    my @plugins = Koha::Plugins->new->GetPlugins( { all => 1 } );
+    foreach my $plugin (@plugins) {
+        $plugin->disable;
+        $good_plugin = $plugin
+            if $plugin->{metadata}->{description} eq 'Test plugin';
+    }
+
+    # initialize Koha::REST::V1 after mocking
+    my $t = Test::Mojo->new('Koha::REST::V1');
+
+    my $routes = get_defined_routes($t);
+    ok( !exists $routes->{'/contrib/testplugin/patrons/(:patron_id)/bother'},
+        'Plugin disabled, route not defined' );
+
+    $good_plugin->enable;
+
+    $t      = Test::Mojo->new('Koha::REST::V1');
+    $routes = get_defined_routes($t);
+
+    ok( exists $routes->{'/contrib/testplugin/patrons/(:patron_id)/bother'},
+        'Plugin enabled, route defined' );
+
+    $schema->storage->txn_rollback;
 };
 
 sub get_defined_routes {
index f34500e..e89cebe 100755 (executable)
@@ -9,7 +9,7 @@ use File::Temp qw( tempdir tempfile );
 use FindBin qw($Bin);
 use Module::Load::Conditional qw(can_load);
 use Test::MockModule;
-use Test::More tests => 44;
+use Test::More tests => 46;
 
 use C4::Context;
 use Koha::Database;
@@ -77,7 +77,8 @@ close $fh;
 my $classname = ref($plugin);
 like( $plugin->test_template($fn), qr/^I am $fn/, 'Template works' );
 
-$plugin->enable;
+my $result = $plugin->enable;
+is( ref($result), 'Koha::Plugin::Test' );
 
 # testing GetPlugins
 my @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
@@ -99,7 +100,8 @@ my @plugins2 = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
 });
 isnt( scalar @plugins2, scalar @plugins, 'GetPlugins with two metadata conditions' );
 
-$plugin->disable;
+$result = $plugin->disable;
+is( ref($result), 'Koha::Plugin::Test' );
 
 @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins();
 @names = map { $_->get_metadata()->{'name'} } @plugins;