From 3d71186075f58f7a2cde05985d09b956d4643453 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Fri, 8 Dec 2023 17:23:44 -0300 Subject: [PATCH] Bug 34943: Unit tests Signed-off-by: David Nind --- .../Plugins/Biblio_and_Items_plugin_hooks.t | 53 ++++++++++++++++++- t/lib/plugins/Koha/Plugin/Test.pm | 14 +++++ 2 files changed, 66 insertions(+), 1 deletion(-) 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 4f29aae60f..cd4d61c530 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 @@ -16,11 +16,12 @@ use Modern::Perl; -use Test::More tests => 4; +use Test::More tests => 5; use Test::Warn; use File::Basename; +use C4::Biblio qw(AddBiblio ModBiblio); use C4::Items; use t::lib::Mocks; @@ -86,3 +87,53 @@ subtest 'after_biblio_action() and after_item_action() hooks tests' => sub { $schema->storage->txn_rollback; Koha::Plugins::Methods->delete; }; + +subtest 'before_biblio_metadata_store() hooks tests' => sub { + + plan tests => 5; + + $schema->storage->txn_begin; + + my $plugins = Koha::Plugins->new; + $plugins->InstallPlugins; + + my $plugin = Koha::Plugin::Test->new->enable; + + my $subfield_contents = 'Arte club'; + + my $test_plugin = Test::MockModule->new('Koha::Plugin::Test'); + $test_plugin->mock( 'after_biblio_action', undef ); + + # Defaults to avoid noise + my $options = { + disable_autolink => 1, + skip_holds_queue => 1, + skip_record_index => 1, + }; + + # Add a record + my ( $biblio_id, undef ) = C4::Biblio::AddBiblio( MARC::Record->new(), '' ); + + my $record = Koha::Biblios->find($biblio_id)->metadata->record; + + my @fields_990 = $record->field('990'); + + is( scalar @fields_990, 1, 'One field added' ); + is( $fields_990[0]->subfield('a'), $subfield_contents ); + + # Simulate editing the record + ModBiblio( $record, $biblio_id, '', $options ); + + $record = Koha::Biblios->find($biblio_id)->record; + + @fields_990 = $record->field('990'); + # This is to highlight that is the plugin responsibility to choose what to do on the record + is( scalar @fields_990, 2, 'Two saves, two fields' ); + + foreach my $index (qw(0 1)) { + is( $fields_990[$index]->subfield('a'), $subfield_contents ); + } + + $schema->storage->txn_rollback; + Koha::Plugins::Methods->delete; +}; diff --git a/t/lib/plugins/Koha/Plugin/Test.pm b/t/lib/plugins/Koha/Plugin/Test.pm index 6744a7e7f3..d2817cfbdb 100644 --- a/t/lib/plugins/Koha/Plugin/Test.pm +++ b/t/lib/plugins/Koha/Plugin/Test.pm @@ -6,6 +6,7 @@ use Modern::Perl; use Koha::Exception; use Koha::Plugins::Tab; +use MARC::Field; use Mojo::JSON qw( decode_json ); ## Required for all plugins @@ -148,6 +149,19 @@ sub after_hold_create { Koha::Exception->throw("after_hold_create called with parameter " . ref($param) ); } +sub before_biblio_metadata_store { + my ( $self, $record ) = @_; + + $record->insert_fields_ordered( + MARC::Field->new( + '990', '', '', + 'a' => 'Arte club' + ) + ); + + return $record; +} + sub after_biblio_action { my ( $self, $params ) = @_; my $action = $params->{action} // ''; -- 2.30.2