Bug 22944: avoid AnonymousPatron in search_patrons_to_anonymise
[koha.git] / t / db_dependent / Plugins.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use Archive::Extract;
6 use CGI;
7 use File::Basename;
8 use File::Temp qw( tempdir tempfile );
9 use FindBin qw($Bin);
10 use Module::Load::Conditional qw(can_load);
11 use Test::MockModule;
12 use Test::More tests => 47;
13
14 use C4::Context;
15 use Koha::Database;
16
17 use t::lib::Mocks;
18
19 BEGIN {
20     push( @INC, dirname(__FILE__) . '/../lib' );
21
22     use_ok('Koha::Plugins');
23     use_ok('Koha::Plugins::Handler');
24     use_ok('Koha::Plugins::Base');
25     use_ok('Koha::Plugin::Test');
26 }
27
28 my $schema = Koha::Database->new->schema;
29
30 my $mock_plugin = Test::MockModule->new( 'Koha::Plugin::Test' );
31 $mock_plugin->mock( 'test_template', sub {
32     my ( $self, $file ) = @_;
33     my $template = $self->get_template({ file => $file });
34     $template->param( filename => $file );
35     return $template->output;
36 });
37
38 ok( can_load( modules => { "Koha::Plugin::Test" => undef } ), 'Test can_load' );
39
40 my $plugin = Koha::Plugin::Test->new({ enable_plugins => 1, cgi => CGI->new });
41
42 isa_ok( $plugin, "Koha::Plugin::Test", 'Test plugin class' );
43 isa_ok( $plugin, "Koha::Plugins::Base", 'Test plugin parent class' );
44
45 ok( $plugin->can('report'), 'Test plugin can report' );
46 ok( $plugin->can('tool'), 'Test plugin can tool' );
47 ok( $plugin->can('to_marc'), 'Test plugin can to_marc' );
48 ok( $plugin->can('intranet_catalog_biblio_enhancements'), 'Test plugin can intranet_catalog_biblio_enhancements');
49 ok( $plugin->can('intranet_catalog_biblio_enhancements_toolbar_button'), 'Test plugin can intranet_catalog_biblio_enhancements_toolbar_button' );
50 ok( $plugin->can('opac_online_payment'), 'Test plugin can opac_online_payment' );
51 ok( $plugin->can('opac_online_payment_begin'), 'Test plugin can opac_online_payment_begin' );
52 ok( $plugin->can('opac_online_payment_end'), 'Test plugin can opac_online_payment_end' );
53 ok( $plugin->can('opac_head'), 'Test plugin can opac_head' );
54 ok( $plugin->can('opac_js'), 'Test plugin can opac_js' );
55 ok( $plugin->can('intranet_head'), 'Test plugin can intranet_head' );
56 ok( $plugin->can('intranet_js'), 'Test plugin can intranet_js' );
57 ok( $plugin->can('configure'), 'Test plugin can configure' );
58 ok( $plugin->can('install'), 'Test plugin can install' );
59 ok( $plugin->can('upgrade'), 'Test plugin can upgrade' );
60 ok( $plugin->can('uninstall'), 'Test plugin can install' );
61
62 is( Koha::Plugins::Handler->run({ class => "Koha::Plugin::Test", method => 'report', enable_plugins => 1 }), "Koha::Plugin::Test::report", 'Test run plugin report method' );
63
64 my $metadata = $plugin->get_metadata();
65 is( $metadata->{'name'}, 'Test Plugin', 'Test $plugin->get_metadata()' );
66
67 is( $plugin->get_qualified_table_name('mytable'), 'koha_plugin_test_mytable', 'Test $plugin->get_qualified_table_name()' );
68 is( $plugin->get_plugin_http_path(), '/plugin/Koha/Plugin/Test', 'Test $plugin->get_plugin_http_path()' );
69
70 # test absolute path change in get_template with Koha::Plugin::Test
71 # using the mock set before
72 # we also add tmpdir as an approved template dir
73 t::lib::Mocks::mock_config( 'pluginsdir', [ C4::Context->temporary_directory ] );
74 my ( $fh, $fn ) = tempfile( SUFFIX => '.tt', UNLINK => 1, DIR => C4::Context->temporary_directory );
75 print $fh 'I am [% filename %]';
76 close $fh;
77 my $classname = ref($plugin);
78 like( $plugin->test_template($fn), qr/^I am $fn/, 'Template works' );
79
80 my $result = $plugin->enable;
81 is( ref($result), 'Koha::Plugin::Test' );
82
83 # testing GetPlugins
84 my @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
85     method => 'report'
86 });
87
88 my @names = map { $_->get_metadata()->{'name'} } @plugins;
89 is( scalar grep( /^Test Plugin$/, @names), 1, "Koha::Plugins::GetPlugins functions correctly" );
90 @plugins =  Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
91     metadata => { my_example_tag  => 'find_me' },
92 });
93
94 @names = map { $_->get_metadata()->{'name'} } @plugins;
95 is( scalar grep( /^Test Plugin$/, @names), 1, "GetPlugins also found Test Plugin via a metadata tag" );
96 # Test two metadata conditions; one does not exist for Test.pm
97 # Since it is a required key, we should not find the same results
98 my @plugins2 = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
99     metadata => { my_example_tag  => 'find_me', not_there => '1' },
100 });
101 isnt( scalar @plugins2, scalar @plugins, 'GetPlugins with two metadata conditions' );
102
103 $result = $plugin->disable;
104 is( ref($result), 'Koha::Plugin::Test' );
105
106 @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins();
107 @names = map { $_->get_metadata()->{'name'} } @plugins;
108 is( scalar grep( /^Test Plugin$/, @names), 0, "GetPlugins does not found disabled Test Plugin" );
109
110 @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({ all => 1 });
111 @names = map { $_->get_metadata()->{'name'} } @plugins;
112 is( scalar grep( /^Test Plugin$/, @names), 1, "With all param, GetPlugins found disabled Test Plugin" );
113
114 for my $pass ( 1 .. 2 ) {
115     my $plugins_dir;
116     my $module_name = 'Koha::Plugin::Com::ByWaterSolutions::KitchenSink';
117     my $pm_path = 'Koha/Plugin/Com/ByWaterSolutions/KitchenSink.pm';
118     if ( $pass == 1 ) {
119         my $plugins_dir1 = tempdir( CLEANUP => 1 );
120         t::lib::Mocks::mock_config('pluginsdir', $plugins_dir1);
121         $plugins_dir = $plugins_dir1;
122         push @INC, $plugins_dir1;
123     } else {
124         my $plugins_dir1 = tempdir( CLEANUP => 1 );
125         my $plugins_dir2 = tempdir( CLEANUP => 1 );
126         t::lib::Mocks::mock_config('pluginsdir', [ $plugins_dir2, $plugins_dir1 ]);
127         $plugins_dir = $plugins_dir2;
128         pop @INC;
129         push @INC, $plugins_dir2;
130         push @INC, $plugins_dir1;
131     }
132     my $full_pm_path = $plugins_dir . '/' . $pm_path;
133
134     my $ae = Archive::Extract->new( archive => "$Bin/KitchenSinkPlugin.kpz", type => 'zip' );
135     unless ( $ae->extract( to => $plugins_dir ) ) {
136         warn "ERROR: " . $ae->error;
137     }
138     use_ok('Koha::Plugin::Com::ByWaterSolutions::KitchenSink');
139     $plugin = Koha::Plugin::Com::ByWaterSolutions::KitchenSink->new({ enable_plugins => 1});
140     my $table = $plugin->get_qualified_table_name( 'mytable' );
141
142     ok( -f $plugins_dir . "/Koha/Plugin/Com/ByWaterSolutions/KitchenSink.pm", "KitchenSink plugin installed successfully" );
143     $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"
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." );
149 }
150
151 subtest 'output and output_html tests' => sub {
152
153     plan tests => 6;
154
155     # Trick stdout to be able to test
156     local *STDOUT;
157     my $stdout;
158     open STDOUT, '>', \$stdout;
159
160     my $plugin = Koha::Plugin::Test->new({ enable_plugins => 1, cgi => CGI->new });
161     $plugin->test_output;
162
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');
166
167     # reset the stdout buffer
168     $stdout = '';
169     close STDOUT;
170     open STDOUT, '>', \$stdout;
171
172     $plugin->test_output_html;
173
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');
177 };
178
179 subtest 'Version upgrade tests' => sub {
180
181     plan tests => 1;
182
183     $schema->storage->txn_begin;
184
185     my $plugin = Koha::Plugin::Test->new( { enable_plugins => 1, cgi => CGI->new } );
186
187     # make sure there's no version on the DB
188     $schema->resultset('PluginData')
189         ->search( { plugin_class => $plugin->{class}, plugin_key => '__INSTALLED_VERSION__' } )
190         ->delete;
191
192     $plugin = Koha::Plugin::Test->new( { enable_plugins => 1, cgi => CGI->new } );
193     my $version = $plugin->retrieve_data('__INSTALLED_VERSION__');
194
195     is( $version, $plugin->get_metadata->{version}, 'Version has been populated correctly' );
196
197     $schema->storage->txn_rollback;
198 };
199
200
201 subtest 'Test _version_compare' => sub {
202
203     plan tests => 6;
204
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" );
211 };
212
213 subtest 'new() tests' => sub {
214
215     plan tests => 2;
216
217     t::lib::Mocks::mock_config( 'pluginsdir', [ C4::Context->temporary_directory ] );
218     t::lib::Mocks::mock_config( 'enable_plugins', 0 );
219
220     my $result = Koha::Plugins->new();
221     is( $result, undef, 'calling new() on disabled plugins returns undef' );
222
223     $result = Koha::Plugins->new({ enable_plugins => 1 });
224     is( ref($result), 'Koha::Plugins', 'calling new with enable_plugins makes it override the config' );
225 };