From 44091772c4f291824b1710172e77a0cd4e09d0ac Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 22 Oct 2021 12:34:57 +0200 Subject: [PATCH] Bug 11175: Add tests and correct show_analytics logic --- C4/XSLT.pm | 29 +++++++-------- t/db_dependent/XSLT.t | 82 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 96 insertions(+), 15 deletions(-) diff --git a/C4/XSLT.pm b/C4/XSLT.pm index d133cdf6759..443d07ba99b 100644 --- a/C4/XSLT.pm +++ b/C4/XSLT.pm @@ -282,21 +282,22 @@ sub XSLTParse4Display { } # possibly show analytics link in Detail views - if ( $xslsyspref eq "OPACXSLTDetailsDisplay" || $xslsyspref eq "XSLTDetailsDisplay" ) { - $biblio //= Koha::Biblios->find( $biblionumber ); + my $ShowComponentRecords = C4::Context->preference('ShowComponentRecords'); + if ( + ( + $xslsyspref eq "OPACXSLTDetailsDisplay" + || $xslsyspref eq "XSLTDetailsDisplay" + ) + && ( $ShowComponentRecords ne 'both' + || ( $ShowComponentRecords eq 'staff' && $xslsyspref =~ m/OPAC/ ) + || ( $ShowComponentRecords eq 'opac' && $xslsyspref !~ m/OPAC/ ) ) + ) + { + + $biblio //= Koha::Biblios->find($biblionumber); my $components = $biblio->get_marc_components(); - $variables->{show_analytics_link} = ( scalar @{$components} == 0 ) ? 0 : 1; - - my $showcomp = C4::Context->preference('ShowComponentRecords'); - if ( - $variables->{show_analytics_link} - && ( $showcomp eq 'both' - || ( $showcomp eq 'staff' && $xslsyspref !~ m/OPAC/ ) - || ( $showcomp eq 'opac' && $xslsyspref =~ m/OPAC/ ) ) - ) - { - $variables->{show_analytics_link} = 0; - } + $variables->{show_analytics_link} = ( scalar @$components ) ? 1 : 0; + } my $varxml = "\n"; diff --git a/t/db_dependent/XSLT.t b/t/db_dependent/XSLT.t index 9cda1c5b8ce..e5791c364cd 100755 --- a/t/db_dependent/XSLT.t +++ b/t/db_dependent/XSLT.t @@ -18,7 +18,7 @@ use Modern::Perl; use MARC::Record; -use Test::More tests => 4; +use Test::More tests => 5; use Test::Warn; use t::lib::TestBuilder; use t::lib::Mocks; @@ -213,3 +213,83 @@ subtest 'buildKohaItemsNamespace() including/omitting items tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'XSLTParse4Display and show_analytics_link' => sub { + plan tests => 4; + + $schema->storage->txn_begin; + + my ($host_bibnum) = C4::Biblio::AddBiblio( host_record(), '' ); + t::lib::Mocks::mock_preference( 'SearchEngine', 'Zebra' ); + + my $biblio = Koha::Biblios->find($host_bibnum); + my $marc_record = $biblio->metadata->record; + + t::lib::Mocks::mock_preference( 'ShowComponentRecords', 'both' ); + my $xml = C4::XSLT::XSLTParse4Display( + { + biblionumber => $host_bibnum, + record => $marc_record, + xsl_syspref => 'OPACXSLTDetailsDisplay' + } + ); + unlike( $xml, qr{Show analytics}, 'If pref is on and record do not have component, show analytics link is not displayed' ); + + t::lib::Mocks::mock_preference( 'ShowComponentRecords', 'nowhere' ); + $xml = C4::XSLT::XSLTParse4Display( + { + biblionumber => $host_bibnum, + record => $marc_record, + xsl_syspref => 'OPACXSLTDetailsDisplay' + } + ); + unlike( $xml, qr{Show analytics}, 'If pref is off and record do not have component, show analytics link is not displayed' ); + + + my $search_mod = Test::MockModule->new('Koha::SearchEngine::Zebra::Search'); + $search_mod->mock( 'simple_search_compat', \&search_component_record ); + + t::lib::Mocks::mock_preference( 'ShowComponentRecords', 'both' ); + $xml = C4::XSLT::XSLTParse4Display( + { + biblionumber => $host_bibnum, + record => $marc_record, + xsl_syspref => 'OPACXSLTDetailsDisplay' + } + ); + unlike( $xml, qr{Show analytics}, 'If pref is on and record has component, show analytics link is not displayed' ); + + t::lib::Mocks::mock_preference( 'ShowComponentRecords', 'nowhere' ); + $xml = C4::XSLT::XSLTParse4Display( + { + biblionumber => $host_bibnum, + record => $marc_record, + xsl_syspref => 'OPACXSLTDetailsDisplay' + } + ); + like( $xml, qr{Show analytics}, 'If pref is off and record has component, show analytics link is displayed' ); + + $search_mod->unmock('simple_search_compat'); + + $schema->storage->txn_rollback; +}; + +sub host_record { + my $marc = MARC::Record->new; + $marc->append_fields( + MARC::Field->new( '001', '1234' ), + MARC::Field->new( '003', 'FIRST' ), + MARC::Field->new( '245', '', '', a => 'Some title 1' ), + ); + return $marc; +} + +sub search_component_record { + my $record = MARC::Record->new; + $record->append_fields( + MARC::Field->new( '001', '3456' ), + MARC::Field->new( '245', '', '', a => 'Some title' ), + MARC::Field->new( '773', '', '', w => '(FIRST)1234' ), + ); + return ( undef, [$record], 1 ); +} -- 2.25.1