From b186d8901f32e276b407f1b5716490069c0b2fd7 Mon Sep 17 00:00:00 2001 From: Janusz Kaczmarek Date: Tue, 1 Apr 2025 11:20:13 +0000 Subject: [PATCH] Bug 39503: Unit tests NB, some previous test had to be adjusted to make it posible to call search_auth_compat in _search and properly interpret its results. --- t/db_dependent/Heading.t | 82 ++++++++++++++++++++++++++++------------ 1 file changed, 58 insertions(+), 24 deletions(-) diff --git a/t/db_dependent/Heading.t b/t/db_dependent/Heading.t index 1356bca8a0..e93750a5f5 100755 --- a/t/db_dependent/Heading.t +++ b/t/db_dependent/Heading.t @@ -24,6 +24,8 @@ use Test::More tests => 5; use t::lib::Mocks; use Test::MockModule; +use C4::AuthoritiesMarc qw/ AddAuthority ModAuthority /; + BEGIN { use_ok( 'C4::Heading', qw( field valid_heading_subfield ) ); } @@ -66,7 +68,10 @@ subtest "UNIMARC tests" => sub { subtest "_search tests" => sub { - plan tests => 10; + plan tests => 11; + + my $schema = Koha::Database->new->schema; + $schema->storage->txn_begin; t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' ); t::lib::Mocks::mock_preference( 'SearchEngine', 'Elasticsearch' ); @@ -177,46 +182,75 @@ subtest "_search tests" => sub { "Search formed as expected for a non-subject field with double punctuation, period+comma " ); + # Special case where thesaurus defined in subfield 2 should also match record with no thesaurus + my $auth_record_1 = MARC::Record->new(); + $auth_record_1->insert_fields_ordered( MARC::Field->new( '008', ' ' x 10 . 'z' . ' ' x 29 ) ); + $auth_record_1->insert_fields_ordered( MARC::Field->new( '150', ' ', ' ', a => 'Uncles', x => 'Fiction' ) ); + my $authid_1 = AddAuthority( + $auth_record_1, undef, 'TOPIC_TERM', + { skip_record_index => 1 } + ); + $search->mock( 'search_auth_compat', sub { my $self = shift; my $search_query = shift; - if ( scalar @{ $search_query->{query}->{bool}->{must} } == 2 - && $search_query->{query}->{bool}->{must}[1]->{term}->{'subject-heading-thesaurus.ci_raw'} eq - 'special_sauce' ) - { - return; + if ( scalar @{ $search_query->{query}->{bool}->{must} } == 2 ) { + if ( $search_query->{query}->{bool}->{must}[1]->{term}->{'subject-heading-thesaurus.ci_raw'} eq + 'special_sauce' ) + { + # no record with 'special_sauce' + return ( [], 0 ); + } elsif ( + $search_query->{query}->{bool}->{must}[1]->{term}->{'subject-heading-thesaurus.ci_raw'} eq 'z' ) + { + # for 'notdefined' we return the only record with 008/11 = 'z' + return ( [ { authid => $authid_1 } ], 1 ); + } else { + + # other cases - nothing + return ( [], 0 ); + } + } elsif ( scalar @{ $search_query->{query}->{bool}->{must} } == 1 ) { + return ['no thesaurus checking at all']; } - return ( $search_query, 1 ); } ); - # Special case where thesaurus defined in subfield 2 should also match record with no thesaurus $field = MARC::Field->new( '650', ' ', '7', a => 'Uncles', x => 'Fiction', 2 => 'special_sauce' ); $heading = C4::Heading->new_from_field($field); - ($search_query) = $heading->_search('match-heading'); - $terms = $search_query->{query}->{bool}->{must}; - $expected_terms = [ - { term => { 'match-heading.ci_raw' => 'Uncles generalsubdiv Fiction' } }, - { term => { 'subject-heading-thesaurus.ci_raw' => 'z' } }, - ]; + my ($matched_auths) = $heading->_search('match-heading'); + my $expected_result = [ { authid => $authid_1 } ]; is_deeply( - $terms, $expected_terms, - "When thesaurus in subfield 2, and nothing is found, we should search again for notdefined (008_11 = z) " + $matched_auths, $expected_result, + "When thesaurus in subfield 2, and nothing is found, we should search again for notdefined (008_11 = z) and get a result" + ); + + # Special case continued: but it should not match an authority record with a different thesaurus + # defined in 040 $f + $auth_record_1->insert_fields_ordered( MARC::Field->new( '040', ' ', ' ', f => 'special_sauce_2' ) ); + $authid_1 = ModAuthority( + $authid_1, $auth_record_1, 'TOPIC_TERM', + { skip_record_index => 1 } + ); + + ($matched_auths) = $heading->_search('match-heading'); + $expected_result = []; + is_deeply( + $matched_auths, $expected_result, + 'When thesaurus in subfield 2, and nothing is found, we search again for notdefined (008_11 = z), and get no results because biblio\'s subfield $2 does not conform to auth\'s 040 $f' ); + # When LinkerConsiderThesaurus off, no attantion is being paid on the thesaurus t::lib::Mocks::mock_preference( 'LinkerConsiderThesaurus', '0' ); - $search_query = undef; - ($search_query) = $heading->_search('match-heading'); - $terms = $search_query->{query}->{bool}->{must}; - $expected_terms = [ - { term => { 'match-heading.ci_raw' => 'Uncles generalsubdiv Fiction' } }, - ]; + ($matched_auths) = $heading->_search('match-heading'); + $expected_result = ['no thesaurus checking at all']; is_deeply( - $terms, $expected_terms, - "When thesaurus in subfield 2, and nothing is found, we don't search again if LinkerConsiderThesaurusDisabled" + $matched_auths, $expected_result, + "When thesaurus in subfield 2, and nothing is found, we don't search again if LinkerConsiderThesaurus disabled" ); + $schema->storage->txn_rollback; }; -- 2.39.5