|
Line 0
Link Here
|
|
|
1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
# This file is part of Koha. |
| 4 |
# |
| 5 |
# Copyright 2015 Mark Tompsett |
| 6 |
# |
| 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. |
| 11 |
# |
| 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. |
| 16 |
# |
| 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>. |
| 19 |
|
| 20 |
use Modern::Perl; |
| 21 |
|
| 22 |
use Test::More tests => 3; |
| 23 |
|
| 24 |
use List::MoreUtils qw/any/; |
| 25 |
use MARC::Record; |
| 26 |
use MARC::Field; |
| 27 |
use C4::Context; |
| 28 |
use Koha::Database; |
| 29 |
|
| 30 |
BEGIN { |
| 31 |
use_ok('Koha::RecordProcessor'); |
| 32 |
} |
| 33 |
|
| 34 |
my $dbh = C4::Context->dbh; |
| 35 |
|
| 36 |
my $database = Koha::Database->new(); |
| 37 |
my $schema = $database->schema(); |
| 38 |
$dbh->{RaiseError} = 1; |
| 39 |
|
| 40 |
my @valid_hidden_values = ( |
| 41 |
'-7', '-6', '-5', '-4', '-3', '-2', '-1', '0', |
| 42 |
'1', '2', '3', '4', '5', '6', '7', '8' |
| 43 |
); |
| 44 |
|
| 45 |
my $hidden = { |
| 46 |
opac => [ '1', '2', '3', '4', '5', '6', '7', '8' ], |
| 47 |
intranet => [ '-7', '-4', '-3', '-2', '2', '3', '5', '8' ] |
| 48 |
}; |
| 49 |
|
| 50 |
sub run_hiding_tests { |
| 51 |
|
| 52 |
my $interface = shift; |
| 53 |
|
| 54 |
# foreach my $hidden_value ( @{ $hidden->{ $interface } } ) { |
| 55 |
foreach my $hidden_value ( @valid_hidden_values ) { |
| 56 |
|
| 57 |
$schema->storage->txn_begin(); |
| 58 |
|
| 59 |
my $sth = $dbh->prepare(" |
| 60 |
UPDATE marc_subfield_structure SET hidden=? |
| 61 |
WHERE tagfield='020' OR |
| 62 |
tagfield='008'; |
| 63 |
"); |
| 64 |
$sth->execute( $hidden_value ); |
| 65 |
|
| 66 |
my $processor = Koha::RecordProcessor->new({ |
| 67 |
schema => 'MARC', |
| 68 |
filters => ( 'ViewPolicy' ), |
| 69 |
options => { interface => $interface } |
| 70 |
}); |
| 71 |
|
| 72 |
is( |
| 73 |
ref( $processor->filters->[0] ), |
| 74 |
'Koha::Filter::MARC::ViewPolicy', |
| 75 |
"Created record processor with ViewPolicy filter ($hidden_value)" |
| 76 |
); |
| 77 |
|
| 78 |
# Create a fresh record |
| 79 |
my $record = create_marc_record(); |
| 80 |
# Apply filters |
| 81 |
my $filtered_record = $processor->process( $record ); |
| 82 |
# Data fields |
| 83 |
|
| 84 |
if ( any { $_ eq $hidden_value } @{ $hidden->{ $interface } }) { |
| 85 |
# Subfield and controlfield are set to be hidden |
| 86 |
|
| 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" ); |
| 96 |
|
| 97 |
} else { |
| 98 |
|
| 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" ); |
| 108 |
} |
| 109 |
|
| 110 |
is_deeply( $filtered_record, $record, |
| 111 |
"Records are the same" ); |
| 112 |
|
| 113 |
$schema->storage->txn_rollback(); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
sub create_marc_record { |
| 118 |
|
| 119 |
my $isbn = '0590353403'; |
| 120 |
my $title = 'Foundation'; |
| 121 |
my $record = MARC::Record->new; |
| 122 |
my @fields = ( |
| 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 ) |
| 127 |
); |
| 128 |
|
| 129 |
$record->insert_fields_ordered( @fields ); |
| 130 |
|
| 131 |
return $record; |
| 132 |
} |
| 133 |
|
| 134 |
subtest 'Koha::Filter::MARC::ViewPolicy opac tests' => sub { |
| 135 |
|
| 136 |
plan tests => 96; |
| 137 |
|
| 138 |
run_hiding_tests('opac'); |
| 139 |
}; |
| 140 |
|
| 141 |
subtest 'Koha::Filter::MARC::ViewPolicy intranet tests' => sub { |
| 142 |
|
| 143 |
plan tests => 96; |
| 144 |
|
| 145 |
run_hiding_tests('intranet'); |
| 146 |
}; |
| 147 |
|
| 148 |
|
| 149 |
1; |