8 use File::Temp qw( tempdir tempfile );
10 use Module::Load::Conditional qw(can_load);
12 use Test::More tests => 47;
16 use Koha::Plugins::Methods;
21 push( @INC, dirname(__FILE__) . '/../lib' );
23 use_ok('Koha::Plugins');
24 use_ok('Koha::Plugins::Handler');
25 use_ok('Koha::Plugins::Base');
26 use_ok('Koha::Plugin::Test');
29 my $schema = Koha::Database->new->schema;
31 Koha::Plugins->new( { enable_plugins => 1 } )->InstallPlugins();
33 ok( Koha::Plugins::Methods->search( { plugin_class => 'Koha::Plugin::Test' } )->count, 'Test plugin methods added to database' );
35 my $mock_plugin = Test::MockModule->new( 'Koha::Plugin::Test' );
36 $mock_plugin->mock( 'test_template', sub {
37 my ( $self, $file ) = @_;
38 my $template = $self->get_template({ file => $file });
39 $template->param( filename => $file );
40 return $template->output;
43 ok( can_load( modules => { "Koha::Plugin::Test" => undef } ), 'Test can_load' );
45 my $plugin = Koha::Plugin::Test->new({ enable_plugins => 1, cgi => CGI->new });
47 isa_ok( $plugin, "Koha::Plugin::Test", 'Test plugin class' );
48 isa_ok( $plugin, "Koha::Plugins::Base", 'Test plugin parent class' );
50 ok( $plugin->can('report'), 'Test plugin can report' );
51 ok( $plugin->can('tool'), 'Test plugin can tool' );
52 ok( $plugin->can('to_marc'), 'Test plugin can to_marc' );
53 ok( $plugin->can('intranet_catalog_biblio_enhancements'), 'Test plugin can intranet_catalog_biblio_enhancements');
54 ok( $plugin->can('intranet_catalog_biblio_enhancements_toolbar_button'), 'Test plugin can intranet_catalog_biblio_enhancements_toolbar_button' );
55 ok( $plugin->can('opac_online_payment'), 'Test plugin can opac_online_payment' );
56 ok( $plugin->can('opac_online_payment_begin'), 'Test plugin can opac_online_payment_begin' );
57 ok( $plugin->can('opac_online_payment_end'), 'Test plugin can opac_online_payment_end' );
58 ok( $plugin->can('opac_head'), 'Test plugin can opac_head' );
59 ok( $plugin->can('opac_js'), 'Test plugin can opac_js' );
60 ok( $plugin->can('intranet_head'), 'Test plugin can intranet_head' );
61 ok( $plugin->can('intranet_js'), 'Test plugin can intranet_js' );
62 ok( $plugin->can('configure'), 'Test plugin can configure' );
63 ok( $plugin->can('install'), 'Test plugin can install' );
64 ok( $plugin->can('upgrade'), 'Test plugin can upgrade' );
65 ok( $plugin->can('uninstall'), 'Test plugin can install' );
67 is( Koha::Plugins::Handler->run({ class => "Koha::Plugin::Test", method => 'report', enable_plugins => 1 }), "Koha::Plugin::Test::report", 'Test run plugin report method' );
69 my $metadata = $plugin->get_metadata();
70 is( $metadata->{'name'}, 'Test Plugin', 'Test $plugin->get_metadata()' );
72 is( $plugin->get_qualified_table_name('mytable'), 'koha_plugin_test_mytable', 'Test $plugin->get_qualified_table_name()' );
73 is( $plugin->get_plugin_http_path(), '/plugin/Koha/Plugin/Test', 'Test $plugin->get_plugin_http_path()' );
75 # test absolute path change in get_template with Koha::Plugin::Test
76 # using the mock set before
77 # we also add tmpdir as an approved template dir
78 t::lib::Mocks::mock_config( 'pluginsdir', [ C4::Context->temporary_directory ] );
79 my ( $fh, $fn ) = tempfile( SUFFIX => '.tt', UNLINK => 1, DIR => C4::Context->temporary_directory );
80 print $fh 'I am [% filename %]';
82 my $classname = ref($plugin);
83 like( $plugin->test_template($fn), qr/^I am $fn/, 'Template works' );
85 my $result = $plugin->enable;
86 is( ref($result), 'Koha::Plugin::Test' );
89 my @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
93 my @names = map { $_->get_metadata()->{'name'} } @plugins;
94 is( scalar grep( /^Test Plugin$/, @names), 1, "Koha::Plugins::GetPlugins functions correctly" );
95 @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
96 metadata => { my_example_tag => 'find_me' },
99 @names = map { $_->get_metadata()->{'name'} } @plugins;
100 is( scalar grep( /^Test Plugin$/, @names), 1, "GetPlugins also found Test Plugin via a metadata tag" );
102 $result = $plugin->disable;
103 is( ref($result), 'Koha::Plugin::Test' );
105 @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins();
106 @names = map { $_->get_metadata()->{'name'} } @plugins;
107 is( scalar grep( /^Test Plugin$/, @names), 0, "GetPlugins does not found disabled Test Plugin" );
109 @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({ all => 1 });
110 @names = map { $_->get_metadata()->{'name'} } @plugins;
111 is( scalar grep( /^Test Plugin$/, @names), 1, "With all param, GetPlugins found disabled Test Plugin" );
113 for my $pass ( 1 .. 2 ) {
115 my $module_name = 'Koha::Plugin::Com::ByWaterSolutions::KitchenSink';
116 my $pm_path = 'Koha/Plugin/Com/ByWaterSolutions/KitchenSink.pm';
118 my $plugins_dir1 = tempdir( CLEANUP => 1 );
119 t::lib::Mocks::mock_config('pluginsdir', $plugins_dir1);
120 $plugins_dir = $plugins_dir1;
121 push @INC, $plugins_dir1;
123 my $plugins_dir1 = tempdir( CLEANUP => 1 );
124 my $plugins_dir2 = tempdir( CLEANUP => 1 );
125 t::lib::Mocks::mock_config('pluginsdir', [ $plugins_dir2, $plugins_dir1 ]);
126 $plugins_dir = $plugins_dir2;
128 push @INC, $plugins_dir2;
129 push @INC, $plugins_dir1;
131 my $full_pm_path = $plugins_dir . '/' . $pm_path;
133 my $ae = Archive::Extract->new( archive => "$Bin/KitchenSinkPlugin.kpz", type => 'zip' );
134 unless ( $ae->extract( to => $plugins_dir ) ) {
135 warn "ERROR: " . $ae->error;
137 use_ok('Koha::Plugin::Com::ByWaterSolutions::KitchenSink');
138 $plugin = Koha::Plugin::Com::ByWaterSolutions::KitchenSink->new({ enable_plugins => 1});
139 my $table = $plugin->get_qualified_table_name( 'mytable' );
141 ok( -f $plugins_dir . "/Koha/Plugin/Com/ByWaterSolutions/KitchenSink.pm", "KitchenSink plugin installed successfully" );
142 $INC{$pm_path} = $full_pm_path; # FIXME I do not really know why, but if this is moved before the $plugin constructor, it will fail with Can't locate object method "new" via package "Koha::Plugin::Com::ByWaterSolutions::KitchenSink"
143 Koha::Plugins->new( { enable_plugins => 1 } )->InstallPlugins();
144 Koha::Plugins::Handler->delete({ class => "Koha::Plugin::Com::ByWaterSolutions::KitchenSink", enable_plugins => 1 });
145 my $sth = C4::Context->dbh->table_info( undef, undef, $table, 'TABLE' );
146 my $info = $sth->fetchall_arrayref;
147 is( @$info, 0, "Table $table does no longer exist" );
148 ok( !( -f $full_pm_path ), "Koha::Plugins::Handler::delete works correctly." );
151 subtest 'output and output_html tests' => sub {
155 # Trick stdout to be able to test
158 open STDOUT, '>', \$stdout;
160 my $plugin = Koha::Plugin::Test->new({ enable_plugins => 1, cgi => CGI->new });
161 $plugin->test_output;
163 like($stdout, qr/Cache-control: no-cache/, 'force_no_caching sets Cache-control as desired');
164 like($stdout, qr{Content-Type: application/json; charset=UTF-8}, 'Correct content-type');
165 like($stdout, qr{¡Hola output!}, 'Correct data');
167 # reset the stdout buffer
170 open STDOUT, '>', \$stdout;
172 $plugin->test_output_html;
174 like($stdout, qr/Cache-control: no-cache/, 'force_no_caching sets Cache-control as desired');
175 like($stdout, qr{Content-Type: text/html; charset=UTF-8}, 'Correct content-type');
176 like($stdout, qr{¡Hola output_html!}, 'Correct data');
179 subtest 'Version upgrade tests' => sub {
183 $schema->storage->txn_begin;
185 my $plugin = Koha::Plugin::Test->new( { enable_plugins => 1, cgi => CGI->new } );
187 # make sure there's no version on the DB
188 $schema->resultset('PluginData')
189 ->search( { plugin_class => $plugin->{class}, plugin_key => '__INSTALLED_VERSION__' } )
192 $plugin = Koha::Plugin::Test->new( { enable_plugins => 1, cgi => CGI->new } );
193 my $version = $plugin->retrieve_data('__INSTALLED_VERSION__');
195 is( $version, $plugin->get_metadata->{version}, 'Version has been populated correctly' );
197 $schema->storage->txn_rollback;
201 subtest 'Test _version_compare' => sub {
205 is( Koha::Plugins::Base::_version_compare( '1.1.1', '2.2.2' ), -1, "1.1.1 is less then 2.2.2" );
206 is( Koha::Plugins::Base::_version_compare( '2.2.2', '1.1.1' ), 1, "1.1.1 is greater then 2.2.2" );
207 is( Koha::Plugins::Base::_version_compare( '1.1.1', '1.1.1' ), 0, "1.1.1 is equal to 1.1.1" );
208 is( Koha::Plugins::Base::_version_compare( '1.01.001', '1.1.1' ), 0, "1.01.001 is equal to 1.1.1" );
209 is( Koha::Plugins::Base::_version_compare( '1', '1.0.0' ), 0, "1 is equal to 1.0.0" );
210 is( Koha::Plugins::Base::_version_compare( '1.0', '1.0.0' ), 0, "1.0 is equal to 1.0.0" );
213 subtest 'new() tests' => sub {
217 t::lib::Mocks::mock_config( 'pluginsdir', [ C4::Context->temporary_directory ] );
218 t::lib::Mocks::mock_config( 'enable_plugins', 0 );
220 my $result = Koha::Plugins->new();
221 is( $result, undef, 'calling new() on disabled plugins returns undef' );
223 $result = Koha::Plugins->new({ enable_plugins => 1 });
224 is( ref($result), 'Koha::Plugins', 'calling new with enable_plugins makes it override the config' );