Bug 18501: (follow-up) Test undefined userenv behaviour
[koha.git] / t / db_dependent / Filter_MARC_ViewPolicy.t
blob552245a01b66f38f015b913ef1e683349b5b1cfc
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();
45 sub run_hiding_tests {
47 my $interface = shift;
49 # TODO: -8 is Flagged, which doesn't seem used.
50 # -9 and +9 are supposedly valid future values
51 # according to older documentation in 3.10.x
52 my @valid_hidden_values =
53 ( -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8 );
55 my $hidden = {
56 'opac' => [ -8, 1, 2, 3, 4, 5, 6, 7, 8 ],
57 'intranet' => [ -8, -7, -4, -3, -2, 2, 3, 5, 8 ]
60 my ( $isbn_field, $isbn_subfield ) =
61 GetMarcFromKohaField( 'biblioitems.isbn' );
62 my $update_sql = q{UPDATE marc_subfield_structure SET hidden=? };
63 my $sth = $dbh->prepare($update_sql);
64 foreach my $hidden_value (@valid_hidden_values) {
66 $sth->execute($hidden_value);
68 my $cache = Koha::Caches->get_instance();
69 $cache->flush_all(); # easy way to ensure DB is queried again.
71 my $processor = Koha::RecordProcessor->new(
73 schema => 'MARC',
74 filters => ('ViewPolicy'),
75 options => { interface => $interface }
79 is(
80 ref( $processor->filters->[0] ),
81 'Koha::Filter::MARC::ViewPolicy',
82 "Created record processor with ViewPolicy filter ($hidden_value)"
85 # Create a fresh record
86 my $sample_record = create_marc_record();
87 my $unfiltered_record = $sample_record->clone();
89 # Apply filters
90 my $filtered_record = $processor->process($sample_record);
92 # Data fields
93 if ( any { $_ == $hidden_value } @{ $hidden->{$interface} } ) {
95 # Subfield and controlfield are set to be hidden
96 is( $filtered_record->field($isbn_field),
97 undef,
98 "Data field has been deleted because of hidden=$hidden_value" );
99 isnt( $unfiltered_record->field($isbn_field), undef,
100 "Data field has been deleted in the original record because of hidden=$hidden_value"
103 # Control fields have a different behaviour in code
104 is( $filtered_record->field('008'), undef,
105 "Control field has been deleted because of hidden=$hidden_value"
107 isnt( $unfiltered_record->field('008'), undef,
108 "Control field has been deleted in the original record because of hidden=$hidden_value"
111 ok( $filtered_record && $unfiltered_record, 'Records exist' );
114 else {
115 isnt( $filtered_record->field($isbn_field), undef,
116 "Data field hasn't been deleted because of hidden=$hidden_value"
118 isnt( $unfiltered_record->field($isbn_field), undef,
119 "Data field hasn't been deleted in the original record because of hidden=$hidden_value"
122 # Control fields have a different behaviour in code
123 isnt( $filtered_record->field('008'), undef,
124 "Control field hasn't been deleted because of hidden=$hidden_value"
126 isnt( $unfiltered_record->field('008'), undef,
127 "Control field hasn't been deleted in the original record because of hidden=$hidden_value"
130 # force all the hidden values the same, so filtered and unfiltered
131 # records should be identical.
132 is_deeply( $filtered_record, $unfiltered_record,
133 'Records are the same' );
138 $sth->execute(-1); # -1 is visible in opac and intranet.
140 my $cache = Koha::Caches->get_instance();
141 $cache->flush_all(); # easy way to ensure DB is queried again.
143 my $shouldhidemarc = Koha::Filter::MARC::ViewPolicy->should_hide_marc(
145 frameworkcode => q{},
146 interface => $interface
149 my @hiddenfields = grep { $shouldhidemarc->{$_}==1 } keys %{$shouldhidemarc};
151 $sth->execute(8); # 8 is invisible in opac and intranet.
153 $cache->flush_all(); # easy way to ensure DB is queried again.
155 $shouldhidemarc = Koha::Filter::MARC::ViewPolicy->should_hide_marc(
157 frameworkcode => q{},
158 interface => $interface
161 my @keyvalues = keys %{$shouldhidemarc};
162 my @visiblefields = grep { $shouldhidemarc->{$_}==1 } @keyvalues;
164 is(scalar @hiddenfields,0,'Should Hide MARC - Full Visibility');
165 is_deeply(\@visiblefields,\@keyvalues,'Should Hide MARC - No Visibility');
166 return;
169 sub create_marc_record {
171 my ( $title_field, $title_subfield ) =
172 GetMarcFromKohaField( 'biblio.title' );
173 my ( $isbn_field, $isbn_subfield ) =
174 GetMarcFromKohaField( 'biblioitems.isbn' );
175 my $isbn = '0590353403';
176 my $title = 'Foundation';
177 my $marc_record = MARC::Record->new;
178 my @fields = (
179 MARC::Field->new( '003', 'AR-CdUBM' ),
180 MARC::Field->new( '008', '######suuuu####ag_||||__||||_0||_|_uuu|d' ),
181 MARC::Field->new( $isbn_field, q{}, q{}, $isbn_subfield => $isbn ),
182 MARC::Field->new( $title_field, q{}, q{}, $title_subfield => $title ),
185 $marc_record->insert_fields_ordered(@fields);
187 return $marc_record;
190 subtest 'Koha::Filter::MARC::ViewPolicy opac tests' => sub {
192 plan tests => 104;
194 $schema->storage->txn_begin();
195 run_hiding_tests('opac');
196 $schema->storage->txn_rollback();
199 subtest 'Koha::Filter::MARC::ViewPolicy intranet tests' => sub {
201 plan tests => 104;
203 $schema->storage->txn_begin();
204 run_hiding_tests('intranet');
205 $schema->storage->txn_rollback();