3 # This file is part of Koha.
5 # Copyright 2015 Mark Tompsett
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Test
::More tests
=> 3;
24 use List
::MoreUtils qw
/any/;
31 use_ok
('Koha::RecordProcessor');
34 my $dbh = C4
::Context
->dbh;
36 my $database = Koha
::Database
->new();
37 my $schema = $database->schema();
38 $dbh->{RaiseError
} = 1;
40 my @valid_hidden_values = (
41 '-7', '-6', '-5', '-4', '-3', '-2', '-1', '0',
42 '1', '2', '3', '4', '5', '6', '7', '8'
46 opac
=> [ '1', '2', '3', '4', '5', '6', '7', '8' ],
47 intranet
=> [ '-7', '-4', '-3', '-2', '2', '3', '5', '8' ]
50 sub run_hiding_tests
{
52 my $interface = shift;
54 # foreach my $hidden_value ( @{ $hidden->{ $interface } } ) {
55 foreach my $hidden_value ( @valid_hidden_values ) {
57 $schema->storage->txn_begin();
59 my $sth = $dbh->prepare("
60 UPDATE marc_subfield_structure SET hidden=?
61 WHERE tagfield='020' OR
64 $sth->execute( $hidden_value );
66 my $processor = Koha
::RecordProcessor
->new({
68 filters
=> ( 'ViewPolicy' ),
69 options
=> { interface
=> $interface }
73 ref( $processor->filters->[0] ),
74 'Koha::Filter::MARC::ViewPolicy',
75 "Created record processor with ViewPolicy filter ($hidden_value)"
78 # Create a fresh record
79 my $record = create_marc_record
();
81 my $filtered_record = $processor->process( $record );
84 if ( any
{ $_ eq $hidden_value } @
{ $hidden->{ $interface } }) {
85 # Subfield and controlfield are set to be hidden
87 is
( $filtered_record->field('020'), undef,
88 "Data field has been deleted because of hidden=$hidden_value" );
89 is
( $record->field('020'), undef,
90 "Data field has been deleted in the original record because of hidden=$hidden_value" );
91 # Control fields have a different behaviour in code
92 is
( $filtered_record->field('008'), undef,
93 "Control field has been deleted because of hidden=$hidden_value" );
94 is
( $record->field('008'), undef,
95 "Control field has been deleted in the original record because of hidden=$hidden_value" );
99 isnt
( $filtered_record->field('020'), undef,
100 "Data field hasn't been deleted because of hidden=$hidden_value" );
101 isnt
( $record->field('020'), undef,
102 "Data field hasn't been deleted in the original record because of hidden=$hidden_value" );
103 # Control fields have a different behaviour in code
104 isnt
( $filtered_record->field('008'), undef,
105 "Control field hasn't been deleted because of hidden=$hidden_value" );
106 isnt
( $record->field('008'), undef,
107 "Control field hasn't been deleted in the original record because of hidden=$hidden_value" );
110 is_deeply
( $filtered_record, $record,
111 "Records are the same" );
113 $schema->storage->txn_rollback();
117 sub create_marc_record
{
119 my $isbn = '0590353403';
120 my $title = 'Foundation';
121 my $record = MARC
::Record
->new;
123 MARC
::Field
->new( '003', 'AR-CdUBM'),
124 MARC
::Field
->new( '008', '######suuuu####ag_||||__||||_0||_|_uuu|d'),
125 MARC
::Field
->new( '020', q{}, q{}, 'a' => $isbn ),
126 MARC
::Field
->new( '245', q{}, q{}, 'a' => $title )
129 $record->insert_fields_ordered( @fields );
134 subtest
'Koha::Filter::MARC::ViewPolicy opac tests' => sub {
138 run_hiding_tests
('opac');
141 subtest
'Koha::Filter::MARC::ViewPolicy intranet tests' => sub {
145 run_hiding_tests
('intranet');