3 # This file is part of Koha.
5 # Copyright 2015 Mark Tompsett
6 # - Initial commit, perlcritic clean-up, and
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>.
26 use Test::More tests => 3;
28 use List::MoreUtils qw/any/;
33 use Koha::Cache qw/flush_all/;
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 );
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{} );
64 q{UPDATE marc_subfield_structure SET hidden=? }
68 . q{ tagfield='008';};
69 my $sth = $dbh->prepare($update_sql);
70 foreach my $hidden_value (@valid_hidden_values) {
72 $sth->execute($hidden_value);
74 my $cache = Koha::Cache->get_instance();
75 $cache->flush_all(); # easy way to ensure DB is queried again.
77 my $processor = Koha::RecordProcessor->new(
80 filters => ('ViewPolicy'),
81 options => { interface => $interface }
86 ref( $processor->filters->[0] ),
87 'Koha::Filter::MARC::ViewPolicy',
88 "Created record processor with ViewPolicy filter ($hidden_value)"
91 # Create a fresh record
92 my $sample_record = create_marc_record();
93 my $unfiltered_record = $sample_record->clone();
96 my $filtered_record = $processor->process($sample_record);
99 if ( any { $_ == $hidden_value } @{ $hidden->{$interface} } ) {
101 # Subfield and controlfield are set to be hidden
102 is( $filtered_record->field('020'),
104 "Data field has been deleted because of hidden=$hidden_value" );
105 isnt( $unfiltered_record->field('020'), undef,
106 "Data field has been deleted in the original record because of hidden=$hidden_value"
109 # Control fields have a different behaviour in code
110 is( $filtered_record->field('008'), undef,
111 "Control field has been deleted because of hidden=$hidden_value"
113 isnt( $unfiltered_record->field('008'), undef,
114 "Control field has been deleted in the original record because of hidden=$hidden_value"
117 ok( $filtered_record && $unfiltered_record, 'Records exist' );
121 isnt( $filtered_record->field('020'), undef,
122 "Data field hasn't been deleted because of hidden=$hidden_value"
124 isnt( $unfiltered_record->field('020'), undef,
125 "Data field hasn't been deleted in the original record because of hidden=$hidden_value"
128 # Control fields have a different behaviour in code
129 isnt( $filtered_record->field('008'), undef,
130 "Control field hasn't been deleted because of hidden=$hidden_value"
132 isnt( $unfiltered_record->field('008'), undef,
133 "Control field hasn't been deleted in the original record because of hidden=$hidden_value"
136 is_deeply( $filtered_record, $unfiltered_record,
137 'Records are the same' );
144 sub create_marc_record {
146 my ( $title_field, $title_subfield ) =
147 GetMarcFromKohaField( 'biblio.title', q{} );
148 my ( $isbn_field, $isbn_subfield ) =
149 GetMarcFromKohaField( 'biblioitems.isbn', q{} );
150 my $isbn = '0590353403';
151 my $title = 'Foundation';
152 my $marc_record = MARC::Record->new;
154 MARC::Field->new( '003', 'AR-CdUBM' ),
155 MARC::Field->new( '008', '######suuuu####ag_||||__||||_0||_|_uuu|d' ),
156 MARC::Field->new( $isbn_field, q{}, q{}, $isbn_subfield => $isbn ),
157 MARC::Field->new( $title_field, q{}, q{}, $title_subfield => $title ),
160 $marc_record->insert_fields_ordered(@fields);
165 subtest 'Koha::Filter::MARC::ViewPolicy opac tests' => sub {
169 $schema->storage->txn_begin();
170 run_hiding_tests('opac');
171 $schema->storage->txn_rollback();
174 subtest 'Koha::Filter::MARC::ViewPolicy intranet tests' => sub {
178 $schema->storage->txn_begin();
179 run_hiding_tests('intranet');
180 $schema->storage->txn_rollback();