Bug 23230: (RM follow-up) Add rollback to plugin test
[koha-equinox.git] / t / db_dependent / Plugins.t
index d686971..d5f78f9 100755 (executable)
@@ -1,15 +1,31 @@
 #!/usr/bin/perl
 
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 3 of the License, or (at your option) any later
+# version.
+#
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, see <http://www.gnu.org/licenses>.
+
 use Modern::Perl;
 
 use Archive::Extract;
 use CGI;
+use Cwd qw(abs_path);
 use File::Basename;
+use File::Spec;
 use File::Temp qw( tempdir tempfile );
 use FindBin qw($Bin);
 use Module::Load::Conditional qw(can_load);
 use Test::MockModule;
-use Test::More tests => 47;
+use Test::More tests => 52;
 
 use C4::Context;
 use Koha::Database;
@@ -18,7 +34,9 @@ use Koha::Plugins::Methods;
 use t::lib::Mocks;
 
 BEGIN {
-    push( @INC, dirname(__FILE__) . '/../lib' );
+    # Mock pluginsdir before loading Plugins module
+    my $path = dirname(__FILE__) . '/../lib';
+    t::lib::Mocks::mock_config( 'pluginsdir', $path );
 
     use_ok('Koha::Plugins');
     use_ok('Koha::Plugins::Handler');
@@ -28,9 +46,101 @@ BEGIN {
 
 my $schema = Koha::Database->new->schema;
 
+subtest 'GetPlugins() tests' => sub {
+
+    plan tests => 2;
+
+    $schema->storage->txn_begin;
+    # Temporarily remove any installed plugins data
+    Koha::Plugins::Methods->delete;
+
+    my $plugins = Koha::Plugins->new({ enable_plugins => 1 });
+    $plugins->InstallPlugins;
+
+    my @plugins = $plugins->GetPlugins({ method => 'report', all => 1 });
+
+    my @names = map { $_->get_metadata()->{'name'} } @plugins;
+    is( scalar grep( /^Test Plugin$/, @names), 1, "Koha::Plugins::GetPlugins functions correctly" );
+
+    @plugins = $plugins->GetPlugins({ metadata => { my_example_tag  => 'find_me' }, all => 1 });
+    @names = map { $_->get_metadata()->{'name'} } @plugins;
+    is( scalar @names, 2, "Only two plugins found via a metadata tag" );
+
+    $schema->storage->txn_rollback;
+};
+
+subtest 'Version upgrade tests' => sub {
+
+    plan tests => 1;
+
+    $schema->storage->txn_begin;
+
+    my $plugin = Koha::Plugin::Test->new( { enable_plugins => 1, cgi => CGI->new } );
+
+    # make sure there's no version on the DB
+    $schema->resultset('PluginData')
+        ->search( { plugin_class => $plugin->{class}, plugin_key => '__INSTALLED_VERSION__' } )
+        ->delete;
+
+    $plugin = Koha::Plugin::Test->new( { enable_plugins => 1, cgi => CGI->new } );
+    my $version = $plugin->retrieve_data('__INSTALLED_VERSION__');
+
+    is( $version, $plugin->get_metadata->{version}, 'Version has been populated correctly' );
+
+    $schema->storage->txn_rollback;
+};
+
+subtest 'Version upgrade tests' => sub {
+
+    plan tests => 1;
+
+    $schema->storage->txn_begin;
+
+    my $plugin = Koha::Plugin::Test->new( { enable_plugins => 1, cgi => CGI->new } );
+
+    # make sure there's no version on the DB
+    $schema->resultset('PluginData')
+        ->search( { plugin_class => $plugin->{class}, plugin_key => '__INSTALLED_VERSION__' } )
+        ->delete;
+
+    $plugin = Koha::Plugin::Test->new( { enable_plugins => 1, cgi => CGI->new } );
+    my $version = $plugin->retrieve_data('__INSTALLED_VERSION__');
+
+    is( $version, $plugin->get_metadata->{version}, 'Version has been populated correctly' );
+
+    $schema->storage->txn_rollback;
+};
+
+subtest 'is_enabled() tests' => sub {
+
+    plan tests => 3;
+    $schema->storage->txn_begin;
+
+    # Make sure there's no previous installs or leftovers on DB
+    Koha::Plugins::Methods->delete;
+    $schema->resultset('PluginData')->delete;
+
+    my $plugin = Koha::Plugin::Test->new({ enable_plugins => 1, cgi => CGI->new });
+    ok( $plugin->is_enabled, 'Plugins enabled by default' );
+
+    # disable
+    $plugin->disable;
+    ok( !$plugin->is_enabled, 'Calling ->disable disables the plugin' );
+
+    # enable
+    $plugin->enable;
+    ok( $plugin->is_enabled, 'Calling ->enable enabled the plugin' );
+
+    $schema->storage->txn_rollback;
+};
+
+$schema->storage->txn_begin;
+Koha::Plugins::Methods->delete;
+
 Koha::Plugins->new( { enable_plugins => 1 } )->InstallPlugins();
 
 ok( Koha::Plugins::Methods->search( { plugin_class => 'Koha::Plugin::Test' } )->count, 'Test plugin methods added to database' );
+is( Koha::Plugins::Methods->search({ plugin_class => 'Koha::Plugin::Test', plugin_method => '_private_sub' })->count, 0, 'Private methods are skipped' );
 
 my $mock_plugin = Test::MockModule->new( 'Koha::Plugin::Test' );
 $mock_plugin->mock( 'test_template', sub {
@@ -176,38 +286,45 @@ subtest 'output and output_html tests' => sub {
     like($stdout, qr{¡Hola output_html!}, 'Correct data');
 };
 
-subtest 'Version upgrade tests' => sub {
-
-    plan tests => 1;
-
-    $schema->storage->txn_begin;
-
-    my $plugin = Koha::Plugin::Test->new( { enable_plugins => 1, cgi => CGI->new } );
+subtest 'Test _version_compare' => sub {
 
-    # make sure there's no version on the DB
-    $schema->resultset('PluginData')
-        ->search( { plugin_class => $plugin->{class}, plugin_key => '__INSTALLED_VERSION__' } )
-        ->delete;
+    plan tests => 12;
+
+    t::lib::Mocks::mock_config( 'enable_plugins', 1 );
+
+    is( Koha::Plugins::Base::_version_compare( '1.1.1',    '2.2.2' ), -1, "1.1.1 is less then 2.2.2" );
+    is( Koha::Plugins::Base::_version_compare( '2.2.2',    '1.1.1' ),  1, "1.1.1 is greater then 2.2.2" );
+    is( Koha::Plugins::Base::_version_compare( '1.1.1',    '1.1.1' ),  0, "1.1.1 is equal to 1.1.1" );
+    is( Koha::Plugins::Base::_version_compare( '1.01.001', '1.1.1' ),  0, "1.01.001 is equal to 1.1.1" );
+    is( Koha::Plugins::Base::_version_compare( '1',        '1.0.0' ),  0, "1 is equal to 1.0.0" );
+    is( Koha::Plugins::Base::_version_compare( '1.0',      '1.0.0' ),  0, "1.0 is equal to 1.0.0" );
+
+    # OO tests
+    my $plugin = Koha::Plugin::Test->new;
+    is( $plugin->_version_compare( '1.1.1',    '2.2.2' ), -1, "1.1.1 is less then 2.2.2" );
+    is( $plugin->_version_compare( '2.2.2',    '1.1.1' ),  1, "1.1.1 is greater then 2.2.2" );
+    is( $plugin->_version_compare( '1.1.1',    '1.1.1' ),  0, "1.1.1 is equal to 1.1.1" );
+    is( $plugin->_version_compare( '1.01.001', '1.1.1' ),  0, "1.01.001 is equal to 1.1.1" );
+    is( $plugin->_version_compare( '1',        '1.0.0' ),  0, "1 is equal to 1.0.0" );
+    is( $plugin->_version_compare( '1.0',      '1.0.0' ),  0, "1.0 is equal to 1.0.0" );
+};
 
-    $plugin = Koha::Plugin::Test->new( { enable_plugins => 1, cgi => CGI->new } );
-    my $version = $plugin->retrieve_data('__INSTALLED_VERSION__');
+subtest 'bundle_path() tests' => sub {
 
-    is( $version, $plugin->get_metadata->{version}, 'Version has been populated correctly' );
+    plan tests => 1;
 
-    $schema->storage->txn_rollback;
-};
+    t::lib::Mocks::mock_config( 'enable_plugins', 1 );
 
+    my @current_dir = File::Spec->splitdir(abs_path(__FILE__));
+    # remote Plugins.t
+    pop @current_dir;
+    # remove db_dependent
+    pop @current_dir;
 
-subtest 'Test _version_compare' => sub {
+    my $plugin = Koha::Plugin::Test->new;
 
-    plan tests => 6;
+    is( $plugin->bundle_path, File::Spec->catdir(@current_dir) . '/lib/Koha/Plugin/Test' );
 
-    is( Koha::Plugins::Base::_version_compare( '1.1.1',    '2.2.2' ),   -1, "1.1.1 is less then 2.2.2" );
-    is( Koha::Plugins::Base::_version_compare( '2.2.2',    '1.1.1' ),    1, "1.1.1 is greater then 2.2.2" );
-    is( Koha::Plugins::Base::_version_compare( '1.1.1',    '1.1.1' ),    0, "1.1.1 is equal to 1.1.1" );
-    is( Koha::Plugins::Base::_version_compare( '1.01.001', '1.1.1' ),    0, "1.01.001 is equal to 1.1.1" );
-    is( Koha::Plugins::Base::_version_compare( '1',        '1.0.0' ),    0, "1 is equal to 1.0.0" );
-    is( Koha::Plugins::Base::_version_compare( '1.0',      '1.0.0' ),    0, "1.0 is equal to 1.0.0" );
 };
 
 subtest 'new() tests' => sub {
@@ -223,3 +340,6 @@ subtest 'new() tests' => sub {
     $result = Koha::Plugins->new({ enable_plugins => 1 });
     is( ref($result), 'Koha::Plugins', 'calling new with enable_plugins makes it override the config' );
 };
+
+$schema->storage->txn_rollback;
+Koha::Plugins::Methods->delete;