DBRev Bug 15084 - Move the currency related code to
[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
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
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 subtest "new() tests" => sub {
81
82     plan tests => 14;
83
84     my $processor;
85
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' );
91
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' );
97
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' );
104
105     # Create a processor with both valid and invalid filters.
106     # use hash reference for regression testing
107     my $parameters = {
108         filters => [ 'Null', 'Dummy' ],
109         options => { 'test' => 'true' }
110     };
111     $processor = new Koha::RecordProcessor($parameters);
112     is( ref($processor), 'Koha::RecordProcessor', 'Processor created' );
113     is( scalar @{ $processor->filters }, 1, 'Invalid filter skipped' );
114     is( ref($processor->filters->[0]), 'Koha::Filter::MARC::Null', 'Correct filter initialized' );
115
116     my $filter_params = $processor->filters->[0]->params;
117     is_deeply( $filter_params, $parameters, 'Initialization parameters' );
118 };
119
120 done_testing();