Bug 20434: Update UNIMARC framework - auth (TM)
[koha.git] / t / RecordProcessor.t
blob63683c1b2316cc6e7fd786b1ffd92434bca7b226
1 #!/usr/bin/perl
3 # Copyright 2012 C & P Bibliography Services
5 # This file is part of Koha.
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>.
20 use Modern::Perl;
22 use File::Spec;
23 use MARC::Record;
24 use English qw( -no_match_vars );
25 use Test::More;
27 BEGIN {
28 use_ok('Koha::RecordProcessor');
31 my $isbn = '0590353403';
32 my $title = 'Foundation';
33 my $marc_record = MARC::Record->new;
34 my $field = MARC::Field->new( '020', q{}, q{}, 'a' => $isbn );
35 $marc_record->append_fields($field);
36 $field = MARC::Field->new( '245', q{}, q{}, 'a' => $title );
37 $marc_record->append_fields($field);
39 my $filterdir = File::Spec->rel2abs('Koha/Filter') . '/MARC';
41 my $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;
52 closedir $dh;
53 my @available_filters = Koha::RecordProcessor::AvailableFilters();
55 foreach my $filter (@installed_filters) {
56 ok( grep { /${filter}/xsm } @available_filters, "Found filter $filter" );
59 my $marc_filters = grep { /MARC/sm } @available_filters;
60 is( scalar Koha::RecordProcessor::AvailableFilters('MARC'),
61 $marc_filters, 'Retrieved list of MARC filters' );
63 my $processor =
64 Koha::RecordProcessor->new( { filters => ('ABCD::EFGH::IJKL') } );
66 is( ref($processor), 'Koha::RecordProcessor',
67 'Created record processor with invalid filter' );
69 is( $processor->process($marc_record),
70 $marc_record, 'Process record with empty processor' );
72 $processor = Koha::RecordProcessor->new( { filters => ('Null') } );
73 is( ref( $processor->filters->[0] ),
74 'Koha::Filter::MARC::Null',
75 'Created record processor with implicitly scoped Null filter' );
77 $processor =
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' );
83 is( $processor->process($marc_record), $marc_record, 'Process record' );
85 $processor->bind($marc_record);
87 is( $processor->record, $marc_record, 'Bound record to processor' );
89 is( $processor->process(), $marc_record, 'Filter bound record' );
91 my $destroy_test = eval {
92 $processor =
93 Koha::RecordProcessor->new( { filters => ('Koha::Filter::MARC::Null') } );
94 undef $processor;
95 return 1;
98 ok( !$EVAL_ERROR && $destroy_test == 1, 'Destroyed processor successfully' );
100 subtest 'new() tests' => sub {
102 plan tests => 14;
104 my $record_processor;
106 # Create a processor with a valid filter
107 $record_processor = Koha::RecordProcessor->new( { filters => 'Null' } );
108 is( ref($record_processor), 'Koha::RecordProcessor', 'Processor created' );
109 is( scalar @{ $record_processor->filters }, 1, 'One filter initialized' );
110 is( ref( $record_processor->filters->[0] ),
111 'Koha::Filter::MARC::Null', 'Correct filter initialized' );
113 # Create a processor with an invalid filter
114 $record_processor = Koha::RecordProcessor->new( { filters => 'Dummy' } );
115 is( ref($record_processor), 'Koha::RecordProcessor', 'Processor created' );
116 is( scalar @{ $record_processor->filters }, 0, 'No filter initialized' );
117 is( ref( $record_processor->filters->[0] ),
118 q{}, 'Make sure no filter initialized' );
120 # Create a processor with two valid filters
121 $record_processor = Koha::RecordProcessor->new(
122 { filters => [ 'Null', 'EmbedSeeFromHeadings' ] } );
123 is( ref($record_processor), 'Koha::RecordProcessor', 'Processor created' );
124 is( scalar @{ $record_processor->filters }, 2, 'Two filters initialized' );
126 ref( $record_processor->filters->[0] ),
127 'Koha::Filter::MARC::Null',
128 'Correct first filter initialized'
131 ref( $record_processor->filters->[1] ),
132 'Koha::Filter::MARC::EmbedSeeFromHeadings',
133 'Correct second filter initialized'
136 # Create a processor with both valid and invalid filters.
137 # use hash reference for regression testing
138 my $parameters = {
139 filters => [ 'Null', 'Dummy' ],
140 options => { 'test' => 'true' }
142 $record_processor = Koha::RecordProcessor->new($parameters);
143 is( ref($record_processor), 'Koha::RecordProcessor', 'Processor created' );
144 is( scalar @{ $record_processor->filters }, 1, 'Invalid filter skipped' );
145 is( ref( $record_processor->filters->[0] ),
146 'Koha::Filter::MARC::Null', 'Correct filter initialized' );
148 my $filter_params = $record_processor->filters->[0]->params;
149 is_deeply( $filter_params, $parameters, 'Initialization parameters' );
152 done_testing();