Bug 16849: Move IsDebarred to Koha::Patron->is_debarred
[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 use English qw( -no_match_vars );
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', 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);
38
39 my $filterdir = File::Spec->rel2abs('Koha/Filter') . '/MARC';
40
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;
50     }
51 }
52 closedir $dh;
53 my @available_filters = Koha::RecordProcessor::AvailableFilters();
54
55 foreach my $filter (@installed_filters) {
56     ok( grep { /${filter}/xsm } @available_filters, "Found filter $filter" );
57 }
58
59 my $marc_filters = grep { /MARC/sm } @available_filters;
60 is( scalar Koha::RecordProcessor::AvailableFilters('MARC'),
61     $marc_filters, 'Retrieved list of MARC filters' );
62
63 my $processor =
64   Koha::RecordProcessor->new( { filters => ('ABCD::EFGH::IJKL') } );
65
66 is( ref($processor), 'Koha::RecordProcessor',
67     'Created record processor with invalid filter' );
68
69 is( $processor->process($marc_record),
70     $marc_record, 'Process record with empty processor' );
71
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' );
76
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' );
82
83 is( $processor->process($marc_record), $marc_record, 'Process record' );
84
85 $processor->bind($marc_record);
86
87 is( $processor->record, $marc_record, 'Bound record to processor' );
88
89 is( $processor->process(), $marc_record, 'Filter bound record' );
90
91 my $destroy_test = eval {
92     $processor =
93       Koha::RecordProcessor->new( { filters => ('Koha::Filter::MARC::Null') } );
94     undef $processor;
95     return 1;
96 };
97
98 ok( !$EVAL_ERROR && $destroy_test == 1, 'Destroyed processor successfully' );
99
100 subtest 'new() tests' => sub {
101
102     plan tests => 14;
103
104     my $record_processor;
105
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' );
112
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' );
119
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' );
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     );
135
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' }
141     };
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' );
147
148     my $filter_params = $record_processor->filters->[0]->params;
149     is_deeply( $filter_params, $parameters, 'Initialization parameters' );
150 };
151
152 done_testing();