Bug 21352: (QA follow-up) - correction to testplan
[koha-equinox.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 => 39;
13
14 use C4::Context;
15
16 use t::lib::Mocks;
17
18 BEGIN {
19     push( @INC, dirname(__FILE__) . '/../lib' );
20
21     use_ok('Koha::Plugins');
22     use_ok('Koha::Plugins::Handler');
23     use_ok('Koha::Plugins::Base');
24     use_ok('Koha::Plugin::Test');
25 }
26
27 my $mock_plugin = Test::MockModule->new( 'Koha::Plugin::Test' );
28 $mock_plugin->mock( 'test_template', sub {
29     my ( $self, $file ) = @_;
30     my $template = $self->get_template({ file => $file });
31     $template->param( filename => $file );
32     return $template->output;
33 });
34
35 ok( can_load( modules => { "Koha::Plugin::Test" => undef } ), 'Test can_load' );
36
37 my $plugin = Koha::Plugin::Test->new({ enable_plugins => 1, cgi => CGI->new });
38
39 isa_ok( $plugin, "Koha::Plugin::Test", 'Test plugin class' );
40 isa_ok( $plugin, "Koha::Plugins::Base", 'Test plugin parent class' );
41
42 ok( $plugin->can('report'), 'Test plugin can report' );
43 ok( $plugin->can('tool'), 'Test plugin can tool' );
44 ok( $plugin->can('to_marc'), 'Test plugin can to_marc' );
45 ok( $plugin->can('opac_online_payment'), 'Test plugin can opac_online_payment' );
46 ok( $plugin->can('opac_online_payment_begin'), 'Test plugin can opac_online_payment_begin' );
47 ok( $plugin->can('opac_online_payment_end'), 'Test plugin can opac_online_payment_end' );
48 ok( $plugin->can('opac_head'), 'Test plugin can opac_head' );
49 ok( $plugin->can('opac_js'), 'Test plugin can opac_js' );
50 ok( $plugin->can('intranet_head'), 'Test plugin can intranet_head' );
51 ok( $plugin->can('intranet_js'), 'Test plugin can intranet_js' );
52 ok( $plugin->can('configure'), 'Test plugin can configure' );
53 ok( $plugin->can('install'), 'Test plugin can install' );
54 ok( $plugin->can('upgrade'), 'Test plugin can upgrade' );
55 ok( $plugin->can('uninstall'), 'Test plugin can install' );
56
57 is( Koha::Plugins::Handler->run({ class => "Koha::Plugin::Test", method => 'report', enable_plugins => 1 }), "Koha::Plugin::Test::report", 'Test run plugin report method' );
58
59 my $metadata = $plugin->get_metadata();
60 is( $metadata->{'name'}, 'Test Plugin', 'Test $plugin->get_metadata()' );
61
62 is( $plugin->get_qualified_table_name('mytable'), 'koha_plugin_test_mytable', 'Test $plugin->get_qualified_table_name()' );
63 is( $plugin->get_plugin_http_path(), '/plugin/Koha/Plugin/Test', 'Test $plugin->get_plugin_http_path()' );
64
65 # test absolute path change in get_template with Koha::Plugin::Test
66 # using the mock set before
67 # we also add tmpdir as an approved template dir
68 t::lib::Mocks::mock_config( 'pluginsdir', [ C4::Context->temporary_directory ] );
69 my ( $fh, $fn ) = tempfile( SUFFIX => '.tt', UNLINK => 1, DIR => C4::Context->temporary_directory );
70 print $fh 'I am [% filename %]';
71 close $fh;
72 my $classname = ref($plugin);
73 like( $plugin->test_template($fn), qr/^I am $fn/, 'Template works' );
74
75 # testing GetPlugins
76 my @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
77     method => 'report'
78 });
79 my @names = map { $_->get_metadata()->{'name'} } @plugins;
80 is( scalar grep( /^Test Plugin$/, @names), 1, "Koha::Plugins::GetPlugins functions correctly" );
81 @plugins =  Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
82     metadata => { my_example_tag  => 'find_me' },
83 });
84 @names = map { $_->get_metadata()->{'name'} } @plugins;
85 is( scalar grep( /^Test Plugin$/, @names), 1, "GetPlugins also found Test Plugin via a metadata tag" );
86 # Test two metadata conditions; one does not exist for Test.pm
87 # Since it is a required key, we should not find the same results
88 my @plugins2 = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
89     metadata => { my_example_tag  => 'find_me', not_there => '1' },
90 });
91 isnt( scalar @plugins2, scalar @plugins, 'GetPlugins with two metadata conditions' );
92
93 for my $pass ( 1 .. 2 ) {
94     my $plugins_dir;
95     my $module_name = 'Koha::Plugin::Com::ByWaterSolutions::KitchenSink';
96     my $pm_path = 'Koha/Plugin/Com/ByWaterSolutions/KitchenSink.pm';
97     if ( $pass == 1 ) {
98         my $plugins_dir1 = tempdir( CLEANUP => 1 );
99         t::lib::Mocks::mock_config('pluginsdir', $plugins_dir1);
100         $plugins_dir = $plugins_dir1;
101         push @INC, $plugins_dir1;
102     } else {
103         my $plugins_dir1 = tempdir( CLEANUP => 1 );
104         my $plugins_dir2 = tempdir( CLEANUP => 1 );
105         t::lib::Mocks::mock_config('pluginsdir', [ $plugins_dir2, $plugins_dir1 ]);
106         $plugins_dir = $plugins_dir2;
107         pop @INC;
108         push @INC, $plugins_dir2;
109         push @INC, $plugins_dir1;
110     }
111     my $full_pm_path = $plugins_dir . '/' . $pm_path;
112
113     my $ae = Archive::Extract->new( archive => "$Bin/KitchenSinkPlugin.kpz", type => 'zip' );
114     unless ( $ae->extract( to => $plugins_dir ) ) {
115         warn "ERROR: " . $ae->error;
116     }
117     use_ok('Koha::Plugin::Com::ByWaterSolutions::KitchenSink');
118     $plugin = Koha::Plugin::Com::ByWaterSolutions::KitchenSink->new({ enable_plugins => 1});
119     my $table = $plugin->get_qualified_table_name( 'mytable' );
120
121     ok( -f $plugins_dir . "/Koha/Plugin/Com/ByWaterSolutions/KitchenSink.pm", "KitchenSink plugin installed successfully" );
122     $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"
123     Koha::Plugins::Handler->delete({ class => "Koha::Plugin::Com::ByWaterSolutions::KitchenSink", enable_plugins => 1 });
124     my $sth = C4::Context->dbh->table_info( undef, undef, $table, 'TABLE' );
125     my $info = $sth->fetchall_arrayref;
126     is( @$info, 0, "Table $table does no longer exist" );
127     ok( !( -f $full_pm_path ), "Koha::Plugins::Handler::delete works correctly." );
128 }
129
130 subtest 'output and output_html tests' => sub {
131
132     plan tests => 6;
133
134     # Trick stdout to be able to test
135     local *STDOUT;
136     my $stdout;
137     open STDOUT, '>', \$stdout;
138
139     my $plugin = Koha::Plugin::Test->new({ enable_plugins => 1, cgi => CGI->new });
140     $plugin->test_output;
141
142     like($stdout, qr/Cache-control: no-cache/, 'force_no_caching sets Cache-control as desired');
143     like($stdout, qr{Content-Type: application/json; charset=UTF-8}, 'Correct content-type');
144     like($stdout, qr{¡Hola output!}, 'Correct data');
145
146     # reset the stdout buffer
147     $stdout = '';
148     close STDOUT;
149     open STDOUT, '>', \$stdout;
150
151     $plugin->test_output_html;
152
153     like($stdout, qr/Cache-control: no-cache/, 'force_no_caching sets Cache-control as desired');
154     like($stdout, qr{Content-Type: text/html; charset=UTF-8}, 'Correct content-type');
155     like($stdout, qr{¡Hola output_html!}, 'Correct data');
156 };
157
158 subtest 'Test _version_compare' => sub {
159
160     plan tests => 6;
161
162     is( Koha::Plugins::Base::_version_compare( '1.1.1',    '2.2.2' ),   -1, "1.1.1 is less then 2.2.2" );
163     is( Koha::Plugins::Base::_version_compare( '2.2.2',    '1.1.1' ),    1, "1.1.1 is greater then 2.2.2" );
164     is( Koha::Plugins::Base::_version_compare( '1.1.1',    '1.1.1' ),    0, "1.1.1 is equal to 1.1.1" );
165     is( Koha::Plugins::Base::_version_compare( '1.01.001', '1.1.1' ),    0, "1.01.001 is equal to 1.1.1" );
166     is( Koha::Plugins::Base::_version_compare( '1',        '1.0.0' ),    0, "1 is equal to 1.0.0" );
167     is( Koha::Plugins::Base::_version_compare( '1.0',      '1.0.0' ),    0, "1.0 is equal to 1.0.0" );
168 };