Bug 19223: Unit tests for output and output_html methods
authorTomas Cohen Arazi <tomascohen@theke.io>
Tue, 27 Mar 2018 18:56:41 +0000 (15:56 -0300)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Wed, 4 Apr 2018 18:40:06 +0000 (15:40 -0300)
Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
t/Koha/Plugin/Test.pm
t/db_dependent/Plugins.t

index 636554d..0f51f73 100644 (file)
@@ -72,3 +72,13 @@ sub uninstall {
     my ( $self, $args ) = @_;
     return "Koha::Plugin::Test::uninstall";
 }
+
+sub test_output {
+    my ( $self ) = @_;
+    $self->output( '¡Hola output!', 'json' );
+}
+
+sub test_output_html {
+    my ( $self ) = @_;
+    $self->output_html( '¡Hola output_html!' );
+}
index 4fa327f..106eb91 100755 (executable)
@@ -2,7 +2,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 32;
+use Test::More tests => 33;
 use CGI;
 use File::Basename;
 use File::Spec;
@@ -121,3 +121,31 @@ for my $pass ( 1 .. 2 ) {
     is( @$info, 0, "Table $table does no longer exist" );
     ok( !( -f $full_pm_path ), "Koha::Plugins::Handler::delete works correctly." );
 }
+
+subtest 'output and output_html tests' => sub {
+
+    plan tests => 6;
+
+    # Trick stdout to be able to test
+    local *STDOUT;
+    my $stdout;
+    open STDOUT, '>', \$stdout;
+
+    my $plugin = Koha::Plugin::Test->new({ enable_plugins => 1, cgi => CGI->new });
+    $plugin->test_output;
+
+    like($stdout, qr/Cache-control: no-cache/, 'force_no_caching sets Cache-control as desired');
+    like($stdout, qr{Content-Type: application/json; charset=UTF-8}, 'Correct content-type');
+    like($stdout, qr{¡Hola output!}, 'Correct data');
+
+    # reset the stdout buffer
+    $stdout = '';
+    close STDOUT;
+    open STDOUT, '>', \$stdout;
+
+    $plugin->test_output_html;
+
+    like($stdout, qr/Cache-control: no-cache/, 'force_no_caching sets Cache-control as desired');
+    like($stdout, qr{Content-Type: text/html; charset=UTF-8}, 'Correct content-type');
+    like($stdout, qr{¡Hola output_html!}, 'Correct data');
+};