Translation updates for Koha 3.22.0-beta release
[koha.git] / t / RecordProcessor.t
blob850ad393960dac203f138aa10a58c21a8d802972
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;
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','','','a' => $isbn);
35 $marc_record->append_fields($field);
36 $field = MARC::Field->new('245','','','a' => $title);
37 $marc_record->append_fields($field);
40 my $filterdir = File::Spec->rel2abs('Koha/Filter') . '/MARC';
42 opendir(my $dh, $filterdir);
43 my @installed_filters = map { ( /\.pm$/ && -f "$filterdir/$_" && s/\.pm$// ) ? "Koha::Filters::MARC::$_" : () } readdir($dh);
44 my @available_filters = Koha::RecordProcessor::AvailableFilters();
46 foreach my $filter (@installed_filters) {
47 ok(grep($filter, @available_filters), "Found filter $filter");
50 my $marc_filters = grep (/MARC/, @available_filters);
51 is(scalar Koha::RecordProcessor::AvailableFilters('MARC'), $marc_filters, 'Retrieved list of MARC filters');
53 my $processor = Koha::RecordProcessor->new( { filters => ( 'ABCD::EFGH::IJKL' ) } );
55 is(ref($processor), 'Koha::RecordProcessor', 'Created record processor with invalid filter');
57 is($processor->process($marc_record), $marc_record, 'Process record with empty processor');
59 $processor = Koha::RecordProcessor->new( { filters => ( 'Null' ) } );
60 is(ref($processor->filters->[0]), 'Koha::Filter::MARC::Null', 'Created record processor with implicitly scoped Null filter');
62 $processor = Koha::RecordProcessor->new( { filters => ( 'Koha::Filter::MARC::Null' ) } );
63 is(ref($processor->filters->[0]), 'Koha::Filter::MARC::Null', 'Created record processor with explicitly scoped Null filter');
65 is($processor->process($marc_record), $marc_record, 'Process record');
67 $processor->bind($marc_record);
69 is($processor->record, $marc_record, 'Bound record to processor');
71 is($processor->process(), $marc_record, 'Filter bound record');
73 eval {
74 $processor = Koha::RecordProcessor->new( { filters => ( 'Koha::Filter::MARC::Null' ) } );
75 undef $processor;
78 ok(!$@, 'Destroyed processor successfully');
80 subtest "new() tests" => sub {
82 plan tests => 13;
84 my $processor;
86 # Create a processor with a valid filter
87 $processor = new Koha::RecordProcessor({ filters => 'Null' });
88 is( ref($processor), 'Koha::RecordProcessor', 'Processor created' );
89 is( scalar @{ $processor->filters }, 1, 'One filter initialized' );
90 is( ref($processor->filters->[0]), 'Koha::Filter::MARC::Null', 'Correct filter initialized' );
92 # Create a processor with an invalid filter
93 $processor = new Koha::RecordProcessor({ filters => 'Dummy' });
94 is( ref($processor), 'Koha::RecordProcessor', 'Processor created' );
95 is( scalar @{ $processor->filters }, 0, 'No filter initialized' );
96 is( ref($processor->filters->[0]), '', 'Make sure no filter initialized' );
98 # Create a processor with two valid filters
99 $processor = new Koha::RecordProcessor({ filters => [ 'Null', 'EmbedSeeFromHeadings' ] });
100 is( ref($processor), 'Koha::RecordProcessor', 'Processor created' );
101 is( scalar @{ $processor->filters }, 2, 'Two filters initialized' );
102 is( ref($processor->filters->[0]), 'Koha::Filter::MARC::Null', 'Correct first filter initialized' );
103 is( ref($processor->filters->[1]), 'Koha::Filter::MARC::EmbedSeeFromHeadings', 'Correct second filter initialized' );
105 # Create a processor with both valid and invalid filters.
106 $processor = new Koha::RecordProcessor({ filters => [ 'Null', 'Dummy' ] });
107 is( ref($processor), 'Koha::RecordProcessor', 'Processor created' );
108 is( scalar @{ $processor->filters }, 1, 'Invalid filter skipped' );
109 is( ref($processor->filters->[0]), 'Koha::Filter::MARC::Null', 'Correct filter initialized' );
113 done_testing();