From 94139f1e4a67fa1d128718c844eb6c78ba89b1a4 Mon Sep 17 00:00:00 2001 From: Shi Yao Wang Date: Tue, 1 Apr 2025 12:12:01 -0400 Subject: [PATCH] Bug 39156: Unit test Test plan: 1- Apply the patch 2- run `prove t/db_dependent/Koha/Plugins/Batch_plugin_hooks.t` 3- Tests should pass Signed-off-by: Matthias Meusburger --- .../Koha/Plugins/Batch_plugin_hooks.t | 243 ++++++++++++++++++ t/lib/plugins/Koha/Plugin/Test.pm | 51 +++- 2 files changed, 291 insertions(+), 3 deletions(-) create mode 100644 t/db_dependent/Koha/Plugins/Batch_plugin_hooks.t diff --git a/t/db_dependent/Koha/Plugins/Batch_plugin_hooks.t b/t/db_dependent/Koha/Plugins/Batch_plugin_hooks.t new file mode 100644 index 00000000000..3cb8b0d66c3 --- /dev/null +++ b/t/db_dependent/Koha/Plugins/Batch_plugin_hooks.t @@ -0,0 +1,243 @@ +#!/usr/bin/perl + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, see . + +use Modern::Perl; + +use Test::NoWarnings; +use Test::More tests => 10; +use Test::Warn; +use JSON qw( decode_json ); + +use File::Basename; + +use t::lib::Mocks; +use t::lib::TestBuilder; + +BEGIN { + # Mock pluginsdir before loading Plugins module + my $path = dirname(__FILE__) . '/../../../lib/plugins'; + t::lib::Mocks::mock_config( 'pluginsdir', $path ); + + use_ok('Koha::Plugins'); + use_ok('Koha::Plugins::Handler'); + use_ok('Koha::Plugin::Test'); + + use_ok('Koha::BackgroundJobs'); + use_ok('Koha::BackgroundJob::MARCImportCommitBatch'); + use_ok('Koha::BackgroundJob::StageMARCForImport'); +} + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +t::lib::Mocks::mock_config( 'enable_plugins', 1 ); + + +subtest 'before_batch_action and after_batch_action hooks with after_biblio_action hook tests' => sub { + + plan tests => 1; + + $schema->storage->txn_begin; + + my $plugins = Koha::Plugins->new; + $plugins->InstallPlugins; + + my $plugin = Koha::Plugin::Test->new->enable; + + my $test_plugin3 = Test::MockModule->new('Koha::Plugin::Test'); + $test_plugin3->mock( 'after_item_action', undef ); + $test_plugin3->mock( 'item_barcode_transform', undef ); + + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + my $job = Koha::BackgroundJob::StageMARCForImport->new( + { + status => 'new', + size => 1, + borrowernumber => $patron->borrowernumber, + type => 'stage_marc_for_import', + } + )->store; + $job = Koha::BackgroundJobs->find( $job->id ); + $job->process( + { + job_id => $job->id, + record_type => 'biblio', + encoding => 'UTF-8', + format => 'ISO2709', + filepath => 't/db_dependent/data/marc21/zebraexport/biblio/exported_records', + filename => 'some_records', + parse_items => 1, + } + ); + + my $report = decode_json($job->get_from_storage->data)->{report}; + my $import_batch_id = $report->{import_batch_id}; + + my $job2 = Koha::BackgroundJob::MARCImportCommitBatch->new( + { + status => 'new', + size => 1, + borrowernumber => $patron->borrowernumber, + type => 'marc_import_commit_batch' + } + )->store; + $job2 = Koha::BackgroundJobs->find( $job2->id ); + + warning_like { + $job2->process( + { + job_id => $job2->id, + import_batch_id => $import_batch_id, + frameworkcode => q{}, + } + ); + } qr/after_batch_action called with addBiblio count: 178/, + 'MARCImportCommitBatch calls the after_batch_action hook'; + + Koha::Plugins->RemovePlugins; + $schema->storage->txn_rollback; + +}; + +subtest 'before_batch_action and after_batch_action hooks with after_item_action hook tests' => sub { + + plan tests => 1; + + $schema->storage->txn_begin; + + my $plugins = Koha::Plugins->new; + $plugins->InstallPlugins; + + my $plugin = Koha::Plugin::Test->new->enable; + + my $test_plugin4 = Test::MockModule->new('Koha::Plugin::Test'); + $test_plugin4->mock( 'after_biblio_action', undef ); + $test_plugin4->mock( 'item_barcode_transform', undef ); + + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + my $job = Koha::BackgroundJob::StageMARCForImport->new( + { + status => 'new', + size => 1, + borrowernumber => $patron->borrowernumber, + type => 'stage_marc_for_import', + } + )->store; + $job = Koha::BackgroundJobs->find( $job->id ); + $job->process( + { + job_id => $job->id, + record_type => 'biblio', + encoding => 'UTF-8', + format => 'ISO2709', + filepath => 't/db_dependent/data/marc21/zebraexport/biblio/exported_records', + filename => 'some_records', + parse_items => 1, + } + ); + + my $report = decode_json($job->get_from_storage->data)->{report}; + my $import_batch_id = $report->{import_batch_id}; + + my $job2 = Koha::BackgroundJob::MARCImportCommitBatch->new( + { + status => 'new', + size => 1, + borrowernumber => $patron->borrowernumber, + type => 'marc_import_commit_batch' + } + )->store; + $job2 = Koha::BackgroundJobs->find( $job2->id ); + + warning_like { + $job2->process( + { + job_id => $job2->id, + import_batch_id => $import_batch_id, + frameworkcode => q{}, + } + ); + } qr/after_batch_action called with addItem count: 138/, + 'MARCImportCommitBatch calls the after_batch_action hook'; + + Koha::Plugins->RemovePlugins; + $schema->storage->txn_rollback; + +}; + +subtest 'before_batch_action and after_batch_action hooks with after_authority_action hook tests' => sub { + + plan tests => 1; + + $schema->storage->txn_begin; + + my $plugins = Koha::Plugins->new; + $plugins->InstallPlugins; + + my $plugin = Koha::Plugin::Test->new->enable; + + my $test_plugin5 = Test::MockModule->new('Koha::Plugin::Test'); + $test_plugin5->mock( 'elasticsearch_to_document', undef ); + + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + my $job = Koha::BackgroundJob::StageMARCForImport->new( + { + status => 'new', + size => 1, + borrowernumber => $patron->borrowernumber, + type => 'stage_marc_for_import', + } + )->store; + $job = Koha::BackgroundJobs->find( $job->id ); + $job->process( + { + job_id => $job->id, + record_type => 'auth', + encoding => 'UTF-8', + format => 'ISO2709', + filepath => 't/db_dependent/data/marc21/zebraexport/authority/exported_records', + filename => 'some_auths', + } + ); + + my $report = decode_json($job->get_from_storage->data)->{report}; + my $import_batch_id = $report->{import_batch_id}; + + my $job2 = Koha::BackgroundJob::MARCImportCommitBatch->new( + { + status => 'new', + size => 1, + borrowernumber => $patron->borrowernumber, + type => 'marc_import_commit_batch' + } + )->store; + $job2 = Koha::BackgroundJobs->find( $job2->id ); + + warning_like { + $job2->process( + { + job_id => $job2->id, + import_batch_id => $import_batch_id, + frameworkcode => q{}, + } + ); + } qr/after_batch_action called with addAuthority count: 2/, + 'MARCImportCommitBatch calls the after_batch_action hook'; + + Koha::Plugins->RemovePlugins; + $schema->storage->txn_rollback; + +}; diff --git a/t/lib/plugins/Koha/Plugin/Test.pm b/t/lib/plugins/Koha/Plugin/Test.pm index f2f10330d35..ccce37fb77a 100644 --- a/t/lib/plugins/Koha/Plugin/Test.pm +++ b/t/lib/plugins/Koha/Plugin/Test.pm @@ -174,13 +174,19 @@ sub before_biblio_action { return $record; } +my $is_batch = 0; +my (@addBiblio, @addItem, @addAuthority); sub after_biblio_action { my ( $self, $params ) = @_; my $action = $params->{action} // ''; my $biblio = $params->{biblio}; my $biblio_id = $params->{biblio_id}; - if ( $action ne 'delete' ) { + if ($is_batch) { + if ( $action eq 'create' ) { + push @addBiblio, $biblio_id; + } + } elsif ( $action ne 'delete' ) { Koha::Exception->throw( "after_biblio_action called with action: $action, ref: " . ref($biblio) ); } else { Koha::Exception->throw("after_biblio_action called with action: $action, id: $biblio_id") if $biblio_id; @@ -193,7 +199,11 @@ sub after_item_action { my $item = $params->{item}; my $item_id = $params->{item_id}; - if ( $action ne 'delete' ) { + if ($is_batch) { + if ( $action eq 'create' ) { + push @addItem, $item_id; + } + } elsif ( $action ne 'delete' ) { my $itemnumber_defined = ( defined $item->itemnumber ) ? 'yes' : 'no'; my $item_id_defined = ( defined $item_id ) ? 'yes' : 'no'; Koha::Exception->throw( "after_item_action called with action: $action, ref: " @@ -209,7 +219,14 @@ sub after_authority_action { my ( $self, $params ) = @_; my $action = $params->{action} // q{}; my $id = $params->{authority_id} // 0; - Koha::Exception->throw("after_authority_action called with action: $action, id: $id"); + + if ($is_batch) { + if ( $action eq 'create' ) { + push @addAuthority, $id; + } + } else { + Koha::Exception->throw("after_authority_action called with action: $action, id: $id"); + } } sub after_circ_action { @@ -389,6 +406,34 @@ sub after_recall_action { Koha::Exception->throw( "after_recall_action called with action: $action, ref: " . ref($recall) ); } +sub before_batch_action { + my ( $self ) = @_; + $is_batch = 1; +} + +sub after_batch_action { + my ( $self ) = @_; + $is_batch = 0; + + if (@addBiblio) { + my $count = scalar(@addBiblio); + @addBiblio = (); + Koha::Exception->throw("after_batch_action called with addBiblio count: " . $count); + } + + if (@addItem) { + my $count = scalar(@addItem); + @addItem = (); + Koha::Exception->throw("after_batch_action called with addItem count: " . $count); + } + + if (@addAuthority) { + my $count = scalar(@addAuthority); + @addAuthority = (); + Koha::Exception->throw("after_batch_action called with addAuthority count: " . $count); + } +} + sub template_include_paths { my ($self) = @_; -- 2.39.5