From b7e5dab1e71c46a52a94a0b5c244a716653b61be Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Mon, 13 May 2024 14:03:47 +0000 Subject: [PATCH] Bug 30047: (follow-up) Fix failing tests This patch updates the test files to correctly create authorities and/or correctly mock the needed data for authorities. the tests in t/db_dependent/Authority/Merge.t cover the case of 'Default' authorities which don't have valid headings - so I had to add handling for blanking the heading field when Default type is used. Signed-off-by: Katrin Fischer --- C4/AuthoritiesMarc.pm | 4 +++- C4/Heading.pm | 1 + t/db_dependent/Authority/Merge.t | 10 ++++++++- t/db_dependent/Biblio.t | 4 ++++ t/db_dependent/Koha/Authorities.t | 6 +++++ t/db_dependent/Koha/Plugins/authority_hooks.t | 8 +++++-- t/db_dependent/api/v1/authorities.t | 22 +++++++++++++++---- 7 files changed, 47 insertions(+), 8 deletions(-) diff --git a/C4/AuthoritiesMarc.pm b/C4/AuthoritiesMarc.pm index a0fb181de7..b595230c5d 100644 --- a/C4/AuthoritiesMarc.pm +++ b/C4/AuthoritiesMarc.pm @@ -687,6 +687,8 @@ sub AddAuthority { } else { $action = 'modify'; $authority = Koha::Authorities->find($authid); + $authority->authtypecode($authtypecode); + # In the case we are changing type we need to set this to ensure heading field is correctly chosen } # Insert/update the recordID in MARC record @@ -701,7 +703,7 @@ sub AddAuthority { authtypecode => $authtypecode, marc => $record->as_usmarc, marcxml => $record->as_xml_record($format), - heading => $heading->display_form, + heading => $heading ? $heading->display_form : '', } ); diff --git a/C4/Heading.pm b/C4/Heading.pm index 9a56bf9a01..653cd9636d 100644 --- a/C4/Heading.pm +++ b/C4/Heading.pm @@ -66,6 +66,7 @@ sub new_from_field { my $marcflavour = C4::Context->preference('marcflavour'); my $marc_handler = _marc_format_handler($marcflavour); + return unless $field; my $tag = $field->tag(); return unless $marc_handler->valid_heading_tag( $tag, $frameworkcode, $auth ); my $self = {}; diff --git a/t/db_dependent/Authority/Merge.t b/t/db_dependent/Authority/Merge.t index cfb5fd26fe..5632141dfd 100755 --- a/t/db_dependent/Authority/Merge.t +++ b/t/db_dependent/Authority/Merge.t @@ -33,6 +33,14 @@ t::lib::Mocks::mock_preference( 'marcflavour', $marcflavour ) if $marcflavour; my $schema = Koha::Database->new->schema; $schema->storage->txn_begin; +my $heading_module = Test::MockModule->new('C4::Heading::MARC21'); +$heading_module->mock( + 'valid_heading_tag', + sub { + return 1; + } +); + # Global variables, mocking and framework modifications our @linkedrecords; my $mocks = set_mocks(); @@ -183,7 +191,7 @@ subtest 'Test merge A1 to B1 (changing authtype)' => sub { my $authid1 = AddAuthority( $auth1, undef, $authtype1 ); my $auth2 = MARC::Record->new; $auth2->append_fields( MARC::Field->new( '112', '0', '0', 'a' => 'Batman', c => 'cc' )); - my $authid2 = AddAuthority($auth1, undef, $authtype2 ); + my $authid2 = AddAuthority($auth2, undef, $authtype2 ); # create a biblio with one 109 and two 609s to be touched # seems exceptional see bug 13760 comment10 diff --git a/t/db_dependent/Biblio.t b/t/db_dependent/Biblio.t index 42f1c86188..1a5bf99fd2 100755 --- a/t/db_dependent/Biblio.t +++ b/t/db_dependent/Biblio.t @@ -643,6 +643,10 @@ subtest 'UNIMARC' => sub { # Mock the auth type data for UNIMARC $dbh->do("UPDATE auth_types SET auth_tag_to_report = '106' WHERE auth_tag_to_report = '100'") or die $dbh->errstr; + $dbh->do( + "UPDATE marc_subfield_structure SET tagfield = '106' WHERE authtypecode = 'PERSO_NAME' AND frameworkcode='' AND tagfield='100' " + ) or die $dbh->errstr; + run_tests('UNIMARC'); $schema->storage->txn_rollback; $schema->storage->txn_begin; diff --git a/t/db_dependent/Koha/Authorities.t b/t/db_dependent/Koha/Authorities.t index 111a68dbd2..6d0e1ffd74 100755 --- a/t/db_dependent/Koha/Authorities.t +++ b/t/db_dependent/Koha/Authorities.t @@ -187,6 +187,12 @@ unimarc,*,ind1:auth2,ind2:auth1|); $record = MARC::Record->new; $record->append_fields( MARC::Field->new( '210', '1', '2', a => 'Name' ) ); $type = $builder->build({ source => 'AuthType', value => { auth_tag_to_report => '210'} }); + my $dbh = C4::Context->dbh; + $dbh->do( + "INSERT INTO marc_subfield_structure (tagfield,authtypecode,frameworkcode) VALUES ('210',?,'')", undef, + $type->{authtypecode} + ) or die $dbh->errstr; + $authid = C4::AuthoritiesMarc::AddAuthority( $record, undef, $type->{authtypecode} ); $auth = Koha::Authorities->find( $authid ); is( $auth->controlled_indicators({ biblio_tag => '345' })->{ind1}, '2', 'UNIMARC: Swapped ind2' ); diff --git a/t/db_dependent/Koha/Plugins/authority_hooks.t b/t/db_dependent/Koha/Plugins/authority_hooks.t index f0f1575088..be0b12c491 100755 --- a/t/db_dependent/Koha/Plugins/authority_hooks.t +++ b/t/db_dependent/Koha/Plugins/authority_hooks.t @@ -53,11 +53,15 @@ subtest 'after_authority_action hook' => sub { my $plugin = Koha::Plugin::Test->new->enable; my $id; - warnings_exist { ( $id ) = C4::AuthoritiesMarc::AddAuthority( MARC::Record->new, undef, 'PERSO_NAME' ); } + my $record = MARC::Record->new; + $record->append_fields( MARC::Field->new( '100', '1', '2', a => 'Name' ) ); + my $type = $builder->build( { source => 'AuthType', value => { auth_tag_to_report => '100' } } ); + + warnings_exist { ( $id ) = C4::AuthoritiesMarc::AddAuthority( $record, undef, $type->{authtypecode} ); } qr/after_authority_action called with action: create, id: \d+/, 'AddAuthority calls the hook with action=create, id passed'; - warnings_exist { C4::AuthoritiesMarc::ModAuthority( $id, MARC::Record->new, 'PERSO_NAME', { skip_merge => 1 } ); } + warnings_exist { C4::AuthoritiesMarc::ModAuthority( $id, $record, $type->{authtypecode}, { skip_merge => 1 } ); } qr/after_authority_action called with action: modify, id: $id/, 'ModAuthority calls the hook with action=modify, id passed'; diff --git a/t/db_dependent/api/v1/authorities.t b/t/db_dependent/api/v1/authorities.t index d0701b1415..41c9be864e 100755 --- a/t/db_dependent/api/v1/authorities.t +++ b/t/db_dependent/api/v1/authorities.t @@ -252,16 +252,30 @@ subtest 'put() tests' => sub { $patron->set_password( { password => $password, skip_validation => 1 } ); my $userid = $patron->userid; - my $authority = $builder->build_object({ 'class' => 'Koha::Authorities', value => { - marcxml => q| + my $authority_type = $builder->build_object( + { + 'class' => 'Koha::Authority::Types', + value => { + auth_tag_to_report => '110', + } + } + ); + my $authority = $builder->build_object( + { + 'class' => 'Koha::Authorities', + value => { + marcxml => q| 1001 102 My Corporation -| - } }); +|, + authtypecode => $authority_type->authtypecode, + } + } + ); my $authid = $authority->id; my $authtypecode = $authority->authtypecode; -- 2.39.2