|
Lines 21-106
use Modern::Perl;
Link Here
|
| 21 |
|
21 |
|
| 22 |
use File::Spec; |
22 |
use File::Spec; |
| 23 |
use MARC::Record; |
23 |
use MARC::Record; |
| 24 |
|
24 |
use English qw( -no_match_vars ); |
| 25 |
use Test::More; |
25 |
use Test::More; |
| 26 |
|
26 |
|
| 27 |
BEGIN { |
27 |
BEGIN { |
| 28 |
use_ok('Koha::RecordProcessor'); |
28 |
use_ok('Koha::RecordProcessor'); |
| 29 |
} |
29 |
} |
| 30 |
|
30 |
|
| 31 |
my $isbn = '0590353403'; |
31 |
my $isbn = '0590353403'; |
| 32 |
my $title = 'Foundation'; |
32 |
my $title = 'Foundation'; |
| 33 |
my $marc_record=MARC::Record->new; |
33 |
my $marc_record = MARC::Record->new; |
| 34 |
my $field = MARC::Field->new('020','','','a' => $isbn); |
34 |
my $field = MARC::Field->new( '020', q{}, q{}, 'a' => $isbn ); |
| 35 |
$marc_record->append_fields($field); |
35 |
$marc_record->append_fields($field); |
| 36 |
$field = MARC::Field->new('245','','','a' => $title); |
36 |
$field = MARC::Field->new( '245', q{}, q{}, 'a' => $title ); |
| 37 |
$marc_record->append_fields($field); |
37 |
$marc_record->append_fields($field); |
| 38 |
|
38 |
|
| 39 |
|
|
|
| 40 |
my $filterdir = File::Spec->rel2abs('Koha/Filter') . '/MARC'; |
39 |
my $filterdir = File::Spec->rel2abs('Koha/Filter') . '/MARC'; |
| 41 |
|
40 |
|
| 42 |
opendir(my $dh, $filterdir); |
41 |
my $dh; |
| 43 |
my @installed_filters = map { ( /\.pm$/ && -f "$filterdir/$_" && s/\.pm$// ) ? "Koha::Filters::MARC::$_" : () } readdir($dh); |
42 |
opendir $dh, $filterdir; |
|
|
43 |
my @installed_filters; |
| 44 |
my @directory_entries = readdir $dh; |
| 45 |
foreach my $entry (@directory_entries) { |
| 46 |
if ( $entry =~ /[.]pm$/xsm && -f "$filterdir/$entry" ) { |
| 47 |
my $filter_name = $entry; |
| 48 |
$filter_name =~ s/[.]pm$//xsm; |
| 49 |
push @installed_filters, $filter_name; |
| 50 |
} |
| 51 |
} |
| 52 |
closedir $dh; |
| 44 |
my @available_filters = Koha::RecordProcessor::AvailableFilters(); |
53 |
my @available_filters = Koha::RecordProcessor::AvailableFilters(); |
| 45 |
|
54 |
|
| 46 |
foreach my $filter (@installed_filters) { |
55 |
foreach my $filter (@installed_filters) { |
| 47 |
ok(grep($filter, @available_filters), "Found filter $filter"); |
56 |
ok( grep { /${filter}/xsm } @available_filters, "Found filter $filter" ); |
| 48 |
} |
57 |
} |
| 49 |
|
58 |
|
| 50 |
my $marc_filters = grep (/MARC/, @available_filters); |
59 |
my $marc_filters = grep { /MARC/sm } @available_filters; |
| 51 |
is(scalar Koha::RecordProcessor::AvailableFilters('MARC'), $marc_filters, 'Retrieved list of MARC filters'); |
60 |
is( scalar Koha::RecordProcessor::AvailableFilters('MARC'), |
|
|
61 |
$marc_filters, 'Retrieved list of MARC filters' ); |
| 52 |
|
62 |
|
| 53 |
my $processor = Koha::RecordProcessor->new( { filters => ( 'ABCD::EFGH::IJKL' ) } ); |
63 |
my $processor = |
|
|
64 |
Koha::RecordProcessor->new( { filters => ('ABCD::EFGH::IJKL') } ); |
| 54 |
|
65 |
|
| 55 |
is(ref($processor), 'Koha::RecordProcessor', 'Created record processor with invalid filter'); |
66 |
is( ref($processor), 'Koha::RecordProcessor', |
|
|
67 |
'Created record processor with invalid filter' ); |
| 56 |
|
68 |
|
| 57 |
is($processor->process($marc_record), $marc_record, 'Process record with empty processor'); |
69 |
is( $processor->process($marc_record), |
|
|
70 |
$marc_record, 'Process record with empty processor' ); |
| 58 |
|
71 |
|
| 59 |
$processor = Koha::RecordProcessor->new( { filters => ( 'Null' ) } ); |
72 |
$processor = Koha::RecordProcessor->new( { filters => ('Null') } ); |
| 60 |
is(ref($processor->filters->[0]), 'Koha::Filter::MARC::Null', 'Created record processor with implicitly scoped Null filter'); |
73 |
is( ref( $processor->filters->[0] ), |
|
|
74 |
'Koha::Filter::MARC::Null', |
| 75 |
'Created record processor with implicitly scoped Null filter' ); |
| 61 |
|
76 |
|
| 62 |
$processor = Koha::RecordProcessor->new( { filters => ( 'Koha::Filter::MARC::Null' ) } ); |
77 |
$processor = |
| 63 |
is(ref($processor->filters->[0]), 'Koha::Filter::MARC::Null', 'Created record processor with explicitly scoped Null filter'); |
78 |
Koha::RecordProcessor->new( { filters => ('Koha::Filter::MARC::Null') } ); |
|
|
79 |
is( ref( $processor->filters->[0] ), |
| 80 |
'Koha::Filter::MARC::Null', |
| 81 |
'Created record processor with explicitly scoped Null filter' ); |
| 64 |
|
82 |
|
| 65 |
is($processor->process($marc_record), $marc_record, 'Process record'); |
83 |
is( $processor->process($marc_record), $marc_record, 'Process record' ); |
| 66 |
|
84 |
|
| 67 |
$processor->bind($marc_record); |
85 |
$processor->bind($marc_record); |
| 68 |
|
86 |
|
| 69 |
is($processor->record, $marc_record, 'Bound record to processor'); |
87 |
is( $processor->record, $marc_record, 'Bound record to processor' ); |
| 70 |
|
88 |
|
| 71 |
is($processor->process(), $marc_record, 'Filter bound record'); |
89 |
is( $processor->process(), $marc_record, 'Filter bound record' ); |
| 72 |
|
90 |
|
| 73 |
eval { |
91 |
my $destroy_test = eval { |
| 74 |
$processor = Koha::RecordProcessor->new( { filters => ( 'Koha::Filter::MARC::Null' ) } ); |
92 |
$processor = |
|
|
93 |
Koha::RecordProcessor->new( { filters => ('Koha::Filter::MARC::Null') } ); |
| 75 |
undef $processor; |
94 |
undef $processor; |
|
|
95 |
return 1; |
| 76 |
}; |
96 |
}; |
| 77 |
|
97 |
|
| 78 |
ok(!$@, 'Destroyed processor successfully'); |
98 |
ok( !$EVAL_ERROR && $destroy_test == 1, 'Destroyed processor successfully' ); |
| 79 |
|
99 |
|
| 80 |
subtest "new() tests" => sub { |
100 |
subtest 'new() tests' => sub { |
| 81 |
|
101 |
|
| 82 |
plan tests => 14; |
102 |
plan tests => 14; |
| 83 |
|
103 |
|
| 84 |
my $processor; |
104 |
my $record_processor; |
| 85 |
|
105 |
|
| 86 |
# Create a processor with a valid filter |
106 |
# Create a processor with a valid filter |
| 87 |
$processor = new Koha::RecordProcessor({ filters => 'Null' }); |
107 |
$record_processor = Koha::RecordProcessor->new( { filters => 'Null' } ); |
| 88 |
is( ref($processor), 'Koha::RecordProcessor', 'Processor created' ); |
108 |
is( ref($record_processor), 'Koha::RecordProcessor', 'Processor created' ); |
| 89 |
is( scalar @{ $processor->filters }, 1, 'One filter initialized' ); |
109 |
is( scalar @{ $record_processor->filters }, 1, 'One filter initialized' ); |
| 90 |
is( ref($processor->filters->[0]), 'Koha::Filter::MARC::Null', 'Correct filter initialized' ); |
110 |
is( ref( $record_processor->filters->[0] ), |
|
|
111 |
'Koha::Filter::MARC::Null', 'Correct filter initialized' ); |
| 91 |
|
112 |
|
| 92 |
# Create a processor with an invalid filter |
113 |
# Create a processor with an invalid filter |
| 93 |
$processor = new Koha::RecordProcessor({ filters => 'Dummy' }); |
114 |
$record_processor = Koha::RecordProcessor->new( { filters => 'Dummy' } ); |
| 94 |
is( ref($processor), 'Koha::RecordProcessor', 'Processor created' ); |
115 |
is( ref($record_processor), 'Koha::RecordProcessor', 'Processor created' ); |
| 95 |
is( scalar @{ $processor->filters }, 0, 'No filter initialized' ); |
116 |
is( scalar @{ $record_processor->filters }, 0, 'No filter initialized' ); |
| 96 |
is( ref($processor->filters->[0]), '', 'Make sure no filter initialized' ); |
117 |
is( ref( $record_processor->filters->[0] ), |
|
|
118 |
q{}, 'Make sure no filter initialized' ); |
| 97 |
|
119 |
|
| 98 |
# Create a processor with two valid filters |
120 |
# Create a processor with two valid filters |
| 99 |
$processor = new Koha::RecordProcessor({ filters => [ 'Null', 'EmbedSeeFromHeadings' ] }); |
121 |
$record_processor = Koha::RecordProcessor->new( |
| 100 |
is( ref($processor), 'Koha::RecordProcessor', 'Processor created' ); |
122 |
{ filters => [ 'Null', 'EmbedSeeFromHeadings' ] } ); |
| 101 |
is( scalar @{ $processor->filters }, 2, 'Two filters initialized' ); |
123 |
is( ref($record_processor), 'Koha::RecordProcessor', 'Processor created' ); |
| 102 |
is( ref($processor->filters->[0]), 'Koha::Filter::MARC::Null', 'Correct first filter initialized' ); |
124 |
is( scalar @{ $record_processor->filters }, 2, 'Two filters initialized' ); |
| 103 |
is( ref($processor->filters->[1]), 'Koha::Filter::MARC::EmbedSeeFromHeadings', 'Correct second filter initialized' ); |
125 |
is( |
|
|
126 |
ref( $record_processor->filters->[0] ), |
| 127 |
'Koha::Filter::MARC::Null', |
| 128 |
'Correct first filter initialized' |
| 129 |
); |
| 130 |
is( |
| 131 |
ref( $record_processor->filters->[1] ), |
| 132 |
'Koha::Filter::MARC::EmbedSeeFromHeadings', |
| 133 |
'Correct second filter initialized' |
| 134 |
); |
| 104 |
|
135 |
|
| 105 |
# Create a processor with both valid and invalid filters. |
136 |
# Create a processor with both valid and invalid filters. |
| 106 |
# use hash reference for regression testing |
137 |
# use hash reference for regression testing |
|
Lines 108-119
subtest "new() tests" => sub {
Link Here
|
| 108 |
filters => [ 'Null', 'Dummy' ], |
139 |
filters => [ 'Null', 'Dummy' ], |
| 109 |
options => { 'test' => 'true' } |
140 |
options => { 'test' => 'true' } |
| 110 |
}; |
141 |
}; |
| 111 |
$processor = new Koha::RecordProcessor($parameters); |
142 |
$record_processor = Koha::RecordProcessor->new($parameters); |
| 112 |
is( ref($processor), 'Koha::RecordProcessor', 'Processor created' ); |
143 |
is( ref($record_processor), 'Koha::RecordProcessor', 'Processor created' ); |
| 113 |
is( scalar @{ $processor->filters }, 1, 'Invalid filter skipped' ); |
144 |
is( scalar @{ $record_processor->filters }, 1, 'Invalid filter skipped' ); |
| 114 |
is( ref($processor->filters->[0]), 'Koha::Filter::MARC::Null', 'Correct filter initialized' ); |
145 |
is( ref( $record_processor->filters->[0] ), |
|
|
146 |
'Koha::Filter::MARC::Null', 'Correct filter initialized' ); |
| 115 |
|
147 |
|
| 116 |
my $filter_params = $processor->filters->[0]->params; |
148 |
my $filter_params = $record_processor->filters->[0]->params; |
| 117 |
is_deeply( $filter_params, $parameters, 'Initialization parameters' ); |
149 |
is_deeply( $filter_params, $parameters, 'Initialization parameters' ); |
| 118 |
}; |
150 |
}; |
| 119 |
|
151 |
|
| 120 |
- |
|
|