Bug 16911: Koha::Patron::Categories - Add tests for ->get_expiry_date
[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::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 $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 = q{UPDATE marc_subfield_structure SET hidden=? };
64     my $sth        = $dbh->prepare($update_sql);
65     foreach my $hidden_value (@valid_hidden_values) {
66
67         $sth->execute($hidden_value);
68
69         my $cache = Koha::Caches->get_instance();
70         $cache->flush_all();    # easy way to ensure DB is queried again.
71
72         my $processor = Koha::RecordProcessor->new(
73             {
74                 schema  => 'MARC',
75                 filters => ('ViewPolicy'),
76                 options => { interface => $interface }
77             }
78         );
79
80         is(
81             ref( $processor->filters->[0] ),
82             'Koha::Filter::MARC::ViewPolicy',
83             "Created record processor with ViewPolicy filter ($hidden_value)"
84         );
85
86         # Create a fresh record
87         my $sample_record     = create_marc_record();
88         my $unfiltered_record = $sample_record->clone();
89
90         # Apply filters
91         my $filtered_record = $processor->process($sample_record);
92
93         # Data fields
94         if ( any { $_ == $hidden_value } @{ $hidden->{$interface} } ) {
95
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"
102             );
103
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"
107             );
108             isnt( $unfiltered_record->field('008'), undef,
109 "Control field has been deleted in the original record because of hidden=$hidden_value"
110             );
111
112             ok( $filtered_record && $unfiltered_record, 'Records exist' );
113
114         }
115         else {
116             isnt( $filtered_record->field($isbn_field), undef,
117                 "Data field hasn't been deleted because of hidden=$hidden_value"
118             );
119             isnt( $unfiltered_record->field($isbn_field), undef,
120 "Data field hasn't been deleted in the original record because of hidden=$hidden_value"
121             );
122
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"
126             );
127             isnt( $unfiltered_record->field('008'), undef,
128 "Control field hasn't been deleted in the original record because of hidden=$hidden_value"
129             );
130
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' );
135         }
136
137     }
138
139     $sth->execute(-1); # -1 is visible in opac and intranet.
140
141     my $cache = Koha::Caches->get_instance();
142     $cache->flush_all();    # easy way to ensure DB is queried again.
143
144     my $shouldhidemarc = Koha::Filter::MARC::ViewPolicy->should_hide_marc(
145         {
146             frameworkcode => q{},
147             interface     => $interface
148         }
149     );
150     my @hiddenfields = grep { $shouldhidemarc->{$_}==1 } keys %{$shouldhidemarc};
151
152     $sth->execute(8); # 8 is invisible in opac and intranet.
153
154     $cache->flush_all();    # easy way to ensure DB is queried again.
155
156     $shouldhidemarc = Koha::Filter::MARC::ViewPolicy->should_hide_marc(
157         {
158             frameworkcode => q{},
159             interface     => $interface
160         }
161     );
162     my @keyvalues = keys %{$shouldhidemarc};
163     my @visiblefields = grep { $shouldhidemarc->{$_}==1 } @keyvalues;
164
165     is(scalar @hiddenfields,0,'Should Hide MARC - Full Visibility');
166     is_deeply(\@visiblefields,\@keyvalues,'Should Hide MARC - No Visibility');
167     return;
168 }
169
170 sub create_marc_record {
171
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 ),
184     );
185
186     $marc_record->insert_fields_ordered(@fields);
187
188     return $marc_record;
189 }
190
191 subtest 'Koha::Filter::MARC::ViewPolicy opac tests' => sub {
192
193     plan tests => 104;
194
195     $schema->storage->txn_begin();
196     run_hiding_tests('opac');
197     $schema->storage->txn_rollback();
198 };
199
200 subtest 'Koha::Filter::MARC::ViewPolicy intranet tests' => sub {
201
202     plan tests => 104;
203
204     $schema->storage->txn_begin();
205     run_hiding_tests('intranet');
206     $schema->storage->txn_rollback();
207 };
208
209 1;