From 939f9b7b40c31f754bc546e7e40ff2208309e8f0 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 1 Apr 2025 12:11:43 +0200 Subject: [PATCH] Bug 39405: Add more plugin examples to build tests Signed-off-by: Tomas Cohen Arazi --- t/db_dependent/Koha/Plugins/Overdues.t | 98 +++++++++++++------ t/lib/plugins/Koha/Plugin/1_CalcFineEmpty.pm | 43 ++++++++ .../plugins/Koha/Plugin/2_CalcFineNotEmpty.pm | 43 ++++++++ .../plugins/Koha/Plugin/3_CalcFineBadValue.pm | 43 ++++++++ t/lib/plugins/Koha/Plugin/Test.pm | 10 +- 5 files changed, 203 insertions(+), 34 deletions(-) create mode 100644 t/lib/plugins/Koha/Plugin/1_CalcFineEmpty.pm create mode 100644 t/lib/plugins/Koha/Plugin/2_CalcFineNotEmpty.pm create mode 100644 t/lib/plugins/Koha/Plugin/3_CalcFineBadValue.pm diff --git a/t/db_dependent/Koha/Plugins/Overdues.t b/t/db_dependent/Koha/Plugins/Overdues.t index 38a2e013dd9..edde9f9d126 100755 --- a/t/db_dependent/Koha/Plugins/Overdues.t +++ b/t/db_dependent/Koha/Plugins/Overdues.t @@ -16,8 +16,8 @@ use Modern::Perl; -use Test::NoWarnings; -use Test::More tests => 6; +use Test::More tests => 8; +use Test::Warn; use C4::Overdues qw(CalcFine); @@ -34,36 +34,29 @@ BEGIN { use_ok('C4::Overdues'); use_ok('Koha::Plugins'); use_ok('Koha::Plugins::Handler'); + use_ok('Koha::Plugin::1_CalcFineEmpty'); + use_ok('Koha::Plugin::2_CalcFineNotEmpty'); + use_ok('Koha::Plugin::3_CalcFineBadValue'); use_ok('Koha::Plugin::Test'); } t::lib::Mocks::mock_config( 'enable_plugins', 1 ); my $builder = t::lib::TestBuilder->new; - -my $branch = $builder->build( - { - source => 'Branch', - } -); - -my $category = $builder->build( - { - source => 'Category', - } -); - -my $item = $builder->build_sample_item; +my $schema = Koha::Database->new->schema; subtest 'overwrite_calc_fine hook tests' => sub { - plan tests => 6; - my $schema = Koha::Database->new->schema; + plan tests => 13; + $schema->storage->txn_begin; - my $plugins = Koha::Plugins->new; - $plugins->InstallPlugins; - Koha::Plugin::Test->new->enable; + my $library = $builder->build_object( { class => 'Koha::Libraries', } ); + my $category = $builder->build_object( { class => 'Koha::Patron::Categories', } ); + my $item = $builder->build_sample_item; + + Koha::Plugins->new->InstallPlugins(); + my $test_plugin = Koha::Plugin::Test->new->disable(); Koha::CirculationRules->set_rules( { @@ -93,19 +86,64 @@ subtest 'overwrite_calc_fine hook tests' => sub { day => 30, ); - my ( $amount, $units, $chargable_units ) = CalcFine( $item->{itemnumber}, undef, undef, $due_date, $end_date ); - - is( $amount, 87, 'Amount is calculated correctly with custom function' ); - is( $units, 29, 'Units are calculated correctly with custom function' ); - is( $chargable_units, 27, 'Chargable units are calculated correctly with custom function' ); - - ( $amount, $units, $chargable_units ) = - CalcFine( $item->{itemnumber}, $category->{categorycode}, $branch->{branchcode}, $due_date, $end_date ); + my ( $amount, $units, $chargable_units ) = + CalcFine( $item->unblessed, $category->categorycode, $library->branchcode, $due_date, $end_date ); is( $amount, 58, 'Amount is calculated correctly with original function' ); is( $units, 29, 'Units are calculated correctly with original function' ); is( $chargable_units, 29, 'Chargable units are calculated correctly with original function' ); - Koha::Plugins->RemovePlugins; + # initialize undef value plugin + my $empty_value_plugin = Koha::Plugin::1_CalcFineEmpty->new->enable(); + ( $amount, $units, $chargable_units ) = + CalcFine( $item->unblessed, $category->categorycode, $library->branchcode, $due_date, $end_date ); + + is( $amount, 58, 'Amount is calculated correctly with original function, undef plugin skipped' ); + is( $units, 29, 'Units are calculated correctly with original function, undef plugin skipped' ); + is( $chargable_units, 29, 'Chargable units are calculated correctly with original function, undef plugin skipped' ); + + # initialize valid value plugin + my $not_empty_value_plugin = Koha::Plugin::2_CalcFineNotEmpty->new->enable(); + + ( $amount, $units, $chargable_units ) = CalcFine( $item->unblessed, undef, undef, $due_date, $end_date ); + + is( $amount, 1, 'Amount is calculated correctly with custom function' ); + is( $units, 2, 'Units are calculated correctly with custom function' ); + is( $chargable_units, 3, 'Chargable units are calculated correctly with custom function' ); + + # initialize bad value plugin + my $bad_value_plugin = Koha::Plugin::3_CalcFineBadValue->new->enable(); + $not_empty_value_plugin->disable(); + + ( $amount, $units, $chargable_units ) = CalcFine( $item->unblessed, undef, undef, $due_date, $end_date ); + + is( $amount, 'a', 'Amount is calculated correctly with custom function, bad value returned anyway' ); + is( $units, 'b', 'Units are calculated correctly with custom function, bad value returned anyway' ); + is( + $chargable_units, undef, + 'Chargable units are calculated correctly with custom function, bad value returned anyway' + ); + + Koha::Plugin::Test->new->enable(); + + $empty_value_plugin->disable(); + $bad_value_plugin->disable(); + + my $itemnumber = $item->itemnumber; + my $branchcode = $library->branchcode; + my $categorycode = $category->categorycode; + + warnings_like { CalcFine( $item->unblessed, $category->categorycode, $library->branchcode, $due_date, $end_date ); } + [ + qr/itemnumber:$itemnumber/, + qr/branchcode:$branchcode/, + qr/categorycode:$categorycode/, + qr/due_date_type:DateTime/, + qr/end_date_type:DateTime/ + ], + 'Parameters are correct'; + + Koha::Plugins->RemovePlugins(); + $schema->storage->txn_rollback; }; diff --git a/t/lib/plugins/Koha/Plugin/1_CalcFineEmpty.pm b/t/lib/plugins/Koha/Plugin/1_CalcFineEmpty.pm new file mode 100644 index 00000000000..2dee85c89d1 --- /dev/null +++ b/t/lib/plugins/Koha/Plugin/1_CalcFineEmpty.pm @@ -0,0 +1,43 @@ +package Koha::Plugin::1_CalcFineEmpty; + +use Modern::Perl; + +use base qw(Koha::Plugins::Base); + +our $VERSION = "0.0.1"; +our $metadata = { + name => 'Calc Fine Plugin', + author => 'Kyle M Hall', + description => 'Test plugin', + date_authored => '2013-01-14', + date_updated => '2013-01-14', + minimum_version => '3.11', + maximum_version => undef, + version => $VERSION, + namespace => 'calc_fine_empty', +}; + +=head1 Methods + +=head2 new + +=cut + +sub new { + my ( $class, $args ) = @_; + $args->{'metadata'} = $metadata; + my $self = $class->SUPER::new($args); + return $self; +} + +=head2 overwrite_calc_fine + +=cut + +sub overwrite_calc_fine { + my ( $self, $params ) = @_; + + return; +} + +1; diff --git a/t/lib/plugins/Koha/Plugin/2_CalcFineNotEmpty.pm b/t/lib/plugins/Koha/Plugin/2_CalcFineNotEmpty.pm new file mode 100644 index 00000000000..40564115507 --- /dev/null +++ b/t/lib/plugins/Koha/Plugin/2_CalcFineNotEmpty.pm @@ -0,0 +1,43 @@ +package Koha::Plugin::2_CalcFineNotEmpty; + +use Modern::Perl; + +use base qw(Koha::Plugins::Base); + +our $VERSION = "0.0.1"; +our $metadata = { + name => 'Calc Fine Not Empty Plugin', + author => 'Kyle M Hall', + description => 'Test plugin', + date_authored => '2013-01-14', + date_updated => '2013-01-14', + minimum_version => '3.11', + maximum_version => undef, + version => $VERSION, + namespace => 'calc_fine_not_empty', +}; + +=head1 Methods + +=head2 new + +=cut + +sub new { + my ( $class, $args ) = @_; + $args->{'metadata'} = $metadata; + my $self = $class->SUPER::new($args); + return $self; +} + +=head2 overwrite_calc_fine + +=cut + +sub overwrite_calc_fine { + my ( $self, $params ) = @_; + + return [ 1, 2, 3 ]; +} + +1; diff --git a/t/lib/plugins/Koha/Plugin/3_CalcFineBadValue.pm b/t/lib/plugins/Koha/Plugin/3_CalcFineBadValue.pm new file mode 100644 index 00000000000..4ef2860f604 --- /dev/null +++ b/t/lib/plugins/Koha/Plugin/3_CalcFineBadValue.pm @@ -0,0 +1,43 @@ +package Koha::Plugin::3_CalcFineBadValue; + +use Modern::Perl; + +use base qw(Koha::Plugins::Base); + +our $VERSION = "0.0.1"; +our $metadata = { + name => 'Calc Fine Plugin', + author => 'Kyle M Hall', + description => 'Test plugin', + date_authored => '2013-01-14', + date_updated => '2013-01-14', + minimum_version => '3.11', + maximum_version => undef, + version => $VERSION, + namespace => 'calc_fine_bad_value', +}; + +=head1 Methods + +=head2 new + +=cut + +sub new { + my ( $class, $args ) = @_; + $args->{'metadata'} = $metadata; + my $self = $class->SUPER::new($args); + return $self; +} + +=head2 overwrite_calc_fine + +=cut + +sub overwrite_calc_fine { + my ( $self, $params ) = @_; + + return [ "a", "b" ]; +} + +1; diff --git a/t/lib/plugins/Koha/Plugin/Test.pm b/t/lib/plugins/Koha/Plugin/Test.pm index 4d9904da86e..b3539b1ac67 100644 --- a/t/lib/plugins/Koha/Plugin/Test.pm +++ b/t/lib/plugins/Koha/Plugin/Test.pm @@ -451,11 +451,13 @@ sub auth_client_get_user { sub overwrite_calc_fine { my ( $self, $params ) = @_; - my $days = $params->{end_date}->delta_days( $params->{due_date} )->delta_days; + warn "itemnumber:" . $params->{itemnumber}; + warn "branchcode:" . $params->{branchcode}; + warn "categorycode:" . $params->{categorycode}; + warn "due_date_type:" . ref( $params->{due_date} ); + warn "end_date_type:" . ref( $params->{end_date} ); - return $params->{categorycode} && $params->{branchcode} - ? undef - : [ 3 * $days, $days, $days - 2 ]; + return [ 1, 2, 3 ]; } sub _private_sub { -- 2.49.0