From 585a24e390b85215b73a9d4b9e1cbfcf1c993eea Mon Sep 17 00:00:00 2001 From: Mark Tompsett Date: Wed, 16 Mar 2016 17:10:02 -0400 Subject: [PATCH] Bug 11592 - Add should_hide_marc method to filter Add should_hide_marc to ViewPolicy filter Add should_hide_marc tests to t/db_dependent/Filter_MARC_ViewPolicy.t Signed-off-by: Nick Clemens --- Koha/Filter/MARC/ViewPolicy.pm | 59 +++++++++++++++++++++++++++++++++ t/db_dependent/Filter_MARC_ViewPolicy.t | 54 ++++++++++++++++++++++-------- 2 files changed, 99 insertions(+), 14 deletions(-) diff --git a/Koha/Filter/MARC/ViewPolicy.pm b/Koha/Filter/MARC/ViewPolicy.pm index 98df72d..69a144c 100644 --- a/Koha/Filter/MARC/ViewPolicy.pm +++ b/Koha/Filter/MARC/ViewPolicy.pm @@ -215,6 +215,65 @@ sub _should_hide_on_interface { return $hide; } +=head2 should_hide_marc + +Return a hash reference of whether a field, built from +kohafield and tag, is hidden (1) or not (0) for a given +interface + + my $OpacHideMARC = + should_hide_marc( { + frameworkcode => $frameworkcode, + interface => 'opac', + } ); + + if ($OpacHideMARC->{'stocknumber'}==1) { + print "Hidden!\n"; + } + +C<$OpacHideMARC> is a ref to a hash which contains a series +of key value pairs indicating if that field (key) is +hidden (value == 1) or not (value == 0). + +C<$frameworkcode> is the framework code. + +C<$interface> is the interface. It defaults to 'opac' if +nothing is passed. Valid values include 'opac' or 'intranet'. + +=cut + +sub should_hide_marc { + my ( $self, $parms ) = @_; + my $frameworkcode = $parms->{frameworkcode} // q{}; + my $interface = $parms->{interface} // 'opac'; + my $hide = _should_hide_on_interface(); + + my %shouldhidemarc; + my $marc_subfield_structure = GetMarcStructure( 0, $frameworkcode ); + foreach my $tag ( keys %{$marc_subfield_structure} ) { + foreach my $subtag ( keys %{ $marc_subfield_structure->{$tag} } ) { + my $subfield_record = $marc_subfield_structure->{$tag}->{$subtag}; + if ( ref $subfield_record eq 'HASH' ) { + my $kohafield = $subfield_record->{'kohafield'}; + if ($kohafield) { + my @tmpsplit = split /[.]/xsm, $kohafield; + my $field = $tmpsplit[-1]; + my $hidden = $subfield_record->{'hidden'}; + my $shouldhide = $hide->{$interface}->{$hidden}; + if ($shouldhide) { + $shouldhidemarc{$field} = 1; + } + elsif ( !exists $shouldhidemarc{$field} ) { + $shouldhidemarc{$field} = 0; + } + } + } + } + } + + return \%shouldhidemarc; +} + =head1 DIAGNOSTICS $ prove -v t/RecordProcessor.t diff --git a/t/db_dependent/Filter_MARC_ViewPolicy.t b/t/db_dependent/Filter_MARC_ViewPolicy.t index ba446bc..0c757e5 100644 --- a/t/db_dependent/Filter_MARC_ViewPolicy.t +++ b/t/db_dependent/Filter_MARC_ViewPolicy.t @@ -30,7 +30,7 @@ use MARC::Record; use MARC::Field; use C4::Context; use C4::Biblio; -use Koha::Cache qw/flush_all/; +use Koha::Cache qw/get_instance flush_all/; use Koha::Database; BEGIN { @@ -60,13 +60,8 @@ sub run_hiding_tests { my ( $isbn_field, $isbn_subfield ) = GetMarcFromKohaField( 'biblioitems.isbn', q{} ); - my $update_sql = - q{UPDATE marc_subfield_structure SET hidden=? } - . q{WHERE tagfield='} - . $isbn_field - . q{' OR } - . q{ tagfield='008';}; - my $sth = $dbh->prepare($update_sql); + my $update_sql = q{UPDATE marc_subfield_structure SET hidden=? }; + my $sth = $dbh->prepare($update_sql); foreach my $hidden_value (@valid_hidden_values) { $sth->execute($hidden_value); @@ -99,10 +94,10 @@ sub run_hiding_tests { if ( any { $_ == $hidden_value } @{ $hidden->{$interface} } ) { # Subfield and controlfield are set to be hidden - is( $filtered_record->field('020'), + is( $filtered_record->field($isbn_field), undef, "Data field has been deleted because of hidden=$hidden_value" ); - isnt( $unfiltered_record->field('020'), undef, + isnt( $unfiltered_record->field($isbn_field), undef, "Data field has been deleted in the original record because of hidden=$hidden_value" ); @@ -118,10 +113,10 @@ sub run_hiding_tests { } else { - isnt( $filtered_record->field('020'), undef, + isnt( $filtered_record->field($isbn_field), undef, "Data field hasn't been deleted because of hidden=$hidden_value" ); - isnt( $unfiltered_record->field('020'), undef, + isnt( $unfiltered_record->field($isbn_field), undef, "Data field hasn't been deleted in the original record because of hidden=$hidden_value" ); @@ -133,11 +128,42 @@ sub run_hiding_tests { "Control field hasn't been deleted in the original record because of hidden=$hidden_value" ); + # force all the hidden values the same, so filtered and unfiltered + # records should be identical. is_deeply( $filtered_record, $unfiltered_record, 'Records are the same' ); } } + + $sth->execute(-1); # -1 is visible in opac and intranet. + + my $cache = Koha::Cache->get_instance(); + $cache->flush_all(); # easy way to ensure DB is queried again. + + my $shouldhidemarc = Koha::Filter::MARC::ViewPolicy->should_hide_marc( + { + frameworkcode => q{}, + interface => $interface + } + ); + my @hiddenfields = grep { $shouldhidemarc->{$_}==1 } keys %{$shouldhidemarc}; + + $sth->execute(8); # 8 is invisible in opac and intranet. + + $cache->flush_all(); # easy way to ensure DB is queried again. + + $shouldhidemarc = Koha::Filter::MARC::ViewPolicy->should_hide_marc( + { + frameworkcode => q{}, + interface => $interface + } + ); + my @keyvalues = keys %{$shouldhidemarc}; + my @visiblefields = grep { $shouldhidemarc->{$_}==1 } @keyvalues; + + is(scalar @hiddenfields,0,'Should Hide MARC - Full Visibility'); + is_deeply(\@visiblefields,\@keyvalues,'Should Hide MARC - No Visibility'); return; } @@ -164,7 +190,7 @@ sub create_marc_record { subtest 'Koha::Filter::MARC::ViewPolicy opac tests' => sub { - plan tests => 102; + plan tests => 104; $schema->storage->txn_begin(); run_hiding_tests('opac'); @@ -173,7 +199,7 @@ subtest 'Koha::Filter::MARC::ViewPolicy opac tests' => sub { subtest 'Koha::Filter::MARC::ViewPolicy intranet tests' => sub { - plan tests => 102; + plan tests => 104; $schema->storage->txn_begin(); run_hiding_tests('intranet'); -- 2.1.4