Bug 8995: (QA follow-up) Mock required sysprefs in the tests
[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 => 42;
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 # testing GetPlugins
81 my @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
82     method => 'report'
83 });
84 my @names = map { $_->get_metadata()->{'name'} } @plugins;
85 is( scalar grep( /^Test Plugin$/, @names), 1, "Koha::Plugins::GetPlugins functions correctly" );
86 @plugins =  Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
87     metadata => { my_example_tag  => 'find_me' },
88 });
89 @names = map { $_->get_metadata()->{'name'} } @plugins;
90 is( scalar grep( /^Test Plugin$/, @names), 1, "GetPlugins also found Test Plugin via a metadata tag" );
91 # Test two metadata conditions; one does not exist for Test.pm
92 # Since it is a required key, we should not find the same results
93 my @plugins2 = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
94     metadata => { my_example_tag  => 'find_me', not_there => '1' },
95 });
96 isnt( scalar @plugins2, scalar @plugins, 'GetPlugins with two metadata conditions' );
97
98 for my $pass ( 1 .. 2 ) {
99     my $plugins_dir;
100     my $module_name = 'Koha::Plugin::Com::ByWaterSolutions::KitchenSink';
101     my $pm_path = 'Koha/Plugin/Com/ByWaterSolutions/KitchenSink.pm';
102     if ( $pass == 1 ) {
103         my $plugins_dir1 = tempdir( CLEANUP => 1 );
104         t::lib::Mocks::mock_config('pluginsdir', $plugins_dir1);
105         $plugins_dir = $plugins_dir1;
106         push @INC, $plugins_dir1;
107     } else {
108         my $plugins_dir1 = tempdir( CLEANUP => 1 );
109         my $plugins_dir2 = tempdir( CLEANUP => 1 );
110         t::lib::Mocks::mock_config('pluginsdir', [ $plugins_dir2, $plugins_dir1 ]);
111         $plugins_dir = $plugins_dir2;
112         pop @INC;
113         push @INC, $plugins_dir2;
114         push @INC, $plugins_dir1;
115     }
116     my $full_pm_path = $plugins_dir . '/' . $pm_path;
117
118     my $ae = Archive::Extract->new( archive => "$Bin/KitchenSinkPlugin.kpz", type => 'zip' );
119     unless ( $ae->extract( to => $plugins_dir ) ) {
120         warn "ERROR: " . $ae->error;
121     }
122     use_ok('Koha::Plugin::Com::ByWaterSolutions::KitchenSink');
123     $plugin = Koha::Plugin::Com::ByWaterSolutions::KitchenSink->new({ enable_plugins => 1});
124     my $table = $plugin->get_qualified_table_name( 'mytable' );
125
126     ok( -f $plugins_dir . "/Koha/Plugin/Com/ByWaterSolutions/KitchenSink.pm", "KitchenSink plugin installed successfully" );
127     $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"
128     Koha::Plugins::Handler->delete({ class => "Koha::Plugin::Com::ByWaterSolutions::KitchenSink", enable_plugins => 1 });
129     my $sth = C4::Context->dbh->table_info( undef, undef, $table, 'TABLE' );
130     my $info = $sth->fetchall_arrayref;
131     is( @$info, 0, "Table $table does no longer exist" );
132     ok( !( -f $full_pm_path ), "Koha::Plugins::Handler::delete works correctly." );
133 }
134
135 subtest 'output and output_html tests' => sub {
136
137     plan tests => 6;
138
139     # Trick stdout to be able to test
140     local *STDOUT;
141     my $stdout;
142     open STDOUT, '>', \$stdout;
143
144     my $plugin = Koha::Plugin::Test->new({ enable_plugins => 1, cgi => CGI->new });
145     $plugin->test_output;
146
147     like($stdout, qr/Cache-control: no-cache/, 'force_no_caching sets Cache-control as desired');
148     like($stdout, qr{Content-Type: application/json; charset=UTF-8}, 'Correct content-type');
149     like($stdout, qr{¡Hola output!}, 'Correct data');
150
151     # reset the stdout buffer
152     $stdout = '';
153     close STDOUT;
154     open STDOUT, '>', \$stdout;
155
156     $plugin->test_output_html;
157
158     like($stdout, qr/Cache-control: no-cache/, 'force_no_caching sets Cache-control as desired');
159     like($stdout, qr{Content-Type: text/html; charset=UTF-8}, 'Correct content-type');
160     like($stdout, qr{¡Hola output_html!}, 'Correct data');
161 };
162
163 subtest 'Version upgrade tests' => sub {
164
165     plan tests => 1;
166
167     $schema->storage->txn_begin;
168
169     my $plugin = Koha::Plugin::Test->new( { enable_plugins => 1, cgi => CGI->new } );
170
171     # make sure there's no version on the DB
172     $schema->resultset('PluginData')
173         ->search( { plugin_class => $plugin->{class}, plugin_key => '__INSTALLED_VERSION__' } )
174         ->delete;
175
176     $plugin = Koha::Plugin::Test->new( { enable_plugins => 1, cgi => CGI->new } );
177     my $version = $plugin->retrieve_data('__INSTALLED_VERSION__');
178
179     is( $version, $plugin->get_metadata->{version}, 'Version has been populated correctly' );
180
181     $schema->storage->txn_rollback;
182 };
183
184
185 subtest 'Test _version_compare' => sub {
186
187     plan tests => 6;
188
189     is( Koha::Plugins::Base::_version_compare( '1.1.1',    '2.2.2' ),   -1, "1.1.1 is less then 2.2.2" );
190     is( Koha::Plugins::Base::_version_compare( '2.2.2',    '1.1.1' ),    1, "1.1.1 is greater then 2.2.2" );
191     is( Koha::Plugins::Base::_version_compare( '1.1.1',    '1.1.1' ),    0, "1.1.1 is equal to 1.1.1" );
192     is( Koha::Plugins::Base::_version_compare( '1.01.001', '1.1.1' ),    0, "1.01.001 is equal to 1.1.1" );
193     is( Koha::Plugins::Base::_version_compare( '1',        '1.0.0' ),    0, "1 is equal to 1.0.0" );
194     is( Koha::Plugins::Base::_version_compare( '1.0',      '1.0.0' ),    0, "1.0 is equal to 1.0.0" );
195 };