Bug 6679 - [SIGNED-OFF] fix 3 perlcritic violations in C4/Items.pm
[koha.git] / t / RecordProcessor.t
1 #!/usr/bin/perl
2
3 # Copyright 2012 C & P Bibliography Services
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 use warnings;
22 use File::Spec;
23 use MARC::Record;
24
25 use Test::More;
26
27 BEGIN {
28         use_ok('Koha::RecordProcessor');
29 }
30
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);
38
39
40 my $filterdir = File::Spec->rel2abs('Koha/Filter') . '/MARC';
41
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();
45
46 foreach my $filter (@installed_filters) {
47     ok(grep($filter, @available_filters), "Found filter $filter");
48 }
49
50 my $marc_filters = grep (/MARC/, @available_filters);
51 is(scalar Koha::RecordProcessor::AvailableFilters('MARC'), $marc_filters, 'Retrieved list of MARC filters');
52
53 my $processor = Koha::RecordProcessor->new( { filters => ( 'ABCD::EFGH::IJKL' ) } );
54
55 is(ref($processor), 'Koha::RecordProcessor', 'Created record processor with invalid filter');
56
57 is($processor->process($marc_record), $marc_record, 'Process record with empty processor');
58
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');
61
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');
64
65 is($processor->process($marc_record), $marc_record, 'Process record');
66
67 $processor->bind($marc_record);
68
69 is($processor->record, $marc_record, 'Bound record to processor');
70
71 is($processor->process(), $marc_record, 'Filter bound record');
72
73 eval {
74     $processor = Koha::RecordProcessor->new( { filters => ( 'Koha::Filter::MARC::Null' ) } );
75     undef $processor;
76 };
77
78 ok(!$@, 'Destroyed processor successfully');
79
80 done_testing();