Bug 9302: (QA follow-up) Consistency follow-up
[koha.git] / t / db_dependent / Filter_MARC_ViewPolicy.t
blob64759fa5bb94c51b9ec1c24f7c77cc2fd24e89ff
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Copyright 2015 Mark Tompsett
6 # - Initial commit, perlcritic clean-up, and
7 # debugging
8 # Copyright 2016 Tomas Cohen Arazi
9 # - Expansion of test cases to be comprehensive
11 # Koha is free software; you can redistribute it and/or modify it
12 # under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 3 of the License, or
14 # (at your option) any later version.
16 # Koha is distributed in the hope that it will be useful, but
17 # WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with Koha; if not, see <http://www.gnu.org/licenses>.
24 use Modern::Perl;
26 use Test::More tests => 3;
28 use List::MoreUtils qw/any/;
29 use MARC::Record;
30 use MARC::Field;
31 use C4::Context;
32 use C4::Biblio;
33 use Koha::Caches;
34 use Koha::Database;
36 BEGIN {
37 use_ok('Koha::RecordProcessor');
40 my $dbh = C4::Context->dbh;
42 my $database = Koha::Database->new();
43 my $schema = $database->schema();
44 $dbh->{RaiseError} = 1;
46 sub run_hiding_tests {
48 my $interface = shift;
50 # TODO: -8 is Flagged, which doesn't seem used.
51 # -9 and +9 are supposedly valid future values
52 # according to older documentation in 3.10.x
53 my @valid_hidden_values =
54 ( -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8 );
56 my $hidden = {
57 'opac' => [ -8, 1, 2, 3, 4, 5, 6, 7, 8 ],
58 'intranet' => [ -8, -7, -4, -3, -2, 2, 3, 5, 8 ]
61 my ( $isbn_field, $isbn_subfield ) =
62 GetMarcFromKohaField( 'biblioitems.isbn', q{} );
63 my $update_sql = q{UPDATE marc_subfield_structure SET hidden=? };
64 my $sth = $dbh->prepare($update_sql);
65 foreach my $hidden_value (@valid_hidden_values) {
67 $sth->execute($hidden_value);
69 my $cache = Koha::Caches->get_instance();
70 $cache->flush_all(); # easy way to ensure DB is queried again.
72 my $processor = Koha::RecordProcessor->new(
74 schema => 'MARC',
75 filters => ('ViewPolicy'),
76 options => { interface => $interface }
80 is(
81 ref( $processor->filters->[0] ),
82 'Koha::Filter::MARC::ViewPolicy',
83 "Created record processor with ViewPolicy filter ($hidden_value)"
86 # Create a fresh record
87 my $sample_record = create_marc_record();
88 my $unfiltered_record = $sample_record->clone();
90 # Apply filters
91 my $filtered_record = $processor->process($sample_record);
93 # Data fields
94 if ( any { $_ == $hidden_value } @{ $hidden->{$interface} } ) {
96 # Subfield and controlfield are set to be hidden
97 is( $filtered_record->field($isbn_field),
98 undef,
99 "Data field has been deleted because of hidden=$hidden_value" );
100 isnt( $unfiltered_record->field($isbn_field), undef,
101 "Data field has been deleted in the original record because of hidden=$hidden_value"
104 # Control fields have a different behaviour in code
105 is( $filtered_record->field('008'), undef,
106 "Control field has been deleted because of hidden=$hidden_value"
108 isnt( $unfiltered_record->field('008'), undef,
109 "Control field has been deleted in the original record because of hidden=$hidden_value"
112 ok( $filtered_record && $unfiltered_record, 'Records exist' );
115 else {
116 isnt( $filtered_record->field($isbn_field), undef,
117 "Data field hasn't been deleted because of hidden=$hidden_value"
119 isnt( $unfiltered_record->field($isbn_field), undef,
120 "Data field hasn't been deleted in the original record because of hidden=$hidden_value"
123 # Control fields have a different behaviour in code
124 isnt( $filtered_record->field('008'), undef,
125 "Control field hasn't been deleted because of hidden=$hidden_value"
127 isnt( $unfiltered_record->field('008'), undef,
128 "Control field hasn't been deleted in the original record because of hidden=$hidden_value"
131 # force all the hidden values the same, so filtered and unfiltered
132 # records should be identical.
133 is_deeply( $filtered_record, $unfiltered_record,
134 'Records are the same' );
139 $sth->execute(-1); # -1 is visible in opac and intranet.
141 my $cache = Koha::Caches->get_instance();
142 $cache->flush_all(); # easy way to ensure DB is queried again.
144 my $shouldhidemarc = Koha::Filter::MARC::ViewPolicy->should_hide_marc(
146 frameworkcode => q{},
147 interface => $interface
150 my @hiddenfields = grep { $shouldhidemarc->{$_}==1 } keys %{$shouldhidemarc};
152 $sth->execute(8); # 8 is invisible in opac and intranet.
154 $cache->flush_all(); # easy way to ensure DB is queried again.
156 $shouldhidemarc = Koha::Filter::MARC::ViewPolicy->should_hide_marc(
158 frameworkcode => q{},
159 interface => $interface
162 my @keyvalues = keys %{$shouldhidemarc};
163 my @visiblefields = grep { $shouldhidemarc->{$_}==1 } @keyvalues;
165 is(scalar @hiddenfields,0,'Should Hide MARC - Full Visibility');
166 is_deeply(\@visiblefields,\@keyvalues,'Should Hide MARC - No Visibility');
167 return;
170 sub create_marc_record {
172 my ( $title_field, $title_subfield ) =
173 GetMarcFromKohaField( 'biblio.title', q{} );
174 my ( $isbn_field, $isbn_subfield ) =
175 GetMarcFromKohaField( 'biblioitems.isbn', q{} );
176 my $isbn = '0590353403';
177 my $title = 'Foundation';
178 my $marc_record = MARC::Record->new;
179 my @fields = (
180 MARC::Field->new( '003', 'AR-CdUBM' ),
181 MARC::Field->new( '008', '######suuuu####ag_||||__||||_0||_|_uuu|d' ),
182 MARC::Field->new( $isbn_field, q{}, q{}, $isbn_subfield => $isbn ),
183 MARC::Field->new( $title_field, q{}, q{}, $title_subfield => $title ),
186 $marc_record->insert_fields_ordered(@fields);
188 return $marc_record;
191 subtest 'Koha::Filter::MARC::ViewPolicy opac tests' => sub {
193 plan tests => 104;
195 $schema->storage->txn_begin();
196 run_hiding_tests('opac');
197 $schema->storage->txn_rollback();
200 subtest 'Koha::Filter::MARC::ViewPolicy intranet tests' => sub {
202 plan tests => 104;
204 $schema->storage->txn_begin();
205 run_hiding_tests('intranet');
206 $schema->storage->txn_rollback();