Bug 16550: Add test to NewsChannels.t
[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;
33 use Koha::Cache qw/flush_all/;
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 $dbh->{RaiseError} = 1;
45
46 sub run_hiding_tests {
47
48     my $interface = shift;
49
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 );
55
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 ]
59     };
60
61     my ( $isbn_field, $isbn_subfield ) =
62       GetMarcFromKohaField( 'biblioitems.isbn', q{} );
63     my $update_sql =
64         q{UPDATE marc_subfield_structure SET hidden=? }
65       . q{WHERE tagfield='}
66       . $isbn_field
67       . q{' OR }
68       . q{      tagfield='008';};
69     my $sth = $dbh->prepare($update_sql);
70     foreach my $hidden_value (@valid_hidden_values) {
71
72         $sth->execute($hidden_value);
73
74         my $cache = Koha::Cache->get_instance();
75         $cache->flush_all();    # easy way to ensure DB is queried again.
76
77         my $processor = Koha::RecordProcessor->new(
78             {
79                 schema  => 'MARC',
80                 filters => ('ViewPolicy'),
81                 options => { interface => $interface }
82             }
83         );
84
85         is(
86             ref( $processor->filters->[0] ),
87             'Koha::Filter::MARC::ViewPolicy',
88             "Created record processor with ViewPolicy filter ($hidden_value)"
89         );
90
91         # Create a fresh record
92         my $sample_record     = create_marc_record();
93         my $unfiltered_record = $sample_record->clone();
94
95         # Apply filters
96         my $filtered_record = $processor->process($sample_record);
97
98         # Data fields
99         if ( any { $_ == $hidden_value } @{ $hidden->{$interface} } ) {
100
101             # Subfield and controlfield are set to be hidden
102             is( $filtered_record->field('020'),
103                 undef,
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"
107             );
108
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"
112             );
113             isnt( $unfiltered_record->field('008'), undef,
114 "Control field has been deleted in the original record because of hidden=$hidden_value"
115             );
116
117             ok( $filtered_record && $unfiltered_record, 'Records exist' );
118
119         }
120         else {
121             isnt( $filtered_record->field('020'), undef,
122                 "Data field hasn't been deleted because of hidden=$hidden_value"
123             );
124             isnt( $unfiltered_record->field('020'), undef,
125 "Data field hasn't been deleted in the original record because of hidden=$hidden_value"
126             );
127
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"
131             );
132             isnt( $unfiltered_record->field('008'), undef,
133 "Control field hasn't been deleted in the original record because of hidden=$hidden_value"
134             );
135
136             is_deeply( $filtered_record, $unfiltered_record,
137                 'Records are the same' );
138         }
139
140     }
141     return;
142 }
143
144 sub create_marc_record {
145
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;
153     my @fields      = (
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 ),
158     );
159
160     $marc_record->insert_fields_ordered(@fields);
161
162     return $marc_record;
163 }
164
165 subtest 'Koha::Filter::MARC::ViewPolicy opac tests' => sub {
166
167     plan tests => 102;
168
169     $schema->storage->txn_begin();
170     run_hiding_tests('opac');
171     $schema->storage->txn_rollback();
172 };
173
174 subtest 'Koha::Filter::MARC::ViewPolicy intranet tests' => sub {
175
176     plan tests => 102;
177
178     $schema->storage->txn_begin();
179     run_hiding_tests('intranet');
180     $schema->storage->txn_rollback();
181 };
182
183 1;