From 71914b14d17420e1640feadca8088cb9847de7e6 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Thu, 14 Oct 2021 11:57:55 +0000 Subject: [PATCH] Bug 28211: (QA follow-up) Add unit tests JD amended patch: typo multipled ==> multiplied Signed-off-by: Jonathan Druart --- t/db_dependent/Koha/Plugins/Plugins.t | 7 ++- t/lib/Koha/Plugin/Test.pm | 4 +- t/lib/Koha/Plugin/TestItemBarcodeTransform.pm | 44 +++++++++++++++++++ 3 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 t/lib/Koha/Plugin/TestItemBarcodeTransform.pm diff --git a/t/db_dependent/Koha/Plugins/Plugins.t b/t/db_dependent/Koha/Plugins/Plugins.t index 3f48adb1c3..f3d04d21bb 100755 --- a/t/db_dependent/Koha/Plugins/Plugins.t +++ b/t/db_dependent/Koha/Plugins/Plugins.t @@ -25,7 +25,7 @@ use File::Temp qw( tempdir tempfile ); use FindBin qw($Bin); use Module::Load::Conditional qw(can_load); use Test::MockModule; -use Test::More tests => 60; +use Test::More tests => 61; use Test::Warn; use C4::Context; @@ -43,6 +43,7 @@ BEGIN { use_ok('Koha::Plugins::Handler'); use_ok('Koha::Plugins::Base'); use_ok('Koha::Plugin::Test'); + use_ok('Koha::Plugin::TestItemBarcodeTransform'); } my $schema = Koha::Database->new->schema; @@ -100,9 +101,11 @@ subtest 'more call() tests' => sub { $plugin->enable(); } + # Barcode is multiplied by 2 by Koha::Plugin::Test, and again by 4 by Koha::Plugin::TestItemBarcodeTransform + # showing that call has passed the same ref to multiple plugins to operate on my $bc = 1; Koha::Plugins->call('item_barcode_transform', \$bc); - is( $bc, 4, "Got expected response" ); + is( $bc, 8, "Got expected response" ); my $cn = 'abcd'; Koha::Plugins->call('item_barcode_transform', \$cn); diff --git a/t/lib/Koha/Plugin/Test.pm b/t/lib/Koha/Plugin/Test.pm index cd5b7cbe55..e90648ea6b 100644 --- a/t/lib/Koha/Plugin/Test.pm +++ b/t/lib/Koha/Plugin/Test.pm @@ -96,7 +96,9 @@ sub intranet_js { sub item_barcode_transform { my ( $self, $barcode ) = @_; my $param = $$barcode; - $$barcode = 4 if "$$barcode" eq 1; + if ( Scalar::Util::looks_like_number( $$barcode ) ) { + $$barcode = $$barcode * 2 + } Koha::Exceptions::Exception->throw("item_barcode_transform called with parameter: $param"); } diff --git a/t/lib/Koha/Plugin/TestItemBarcodeTransform.pm b/t/lib/Koha/Plugin/TestItemBarcodeTransform.pm new file mode 100644 index 0000000000..32f0b89550 --- /dev/null +++ b/t/lib/Koha/Plugin/TestItemBarcodeTransform.pm @@ -0,0 +1,44 @@ +package Koha::Plugin::TestItemBarcodeTransform; + +## It's good practice to use Modern::Perl +use Modern::Perl; + +use Koha::Exceptions::Exception; +use Koha::Plugins::Tab; + +use Mojo::JSON qw( decode_json ); + +## Required for all plugins +use base qw(Koha::Plugins::Base); + +our $VERSION = 1.01; +our $metadata = { + name => 'Test Plugin for item_barcode_transform', + author => 'Kyle M Hall', + description => 'Test plugin', + date_authored => '2021-10-14', + date_updated => '2021-10-14', + minimum_version => '21.11', + maximum_version => undef, + version => $VERSION, +}; + +## This is the minimum code required for a plugin's 'new' method +## More can be added, but none should be removed +sub new { + my ( $class, $args ) = @_; + $args->{'metadata'} = $metadata; + my $self = $class->SUPER::new($args); + return $self; +} + +sub item_barcode_transform { + my ( $self, $barcode ) = @_; + my $param = $$barcode; + if ( Scalar::Util::looks_like_number( $$barcode ) ) { + $$barcode = $$barcode * 4 + } + Koha::Exceptions::Exception->throw("item_barcode_transform called with parameter: $param"); +} + +1; -- 2.39.5