From 85f11f180ac52c82dc99380bff761ac5628acd72 Mon Sep 17 00:00:00 2001 From: Shi Yao Wang Date: Mon, 31 Mar 2025 15:03:54 -0400 Subject: [PATCH] Bug 39156: Unit test Test plan: 1- Apply the patch 2- run `prove t/db_dependent/Koha/Plugins/Biblio_and_items_plugin_hooks.t` 3- Tests should pass --- .../Plugins/Biblio_and_Items_plugin_hooks.t | 73 ++++++++++++++++++- t/lib/plugins/Koha/Plugin/Test.pm | 22 +++++- 2 files changed, 93 insertions(+), 2 deletions(-) diff --git a/t/db_dependent/Koha/Plugins/Biblio_and_Items_plugin_hooks.t b/t/db_dependent/Koha/Plugins/Biblio_and_Items_plugin_hooks.t index 73220cdb0b..5161460f77 100755 --- a/t/db_dependent/Koha/Plugins/Biblio_and_Items_plugin_hooks.t +++ b/t/db_dependent/Koha/Plugins/Biblio_and_Items_plugin_hooks.t @@ -17,8 +17,9 @@ use Modern::Perl; use Test::NoWarnings; -use Test::More tests => 7; +use Test::More tests => 11; use Test::Warn; +use JSON qw( decode_json ); use File::Basename; @@ -38,6 +39,10 @@ BEGIN { 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; @@ -190,3 +195,69 @@ subtest 'elasticsearch_to_document() hooks tests' => sub { Koha::Plugins->RemovePlugins; $schema->storage->txn_rollback; }; + +subtest 'before_batch_action() and after_batch_action() hooks 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( 'item_barcode_transform', undef ); + $test_plugin3->mock( 'after_item_action', 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; + +}; diff --git a/t/lib/plugins/Koha/Plugin/Test.pm b/t/lib/plugins/Koha/Plugin/Test.pm index f2f10330d3..3e7842f2d7 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; 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; @@ -389,6 +395,20 @@ 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) { + Koha::Exception->throw("after_batch_action called with addBiblio count: " . scalar(@addBiblio)); + } +} + sub template_include_paths { my ($self) = @_; -- 2.43.0