Bug 31196: Remove 'default_value_for_mod_marc-' clear_from_cache calls
[koha.git] / t / db_dependent / Filter_MARC_ViewPolicy.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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
10 #
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.
15 #
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.
20 #
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>.
23
24 use Modern::Perl;
25
26 use Test::More tests => 3;
27
28 use List::MoreUtils qw/any/;
29 use MARC::Record;
30 use MARC::Field;
31 use C4::Context;
32 use C4::Biblio qw( GetMarcFromKohaField );
33 use Koha::Caches;
34 use Koha::Database;
35
36 BEGIN {
37     use_ok('Koha::RecordProcessor');
38 }
39
40 my $dbh = C4::Context->dbh;
41
42 my $database = Koha::Database->new();
43 my $schema   = $database->schema();
44
45 sub run_hiding_tests {
46
47     my $interface = shift;
48
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 );
54
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 ]
58     };
59
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) {
65
66         $sth->execute($hidden_value);
67
68         my $cache = Koha::Caches->get_instance();
69         $cache->flush_all();    # easy way to ensure DB is queried again.
70
71         my $processor = Koha::RecordProcessor->new(
72             {
73                 schema  => 'MARC',
74                 filters => ('ViewPolicy'),
75                 options => { interface => $interface }
76             }
77         );
78
79         is(
80             ref( $processor->filters->[0] ),
81             'Koha::Filter::MARC::ViewPolicy',
82             "Created record processor with ViewPolicy filter ($hidden_value)"
83         );
84
85         # Create a fresh record
86         my $sample_record     = create_marc_record();
87         my $unfiltered_record = $sample_record->clone();
88
89         # Apply filters
90         my $filtered_record = $processor->process($sample_record);
91
92         # Data fields
93         if ( any { $_ == $hidden_value } @{ $hidden->{$interface} } ) {
94
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"
101             );
102
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"
106             );
107             isnt( $unfiltered_record->field('008'), undef,
108 "Control field has been deleted in the original record because of hidden=$hidden_value"
109             );
110
111             ok( $filtered_record && $unfiltered_record, 'Records exist' );
112
113         }
114         else {
115             isnt( $filtered_record->field($isbn_field), undef,
116                 "Data field hasn't been deleted because of hidden=$hidden_value"
117             );
118             isnt( $unfiltered_record->field($isbn_field), undef,
119 "Data field hasn't been deleted in the original record because of hidden=$hidden_value"
120             );
121
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"
125             );
126             isnt( $unfiltered_record->field('008'), undef,
127 "Control field hasn't been deleted in the original record because of hidden=$hidden_value"
128             );
129
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' );
134         }
135
136     }
137
138     $sth->execute(-1); # -1 is visible in opac and intranet.
139
140     my $cache = Koha::Caches->get_instance();
141     $cache->flush_all();    # easy way to ensure DB is queried again.
142
143     my $shouldhidemarc = Koha::Filter::MARC::ViewPolicy->should_hide_marc(
144         {
145             frameworkcode => q{},
146             interface     => $interface
147         }
148     );
149     my @hiddenfields = grep { $shouldhidemarc->{$_}==1 } keys %{$shouldhidemarc};
150
151     $sth->execute(8); # 8 is invisible in opac and intranet.
152
153     $cache->flush_all();    # easy way to ensure DB is queried again.
154
155     $shouldhidemarc = Koha::Filter::MARC::ViewPolicy->should_hide_marc(
156         {
157             frameworkcode => q{},
158             interface     => $interface
159         }
160     );
161     my @keyvalues = keys %{$shouldhidemarc};
162     my @visiblefields = grep { $shouldhidemarc->{$_}==1 } @keyvalues;
163
164     is(scalar @hiddenfields,0,'Should Hide MARC - Full Visibility');
165     is_deeply(\@visiblefields,\@keyvalues,'Should Hide MARC - No Visibility');
166     return;
167 }
168
169 sub create_marc_record {
170
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 ),
183     );
184
185     $marc_record->insert_fields_ordered(@fields);
186
187     return $marc_record;
188 }
189
190 subtest 'Koha::Filter::MARC::ViewPolicy opac tests' => sub {
191
192     plan tests => 104;
193
194     $schema->storage->txn_begin();
195     run_hiding_tests('opac');
196     $schema->storage->txn_rollback();
197 };
198
199 subtest 'Koha::Filter::MARC::ViewPolicy intranet tests' => sub {
200
201     plan tests => 104;
202
203     $schema->storage->txn_begin();
204     run_hiding_tests('intranet');
205     $schema->storage->txn_rollback();
206 };
207