Lines 3-8
Link Here
|
3 |
# This file is part of Koha. |
3 |
# This file is part of Koha. |
4 |
# |
4 |
# |
5 |
# Copyright 2015 Mark Tompsett |
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 |
6 |
# |
10 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it |
11 |
# 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 |
12 |
# under the terms of the GNU General Public License as published by |
Lines 25-73
use List::MoreUtils qw/any/;
Link Here
|
25 |
use MARC::Record; |
29 |
use MARC::Record; |
26 |
use MARC::Field; |
30 |
use MARC::Field; |
27 |
use C4::Context; |
31 |
use C4::Context; |
|
|
32 |
use Koha::Cache qw/flush_all/; |
28 |
use Koha::Database; |
33 |
use Koha::Database; |
|
|
34 |
use English qw/-no_match_vars/; |
35 |
|
36 |
$OUTPUT_AUTOFLUSH = 1; |
29 |
|
37 |
|
30 |
BEGIN { |
38 |
BEGIN { |
31 |
use_ok('Koha::RecordProcessor'); |
39 |
use_ok('Koha::RecordProcessor'); |
32 |
} |
40 |
} |
33 |
|
41 |
|
34 |
my $dbh = C4::Context->dbh; |
42 |
sub run_hiding_tests { |
35 |
|
43 |
|
36 |
my $database = Koha::Database->new(); |
44 |
my $interface = shift; |
37 |
my $schema = $database->schema(); |
|
|
38 |
$dbh->{RaiseError} = 1; |
39 |
|
45 |
|
40 |
my @valid_hidden_values = ( |
46 |
# TODO: -8 is Flagged, which doesn't seem used. |
41 |
'-7', '-6', '-5', '-4', '-3', '-2', '-1', '0', |
47 |
# -9 and +9 are supposedly valid future values |
42 |
'1', '2', '3', '4', '5', '6', '7', '8' |
48 |
# according to older documentation in 3.10.x |
43 |
); |
49 |
my @valid_hidden_values = |
|
|
50 |
( -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8 ); |
44 |
|
51 |
|
45 |
my $hidden = { |
52 |
my $hidden = { |
46 |
opac => [ '1', '2', '3', '4', '5', '6', '7', '8' ], |
53 |
'opac' => [ -8, 1, 2, 3, 4, 5, 6, 7, 8 ], |
47 |
intranet => [ '-7', '-4', '-3', '-2', '2', '3', '5', '8' ] |
54 |
'intranet' => [ -8, -7, -4, -3, -2, 2, 3, 5, 8 ] |
48 |
}; |
55 |
}; |
49 |
|
56 |
|
50 |
sub run_hiding_tests { |
57 |
my $dbh = C4::Context->dbh; |
51 |
|
58 |
|
52 |
my $interface = shift; |
59 |
my $database = Koha::Database->new(); |
|
|
60 |
my $schema = $database->schema(); |
61 |
$dbh->{RaiseError} = 1; |
53 |
|
62 |
|
54 |
# foreach my $hidden_value ( @{ $hidden->{ $interface } } ) { |
63 |
#$ENV{'DEBUG'} = '1'; # Turn on debugging. |
55 |
foreach my $hidden_value ( @valid_hidden_values ) { |
|
|
56 |
|
64 |
|
57 |
$schema->storage->txn_begin(); |
65 |
foreach my $hidden_value (@valid_hidden_values) { |
58 |
|
66 |
|
59 |
my $sth = $dbh->prepare(" |
67 |
$schema->storage->txn_begin(); |
60 |
UPDATE marc_subfield_structure SET hidden=? |
|
|
61 |
WHERE tagfield='020' OR |
62 |
tagfield='008'; |
63 |
"); |
64 |
$sth->execute( $hidden_value ); |
65 |
|
68 |
|
66 |
my $processor = Koha::RecordProcessor->new({ |
69 |
my $update_sql = |
67 |
schema => 'MARC', |
70 |
q{UPDATE marc_subfield_structure SET hidden=? } |
68 |
filters => ( 'ViewPolicy' ), |
71 |
. q{WHERE tagfield='020' OR } |
69 |
options => { interface => $interface } |
72 |
. q{ tagfield='008';}; |
70 |
}); |
73 |
my $sth = $dbh->prepare($update_sql); |
|
|
74 |
$sth->execute($hidden_value); |
75 |
|
76 |
my $cache = Koha::Cache->get_instance(); |
77 |
$cache->flush_all(); # easy way to ensure DB is queried again. |
78 |
|
79 |
my $processor = Koha::RecordProcessor->new( |
80 |
{ |
81 |
schema => 'MARC', |
82 |
filters => ('ViewPolicy'), |
83 |
options => { interface => $interface } |
84 |
} |
85 |
); |
71 |
|
86 |
|
72 |
is( |
87 |
is( |
73 |
ref( $processor->filters->[0] ), |
88 |
ref( $processor->filters->[0] ), |
Lines 76-149
sub run_hiding_tests {
Link Here
|
76 |
); |
91 |
); |
77 |
|
92 |
|
78 |
# Create a fresh record |
93 |
# Create a fresh record |
79 |
my $record = create_marc_record(); |
94 |
my $sample_record = create_marc_record(); |
|
|
95 |
my $unfiltered_record = $sample_record->clone(); |
96 |
|
80 |
# Apply filters |
97 |
# Apply filters |
81 |
my $filtered_record = $processor->process( $record ); |
98 |
my $filtered_record = $processor->process($sample_record); |
|
|
99 |
|
82 |
# Data fields |
100 |
# Data fields |
|
|
101 |
if ( any { $_ == $hidden_value } @{ $hidden->{$interface} } ) { |
83 |
|
102 |
|
84 |
if ( any { $_ eq $hidden_value } @{ $hidden->{ $interface } }) { |
|
|
85 |
# Subfield and controlfield are set to be hidden |
103 |
# Subfield and controlfield are set to be hidden |
86 |
|
104 |
is( $filtered_record->field('020'), |
87 |
is( $filtered_record->field('020'), undef, |
105 |
undef, |
88 |
"Data field has been deleted because of hidden=$hidden_value" ); |
106 |
"Data field has been deleted because of hidden=$hidden_value" ); |
89 |
is( $record->field('020'), undef, |
107 |
isnt( $unfiltered_record->field('020'), undef, |
90 |
"Data field has been deleted in the original record because of hidden=$hidden_value" ); |
108 |
"Data field has been deleted in the original record because of hidden=$hidden_value" |
|
|
109 |
); |
110 |
|
91 |
# Control fields have a different behaviour in code |
111 |
# Control fields have a different behaviour in code |
92 |
is( $filtered_record->field('008'), undef, |
112 |
is( $filtered_record->field('008'), undef, |
93 |
"Control field has been deleted because of hidden=$hidden_value" ); |
113 |
"Control field has been deleted because of hidden=$hidden_value" |
94 |
is( $record->field('008'), undef, |
114 |
); |
95 |
"Control field has been deleted in the original record because of hidden=$hidden_value" ); |
115 |
isnt( $unfiltered_record->field('008'), undef, |
|
|
116 |
"Control field has been deleted in the original record because of hidden=$hidden_value" |
117 |
); |
96 |
|
118 |
|
97 |
} else { |
119 |
ok( $filtered_record && $unfiltered_record, 'Records exist' ); |
98 |
|
120 |
|
|
|
121 |
} |
122 |
else { |
99 |
isnt( $filtered_record->field('020'), undef, |
123 |
isnt( $filtered_record->field('020'), undef, |
100 |
"Data field hasn't been deleted because of hidden=$hidden_value" ); |
124 |
"Data field hasn't been deleted because of hidden=$hidden_value" |
101 |
isnt( $record->field('020'), undef, |
125 |
); |
102 |
"Data field hasn't been deleted in the original record because of hidden=$hidden_value" ); |
126 |
isnt( $unfiltered_record->field('020'), undef, |
|
|
127 |
"Data field hasn't been deleted in the original record because of hidden=$hidden_value" |
128 |
); |
129 |
|
103 |
# Control fields have a different behaviour in code |
130 |
# Control fields have a different behaviour in code |
104 |
isnt( $filtered_record->field('008'), undef, |
131 |
isnt( $filtered_record->field('008'), undef, |
105 |
"Control field hasn't been deleted because of hidden=$hidden_value" ); |
132 |
"Control field hasn't been deleted because of hidden=$hidden_value" |
106 |
isnt( $record->field('008'), undef, |
133 |
); |
107 |
"Control field hasn't been deleted in the original record because of hidden=$hidden_value" ); |
134 |
isnt( $unfiltered_record->field('008'), undef, |
|
|
135 |
"Control field hasn't been deleted in the original record because of hidden=$hidden_value" |
136 |
); |
137 |
|
138 |
is_deeply( $filtered_record, $unfiltered_record, |
139 |
'Records are the same' ); |
108 |
} |
140 |
} |
109 |
|
141 |
|
110 |
is_deeply( $filtered_record, $record, |
|
|
111 |
"Records are the same" ); |
112 |
|
113 |
$schema->storage->txn_rollback(); |
142 |
$schema->storage->txn_rollback(); |
114 |
} |
143 |
} |
|
|
144 |
return; |
115 |
} |
145 |
} |
116 |
|
146 |
|
117 |
sub create_marc_record { |
147 |
sub create_marc_record { |
118 |
|
148 |
|
119 |
my $isbn = '0590353403'; |
149 |
my $isbn = '0590353403'; |
120 |
my $title = 'Foundation'; |
150 |
my $title = 'Foundation'; |
121 |
my $record = MARC::Record->new; |
151 |
my $marc_record = MARC::Record->new; |
122 |
my @fields = ( |
152 |
my @fields = ( |
123 |
MARC::Field->new( '003', 'AR-CdUBM'), |
153 |
MARC::Field->new( '003', 'AR-CdUBM' ), |
124 |
MARC::Field->new( '008', '######suuuu####ag_||||__||||_0||_|_uuu|d'), |
154 |
MARC::Field->new( '008', '######suuuu####ag_||||__||||_0||_|_uuu|d' ), |
125 |
MARC::Field->new( '020', q{}, q{}, 'a' => $isbn ), |
155 |
MARC::Field->new( '020', q{}, q{}, 'a' => $isbn ), |
126 |
MARC::Field->new( '245', q{}, q{}, 'a' => $title ) |
156 |
MARC::Field->new( '245', q{}, q{}, 'a' => $title ), |
127 |
); |
157 |
); |
128 |
|
158 |
|
129 |
$record->insert_fields_ordered( @fields ); |
159 |
$marc_record->insert_fields_ordered(@fields); |
130 |
|
160 |
|
131 |
return $record; |
161 |
return $marc_record; |
132 |
} |
162 |
} |
133 |
|
163 |
|
134 |
subtest 'Koha::Filter::MARC::ViewPolicy opac tests' => sub { |
164 |
subtest 'Koha::Filter::MARC::ViewPolicy opac tests' => sub { |
135 |
|
165 |
|
136 |
plan tests => 96; |
166 |
plan tests => 102; |
137 |
|
167 |
|
138 |
run_hiding_tests('opac'); |
168 |
run_hiding_tests('opac'); |
139 |
}; |
169 |
}; |
140 |
|
170 |
|
141 |
subtest 'Koha::Filter::MARC::ViewPolicy intranet tests' => sub { |
171 |
subtest 'Koha::Filter::MARC::ViewPolicy intranet tests' => sub { |
142 |
|
172 |
|
143 |
plan tests => 96; |
173 |
plan tests => 102; |
144 |
|
174 |
|
145 |
run_hiding_tests('intranet'); |
175 |
run_hiding_tests('intranet'); |
146 |
}; |
176 |
}; |
147 |
|
177 |
|
148 |
|
|
|
149 |
1; |
178 |
1; |
150 |
- |
|
|